Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/api/src/controllers/progress.Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import type { AppEnv } from '../types'
import { fetchProgress } from '../services/progress.Service'

export const getProgress = async (c: Context<AppEnv>) => {
const userId = c.get('userId')

const userId = c.req.param('userId')

if (!userId) {
return c.json({ success: false, message: 'userId is required' }, 400)
}

try {
const data = await fetchProgress(c.env, userId)
const data = await fetchProgress(c.env.DB, userId)
return c.json(data)
} catch (e: any) {
console.error('Progress error:', e)
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const stalls = sqliteTable('stalls', {
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
description: text('description'),
logo: text('logo'), // ✅ added
logo: text('logo'),
qrSlug: text('qr_slug').unique().notNull(), // ✅ clean unique
createdAt: integer('created_at', { mode: 'timestamp' }).$defaultFn(() => new Date()),
});

Expand All @@ -24,4 +25,4 @@ export const ratings = sqliteTable('ratings', {
createdAt: integer('created_at', { mode: 'timestamp' }).$defaultFn(() => new Date()),
}, (table) => ({
unq: uniqueIndex('unique_vote').on(table.userId, table.stallId),
}));
}));
3 changes: 1 addition & 2 deletions apps/api/src/routes/progress.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Hono } from 'hono'
import type { AppEnv } from '../types'
import { requireAuth } from '../middleware/auth'
import { getProgress } from '../controllers/progress.Controller'

const progress = new Hono<AppEnv>()

progress.get('/', requireAuth, getProgress)
progress.get('/:userId', getProgress)

export default progress
15 changes: 5 additions & 10 deletions apps/api/src/services/progress.Service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { getDb } from '../db/client'
import { ratings, users } from '../db/schema'
import { eq, count } from 'drizzle-orm'
import type { AppEnv } from '../types'
import { ensureUserExists } from './user.Service'

export const fetchProgress = async (env: AppEnv['Bindings'], userId: string) => {
const ormDb = getDb(env.DB)

//ensure the user exists and grab profile data via Clerk
await ensureUserExists(env, userId)
export const fetchProgress = async (db: D1Database, userId: string) => {
const ormDb = getDb(db)

const userRecord = await ormDb.select({ isCompleted: users.isCompleted }).from(users).where(eq(users.id, userId))
const isCompleted = userRecord[0]?.isCompleted || false
const userRecord = await ormDb.select({ completed: users.completed }).from(users).where(eq(users.id, userId))
const isCompleted = userRecord[0]?.completed || false

const countResult = await ormDb.select({ count: count() }).from(ratings).where(eq(ratings.userId, userId))
const progressCount = countResult[0]?.count || 0

return { progress: progressCount, isCompleted }
return { userId, progress: progressCount, isCompleted }
}
2 changes: 1 addition & 1 deletion apps/api/src/services/vote.Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const submitVote = async (
if (progressCount >= 15) {
await ormDb
.update(users)
.set({ isCompleted: true })
.set({ completed: true })
.where(eq(users.id, userId))
}

Expand Down