/** * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import { checkSubmissionExists, markSubmissionReviewed, } from "../../modules/genericDataQueries.js"; import { logger } from "../../utils/logger.js"; import type { DatabasePath } from "../../interfaces/databasePath.js"; import type { PrismaClient } from "@prisma/client"; import type { ErrorResponse, ReviewRequest, SuccessResponse, } from "@repo/types"; import type { FastifyReply, FastifyRequest } from "fastify"; /** * Queries the database for a specific submission type (based on * the route parameter) and returns all unreviewed submissions.). * @param database - The Prisma client. * @param route - The type of data to list. * @param data - The fastify data. * @param data.request - The request body. * @param data.response - The Fastify reply object. */ export const reviewHandler = async( database: PrismaClient, route: DatabasePath, { request, response, }: { request: FastifyRequest<{ Body: ReviewRequest }>; response: FastifyReply<{ Reply: SuccessResponse | ErrorResponse }>; }, ): Promise => { try { const { submissionId } = request.body; const exists = await checkSubmissionExists(database, route, submissionId); if (!exists) { response.code(404).send({ error: `${route} submission not found.` }); return; } await markSubmissionReviewed(database, route, submissionId); await response.code(200).send({ success: true }); } catch (error) { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- we bein' lazy. await logger.error(`/review/${route}`, error as Error); await response.status(500).send({ error: error instanceof Error ? error.message : "Internal Server Error" }); } };