( req: MonkeyRequest<undefined, AddQuoteRatingRequest>, )
| 105 | } |
| 106 | |
| 107 | export async function submitRating( |
| 108 | req: MonkeyRequest<undefined, AddQuoteRatingRequest>, |
| 109 | ): Promise<MonkeyResponse> { |
| 110 | const { uid } = req.ctx.decodedToken; |
| 111 | const { quoteId, rating, language } = req.body; |
| 112 | |
| 113 | const user = await getPartialUser(uid, "submit rating", ["quoteRatings"]); |
| 114 | |
| 115 | const userQuoteRatings = user.quoteRatings ?? {}; |
| 116 | const currentRating = userQuoteRatings[language]?.[quoteId] ?? 0; |
| 117 | |
| 118 | const newRating = rating - currentRating; |
| 119 | const shouldUpdateRating = currentRating !== 0; |
| 120 | |
| 121 | await QuoteRatingsDAL.submit( |
| 122 | quoteId, |
| 123 | language, |
| 124 | newRating, |
| 125 | shouldUpdateRating, |
| 126 | ); |
| 127 | |
| 128 | userQuoteRatings[language] ??= {}; |
| 129 | userQuoteRatings[language][quoteId] = rating; |
| 130 | |
| 131 | await updateQuoteRatings(uid, userQuoteRatings); |
| 132 | |
| 133 | const responseMessage = `Rating ${ |
| 134 | shouldUpdateRating ? "updated" : "submitted" |
| 135 | }`; |
| 136 | return new MonkeyResponse(responseMessage, null); |
| 137 | } |
| 138 | |
| 139 | export async function reportQuote( |
| 140 | req: MonkeyRequest<undefined, ReportQuoteRequest>, |
nothing calls this directly
no test coverage detected