-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvote.Service.ts
More file actions
68 lines (59 loc) · 2.01 KB
/
Copy pathvote.Service.ts
File metadata and controls
68 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { VoteRequest } from '@repo/shared'
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'
import { refreshStallAggregates } from './stalls.Service'
export const submitVote = async (
env: AppEnv['Bindings'],
userId: string,
vote: VoteRequest
) => {
const { stallId, rating } = vote
const ormDb = getDb(env.DB)
try {
// 1. Ensure whether the user exists
await ensureUserExists(env, userId)
// 2. Now proceed with the vote
await ormDb.insert(ratings).values({ userId, stallId, rating })
} catch (e: any) {
if (e.message && e.message.includes('UNIQUE constraint failed')) {
throw new Error('Already voted')
}
console.error('Database insertion error:', e)
throw new Error('Internal Server Error')
}
try {
const countResult = await ormDb
.select({ count: count() })
.from(ratings)
.where(eq(ratings.userId, userId))
const progressCount = countResult[0]?.count || 1
if (progressCount >= 11) {
await ormDb
.update(users)
.set({ completed: true })
.where(eq(users.id, userId))
}
// Update stall aggregates immediately
if (progressCount === 11) {
// User just became qualified! All their stalls need refreshing
const userRatings = await ormDb
.select({ stallId: ratings.stallId })
.from(ratings)
.where(eq(ratings.userId, userId));
const stallIds = userRatings
.map(r => r.stallId)
.filter((id): id is number => id !== null);
await refreshStallAggregates(env.DB, stallIds);
} else {
// Normal vote (could be qualified or not, but only this stall is affected)
await refreshStallAggregates(env.DB, [stallId]);
}
return progressCount
} catch (e: any) {
console.error('Database query/update error:', e)
throw new Error('Internal Server Error')
}
}