generated from nhcarrigan/template
feat: add form for submitting testimonials
Node.js CI / Lint and Test (pull_request) Failing after 1m15s
Node.js CI / Lint and Test (pull_request) Failing after 1m15s
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import { validateBody } from "../../modules/validateBody.js";
|
||||
import { logger } from "../../utils/logger.js";
|
||||
import { sendMail } from "../../utils/mailer.js";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import type { Testimonial, ErrorResponse, SuccessResponse } from "@repo/types";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
|
||||
/**
|
||||
*Handles testimonial form submissions.
|
||||
* @param database - The Prisma database client.
|
||||
* @param request - The request object.
|
||||
* @param response - The Fastify reply utility.
|
||||
*/
|
||||
export const submitTestimonialHandler = async(
|
||||
database: PrismaClient,
|
||||
request: FastifyRequest<{ Body: Testimonial }>,
|
||||
response: FastifyReply<{ Reply: SuccessResponse | ErrorResponse }>,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const isInvalid = validateBody(
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We're passing a narrower type and TS hates that?
|
||||
request.body as unknown as Record<string, unknown>,
|
||||
"testimonials",
|
||||
);
|
||||
if (isInvalid !== null) {
|
||||
await response.status(400).send({ error: isInvalid });
|
||||
return;
|
||||
}
|
||||
const exists = await database.staff.findUnique({
|
||||
where: {
|
||||
email: request.body.email,
|
||||
},
|
||||
});
|
||||
if (exists !== null) {
|
||||
await response.
|
||||
status(429).
|
||||
send({
|
||||
error:
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
"You have already submitted a staff application. Please wait for it to be reviewed.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const data = { ...request.body };
|
||||
// @ts-expect-error -- We're deleting a property here.
|
||||
delete data.consent;
|
||||
await database.testimonials.create({
|
||||
data,
|
||||
});
|
||||
await sendMail("testimonials", data);
|
||||
await response.send({ success: true });
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- we bein' lazy.
|
||||
await logger.error("/submit/testimonials", error as Error);
|
||||
await response.status(500).send({ error: error instanceof Error
|
||||
? error.message
|
||||
: "Internal Server Error" });
|
||||
}
|
||||
};
|
||||
@@ -9,4 +9,5 @@ export type DatabasePath = | "appeals"
|
||||
| "events"
|
||||
| "meetings"
|
||||
| "mentorships"
|
||||
| "staff";
|
||||
| "staff"
|
||||
| "testimonials";
|
||||
|
||||
@@ -70,6 +70,12 @@ const validators: Record<DatabasePath, Array<string>> = {
|
||||
"difficultSituation",
|
||||
"leadershipSituation",
|
||||
],
|
||||
testimonials: [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"content",
|
||||
],
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,8 @@ import { submitMeetingHandler } from "../handlers/submit/meetingHandler.js";
|
||||
import { submitMentorshipHandler }
|
||||
from "../handlers/submit/mentorshipHandler.js";
|
||||
import { submitStaffHandler } from "../handlers/submit/staffHandler.js";
|
||||
import { submitTestimonialHandler }
|
||||
from "../handlers/submit/testimonialHandler.js";
|
||||
import type { WrappedRoute } from "../interfaces/wrappedRoute.js";
|
||||
import type {
|
||||
Appeal,
|
||||
@@ -24,6 +26,7 @@ import type {
|
||||
Staff,
|
||||
ErrorResponse,
|
||||
SuccessResponse,
|
||||
Testimonial,
|
||||
} from "@repo/types";
|
||||
|
||||
/**
|
||||
@@ -31,6 +34,7 @@ import type {
|
||||
* @param database - The Prisma client.
|
||||
* @returns A Fastify plugin.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function -- Prisma typings don't allow us to mount these dynamically...
|
||||
export const submitRoutes: WrappedRoute = (database) => {
|
||||
return async(fastify) => {
|
||||
fastify.post<{ Body: Appeal; Reply: SuccessResponse | ErrorResponse }>(
|
||||
@@ -81,5 +85,12 @@ export const submitRoutes: WrappedRoute = (database) => {
|
||||
await submitStaffHandler(database, request, response);
|
||||
},
|
||||
);
|
||||
|
||||
fastify.post<{ Body: Testimonial; Reply: SuccessResponse | ErrorResponse }>(
|
||||
"/submit/testimonials",
|
||||
async(request, response) => {
|
||||
await submitTestimonialHandler(database, request, response);
|
||||
},
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user