Skip to content

Commit

Permalink
feat: api for rating (#542)
Browse files Browse the repository at this point in the history
* 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
RadioNoiseE and qwerzl authored Mar 26, 2024
1 parent 2b3af81 commit 3db73f6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
4 changes: 2 additions & 2 deletions db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ model User {
name String
tsimsStudentId Int @unique
LeaveRequest LeaveRequest[]
ClubRating ClubRating[]
ClubRating ClubRating[]
}

// Json types should be stored like {"zh": xxx, "en": xxx}.
Expand Down Expand Up @@ -67,5 +67,5 @@ model ClubRating {
user User @relation(fields: [rateBy], references: [clerkUserId])
club Club @relation(fields: [clubId], references: [id])
@@unique([id, clubId, rateBy, rateScope]) // One student can vote only once in one semester
@@unique([clubId, rateBy, rateScope]) // One student can vote only once in one semester
}
48 changes: 48 additions & 0 deletions server/api/club/rating/available.get.ts
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
})
57 changes: 57 additions & 0 deletions server/api/club/rating/new.post.ts
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,
},
})
})

0 comments on commit 3db73f6

Please sign in to comment.