-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* database updated for rating * optional comment for a rating * date entry retrieved * feat(api): new a rating * newline added to make eslint happy * make eslint happy * fix(api): check if the rating is valid (unique) * feat(api): get all rateable clubs * fix: make eslint happy * fix: remove unnecessary curly brace * fix(api): misc * fix: resolve nl issue * Delete types/api/club/rating/available.d.ts * Update new.post.ts * Update available.get.ts --------- Co-authored-by: Tom Tang <[email protected]>
- Loading branch information
1 parent
2b3af81
commit 3db73f6
Showing
3 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
export default eventHandler(async (event) => { | ||
const { auth } = event.context | ||
|
||
if (!auth.userId) { | ||
setResponseStatus(event, 403) | ||
return | ||
} | ||
|
||
const now = new Date() | ||
let semester = now.getFullYear().toString() // string for storing scope: yyyy[a|b] | ||
|
||
if (([1, 2]).includes(now.getMonth())) | ||
semester += 'a' // the `a` period of year | ||
else if (([6, 7]).includes(now.getMonth())) | ||
semester += 'b' // the `b` period of year | ||
else | ||
return [] // one should ensure that this api will never be called when the current month is not in [0, 1, 5, 6] | ||
|
||
const tsimsStudentId = (await prisma.user.findUniqueOrThrow({ | ||
where: { | ||
clerkUserId: auth.userId, | ||
}, | ||
})).tsimsStudentId | ||
|
||
const clubsUnrated = await prisma.club.findMany({ | ||
where: { | ||
membersByTsimsStudentId: { | ||
has: tsimsStudentId, | ||
}, | ||
ClubRating: { | ||
none: { | ||
rateBy: { equals: auth.userId }, | ||
rateScope: { equals: semester }, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
id: true, | ||
name: true, | ||
}, | ||
}) | ||
|
||
return clubsUnrated | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
import * as z from 'zod' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
const requestSchema = z.object({ | ||
club: z.string(), | ||
date: z.string().datetime(), | ||
score: z.number().lte(5), | ||
comment: z.string().max(100), | ||
}) | ||
|
||
export default eventHandler(async (event) => { | ||
const { auth } = event.context | ||
|
||
if (!auth.userId) { | ||
setResponseStatus(event, 403) | ||
return | ||
} | ||
|
||
const now = new Date() | ||
let semester = now.getFullYear().toString() // string for storing scope: yyyy[a|b] | ||
|
||
if (([1, 2]).includes(now.getMonth())) | ||
semester += 'a' // the `a` period of year | ||
else if (([6, 7]).includes(now.getMonth())) | ||
semester += 'b' // the `b` period of year | ||
else | ||
return // one should ensure that this api will never be called when the current month is not in [0, 1, 5, 6] | ||
|
||
const requestBody = await readValidatedBody(event, body => requestSchema.parse(body)) | ||
const record = await prisma.clubRating.findUnique({ | ||
where: { | ||
clubId_rateBy_rateScope: { | ||
clubId: Number(requestBody.club), | ||
rateBy: auth.userId, | ||
rateScope: semester, | ||
}, | ||
}, | ||
}) | ||
|
||
if (record) { | ||
setResponseStatus(event, 403) | ||
return | ||
} | ||
|
||
await prisma.clubRating.create({ | ||
data: { | ||
rateBy: auth.userId, | ||
clubId: Number(requestBody.club), | ||
rating: requestBody.score, | ||
comment: requestBody.comment, | ||
ratedAt: requestBody.date, | ||
rateScope: semester, | ||
}, | ||
}) | ||
}) |