feat: unify new/index of attendees, availabilities, booking references, bookings, selected calendars, update response types

This commit is contained in:
Agusti Fernandez Pardo committed 2022-04-11 12:03:15 +02:00
1 parent ddc1b4043c
commit 963c893727
21 files changed
+237 -284

No files matched your search

+34 -42
View File
@@ -27,147 +27,139 @@ export type BaseResponse = {
// User
export type UserResponse = BaseResponse & {
data?: Partial<User>;
user?: Partial<User>;
};
export type UsersResponse = BaseResponse & {
data?: Partial<User>[];
};
// API Key
export type ApiKeyResponse = BaseResponse & {
data?: Partial<ApiKey>;
};
export type ApiKeysResponse = BaseResponse & {
data?: Partial<ApiKey>[];
users?: Partial<User>[];
};
// Team
export type TeamResponse = BaseResponse & {
data?: Partial<Team>;
team?: Partial<Team>;
};
export type TeamsResponse = BaseResponse & {
data?: Partial<Team>[];
teams?: Partial<Team>[];
};
// SelectedCalendar
export type SelectedCalendarResponse = BaseResponse & {
data?: Partial<SelectedCalendar>;
selected_calendar?: Partial<SelectedCalendar>;
};
export type SelectedCalendarsResponse = BaseResponse & {
data?: Partial<SelectedCalendar>[];
selected_calendars?: Partial<SelectedCalendar>[];
};
// Attendee
export type AttendeeResponse = BaseResponse & {
data?: Partial<Attendee>;
attendee?: Partial<Attendee>;
};
// Grouping attendees in booking arrays for now,
// later might remove endpoint and move to booking endpoint altogether.
export type AttendeesResponse = BaseResponse & {
data?: Partial<Attendee>[];
attendees?: Partial<Attendee>[];
};
// Availability
export type AvailabilityResponse = BaseResponse & {
data?: Partial<Availability>;
availability?: Partial<Availability>;
};
export type AvailabilitiesResponse = BaseResponse & {
data?: Partial<Availability>[];
availabilities?: Partial<Availability>[];
};
// BookingReference
export type BookingReferenceResponse = BaseResponse & {
data?: Partial<BookingReference>;
booking_reference?: Partial<BookingReference>;
};
export type BookingReferencesResponse = BaseResponse & {
data?: Partial<BookingReference>[];
booking_references?: Partial<BookingReference>[];
};
// Booking
export type BookingResponse = BaseResponse & {
data?: Partial<Booking>;
booking?: Partial<Booking>;
};
export type BookingsResponse = BaseResponse & {
data?: Partial<Booking>[];
bookings?: Partial<Booking>[];
};
// Credential
export type CredentialResponse = BaseResponse & {
data?: Partial<Credential>;
credential?: Partial<Credential>;
};
export type CredentialsResponse = BaseResponse & {
data?: Partial<Credential>[];
credentials?: Partial<Credential>[];
};
// DailyEventReference
export type DailyEventReferenceResponse = BaseResponse & {
data?: Partial<DailyEventReference>;
daily_event_reference?: Partial<DailyEventReference>;
};
export type DailyEventReferencesResponse = BaseResponse & {
data?: Partial<DailyEventReference>[];
daily_event_references?: Partial<DailyEventReference>[];
};
// DestinationCalendar
export type DestinationCalendarResponse = BaseResponse & {
data?: Partial<DestinationCalendar>;
destination_calendar?: Partial<DestinationCalendar>;
};
export type DestinationCalendarsResponse = BaseResponse & {
data?: Partial<DestinationCalendar>[];
destination_calendars?: Partial<DestinationCalendar>[];
};
// Membership
export type MembershipResponse = BaseResponse & {
data?: Partial<Membership>;
membership?: Partial<Membership>;
};
export type MembershipsResponse = BaseResponse & {
data?: Partial<Membership>[];
memberships?: Partial<Membership>[];
};
// EventTypeCustomInput
export type EventTypeCustomInputResponse = BaseResponse & {
data?: Partial<EventTypeCustomInput>;
event_type_custom_input?: Partial<EventTypeCustomInput>;
};
export type EventTypeCustomInputsResponse = BaseResponse & {
data?: Partial<EventTypeCustomInput>[];
event_type_custom_inputs?: Partial<EventTypeCustomInput>[];
};
// EventType
export type EventTypeResponse = BaseResponse & {
data?: Partial<EventType>;
event_type?: Partial<EventType>;
};
export type EventTypesResponse = BaseResponse & {
data?: Partial<EventType>[];
event_types?: Partial<EventType>[];
};
// Payment
export type PaymentResponse = BaseResponse & {
data?: Partial<Payment>;
payment?: Partial<Payment>;
};
export type PaymentsResponse = BaseResponse & {
data?: Partial<Payment>[];
payments?: Partial<Payment>[];
};
// Schedule
export type ScheduleResponse = BaseResponse & {
data?: Partial<Schedule>;
schedule?: Partial<Schedule>;
};
export type SchedulesResponse = BaseResponse & {
data?: Partial<Schedule>[];
schedules?: Partial<Schedule>[];
};
// Webhook
export type WebhookResponse = BaseResponse & {
data?: Partial<Webhook>;
webhook?: Partial<Webhook>;
};
export type WebhooksResponse = BaseResponse & {
data?: Partial<Webhook>[];
webhooks?: Partial<Webhook>[];
};
// ReminderMail
export type ReminderMailResponse = BaseResponse & {
data?: Partial<ReminderMail>;
reminder_mail?: Partial<ReminderMail>;
};
export type ReminderMailsResponse = BaseResponse & {
data?: Partial<ReminderMail>[];
reminder_mails?: Partial<ReminderMail>[];
};
+4 -4
View File
@@ -89,8 +89,8 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
case "GET":
await prisma.attendee
.findUnique({ where: { id: safeQuery.data.id } })
.then((attendee) => schemaAttendeePublic.parse(attendee))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaAttendeePublic.parse(data))
.then((attendee) => res.status(200).json({ attendee }))
.catch((error: Error) =>
res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error })
);
@@ -103,8 +103,8 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((attendee) => schemaAttendeePublic.parse(attendee))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaAttendeePublic.parse(data))
.then((attendee) => res.status(200).json({ attendee }))
.catch((error: Error) =>
res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error })
);
+5 -5
View File
@@ -49,9 +49,9 @@ async function createOrlistAllAttendees(
attendees: true,
},
});
const userBookingsAttendees = userBookings.map((booking) => booking.attendees).flat();
const attendees = userBookings.map((booking) => booking.attendees).flat();
if (method === "GET") {
if (userBookingsAttendees) res.status(200).json({ data: userBookingsAttendees });
if (attendees) res.status(200).json({ attendees });
else
(error: Error) =>
res.status(404).json({
@@ -67,12 +67,12 @@ async function createOrlistAllAttendees(
const bookingId = safe.data.bookingId;
delete safe.data.bookingId;
const noBookingId = safe.data;
const attendee = await prisma.attendee.create({
const data = await prisma.attendee.create({
data: { ...noBookingId, booking: { connect: { id: parseInt(bookingId as string) } } },
});
const data = schemaAttendeePublic.parse(attendee);
const attendee = schemaAttendeePublic.parse(data);
if (data) res.status(201).json({ data, message: "Attendee created successfully" });
if (attendee) res.status(201).json({ attendee, message: "Attendee created successfully" });
else
(error: Error) =>
res.status(400).json({
+4 -4
View File
@@ -89,8 +89,8 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
case "GET":
await prisma.availability
.findUnique({ where: { id: safeQuery.data.id } })
.then((availability) => schemaAvailabilityPublic.parse(availability))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaAvailabilityPublic.parse(data))
.then((availability) => res.status(200).json({ availability }))
.catch((error: Error) =>
res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error })
);
@@ -103,8 +103,8 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((availability) => schemaAvailabilityPublic.parse(availability))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaAvailabilityPublic.parse(data))
.then((availability) => res.status(200).json({ availability }))
.catch((error: Error) =>
res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error })
);
+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 { AvailabilitiesResponse } from "@lib/types";
import { schemaAvailabilityPublic } from "@lib/validations/availability";
import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types";
import { schemaAvailabilityBodyParams, schemaAvailabilityPublic } from "@lib/validations/availability";
/**
* @swagger
@@ -20,18 +20,49 @@ import { schemaAvailabilityPublic } from "@lib/validations/availability";
* description: Authorization information is missing or invalid.
* 404:
* description: No availabilities were found
* post:
* summary: Creates a new availability
* tags:
* - availabilities
* responses:
* 201:
* description: OK, availability created
* model: Availability
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allAvailabilities(_: NextApiRequest, res: NextApiResponse<AvailabilitiesResponse>) {
const availabilities = await prisma.availability.findMany();
const data = availabilities.map((availability) => schemaAvailabilityPublic.parse(availability));
async function createOrlistAllAvailabilities(
req: NextApiRequest,
res: NextApiResponse<AvailabilitiesResponse | AvailabilityResponse>
) {
const { method } = req;
if (method === "GET") {
const data = await prisma.availability.findMany();
const availabilities = data.map((availability) => schemaAvailabilityPublic.parse(availability));
if (availabilities) res.status(200).json({ availabilities });
else
(error: Error) =>
res.status(404).json({
message: "No Availabilities were found",
error,
});
} else if (method === "POST") {
const safe = schemaAvailabilityBodyParams.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 Availabilities were found",
error,
});
const data = await prisma.availability.create({ data: safe.data });
const availability = schemaAvailabilityPublic.parse(data);
if (availability) res.status(201).json({ availability, message: "Availability created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new availability",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allAvailabilities);
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllAvailabilities);
-51
View File
@@ -1,51 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AvailabilityResponse } from "@lib/types";
import {
schemaAvailabilityBodyParams,
schemaAvailabilityPublic,
withValidAvailability,
} from "@lib/validations/availability";
/**
* @swagger
* /api/availabilities/new:
* post:
* summary: Creates a new availability
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Availability'
* tags:
* - availabilities
* responses:
* 201:
* description: OK, availability created
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createAvailability(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const safe = schemaAvailabilityBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const availability = await prisma.availability.create({ data: safe.data });
const data = schemaAvailabilityPublic.parse(availability);
if (data) res.status(201).json({ data, message: "Availability created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new availability",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidAvailability(createAvailability));
+3 -3
View File
@@ -96,7 +96,7 @@ export async function bookingReferenceById(
await prisma.bookingReference
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaBookingReferencePublic.parse(data))
.then((data) => res.status(200).json({ data }))
.then((booking_reference) => res.status(200).json({ booking_reference }))
.catch((error: Error) =>
res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error })
);
@@ -109,8 +109,8 @@ export async function bookingReferenceById(
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((bookingReference) => schemaBookingReferencePublic.parse(bookingReference))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaBookingReferencePublic.parse(data))
.then((booking_reference) => res.status(200).json({ booking_reference }))
.catch((error: Error) =>
res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error })
);
+51 -17
View File
@@ -3,14 +3,17 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { BookingReferencesResponse } from "@lib/types";
import { schemaBookingReferencePublic } from "@lib/validations/booking-reference";
import { BookingReferenceResponse, BookingReferencesResponse } from "@lib/types";
import {
schemaBookingReferenceBodyParams,
schemaBookingReferencePublic,
} from "@lib/validations/booking-reference";
/**
* @swagger
* /api/booking-references:
* get:
* summary: Get all bookingReferences
* summary: Get all booking references
* tags:
* - booking-references
* responses:
@@ -19,21 +22,52 @@ import { schemaBookingReferencePublic } from "@lib/validations/booking-reference
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No bookingReferences were found
* description: No booking references were found
* post:
* summary: Creates a new booking reference
* tags:
* - booking-references
* responses:
* 201:
* description: OK, booking reference created
* model: BookingReference
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allBookingReferences(_: NextApiRequest, res: NextApiResponse<BookingReferencesResponse>) {
const bookingReferences = await prisma.bookingReference.findMany();
const data = bookingReferences.map((bookingReference) =>
schemaBookingReferencePublic.parse(bookingReference)
);
async function createOrlistAllBookingReferences(
req: NextApiRequest,
res: NextApiResponse<BookingReferencesResponse | BookingReferenceResponse>
) {
const { method } = req;
if (method === "GET") {
const data = await prisma.bookingReference.findMany();
const booking_references = data.map((bookingReference) =>
schemaBookingReferencePublic.parse(bookingReference)
);
if (booking_references) res.status(200).json({ booking_references });
else
(error: Error) =>
res.status(404).json({
message: "No BookingReferences were found",
error,
});
} else if (method === "POST") {
const safe = schemaBookingReferenceBodyParams.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 BookingReferences were found",
error,
});
const data = await prisma.bookingReference.create({ data: safe.data });
const booking_reference = schemaBookingReferencePublic.parse(data);
if (data) res.status(201).json({ booking_reference, message: "BookingReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new booking reference",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allBookingReferences);
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllBookingReferences);
-51
View File
@@ -1,51 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingReferenceResponse } from "@lib/types";
import {
schemaBookingReferenceBodyParams,
schemaBookingReferencePublic,
withValidBookingReference,
} from "@lib/validations/booking-reference";
/**
* @swagger
* /api/booking-references/new:
* post:
* summary: Creates a new bookingReference
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BookingReference'
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference created
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createBookingReference(req: NextApiRequest, res: NextApiResponse<BookingReferenceResponse>) {
const safe = schemaBookingReferenceBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const bookingReference = await prisma.bookingReference.create({ data: safe.data });
const data = schemaBookingReferencePublic.parse(bookingReference);
if (data) res.status(201).json({ data, message: "BookingReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new bookingReference",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidBookingReference(createBookingReference));
+4 -4
View File
@@ -89,8 +89,8 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse<Book
case "GET":
await prisma.booking
.findUnique({ where: { id: safeQuery.data.id } })
.then((booking) => schemaBookingPublic.parse(booking))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaBookingPublic.parse(data))
.then((booking) => res.status(200).json({ booking }))
.catch((error: Error) =>
res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error })
);
@@ -103,8 +103,8 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse<Book
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((booking) => schemaBookingPublic.parse(booking))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaBookingPublic.parse(data))
.then((booking) => res.status(200).json({ booking }))
.catch((error: Error) =>
res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error })
);
+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 { BookingsResponse } from "@lib/types";
import { schemaBookingPublic } from "@lib/validations/booking";
import { BookingResponse, BookingsResponse } from "@lib/types";
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
/**
* @swagger
@@ -20,18 +20,49 @@ import { schemaBookingPublic } from "@lib/validations/booking";
* description: Authorization information is missing or invalid.
* 404:
* description: No bookings were found
* post:
* summary: Creates a new booking
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking created
* model: Booking
* 400:
* description: Bad request. Booking body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allBookings(_: NextApiRequest, res: NextApiResponse<BookingsResponse>) {
const bookings = await prisma.booking.findMany();
const data = bookings.map((booking) => schemaBookingPublic.parse(booking));
async function createOrlistAllBookings(
req: NextApiRequest,
res: NextApiResponse<BookingsResponse | BookingResponse>
) {
const { method } = req;
if (method === "GET") {
const data = await prisma.booking.findMany();
const bookings = data.map((booking) => schemaBookingPublic.parse(booking));
if (bookings) res.status(200).json({ bookings });
else
(error: Error) =>
res.status(404).json({
message: "No Bookings were found",
error,
});
} else if (method === "POST") {
const safe = schemaBookingBodyParams.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 Bookings were found",
error,
});
const data = await prisma.booking.create({ data: safe.data });
const booking = schemaBookingPublic.parse(data);
if (booking) res.status(201).json({ booking, message: "Booking created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new booking",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allBookings);
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllBookings);
-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 { BookingResponse } from "@lib/types";
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
/**
* @swagger
* /api/bookings/new:
* post:
* summary: Creates a new booking
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Booking'
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking created
* model: Booking
* 400:
* description: Bad request. Booking body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createBooking(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const safe = schemaBookingBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const booking = await prisma.booking.create({ data: safe.data });
const data = schemaBookingPublic.parse(booking);
if (data) res.status(201).json({ data, message: "Booking created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new booking",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidBooking(createBooking));
+1 -1
View File
@@ -15,7 +15,7 @@ import {
* @swagger
* /api/payments/{id}:
* get:
* summary: Get a payment by ID
* summary: Get one of your own payments by ID
* parameters:
* - in: path
* name: id
+1 -1
View File
@@ -8,7 +8,7 @@ import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } fro
/**
* @swagger
* /api/schedules:
* /v1/schedules:
* get:
* summary: Get all schedules
* tags:
+2 -2
View File
@@ -139,7 +139,7 @@ export async function selectedCalendarById(
},
})
.then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar))
.then((data) => res.status(200).json({ data }))
.then((selected_calendar) => res.status(200).json({ selected_calendar }))
.catch((error: Error) =>
res.status(404).json({ message: `SelectedCalendar with id: ${safeQuery.data.id} not found`, error })
);
@@ -159,7 +159,7 @@ export async function selectedCalendarById(
data: safeBody.data,
})
.then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar))
.then((data) => res.status(200).json({ data }))
.then((selected_calendar) => res.status(200).json({ selected_calendar }))
.catch((error: Error) =>
res.status(404).json({ message: `SelectedCalendar with id: ${safeQuery.data.id} not found`, error })
);
+9 -8
View File
@@ -43,11 +43,11 @@ async function createOrlistAllSelectedCalendars(
) {
const { method } = req;
if (method === "GET") {
const selectedCalendars = await prisma.selectedCalendar.findMany();
const data = selectedCalendars.map((selectedCalendar) =>
schemaSelectedCalendarPublic.parse(selectedCalendar)
const data = await prisma.selectedCalendar.findMany();
const selected_calendars = data.map((selected_calendar) =>
schemaSelectedCalendarPublic.parse(selected_calendar)
);
if (data) res.status(200).json({ data });
if (selected_calendars) res.status(200).json({ selected_calendars });
else
(error: Error) =>
res.status(404).json({
@@ -58,14 +58,15 @@ async function createOrlistAllSelectedCalendars(
const safe = schemaSelectedCalendarBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data });
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
const data = await prisma.selectedCalendar.create({ data: safe.data });
const selected_calendar = schemaSelectedCalendarPublic.parse(data);
if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" });
if (selected_calendar)
res.status(201).json({ selected_calendar, message: "SelectedCalendar created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new selectedCalendar",
message: "Could not create new selected calendar",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
+8 -3
View File
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { TeamResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
@@ -84,13 +85,17 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse<TeamRes
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaTeamBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = getCalcomUserId(res);
const userWithMemberships = await prisma.membership.findMany({
where: { userId: userId },
});
console.log(userWithMemberships);
switch (method) {
case "GET":
await prisma.team
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaTeamPublic.parse(data))
.then((data) => res.status(200).json({ data }))
.then((team) => res.status(200).json({ team }))
.catch((error: Error) =>
res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error })
);
@@ -104,7 +109,7 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse<TeamRes
data: safeBody.data,
})
.then((team) => schemaTeamPublic.parse(team))
.then((data) => res.status(200).json({ data }))
.then((team) => res.status(200).json({ team }))
.catch((error: Error) =>
res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error })
);
+15 -6
View File
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { TeamResponse, TeamsResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team";
/**
@@ -35,10 +36,14 @@ import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/vali
*/
async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<TeamsResponse | TeamResponse>) {
const { method } = req;
const userId = getCalcomUserId(res);
if (method === "GET") {
const teams = await prisma.team.findMany();
const data = teams.map((team) => schemaTeamPublic.parse(team));
if (data) res.status(200).json({ data });
const userWithMemberships = await prisma.membership.findMany({
where: { userId: userId },
});
const teamIds = userWithMemberships.map((membership) => membership.teamId);
const teams = await prisma.team.findMany({ where: { id: { in: teamIds } } });
if (teams) res.status(200).json({ teams });
else
(error: Error) =>
res.status(404).json({
@@ -46,13 +51,17 @@ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<Te
error,
});
} else if (method === "POST") {
// FIXME: add userId as owner of the team
const safe = schemaTeamBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const team = await prisma.team.create({ data: safe.data });
const membership = await prisma.membership.create({
data: { userId: userId, teamId: team.id, role: "ADMIN" },
});
console.log(membership);
const data = schemaTeamPublic.parse(team);
if (data) res.status(201).json({ data, message: "Team created successfully" });
if (data) res.status(201).json({ team: data, message: "Team created successfully" });
else
(error: Error) =>
res.status(400).json({
@@ -62,4 +71,4 @@ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<Te
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(withValidTeam(createOrlistAllTeams));
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllTeams);
+4 -4
View File
@@ -91,8 +91,8 @@ export async function userById(req: NextApiRequest, res: NextApiResponse<UserRes
case "GET":
await prisma.user
.findUnique({ where: { id: safeQuery.data.id } })
.then((user) => schemaUserPublic.parse(user))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaUserPublic.parse(data))
.then((user) => res.status(200).json({ user }))
.catch((error: Error) =>
res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error })
);
@@ -105,8 +105,8 @@ export async function userById(req: NextApiRequest, res: NextApiResponse<UserRes
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((user) => schemaUserPublic.parse(user))
.then((data) => res.status(200).json({ data }))
.then((data) => schemaUserPublic.parse(data))
.then((user) => res.status(200).json({ user }))
.catch((error: Error) =>
res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error })
);
+3 -3
View File
@@ -24,13 +24,13 @@ import { schemaUserPublic } from "@lib/validations/user";
*/
async function allUsers(req: NextApiRequest, res: NextApiResponse<UsersResponse>) {
const userId = getCalcomUserId(res);
const users = await prisma.user.findMany({
const data = await prisma.user.findMany({
where: {
id: userId,
},
});
const data = users.map((user) => schemaUserPublic.parse(user));
if (data) res.status(200).json({ data });
const users = data.map((user) => schemaUserPublic.parse(user));
if (users) res.status(200).json({ users });
else
(error: Error) =>
res.status(404).json({
+1 -1
View File
@@ -65,4 +65,4 @@ async function createOrlistAllPayments(
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllPayments);