payments safe, unify endpoints for remindermails schedules

This commit is contained in:
Agusti Fernandez Pardo
2022-04-10 02:10:34 +02:00
parent af72b1f5b5
commit d95325c06c
26 changed files with 167 additions and 434 deletions
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function attendeeById(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaAttendeeBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaAttendeeBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaAvailabilityBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaAvailabilityBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -87,8 +87,8 @@ export async function bookingReferenceById(
res: NextApiResponse<BookingReferenceResponse>
) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaBookingReferenceBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaBookingReferenceBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function bookingById(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaBookingBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaBookingBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function credentialById(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaCredentialBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaCredentialBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -87,8 +87,8 @@ export async function dailyEventReferenceById(
res: NextApiResponse<DailyEventReferenceResponse>
) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaDailyEventReferenceBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -87,8 +87,8 @@ export async function destionationCalendarById(
res: NextApiResponse<DestinationCalendarResponse>
) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaDestinationCalendarBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaDestinationCalendarBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -84,8 +84,8 @@ import {
*/
async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventTypeCustomInputResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventTypeResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaEventTypeBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaEventTypeBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+2 -2
View File
@@ -96,8 +96,8 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
*/
export async function membershipById(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeBody = await schemaMembershipBodyParams.safeParse(body);
const safeQuery = schemaQueryIdAsString.safeParse(query);
const safeBody = schemaMembershipBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
// This is how we set the userId and teamId in the query for managing compoundId.
const [userId, teamId] = safeQuery.data.id.split("_");
+27 -93
View File
@@ -4,7 +4,8 @@ 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 { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaPaymentPublic } from "@lib/validations/payment";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
@@ -14,7 +15,7 @@ import {
* @swagger
* /api/payments/{id}:
* get:
* summary: Get an payment by ID
* summary: Get a payment by ID
* parameters:
* - in: path
* name: id
@@ -31,100 +32,33 @@ import {
* description: Authorization information is missing or invalid.
* 404:
* description: Payment was not found
* patch:
* summary: Edit an existing payment
* consumes:
* - application/json
* parameters:
* - in: body
* name: payment
* description: The payment to edit
* schema:
* type: object
* $ref: '#/components/schemas/Payment'
* required: true
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the payment to edit
* tags:
* - payments
* responses:
* 201:
* description: OK, payment edited successfuly
* model: Payment
* 400:
* description: Bad request. Payment body is invalid.
* 401:
* description: Authorization information is missing or invalid.
* delete:
* summary: Remove an existing payment
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the payment to delete
* tags:
* - payments
* responses:
* 201:
* description: OK, payment removed successfuly
* model: Payment
* 400:
* description: Bad request. Payment id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function paymentById(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaPaymentBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const { method, query } = req;
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const userId = await getCalcomUserId(res);
switch (method) {
case "GET":
await prisma.payment
.findUnique({ where: { id: safeQuery.data.id } })
.then((payment) => schemaPaymentPublic.parse(payment))
.then((data) => res.status(200).json({ data }))
.catch((error: Error) =>
res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })
);
break;
case "PATCH":
if (!safeBody.success) throw new Error("Invalid request body");
await prisma.payment
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
if (safeQuery.success && method === "GET") {
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
});
await prisma.payment
.findUnique({ where: { id: safeQuery.data.id } })
.then((payment) => schemaPaymentPublic.parse(payment))
.then((data) => {
if (userWithBookings?.bookings.map((b) => b.id).includes(data.bookingId)) {
res.status(200).json({ data });
} else {
res.status(401).json({ message: "Unauthorized" });
}
})
.catch((error: Error) =>
res.status(404).json({
message: `Payment with id: ${safeQuery.data.id} not found`,
error,
})
.then((payment) => schemaPaymentPublic.parse(payment))
.then((data) => res.status(200).json({ data }))
.catch((error: Error) =>
res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })
);
break;
case "DELETE":
await prisma.payment
.delete({ where: { id: safeQuery.data.id } })
.then(() =>
res.status(200).json({ message: `Payment with id: ${safeQuery.data.id} deleted successfully` })
)
.catch((error: Error) =>
res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })
);
break;
default:
res.status(405).json({ message: "Method not allowed" });
break;
);
}
}
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(paymentById));
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(paymentById));
+11 -1
View File
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { PaymentsResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaPaymentPublic } from "@lib/validations/payment";
/**
@@ -22,7 +23,16 @@ import { schemaPaymentPublic } from "@lib/validations/payment";
* description: No payments were found
*/
async function allPayments(_: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {
const payments = await prisma.payment.findMany();
const userId = await getCalcomUserId(res);
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
});
if (!userWithBookings) throw new Error("No user found");
const bookings = userWithBookings.bookings;
const bookingIds = bookings.map((booking) => booking.id);
const payments = await prisma.payment.findMany({ where: { bookingId: { in: bookingIds } } });
const data = payments.map((payment) => schemaPaymentPublic.parse(payment));
if (data) res.status(200).json({ data });
-48
View File
@@ -1,48 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { PaymentResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
/**
* @swagger
* /api/payments/new:
* post:
* summary: Creates a new payment
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Payment'
* tags:
* - payments
* responses:
* 201:
* description: OK, payment created
* model: Payment
* 400:
* description: Bad request. Payment body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createPayment(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
const safe = schemaPaymentBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const payment = await prisma.payment.create({ data: safe.data });
const data = schemaPaymentPublic.parse(payment);
if (data) res.status(201).json({ data, message: "Payment created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new payment",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidPayment(createPayment));
+2 -2
View File
@@ -81,8 +81,8 @@ import {
*/
export async function reminderMailById(req: NextApiRequest, res: NextApiResponse<ReminderMailResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaReminderMailBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaReminderMailBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+50 -15
View File
@@ -3,12 +3,16 @@ 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";
import { ReminderMailResponse, ReminderMailsResponse } from "@lib/types";
import {
schemaReminderMailBodyParams,
schemaReminderMailPublic,
withValidReminderMail,
} from "@lib/validations/reminder-mail";
/**
* @swagger
* /api/reminder-mails:
* /v1/reminder-mails:
* get:
* summary: Get all reminder mails
* tags:
@@ -19,19 +23,50 @@ import { schemaReminderMailPublic } from "@lib/validations/reminder-mail";
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No reminderMails were found
* description: No reminder mails were found
* post:
* summary: Creates a new reminder mail
* tags:
* - reminder-mails
* responses:
* 201:
* description: OK, reminder mail created
* model: ReminderMail
* 400:
* description: Bad request. ReminderMail body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allReminderMails(_: NextApiRequest, res: NextApiResponse<ReminderMailsResponse>) {
const reminderMails = await prisma.reminderMail.findMany();
const data = reminderMails.map((reminderMail) => schemaReminderMailPublic.parse(reminderMail));
async function createOrlistAllReminderMails(
req: NextApiRequest,
res: NextApiResponse<ReminderMailsResponse | ReminderMailResponse>
) {
const { method } = req;
if (method === "GET") {
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,
});
} else if (method === "POST") {
const safe = schemaReminderMailBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No ReminderMails were found",
error,
});
const reminderMail = await prisma.reminderMail.create({ data: safe.data });
const data = schemaReminderMailPublic.parse(reminderMail);
if (data) res.status(201).json({ data, message: "reminder mail created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "could not create new reminder mail",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allReminderMails);
export default withMiddleware("HTTP_GET_OR_POST")(withValidReminderMail(createOrlistAllReminderMails));
-52
View File
@@ -1,52 +0,0 @@
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 reminder mail
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ReminderMail'
* tags:
* - reminder-mails
* 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<ReminderMailResponse>) {
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));
-68
View File
@@ -1,68 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { PaymentResponse, PaymentsResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
/**
* @swagger
* /api/payments:
* get:
* summary: Get all payments
* tags:
* - payments
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No payments were found
* post:
* summary: Creates a new payment
* tags:
* - payments
* responses:
* 201:
* description: OK, payment created
* model: Payment
* 400:
* description: Bad request. Payment body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllPayments(
req: NextApiRequest,
res: NextApiResponse<PaymentsResponse | PaymentResponse>
) {
const { method } = req;
if (method === "GET") {
const payments = await prisma.payment.findMany();
const data = payments.map((payment) => schemaPaymentPublic.parse(payment));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Payments were found",
error,
});
} else if (method === "POST") {
const safe = schemaPaymentBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const payment = await prisma.payment.create({ data: safe.data });
const data = schemaPaymentPublic.parse(payment);
if (data) res.status(201).json({ data, message: "Payment created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new payment",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));
+3 -3
View File
@@ -12,7 +12,7 @@ import {
/**
* @swagger
* /api/schedules/{id}:
* /v1/schedules/{id}:
* get:
* summary: Get a schedule by ID
* parameters:
@@ -81,8 +81,8 @@ import {
*/
export async function scheduleById(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaScheduleBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaScheduleBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+45 -14
View File
@@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { SchedulesResponse } from "@lib/types";
import { schemaSchedulePublic } from "@lib/validations/schedule";
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
/**
* @swagger
* /api/schedules:
* get:
* summary: Returns all schedules
* summary: Get all schedules
* tags:
* - schedules
* responses:
@@ -20,18 +20,49 @@ import { schemaSchedulePublic } from "@lib/validations/schedule";
* description: Authorization information is missing or invalid.
* 404:
* description: No schedules were found
* post:
* summary: Creates a new schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule created
* model: Schedule
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allSchedules(_: NextApiRequest, res: NextApiResponse<SchedulesResponse>) {
const schedules = await prisma.schedule.findMany();
const data = schedules.map((schedule) => schemaSchedulePublic.parse(schedule));
async function createOrlistAllSchedules(
req: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) {
const { method } = req;
if (method === "GET") {
const schedules = await prisma.schedule.findMany();
const data = schedules.map((schedule) => schemaSchedulePublic.parse(schedule));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Schedules were found",
error,
});
} else if (method === "POST") {
const safe = schemaScheduleBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Schedules were found",
error,
});
const schedule = await prisma.schedule.create({ data: safe.data });
const data = schemaSchedulePublic.parse(schedule);
if (data) res.status(201).json({ data, message: "Schedule created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new schedule",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allSchedules);
export default withMiddleware("HTTP_GET_OR_POST")(withValidSchedule(createOrlistAllSchedules));
-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 { ScheduleResponse } from "@lib/types";
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
/**
* @swagger
* /api/schedules/new:
* post:
* summary: Creates a new schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule created
* model: Schedule
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createSchedule(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const safe = schemaScheduleBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const schedule = await prisma.schedule.create({ data: safe.data });
const data = schemaSchedulePublic.parse(schedule);
if (data) res.status(201).json({ data, message: "Schedule created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new schedule",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidSchedule(createSchedule));
-68
View File
@@ -1,68 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { PaymentResponse, PaymentsResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
/**
* @swagger
* /api/payments:
* get:
* summary: Get all payments
* tags:
* - payments
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No payments were found
* post:
* summary: Creates a new payment
* tags:
* - payments
* responses:
* 201:
* description: OK, payment created
* model: Payment
* 400:
* description: Bad request. Payment body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllPayments(
req: NextApiRequest,
res: NextApiResponse<PaymentsResponse | PaymentResponse>
) {
const { method } = req;
if (method === "GET") {
const payments = await prisma.payment.findMany();
const data = payments.map((payment) => schemaPaymentPublic.parse(payment));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Payments were found",
error,
});
} else if (method === "POST") {
const safe = schemaPaymentBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const payment = await prisma.payment.create({ data: safe.data });
const data = schemaPaymentPublic.parse(payment);
if (data) res.status(201).json({ data, message: "Payment created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new payment",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));
+2 -2
View File
@@ -120,8 +120,8 @@ export async function selectedCalendarById(
res: NextApiResponse<SelectedCalendarResponse>
) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeBody = await schemaSelectedCalendarBodyParams.safeParse(body);
const safeQuery = schemaQueryIdAsString.safeParse(query);
const safeBody = schemaSelectedCalendarBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
// This is how we set the userId and externalId in the query for managing compoundId.
const [userId, integration, externalId] = safeQuery.data.id.split("_");
+3 -3
View File
@@ -13,7 +13,7 @@ import { schemaUserBodyParams, schemaUserPublic } from "@lib/validations/user";
/**
* @swagger
* /api/users/{id}:
* /v1/users/{id}:
* get:
* summary: Get a user by ID, returns your user if regular user.
* parameters:
@@ -82,8 +82,8 @@ import { schemaUserBodyParams, schemaUserPublic } from "@lib/validations/user";
*/
export async function userById(req: NextApiRequest, res: NextApiResponse<UserResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaUserBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaUserBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = getCalcomUserId(res);
if (safeQuery.data.id === userId) {
+1 -1
View File
@@ -33,7 +33,7 @@ import {
* description: Authorization information is missing or invalid.
*/
export async function deleteResource(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
const data = await prisma.resource.delete({ where: { id: safe.data.id } });
+2 -2
View File
@@ -34,8 +34,8 @@ import { schemaResourceBodyParams, schemaResourcePublic, withValidResource } fro
* description: Authorization information is missing or invalid.
*/
export async function editResource(req: NextApiRequest, res: NextApiResponse<ResourceResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaResourceBodyParams.safeParse(req.body);
const safeQuery = schemaQueryIdParseInt.safeParse(req.query);
const safeBody = schemaResourceBodyParams.safeParse(req.body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const resource = await prisma.resource.update({
+1 -1
View File
@@ -33,7 +33,7 @@ import { schemaResourcePublic } from "@lib/validations/resource";
* description: Resource was not found
*/
export async function resourceById(req: NextApiRequest, res: NextApiResponse<ResourceResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
const resource = await prisma.resource.findUnique({ where: { id: safe.data.id } });