generated from nhcarrigan/template
feat: we have a functional prototype
This commit is contained in:
55
server/src/handlers/review/generateHandlers.ts
Normal file
55
server/src/handlers/review/generateHandlers.ts
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @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<void> => {
|
||||
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) {
|
||||
await logger.error(`/review/${route}`, error);
|
||||
await response.status(500).send({ error: error instanceof Error
|
||||
? error.message
|
||||
: "Internal Server Error" });
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user