Skip to content

Commit 844d309

Browse files
authored
Merge pull request #44 from Developer-Kommunity-24/timings
timimgs fix
2 parents 70c9e00 + 8c037b2 commit 844d309

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

apps/api/src/controllers/vote.Controller.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { submitVote } from '../services/vote.Service'
55

66
export const createVote = async (c: Context<AppEnv>) => {
77
const userId = c.get('userId')
8-
8+
99
let body: VoteRequest
1010
try {
1111
body = await c.req.json()
@@ -18,14 +18,14 @@ export const createVote = async (c: Context<AppEnv>) => {
1818
if (typeof stallId !== 'number' || typeof rating !== 'number') {
1919
return c.json({ success: false, message: 'stallId and rating must be numbers' }, 400)
2020
}
21-
21+
2222
if (rating < 0 || rating > 10) {
2323
return c.json({ success: false, message: 'rating must be between 0 and 10' }, 400)
2424
}
2525

2626
try {
2727
const progressCount = await submitVote(c.env, userId, body)
28-
28+
2929
const response: VoteResponse = {
3030
success: true,
3131
message: 'Vote submitted successfully',
@@ -37,6 +37,9 @@ export const createVote = async (c: Context<AppEnv>) => {
3737
if (e.message === 'Already voted') {
3838
return c.json({ success: false, message: 'Already voted' }, 400)
3939
}
40+
if (e.message === 'Voting is currently closed') {
41+
return c.json({ success: false, message: 'Voting is currently closed' }, 403)
42+
}
4043
return c.json({ success: false, message: 'Internal Server Error' }, 500)
4144
}
4245
}

apps/api/src/services/vote.Service.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,41 @@ import { eq, count } from 'drizzle-orm'
55
import type { AppEnv } from '../types'
66
import { ensureUserExists } from './user.Service'
77
import { 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+
834
export 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

Comments
 (0)