@@ -5,11 +5,41 @@ import { eq, count } from 'drizzle-orm'
55import type { AppEnv } from '../types'
66import { ensureUserExists } from './user.Service'
77import { refreshStallAggregates } from './stalls.Service'
8+ function isVotingOpen ( ) : boolean {
9+ const istOffset = 5.5 * 60 * 60 * 1000 ;
10+ const istTime = new Date ( Date . now ( ) + istOffset ) ;
11+
12+ const year = istTime . getUTCFullYear ( ) ;
13+ const month = istTime . getUTCMonth ( ) + 1 ;
14+ const day = istTime . getUTCDate ( ) ;
15+
16+ if ( year !== 2026 || month !== 3 ) return false ;
17+
18+ const timeInMinutes = istTime . getUTCHours ( ) * 60 + istTime . getUTCMinutes ( ) ;
19+ const startMins = 9 * 60 + 30 ; // 9:30 AM (570)
20+
21+ if ( day === 28 ) {
22+ const endMins = 22 * 60 ; // 10:00 PM (1320)
23+ return timeInMinutes >= startMins && timeInMinutes <= endMins ;
24+ }
25+
26+ if ( day === 29 ) {
27+ const endMins = 12 * 60 ; // 12:00 PM (720)
28+ return timeInMinutes >= startMins && timeInMinutes <= endMins ;
29+ }
30+
31+ return false ;
32+ }
33+
834export const submitVote = async (
935 env : AppEnv [ 'Bindings' ] ,
1036 userId : string ,
1137 vote : VoteRequest
1238) => {
39+ if ( ! isVotingOpen ( ) ) {
40+ throw new Error ( 'Voting is currently closed' ) ;
41+ }
42+
1343 const { stallId, rating } = vote
1444 const ormDb = getDb ( env . DB )
1545
@@ -49,11 +79,11 @@ export const submitVote = async (
4979 . select ( { stallId : ratings . stallId } )
5080 . from ( ratings )
5181 . where ( eq ( ratings . userId , userId ) ) ;
52-
82+
5383 const stallIds = userRatings
5484 . map ( r => r . stallId )
5585 . filter ( ( id ) : id is number => id !== null ) ;
56-
86+
5787 await refreshStallAggregates ( env . DB , stallIds ) ;
5888 } else {
5989 // Normal vote (could be qualified or not, but only this stall is affected)
0 commit comments