forms/server/src/handlers/submit/appealHandler.ts
Naomi Carrigan e8f5078aa3
Some checks failed
Node.js CI / Lint and Test (push) Has been cancelled
feat: initial project prototype (#2)
### Explanation

_No response_

### Issue

_No response_

### Attestations

- [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [x] I have pinned the dependencies to a specific patch version.

### Style

- [x] I have run the linter and resolved any errors.
- [x] My pull request uses an appropriate title, matching the conventional commit standards.
- [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [ ] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [x] All new and existing tests pass locally with my changes.
- [x] Code coverage remains at or above the configured threshold.

### Documentation

_No response_

### Versioning

Major - My pull request introduces a breaking change.

Reviewed-on: #2
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
2025-02-17 14:03:32 -08:00

63 lines
2.1 KiB
TypeScript

/**
* @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 { Appeal, ErrorResponse, SuccessResponse } from "@repo/types";
import type { FastifyReply, FastifyRequest } from "fastify";
/**
*Handles appeal form submissions.
* @param database - The Prisma database client.
* @param request - The request object.
* @param response - The Fastify reply utility.
*/
export const submitAppealHandler = async(
database: PrismaClient,
request: FastifyRequest<{ Body: Appeal }>,
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>,
"appeals",
);
if (isInvalid !== null) {
await response.status(400).send({ error: isInvalid });
return;
}
const exists = await database.appeals.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 an appeal. 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.appeals.create({
data,
});
await sendMail("appeal", data);
await response.send({ success: true });
} catch (error) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- we bein' lazy.
await logger.error("/submit/appeals", error as Error);
await response.status(500).send({ error: error instanceof Error
? error.message
: "Internal Server Error" });
}
};