feat: initial work unifying new endpoint and generating api key

This commit is contained in:
Agusti Fernandez Pardo
2022-04-07 03:29:53 +02:00
parent a1dcfa59bc
commit fc2978a61b
8 changed files with 135 additions and 92 deletions
+44 -13
View File
@@ -3,8 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { AttendeesResponse } from "@lib/types";
import { schemaAttendeePublic } from "@lib/validations/attendee";
import { AttendeeResponse, AttendeesResponse } from "@lib/types";
import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
/**
* @swagger
@@ -20,18 +20,49 @@ import { schemaAttendeePublic } from "@lib/validations/attendee";
* description: Authorization information is missing or invalid.
* 404:
* description: No attendees were found
* post:
* summary: Creates a new attendee
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee created
* model: Attendee
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allAttendees(_: NextApiRequest, res: NextApiResponse<AttendeesResponse>) {
const attendees = await prisma.attendee.findMany();
const data = attendees.map((attendee) => schemaAttendeePublic.parse(attendee));
async function createOrlistAllAttendees(
req: NextApiRequest,
res: NextApiResponse<AttendeesResponse | AttendeeResponse>
) {
const { method } = req;
if (method === "GET") {
const attendees = await prisma.attendee.findMany();
const data = attendees.map((attendee) => schemaAttendeePublic.parse(attendee));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Attendees were found",
error,
});
} else if (method === "POST") {
const safe = schemaAttendeeBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Attendees were found",
error,
});
const attendee = await prisma.attendee.create({ data: safe.data });
const data = schemaAttendeePublic.parse(attendee);
if (data) res.status(201).json({ data, message: "Attendee created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new attendee",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allAttendees);
export default withMiddleware("HTTP_GET_OR_POST")(withValidAttendee(createOrlistAllAttendees));
-41
View File
@@ -1,41 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AttendeeResponse } from "@lib/types";
import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
/**
* @swagger
* /api/attendees/new:
* post:
* summary: Creates a new attendee
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee created
* model: Attendee
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createAttendee(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const safe = schemaAttendeeBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const attendee = await prisma.attendee.create({ data: safe.data });
const data = schemaAttendeePublic.parse(attendee);
if (data) res.status(201).json({ data, message: "Attendee created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new attendee",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidAttendee(createAttendee));
+8 -8
View File
@@ -5,13 +5,16 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { PaymentResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic } from "@lib/validations/payment";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /api/payments/{id}:
* get:
* summary: Get a payment by ID
* summary: Get an payment by ID
* parameters:
* - in: path
* name: id
@@ -78,17 +81,14 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
*/
export async function paymentById(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaPaymentBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const [userId, teamId] = safeQuery.data.id.split("_");
switch (method) {
case "GET":
await prisma.payment
.findUnique({
where: { userId_teamId: { userId: parseInt(userId), teamId: parseInt(teamId) } },
})
.findUnique({ where: { id: safeQuery.data.id } })
.then((payment) => schemaPaymentPublic.parse(payment))
.then((data) => res.status(200).json({ data }))
.catch((error: Error) =>
@@ -127,4 +127,4 @@ export async function paymentById(req: NextApiRequest, res: NextApiResponse<Paym
}
}
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdString(paymentById));
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(paymentById));