fix: build webhooks and event-types
This commit is contained in:
1 parent
2e8f42c5d7
commit
f2ce2f324b
6 files changed
+188
-187
No files matched your search
+13
-11
@@ -1,19 +1,21 @@
|
|||||||
import type { IncomingMessage } from "http";
|
// import type { NextApiRequest } from "next";
|
||||||
import { NextMiddleware } from "next-api-middleware";
|
import { NextMiddleware } from "next-api-middleware";
|
||||||
|
|
||||||
import { hashAPIKey } from "@calcom/ee/lib/api/apiKeys";
|
import { hashAPIKey } from "@calcom/ee/lib/api/apiKeys";
|
||||||
import prisma from "@calcom/prisma";
|
import prisma from "@calcom/prisma";
|
||||||
|
|
||||||
/** @todo figure how to use the one from `@calcom/types`fi */
|
// /** @todo figure how to use the one from `@calcom/types`fi */
|
||||||
/** @todo: remove once `@calcom/types` is updated with it.*/
|
// /** @todo: remove once `@calcom/types` is updated with it.*/
|
||||||
declare module "next" {
|
// declare module "next" {
|
||||||
export interface NextApiRequest extends IncomingMessage {
|
// export interface NextApiRequest extends NextApiRequest {
|
||||||
userId: number;
|
// userId: number;
|
||||||
query: {
|
// body: any;
|
||||||
apiKey: string;
|
// method: string;
|
||||||
};
|
// query: {
|
||||||
}
|
// apiKey: string;
|
||||||
}
|
// };
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// Used to check if the apiKey is not expired, could be extracted if reused. but not for now.
|
// Used to check if the apiKey is not expired, could be extracted if reused. but not for now.
|
||||||
export const dateNotInPast = function (date: Date) {
|
export const dateNotInPast = function (date: Date) {
|
||||||
|
|||||||
@@ -150,6 +150,7 @@ interface EventTypeExtended extends Omit<EventType, "recurringEvent" | "location
|
|||||||
type: DefaultLocationType | AppStoreLocationType;
|
type: DefaultLocationType | AppStoreLocationType;
|
||||||
}[];
|
}[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// EventType
|
// EventType
|
||||||
export type EventTypeResponse = BaseResponse & {
|
export type EventTypeResponse = BaseResponse & {
|
||||||
event_type?: Partial<EventTypeExtended>;
|
event_type?: Partial<EventTypeExtended>;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(s
|
|||||||
const schemaWebhookEditParams = z
|
const schemaWebhookEditParams = z
|
||||||
.object({
|
.object({
|
||||||
payloadTemplate: z.string().optional(),
|
payloadTemplate: z.string().optional(),
|
||||||
eventTriggers: z.string().optional(),
|
eventTriggers: z.any(),
|
||||||
subscriberUrl: z.string().optional(),
|
subscriberUrl: z.string().optional(),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@calcom/tsconfig": "*",
|
"@calcom/tsconfig": "*",
|
||||||
"@calcom/types": "*",
|
|
||||||
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
||||||
"babel-jest": "^28.1.0",
|
"babel-jest": "^28.1.0",
|
||||||
"jest": "^28.0.3",
|
"jest": "^28.0.3",
|
||||||
|
|||||||
@@ -22,177 +22,177 @@ export async function bookingReferenceById(
|
|||||||
res.status(400).json({ message: "Your query was invalid" });
|
res.status(400).json({ message: "Your query was invalid" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// const userWithBookings = await prisma.user.findUnique({
|
const userWithBookings = await prisma.user.findUnique({
|
||||||
// where: { id: userId },
|
where: { id: userId },
|
||||||
// include: { bookings: true },
|
include: { bookings: true },
|
||||||
// });
|
});
|
||||||
// if (!userWithBookings) throw new Error("User not found");
|
if (!userWithBookings) throw new Error("User not found");
|
||||||
// const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat();
|
const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat();
|
||||||
// console.log(userBookingIds);
|
// console.log(userBookingIds);
|
||||||
// const bookingReferences = await prisma.bookingReference
|
const bookingReferences = await prisma.bookingReference
|
||||||
// .findMany({ where: { id: { in: userBookingIds } } })
|
.findMany({ where: { id: { in: userBookingIds } } })
|
||||||
// .then((bookingReferences) => {
|
.then((bookingReferences) => {
|
||||||
// console.log(bookingReferences);
|
console.log(bookingReferences);
|
||||||
// return bookingReferences.map((bookingReference) => bookingReference.id);
|
return bookingReferences.map((bookingReference) => bookingReference.id);
|
||||||
// });
|
});
|
||||||
|
|
||||||
// res.status(400).json({ message: "Booking reference not found" });
|
// res.status(400).json({ message: "Booking reference not found" });
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
// if (userBookingIds.includes(safeQuery.data.id)) {
|
if (!bookingReferences?.includes(safeQuery.data.id)) {
|
||||||
// if (!bookingReferences?.includes(safeQuery.data.id)) {
|
// if (userBookingIds.includes(safeQuery.data.id)) {
|
||||||
// throw new Error("BookingReference: bookingId not found");
|
// throw new Error("BookingReference: bookingId not found");
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
* /booking-references/{id}:
|
* /booking-references/{id}:
|
||||||
* get:
|
* get:
|
||||||
* operationId: getBookingReferenceById
|
* operationId: getBookingReferenceById
|
||||||
* summary: Find a booking reference
|
* summary: Find a booking reference
|
||||||
* parameters:
|
* parameters:
|
||||||
* - in: path
|
* - in: path
|
||||||
* name: id
|
* name: id
|
||||||
* schema:
|
* schema:
|
||||||
* type: integer
|
* type: integer
|
||||||
* required: true
|
* required: true
|
||||||
* description: ID of the booking reference to get
|
* description: ID of the booking reference to get
|
||||||
* tags:
|
* tags:
|
||||||
* - booking-references
|
* - booking-references
|
||||||
* responses:
|
* responses:
|
||||||
* 200:
|
* 200:
|
||||||
* description: OK
|
* description: OK
|
||||||
* 401:
|
* 401:
|
||||||
* description: Authorization information is missing or invalid.
|
* description: Authorization information is missing or invalid.
|
||||||
* 404:
|
* 404:
|
||||||
* description: BookingReference was not found
|
* description: BookingReference was not found
|
||||||
*/
|
*/
|
||||||
// console.log(safeQuery.data.id);
|
// console.log(safeQuery.data.id);
|
||||||
const bookingReference = await prisma.bookingReference.findFirst().catch((error: Error) => {
|
const bookingReference = await prisma.bookingReference.findFirst().catch((error: Error) => {
|
||||||
console.log("hoerr:", error);
|
console.log("hoerr:", error);
|
||||||
});
|
});
|
||||||
// console.log(bookingReference);
|
// console.log(bookingReference);
|
||||||
if (!bookingReference) res.status(404).json({ message: "Booking reference not found" });
|
if (!bookingReference) res.status(404).json({ message: "Booking reference not found" });
|
||||||
else res.status(200).json({ booking_reference: bookingReference });
|
else res.status(200).json({ booking_reference: bookingReference });
|
||||||
|
|
||||||
// .then((data) => {
|
// .then((data) => {
|
||||||
// console.log(data);
|
// console.log(data);
|
||||||
// return schemaBookingReferenceReadPublic.parse(data);
|
// return schemaBookingReferenceReadPublic.parse(data);
|
||||||
// })
|
// })
|
||||||
// .then((booking_reference) => res.status(200).json({ booking_reference }))
|
// .then((booking_reference) => res.status(200).json({ booking_reference }))
|
||||||
// .catch((error: Error) => {
|
// .catch((error: Error) => {
|
||||||
// console.log(error);
|
// console.log(error);
|
||||||
// res.status(404).json({
|
// res.status(404).json({
|
||||||
// message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
// message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
||||||
// error,
|
// error,
|
||||||
// });
|
// });
|
||||||
// });
|
// });
|
||||||
break;
|
break;
|
||||||
case "PATCH":
|
case "PATCH":
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
* /booking-references/{id}:
|
* /booking-references/{id}:
|
||||||
* patch:
|
* patch:
|
||||||
* operationId: editBookingReferenceById
|
* operationId: editBookingReferenceById
|
||||||
* summary: Edit an existing booking reference
|
* summary: Edit an existing booking reference
|
||||||
* requestBody:
|
* requestBody:
|
||||||
* description: Edit an existing booking reference related to one of your bookings
|
* description: Edit an existing booking reference related to one of your bookings
|
||||||
* required: true
|
* required: true
|
||||||
* content:
|
* content:
|
||||||
* application/json:
|
* application/json:
|
||||||
* schema:
|
* schema:
|
||||||
* type: object
|
* type: object
|
||||||
* properties:
|
* properties:
|
||||||
* days:
|
* days:
|
||||||
* type: array
|
* type: array
|
||||||
* example: [email protected]
|
* example: [email protected]
|
||||||
* startTime:
|
* startTime:
|
||||||
* type: string
|
* type: string
|
||||||
* example: 1970-01-01T17:00:00.000Z
|
* example: 1970-01-01T17:00:00.000Z
|
||||||
* endTime:
|
* endTime:
|
||||||
* type: string
|
* type: string
|
||||||
* example: 1970-01-01T17:00:00.000Z
|
* example: 1970-01-01T17:00:00.000Z
|
||||||
* parameters:
|
* parameters:
|
||||||
* - in: path
|
* - in: path
|
||||||
* name: id
|
* name: id
|
||||||
* schema:
|
* schema:
|
||||||
* type: integer
|
* type: integer
|
||||||
* required: true
|
* required: true
|
||||||
* description: ID of the booking reference to edit
|
* description: ID of the booking reference to edit
|
||||||
* tags:
|
* tags:
|
||||||
* - booking-references
|
* - booking-references
|
||||||
* responses:
|
* responses:
|
||||||
* 201:
|
* 201:
|
||||||
* description: OK, safeBody.data edited successfuly
|
* description: OK, safeBody.data edited successfuly
|
||||||
* 400:
|
* 400:
|
||||||
* description: Bad request. BookingReference body is invalid.
|
* description: Bad request. BookingReference body is invalid.
|
||||||
* 401:
|
* 401:
|
||||||
* description: Authorization information is missing or invalid.
|
* description: Authorization information is missing or invalid.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const safeBody = schemaBookingEditBodyParams.safeParse(body);
|
const safeBody = schemaBookingEditBodyParams.safeParse(body);
|
||||||
if (!safeBody.success) {
|
if (!safeBody.success) {
|
||||||
console.log(safeBody.error);
|
console.log(safeBody.error);
|
||||||
res.status(400).json({ message: "Invalid request body", error: safeBody.error });
|
res.status(400).json({ message: "Invalid request body", error: safeBody.error });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await prisma.bookingReference
|
await prisma.bookingReference
|
||||||
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
||||||
.then((data) => schemaBookingReferenceReadPublic.parse(data))
|
.then((data) => schemaBookingReferenceReadPublic.parse(data))
|
||||||
.then((booking_reference) => res.status(200).json({ booking_reference }))
|
.then((booking_reference) => res.status(200).json({ booking_reference }))
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
||||||
error,
|
error,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "DELETE":
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /booking-references/{id}:
|
||||||
|
* delete:
|
||||||
|
* operationId: removeBookingReferenceById
|
||||||
|
* summary: Remove an existing booking reference
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* schema:
|
||||||
|
* type: integer
|
||||||
|
* required: true
|
||||||
|
* description: 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.
|
||||||
|
*/
|
||||||
|
await prisma.bookingReference
|
||||||
|
.delete({
|
||||||
|
where: { id: safeQuery.data.id },
|
||||||
})
|
})
|
||||||
);
|
.then(() =>
|
||||||
break;
|
res.status(200).json({
|
||||||
case "DELETE":
|
message: `BookingReference with id: ${safeQuery.data.id} deleted`,
|
||||||
/**
|
})
|
||||||
* @swagger
|
)
|
||||||
* /booking-references/{id}:
|
.catch((error: Error) =>
|
||||||
* delete:
|
res.status(404).json({
|
||||||
* operationId: removeBookingReferenceById
|
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
||||||
* summary: Remove an existing booking reference
|
error,
|
||||||
* parameters:
|
})
|
||||||
* - in: path
|
);
|
||||||
* name: id
|
break;
|
||||||
* schema:
|
|
||||||
* type: integer
|
|
||||||
* required: true
|
|
||||||
* description: 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.
|
|
||||||
*/
|
|
||||||
await prisma.bookingReference
|
|
||||||
.delete({
|
|
||||||
where: { id: safeQuery.data.id },
|
|
||||||
})
|
|
||||||
.then(() =>
|
|
||||||
res.status(200).json({
|
|
||||||
message: `BookingReference with id: ${safeQuery.data.id} deleted`,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.catch((error: Error) =>
|
|
||||||
res.status(404).json({
|
|
||||||
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
|
||||||
error,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
res.status(405).json({ message: "Method not allowed" });
|
res.status(405).json({ message: "Method not allowed" });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// } else res.status(401).json({ message: "Unauthorized" });
|
} else res.status(401).json({ message: "Unauthorized" });
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withMiddleware("HTTP_GET_DELETE_PATCH")(
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(
|
||||||
|
|||||||
+11
-12
@@ -40,19 +40,18 @@ async function getAllorCreateUser(
|
|||||||
message: "No Users were found",
|
message: "No Users were found",
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
|
} else if (method === "POST") {
|
||||||
|
const isAdmin = await prisma.user
|
||||||
|
.findUnique({ where: { id: userId } })
|
||||||
|
.then((user) => user?.role === "ADMIN");
|
||||||
|
if (!isAdmin) res.status(401).json({ message: "You are not authorized" });
|
||||||
|
else {
|
||||||
|
const user = await prisma.user.create({
|
||||||
|
data: schemaUserReadPublic.parse(body),
|
||||||
|
});
|
||||||
|
res.status(201).json({ user });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// else if (method === "POST") {
|
|
||||||
// const isAdmin = await prisma.user
|
|
||||||
// .findUnique({ where: { id: userId } })
|
|
||||||
// .then((user) => user?.role === "ADMIN");
|
|
||||||
// if (!isAdmin) res.status(401).json({ message: "You are not authorized" });
|
|
||||||
// else {
|
|
||||||
// const user = await prisma.user.create({
|
|
||||||
// data: schemaUserReadPublic.parse(body),
|
|
||||||
// });
|
|
||||||
// res.status(201).json({ user });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
// No POST endpoint for users for now as a regular user you're expected to signup.
|
// No POST endpoint for users for now as a regular user you're expected to signup.
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user