fix: improve docs in attendees availabilities and booking references

pull/9078/head
Agusti Fernandez Pardo 2022-04-29 02:30:59 +02:00
parent d6c34a8e51
commit e13ea234b8
6 changed files with 360 additions and 342 deletions

View File

@ -10,90 +10,10 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /attendees/{id}:
* get:
* summary: Find an attendee by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to get
* example: 3
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Attendee was not found
* patch:
* summary: Edit an existing attendee
* requestBody:
* description: Edit an existing attendee related to one of your bookings
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - bookingId
* - name
* - email
* - timeZone
* properties:
* email:
* type: string
* example: email@example.com
* name:
* type: string
* example: John Doe
* timeZone:
* type: string
* example: Europe/London
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* example: 3
* required: true
* description: Numeric ID of the attendee to edit
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee edited successfuly
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
* delete:
* summary: Remove an existing attendee
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to delete
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee removed successfuly
* 400:
* description: Bad request. Attendee id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function attendeeById(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const { method, query, body, userId } = req;
export async function attendeeById(
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<AttendeeResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) {
res.status(400).json({ error: safeQuery.error });
@ -109,6 +29,29 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
if (!attendeeIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
/**
* @swagger
* /attendees/{id}:
* get:
* summary: Find an attendee by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to get
* example: 3
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Attendee was not found
*/
case "GET":
await prisma.attendee
.findUnique({ where: { id: safeQuery.data.id } })
@ -121,7 +64,51 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
})
);
break;
/**
* @swagger
* /attendees/{id}:
* patch:
* summary: Edit an existing attendee
* requestBody:
* description: Edit an existing attendee related to one of your bookings
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - bookingId
* - name
* - email
* - timeZone
* properties:
* email:
* type: string
* example: email@example.com
* name:
* type: string
* example: John Doe
* timeZone:
* type: string
* example: Europe/London
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* example: 3
* required: true
* description: Numeric ID of the attendee to edit
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee edited successfuly
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "PATCH":
const safeBody = schemaAttendeeEditBodyParams.safeParse(body);
if (!safeBody.success) {
@ -139,7 +126,28 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
})
);
break;
/**
* @swagger
* /attendees/{id}:
* delete:
* summary: Remove an existing attendee
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to delete
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee removed successfuly
* 400:
* description: Bad request. Attendee id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "DELETE":
await prisma.attendee
.delete({ where: { id: safeQuery.data.id } })

View File

@ -6,63 +6,10 @@ import { withMiddleware } from "@lib/helpers/withMiddleware";
import { AttendeeResponse, AttendeesResponse } from "@lib/types";
import { schemaAttendeeCreateBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee";
/**
* @swagger
* /attendees:
* get:
* summary: Find all attendees
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No attendees were found
* post:
* summary: Creates a new attendee
* requestBody:
* description: Create a new attendee related to one of your bookings
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - bookingId
* - name
* - email
* - timeZone
* properties:
* bookingId:
* type: number
* example: 1
* email:
* type: string
* example: email@example.com
* name:
* type: string
* example: John Doe
* timeZone:
* type: string
* example: Europe/London
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee created
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllAttendees(
req: NextApiRequest,
{ method, userId, body }: NextApiRequest,
res: NextApiResponse<AttendeesResponse | AttendeeResponse>
) {
const { method, userId } = req;
// Here we make sure to only return attendee's of the user's own bookings.
const userBookings = await prisma.booking.findMany({
where: {
userId,
@ -73,6 +20,21 @@ async function createOrlistAllAttendees(
});
const attendees = userBookings.map((booking) => booking.attendees).flat();
if (method === "GET") {
/**
* @swagger
* /attendees:
* get:
* summary: Find all attendees
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No attendees were found
*/
if (attendees) res.status(200).json({ attendees });
else
(error: Error) =>
@ -81,13 +43,51 @@ async function createOrlistAllAttendees(
error,
});
} else if (method === "POST") {
const safePost = schemaAttendeeCreateBodyParams.safeParse(req.body);
/**
* @swagger
* /attendees:
* post:
* summary: Creates a new attendee
* requestBody:
* description: Create a new attendee related to one of your bookings
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - bookingId
* - name
* - email
* - timeZone
* properties:
* bookingId:
* type: number
* example: 1
* email:
* type: string
* example: email@example.com
* name:
* type: string
* example: John Doe
* timeZone:
* type: string
* example: Europe/London
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee created
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
const safePost = schemaAttendeeCreateBodyParams.safeParse(body);
if (!safePost.success) {
console.log(safePost.error);
res.status(400).json({ error: safePost.error });
throw new Error("Invalid request body", safePost.error);
}
const userId = req.userId;
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },

View File

@ -13,80 +13,41 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /availabilities/{id}:
* get:
* summary: Find an availability by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to get
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Availability was not found
* patch:
* summary: Edit an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to edit
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability edited successfuly
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
* delete:
* summary: Remove an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to delete
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability removed successfuly
* 400:
* description: Bad request. Availability id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const { method, query, body } = req;
export async function availabilityById(
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<AvailabilityResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
const data = await prisma.availability.findMany({ where: { userId } });
const availabiltiesIds = data.map((availability) => availability.id);
if (!availabiltiesIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
/**
* @swagger
* /availabilities/{id}:
* get:
* summary: Find an availability by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to get
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Availability was not found
*/
case "GET":
await prisma.availability
.findUnique({ where: { id: safeQuery.data.id } })
@ -96,7 +57,30 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error })
);
break;
/**
* @swagger
* /availabilities/{id}:
* patch:
* summary: Edit an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to edit
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability edited successfuly
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "PATCH":
const safeBody = schemaAvailabilityEditBodyParams.safeParse(body);
if (!safeBody.success) throw new Error("Invalid request body");
@ -115,14 +99,36 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
.then((data) => schemaAvailabilityReadPublic.parse(data))
.then((availability) => res.status(200).json({ availability }))
.catch((error: Error) => {
console.log(error);
res.status(404).json({
message: `Availability with id: ${safeQuery.data.id} not found`,
error,
});
});
break;
/**
* @swagger
* /availabilities/{id}:
* delete:
* summary: Remove an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to delete
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability removed successfuly
* 400:
* description: Bad request. Availability id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "DELETE":
await prisma.availability
.delete({ where: { id: safeQuery.data.id } })

View File

@ -9,44 +9,28 @@ import {
schemaAvailabilityReadPublic,
} from "@lib/validations/availability";
/**
* @swagger
* /availabilities:
* get:
* summary: Find all availabilities
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No availabilities were found
* post:
* summary: Creates a new availability
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability created
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllAvailabilities(
req: NextApiRequest,
{ method, userId }: NextApiRequest,
res: NextApiResponse<AvailabilitiesResponse | AvailabilityResponse>
) {
const { method } = req;
const userId = req.userId;
if (method === "GET") {
/**
* @swagger
* /availabilities:
* get:
* summary: Find all availabilities
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No availabilities were found
*/
const data = await prisma.availability.findMany({ where: { userId } });
const availabilities = data.map((availability) => schemaAvailabilityReadPublic.parse(availability));
if (availabilities) res.status(200).json({ availabilities });
@ -57,11 +41,27 @@ async function createOrlistAllAvailabilities(
error,
});
} else if (method === "POST") {
/**
* @swagger
* /availabilities:
* post:
* summary: Creates a new availability
* tags:
* - availabilities
* externalDocs:
* url: https://docs.cal.com/availability
* responses:
* 201:
* description: OK, availability created
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaAvailabilityCreateBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.availability.create({ data: { ...safe.data, userId } });
console.log(data);
const availability = schemaAvailabilityReadPublic.parse(data);
if (availability) res.status(201).json({ availability, message: "Availability created successfully" });

View File

@ -14,73 +14,12 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /booking-references/{id}:
* get:
* summary: Find a booking reference by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to get
* tags:
* - booking-references
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: BookingReference was not found
* patch:
* summary: Edit an existing booking reference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to edit
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference edited successfuly
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
* delete:
* summary: Remove an existing booking reference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to delete
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference removed successfuly
* 400:
* description: Bad request. BookingReference id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function bookingReferenceById(
req: NextApiRequest,
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<BookingReferenceResponse>
) {
const { method, query, body } = req;
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
@ -91,6 +30,28 @@ export async function bookingReferenceById(
if (!bookingReference) throw new Error("BookingReference not found");
if (userBookingIds.includes(bookingReference.bookingId)) {
switch (method) {
/**
* @swagger
* /booking-references/{id}:
* get:
* summary: Find a booking reference by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to get
* tags:
* - booking-references
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: BookingReference was not found
*/
case "GET":
await prisma.bookingReference
.findUnique({ where: { id: safeQuery.data.id } })
@ -103,10 +64,30 @@ export async function bookingReferenceById(
})
);
break;
/**
* @swagger
* /booking-references/{id}:
* patch:
* summary: Edit an existing booking reference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to edit
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference edited successfuly
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "PATCH":
const safeBody = schemaBookingEditBodyParams.safeParse(body);
if (!safeBody.success) {
throw new Error("Invalid request body");
}
@ -121,7 +102,28 @@ export async function bookingReferenceById(
})
);
break;
/**
* @swagger
* /booking-references/{id}:
* delete:
* summary: Remove an existing booking reference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking reference to delete
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference removed successfuly
* 400:
* description: Bad request. BookingReference id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
case "DELETE":
await prisma.bookingReference
.delete({

View File

@ -9,38 +9,10 @@ import {
schemaBookingReferenceReadPublic,
} from "@lib/validations/booking-reference";
/**
* @swagger
* /booking-references:
* get:
* summary: Find all booking references
* tags:
* - booking-references
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No booking references were found
* post:
* summary: Creates a new booking reference
* tags:
* - booking-references
* responses:
* 201:
* description: OK, booking reference created
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllBookingReferences(
req: NextApiRequest,
{ method, userId }: NextApiRequest,
res: NextApiResponse<BookingReferencesResponse | BookingReferenceResponse>
) {
const { method } = req;
const userId = req.userId;
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
@ -48,6 +20,21 @@ async function createOrlistAllBookingReferences(
if (!userWithBookings) throw new Error("User not found");
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
if (method === "GET") {
/**
* @swagger
* /booking-references:
* get:
* summary: Find all booking references
* tags:
* - booking-references
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No booking references were found
*/
const data = await prisma.bookingReference.findMany({ where: { id: { in: userBookingIds } } });
const booking_references = data.map((bookingReference) =>
schemaBookingReferenceReadPublic.parse(bookingReference)
@ -60,6 +47,21 @@ async function createOrlistAllBookingReferences(
error,
});
} else if (method === "POST") {
/**
* @swagger
* /booking-references:
* post:
* summary: Creates a new booking reference
* tags:
* - booking-references
* responses:
* 201:
* description: OK, booking reference created
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaBookingCreateBodyParams.safeParse(req.body);
if (!safe.success) {
throw new Error("Invalid request body");