fix: move all req to deconstructed

This commit is contained in:
Agusti Fernandez Pardo committed 2022-04-30 20:53:19 +02:00
1 parent e27600b8c7
commit b572e4b0ff
21 files changed
+148 -140

No files matched your search

+2 -2
View File
@@ -10,7 +10,7 @@ import {
} from "@lib/validations/availability";
async function createOrlistAllAvailabilities(
{ method, userId }: NextApiRequest,
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<AvailabilitiesResponse | AvailabilityResponse>
) {
if (method === "GET") {
@@ -58,7 +58,7 @@ async function createOrlistAllAvailabilities(
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaAvailabilityCreateBodyParams.safeParse(req.body);
const safe = schemaAvailabilityCreateBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.availability.create({ data: { ...safe.data, userId } });
+48 -45
View File
@@ -27,32 +27,33 @@ export async function bookingReferenceById(
if (!userWithBookings) throw new Error("User not found");
const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat();
const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safeQuery.data.id } });
if (!bookingReference) throw new Error("BookingReference not found");
if (!bookingReference?.bookingId) throw new Error("BookingReference: bookingId not found");
if (userBookingIds.includes(bookingReference.bookingId)) {
switch (method) {
/**
* @swagger
* /booking-references/{id}:
* get:
* summary: Find a booking reference
* 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":
/**
* @swagger
* /booking-references/{id}:
* get:
* summary: Find a booking reference
* 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
*/
await prisma.bookingReference
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaBookingReferenceReadPublic.parse(data))
@@ -63,30 +64,32 @@ export async function bookingReferenceById(
error,
})
);
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":
/**
* @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.
*/
const safeBody = schemaBookingEditBodyParams.safeParse(body);
if (!safeBody.success) {
throw new Error("Invalid request body");
+1
View File
@@ -75,6 +75,7 @@ async function createOrlistAllBookingReferences(
throw new Error("User not found");
}
const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat();
if (!safe.data.bookingId) throw new Error("BookingReference: bookingId not found");
if (!userBookingIds.includes(safe.data.bookingId)) res.status(401).json({ message: "Unauthorized" });
else {
const booking_reference = await prisma.bookingReference.create({
+2 -2
View File
@@ -7,7 +7,7 @@ import { BookingResponse, BookingsResponse } from "@lib/types";
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
async function createOrlistAllBookings(
{ method, userId }: NextApiRequest,
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<BookingsResponse | BookingResponse>
) {
if (method === "GET") {
@@ -51,7 +51,7 @@ async function createOrlistAllBookings(
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaBookingCreateBodyParams.safeParse(req.body);
const safe = schemaBookingCreateBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.booking.create({ data: { ...safe.data, userId } });
+1 -3
View File
@@ -21,9 +21,7 @@ import { schemaPaymentPublic } from "@lib/validations/payment";
* 404:
* description: No payments were found
*/
async function allPayments(req: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {
const userId = req.userId;
async function allPayments({ userId }: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
+4 -3
View File
@@ -10,12 +10,13 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
export async function scheduleById(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const { method, query, body } = req;
export async function scheduleById(
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<ScheduleResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaScheduleBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
const userSchedules = await prisma.schedule.findMany({ where: { userId } });
const userScheduleIds = userSchedules.map((schedule) => schedule.id);
if (!userScheduleIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
+2 -5
View File
@@ -7,12 +7,9 @@ import { ScheduleResponse, SchedulesResponse } from "@lib/types";
import { schemaScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
async function createOrlistAllSchedules(
req: NextApiRequest,
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) {
const { method } = req;
const userId = req.userId;
if (method === "GET") {
/**
* @swagger
@@ -54,7 +51,7 @@ async function createOrlistAllSchedules(
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaScheduleBodyParams.safeParse(req.body);
const safe = schemaScheduleBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.schedule.create({ data: { ...safe.data, userId } });
const schedule = schemaSchedulePublic.parse(data);
+1 -3
View File
@@ -11,16 +11,14 @@ import {
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
export async function selectedCalendarById(
req: NextApiRequest,
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<SelectedCalendarResponse>
) {
const { method, query, body } = req;
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 [paramUserId, integration, externalId] = safeQuery.data.id.split("_");
const userId = req.userId;
if (userId !== parseInt(paramUserId)) res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
+2 -2
View File
@@ -10,7 +10,7 @@ import {
} from "@lib/validations/selected-calendar";
async function createOrlistAllSelectedCalendars(
{ method, userId }: NextApiRequest,
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<SelectedCalendarsResponse | SelectedCalendarResponse>
) {
if (method === "GET") {
@@ -67,7 +67,7 @@ async function createOrlistAllSelectedCalendars(
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaSelectedCalendarBodyParams.safeParse(req.body);
const safe = schemaSelectedCalendarBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body");
// Create new selectedCalendar connecting it to current userId
const data = await prisma.selectedCalendar.create({
+4 -3
View File
@@ -68,12 +68,13 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team";
* 401:
* description: Authorization information is missing or invalid.
*/
export async function teamById(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
const { method, query, body } = req;
export async function teamById(
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<TeamResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaTeamBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
const userWithMemberships = await prisma.membership.findMany({
where: { userId: userId },
});
+35 -30
View File
@@ -7,36 +7,26 @@ import { TeamResponse, TeamsResponse } from "@lib/types";
import { schemaMembershipPublic } from "@lib/validations/membership";
import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team";
/**
* @swagger
* /teams:
* get:
* summary: Find all teams
* tags:
* - teams
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No teams were found
* post:
* summary: Creates a new team
* tags:
* - teams
* responses:
* 201:
* description: OK, team created
* 400:
* description: Bad request. Team body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<TeamsResponse | TeamResponse>) {
const { method } = req;
const userId = req.userId;
async function createOrlistAllTeams(
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<TeamsResponse | TeamResponse>
) {
if (method === "GET") {
/**
* @swagger
* /teams:
* get:
* summary: Find all teams
* tags:
* - teams
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No teams were found
*/
const userWithMemberships = await prisma.membership.findMany({
where: { userId: userId },
});
@@ -50,7 +40,22 @@ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<Te
error,
});
} else if (method === "POST") {
const safe = schemaTeamBodyParams.safeParse(req.body);
/**
* @swagger
* /teams:
* post:
* summary: Creates a new team
* tags:
* - teams
* responses:
* 201:
* description: OK, team created
* 400:
* description: Bad request. Team body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaTeamBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body");
const team = await prisma.team.create({ data: safe.data });
// We're also creating the relation membership of team ownership in this call.
+1 -2
View File
@@ -71,8 +71,7 @@ import { schemaUserEditBodyParams, schemaUserReadPublic } from "@lib/validations
* 401:
* description: Authorization information is missing or invalid.
*/
export async function userById(req: NextApiRequest, res: NextApiResponse<any>) {
const { method, query, body, userId } = req;
export async function userById({ method, query, body, userId }: NextApiRequest, res: NextApiResponse<any>) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
console.log(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
+1 -2
View File
@@ -22,8 +22,7 @@ import { schemaUserReadPublic } from "@lib/validations/user";
* 404:
* description: No users were found
*/
async function allUsers(req: NextApiRequest, res: NextApiResponse<UsersResponse>) {
const userId = req.userId;
async function allUsers({ userId }: NextApiRequest, res: NextApiResponse<UsersResponse>) {
const data = await prisma.user.findMany({
where: {
id: userId,