From 1e18e9b9451f8d870c2b18c88fc6dff9048f040a Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Sat, 2 Apr 2022 02:45:28 +0200 Subject: [PATCH] feat: adds reminder-mail endpoints --- lib/types.ts | 9 ++++ lib/validations/reminder-mail.ts | 24 ++++++++++ pages/api/reminder-mails/[id]/delete.ts | 50 ++++++++++++++++++++ pages/api/reminder-mails/[id]/edit.ts | 62 +++++++++++++++++++++++++ pages/api/reminder-mails/[id]/index.ts | 51 ++++++++++++++++++++ pages/api/reminder-mails/index.ts | 37 +++++++++++++++ pages/api/reminder-mails/new.ts | 52 +++++++++++++++++++++ 7 files changed, 285 insertions(+) create mode 100644 lib/validations/reminder-mail.ts create mode 100644 pages/api/reminder-mails/[id]/delete.ts create mode 100644 pages/api/reminder-mails/[id]/edit.ts create mode 100644 pages/api/reminder-mails/[id]/index.ts create mode 100644 pages/api/reminder-mails/index.ts create mode 100644 pages/api/reminder-mails/new.ts diff --git a/lib/types.ts b/lib/types.ts index d0c33538c3..d5a179f7b3 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -16,6 +16,7 @@ import { Membership, Payment, Schedule, + ReminderMail, } from "@calcom/prisma/client"; // Base response, used for all responses @@ -160,3 +161,11 @@ export type WebhookResponse = BaseResponse & { export type WebhooksResponse = BaseResponse & { data?: Partial[]; }; + +// ReminderMail +export type ReminderMailResponse = BaseResponse & { + data?: Partial; +}; +export type ReminderMailsResponse = BaseResponse & { + data?: Partial[]; +}; diff --git a/lib/validations/reminder-mail.ts b/lib/validations/reminder-mail.ts new file mode 100644 index 0000000000..d97c65c2b1 --- /dev/null +++ b/lib/validations/reminder-mail.ts @@ -0,0 +1,24 @@ +import { withValidation } from "next-validations"; +import { z } from "zod"; + +import { _ReminderMailModel as ReminderMail } from "@calcom/prisma/zod"; + +export const schemaReminderMailBaseBodyParams = ReminderMail.omit({ id: true }).partial(); + +export const schemaReminderMailPublic = ReminderMail.omit({}); + +const schemaReminderMailRequiredParams = z.object({ + referenceId: z.number().int(), + reminderType: z.enum(["PENDING_BOOKING_CONFIRMATION"]), + elapsedMinutes: z.number().int(), +}); + +export const schemaReminderMailBodyParams = schemaReminderMailBaseBodyParams.merge( + schemaReminderMailRequiredParams +); + +export const withValidReminderMail = withValidation({ + schema: schemaReminderMailBodyParams, + type: "Zod", + mode: "body", +}); diff --git a/pages/api/reminder-mails/[id]/delete.ts b/pages/api/reminder-mails/[id]/delete.ts new file mode 100644 index 0000000000..3211a4fa9d --- /dev/null +++ b/pages/api/reminder-mails/[id]/delete.ts @@ -0,0 +1,50 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { BaseResponse } from "@lib/types"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/reminder-mails/{id}/delete: + * delete: + * summary: Remove an existing reminderMail + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to delete + * tags: + * - reminderMails + * responses: + * 201: + * description: OK, reminderMail removed successfuly + * model: ReminderMail + * 400: + * description: Bad request. ReminderMail id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function deleteReminderMail(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query", safe.error); + + const data = await prisma.reminderMail.delete({ where: { id: safe.data.id } }); + + if (data) res.status(200).json({ message: `ReminderMail with id: ${safe.data.id} deleted successfully` }); + else + (error: Error) => + res.status(400).json({ + message: `ReminderMail with id: ${safe.data.id} was not able to be processed`, + error, + }); +} + +export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteReminderMail)); diff --git a/pages/api/reminder-mails/[id]/edit.ts b/pages/api/reminder-mails/[id]/edit.ts new file mode 100644 index 0000000000..802381bd1c --- /dev/null +++ b/pages/api/reminder-mails/[id]/edit.ts @@ -0,0 +1,62 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { ReminderMailResponse } from "@lib/types"; +import { + schemaReminderMailBodyParams, + schemaReminderMailPublic, + withValidReminderMail, +} from "@lib/validations/reminder-mail"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/reminder-mails/{id}/edit: + * patch: + * summary: Edit an existing reminderMail + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to edit + * tags: + * - reminderMails + * responses: + * 201: + * description: OK, reminderMail edited successfuly + * model: ReminderMail + * 400: + * description: Bad request. ReminderMail body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function editReminderMail(req: NextApiRequest, res: NextApiResponse) { + const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); + const safeBody = await schemaReminderMailBodyParams.safeParse(req.body); + + if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); + const reminderMail = await prisma.reminderMail.update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }); + const data = schemaReminderMailPublic.parse(reminderMail); + + if (data) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, + error, + }); +} + +export default withMiddleware("HTTP_PATCH")( + withValidQueryIdTransformParseInt(withValidReminderMail(editReminderMail)) +); diff --git a/pages/api/reminder-mails/[id]/index.ts b/pages/api/reminder-mails/[id]/index.ts new file mode 100644 index 0000000000..3554f87b64 --- /dev/null +++ b/pages/api/reminder-mails/[id]/index.ts @@ -0,0 +1,51 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { ReminderMailResponse } from "@lib/types"; +import { schemaReminderMailPublic } from "@lib/validations/reminder-mail"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/reminder-mails/{id}: + * get: + * summary: Get a reminderMail by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to get + * tags: + * - reminderMails + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: ReminderMail was not found + */ +export async function reminderMailById(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query"); + + const reminderMail = await prisma.reminderMail.findUnique({ where: { id: safe.data.id } }); + const data = schemaReminderMailPublic.parse(reminderMail); + + if (reminderMail) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "ReminderMail was not found", + error, + }); +} + +export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(reminderMailById)); diff --git a/pages/api/reminder-mails/index.ts b/pages/api/reminder-mails/index.ts new file mode 100644 index 0000000000..546dc58dc7 --- /dev/null +++ b/pages/api/reminder-mails/index.ts @@ -0,0 +1,37 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import { ReminderMailsResponse } from "@lib/types"; +import { schemaReminderMailPublic } from "@lib/validations/reminder-mail"; + +/** + * @swagger + * /api/reminder-mails: + * get: + * summary: Get all reminderMails + * tags: + * - reminderMails + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No reminderMails were found + */ +async function allReminderMails(_: NextApiRequest, res: NextApiResponse) { + const reminderMails = await prisma.reminderMail.findMany(); + const data = reminderMails.map((reminderMail) => schemaReminderMailPublic.parse(reminderMail)); + + if (data) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "No ReminderMails were found", + error, + }); +} + +export default withMiddleware("HTTP_GET")(allReminderMails); diff --git a/pages/api/reminder-mails/new.ts b/pages/api/reminder-mails/new.ts new file mode 100644 index 0000000000..85afef2a34 --- /dev/null +++ b/pages/api/reminder-mails/new.ts @@ -0,0 +1,52 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { ReminderMailResponse } from "@lib/types"; +import { + schemaReminderMailBodyParams, + schemaReminderMailPublic, + withValidReminderMail, +} from "@lib/validations/reminder-mail"; + +/** + * @swagger + * /api/reminder-mails/new: + * post: + * summary: Creates a new reminderMail + * requestBody: + * description: Optional description in *Markdown* + * required: true + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/ReminderMail' + * tags: + * - reminderMails + * responses: + * 201: + * description: OK, reminderMail created + * model: ReminderMail + * 400: + * description: Bad request. ReminderMail body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +async function createReminderMail(req: NextApiRequest, res: NextApiResponse) { + const safe = schemaReminderMailBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body", safe.error); + + const reminderMail = await prisma.reminderMail.create({ data: safe.data }); + const data = schemaReminderMailPublic.parse(reminderMail); + + if (data) res.status(201).json({ data, message: "ReminderMail created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new reminderMail", + error, + }); +} + +export default withMiddleware("HTTP_POST")(withValidReminderMail(createReminderMail));