Merge pull request #69 from calcom/feat-bookings-references

fix: webhooks upgraded to new version, need to update templates
pull/9078/head
Agusti Fernandez Pardo 2022-05-11 15:57:30 +02:00 committed by GitHub
commit 8804495e3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 31 deletions

View File

@ -14,11 +14,10 @@ const schemaBookingBaseBodyParams = Booking.pick({
const schemaBookingCreateParams = z const schemaBookingCreateParams = z
.object({ .object({
uid: z.string(), uid: z.string(),
userId: z.number(),
eventTypeId: z.number(), eventTypeId: z.number(),
title: z.string(), title: z.string(),
startTime: z.date(), startTime: z.date().or(z.string()),
endTime: z.date(), endTime: z.date().or(z.string()),
}) })
.strict(); .strict();

View File

@ -1,5 +1,43 @@
import { z } from "zod";
import { _WebhookModel as Webhook } from "@calcom/prisma/zod"; import { _WebhookModel as Webhook } from "@calcom/prisma/zod";
export const schemaWebhookBodyParams = Webhook.omit({ id: true }).partial(); const schemaWebhookBaseBodyParams = Webhook.pick({
id: true,
userId: true,
eventTypeId: true,
eventTriggers: true,
active: true,
subscriberUrl: true,
payloadTemplate: true,
}).partial();
export const schemaWebhookPublic = Webhook.omit({}); const schemaWebhookCreateParams = z
.object({
id: z.string(),
subscriberUrl: z.string(),
})
.strict();
export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookCreateParams);
const schemaWebhookEditParams = z
.object({
uid: z.string().optional(),
title: z.string().optional(),
startTime: z.date().optional(),
endTime: z.date().optional(),
})
.strict();
export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookEditParams);
export const schemaWebhookReadPublic = Webhook.pick({
id: true,
userId: true,
eventTypeId: true,
uid: true,
title: true,
startTime: true,
endTime: true,
});

View File

@ -10,6 +10,7 @@ async function createOrlistAllBookings(
{ method, body, userId }: NextApiRequest, { method, body, userId }: NextApiRequest,
res: NextApiResponse<BookingsResponse | BookingResponse> res: NextApiResponse<BookingsResponse | BookingResponse>
) { ) {
console.log("userIduserId", userId);
if (method === "GET") { if (method === "GET") {
/** /**
* @swagger * @swagger
@ -77,8 +78,8 @@ async function createOrlistAllBookings(
return; return;
// throw new Error("Invalid request body"); // throw new Error("Invalid request body");
} }
safe.data.userId = userId;
const data = await prisma.booking.create({ data: { ...safe.data, userId } }); const data = await prisma.booking.create({ data: { ...safe.data } });
const booking = schemaBookingReadPublic.parse(data); const booking = schemaBookingReadPublic.parse(data);
if (booking) res.status(201).json({ booking, message: "Booking created successfully" }); if (booking) res.status(201).json({ booking, message: "Booking created successfully" });

View File

@ -7,7 +7,7 @@ import type { DailyEventReferenceResponse } from "@lib/types";
import { import {
schemaDailyEventReferenceEditBodyParams, schemaDailyEventReferenceEditBodyParams,
schemaDailyEventReferenceReadPublic, schemaDailyEventReferenceReadPublic,
} from "@lib/validations/daily-event-reference"; } from "@lib/validations/event-reference";
import { import {
schemaQueryIdParseInt, schemaQueryIdParseInt,
withValidQueryIdTransformParseInt, withValidQueryIdTransformParseInt,
@ -37,6 +37,7 @@ export async function dailyEventReferenceById(
* /event-references/{id}: * /event-references/{id}:
* get: * get:
* summary: Find a event reference * summary: Find a event reference
* operationId: getEventReferenceById
* parameters: * parameters:
* - in: path * - in: path
* name: id * name: id
@ -72,6 +73,7 @@ export async function dailyEventReferenceById(
* /event-references/{id}: * /event-references/{id}:
* patch: * patch:
* summary: Edit an existing event reference * summary: Edit an existing event reference
* operationId: editEventReferenceById
* parameters: * parameters:
* - in: path * - in: path
* name: id * name: id
@ -110,6 +112,7 @@ export async function dailyEventReferenceById(
* /event-references/{id}: * /event-references/{id}:
* delete: * delete:
* summary: Remove an existing event reference * summary: Remove an existing event reference
* operationId: removeEventReferenceById
* parameters: * parameters:
* - in: path * - in: path
* name: id * name: id

View File

@ -7,7 +7,7 @@ import { DailyEventReferenceResponse, DailyEventReferencesResponse } from "@lib/
import { import {
schemaDailyEventReferenceCreateBodyParams, schemaDailyEventReferenceCreateBodyParams,
schemaDailyEventReferenceReadPublic, schemaDailyEventReferenceReadPublic,
} from "@lib/validations/daily-event-reference"; } from "@lib/validations/event-reference";
async function createOrlistAllDailyEventReferences( async function createOrlistAllDailyEventReferences(
{ method, body, userId }: NextApiRequest, { method, body, userId }: NextApiRequest,
@ -21,7 +21,8 @@ async function createOrlistAllDailyEventReferences(
* @swagger * @swagger
* /event-references: * /event-references:
* get: * get:
* summary: Find all daily event reference * summary: Find all event reference
* operationId: listEventReferences
* tags: * tags:
* - event-references * - event-references
* responses: * responses:
@ -30,7 +31,7 @@ async function createOrlistAllDailyEventReferences(
* 401: * 401:
* description: Authorization information is missing or invalid. * description: Authorization information is missing or invalid.
* 404: * 404:
* description: No daily event references were found * description: No event references were found
*/ */
const data = await prisma.dailyEventReference.findMany({ const data = await prisma.dailyEventReference.findMany({
where: { bookingId: { in: userBookingIds } }, where: { bookingId: { in: userBookingIds } },
@ -50,12 +51,13 @@ async function createOrlistAllDailyEventReferences(
* @swagger * @swagger
* /event-references: * /event-references:
* post: * post:
* summary: Creates a new daily event reference * summary: Creates a new event reference
* operationId: addEventReference
* tags: * tags:
* - event-references * - event-references
* responses: * responses:
* 201: * 201:
* description: OK, daily event reference created * description: OK, event reference created
* 400: * 400:
* description: Bad request. DailyEventReference body is invalid. * description: Bad request. DailyEventReference body is invalid.
* 401: * 401:
@ -72,7 +74,7 @@ async function createOrlistAllDailyEventReferences(
else else
(error: Error) => (error: Error) =>
res.status(400).json({ res.status(400).json({
message: "Could not create new daily event reference", message: "Could not create new event reference",
error, error,
}); });
} else res.status(405).json({ message: `Method ${method} not allowed` }); } else res.status(405).json({ message: `Method ${method} not allowed` });

View File

@ -4,17 +4,14 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { WebhookResponse } from "@lib/types"; import type { WebhookResponse } from "@lib/types";
import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook"; import { schemaQueryIdAsString } from "@lib/validations/shared/queryIdString";
import { import { schemaWebhookEditBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook";
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
export async function WebhookById( export async function WebhookById(
{ method, query, body, userId }: NextApiRequest, { method, query, body, userId }: NextApiRequest,
res: NextApiResponse<WebhookResponse> res: NextApiResponse<WebhookResponse>
) { ) {
const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeQuery = schemaQueryIdAsString.safeParse(query);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const data = await prisma.webhook.findMany({ where: { userId } }); const data = await prisma.webhook.findMany({ where: { userId } });
const userWebhooks = data.map((webhook) => webhook.id); const userWebhooks = data.map((webhook) => webhook.id);
@ -50,8 +47,8 @@ export async function WebhookById(
case "GET": case "GET":
await prisma.webhook await prisma.webhook
.findUnique({ where: { id: safeQuery.data.id } }) .findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaWebhookPublic.parse(data)) .then((data) => schemaWebhookReadPublic.parse(data))
.then((hook) => res.status(200).json({ hook })) .then((webhook) => res.status(200).json({ webhook }))
.catch((error: Error) => .catch((error: Error) =>
res.status(404).json({ res.status(404).json({
message: `Webhook with id: ${safeQuery.data.id} not found`, message: `Webhook with id: ${safeQuery.data.id} not found`,
@ -86,14 +83,14 @@ export async function WebhookById(
* description: Authorization information is missing or invalid. * description: Authorization information is missing or invalid.
*/ */
case "PATCH": case "PATCH":
const safeBody = schemaWebhookBodyParams.safeParse(body); const safeBody = schemaWebhookEditBodyParams.safeParse(body);
if (!safeBody.success) { if (!safeBody.success) {
throw new Error("Invalid request body"); throw new Error("Invalid request body");
} }
await prisma.webhook await prisma.webhook
.update({ where: { id: safeQuery.data.id }, data: safeBody.data }) .update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.then((data) => schemaWebhookPublic.parse(data)) .then((data) => schemaWebhookReadPublic.parse(data))
.then((hook) => res.status(200).json({ hook })) .then((webhook) => res.status(200).json({ webhook }))
.catch((error: Error) => .catch((error: Error) =>
res.status(404).json({ res.status(404).json({
message: `Webhook with id: ${safeQuery.data.id} not found`, message: `Webhook with id: ${safeQuery.data.id} not found`,
@ -150,4 +147,4 @@ export async function WebhookById(
} }
} }
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(WebhookById)); export default withMiddleware("HTTP_GET_DELETE_PATCH")(WebhookById);

View File

@ -4,7 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import { WebhookResponse, WebhooksResponse } from "@lib/types"; import { WebhookResponse, WebhooksResponse } from "@lib/types";
import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook"; import { schemaWebhookCreateBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook";
async function createOrlistAllWebhooks( async function createOrlistAllWebhooks(
{ method, body, userId }: NextApiRequest, { method, body, userId }: NextApiRequest,
@ -28,8 +28,8 @@ async function createOrlistAllWebhooks(
* 404: * 404:
* description: No webhooks were found * description: No webhooks were found
*/ */
const data = await prisma.webhooks.findMany({ where: { userId } }); const data = await prisma.webhook.findMany({ where: { userId } });
const webhooks = data.map((webhook) => schemaWebhookPublic.parse(webhook)); const webhooks = data.map((webhook) => schemaWebhookReadPublic.parse(webhook));
if (webhooks) res.status(200).json({ webhooks }); if (webhooks) res.status(200).json({ webhooks });
else else
(error: Error) => (error: Error) =>
@ -55,11 +55,11 @@ async function createOrlistAllWebhooks(
* 401: * 401:
* description: Authorization information is missing or invalid. * description: Authorization information is missing or invalid.
*/ */
const safe = schemaWebhookBodyParams.safeParse(body); const safe = schemaWebhookCreateBodyParams.safeParse(body);
if (!safe.success) throw new Error("Invalid request body"); if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.webhook.create({ data: { ...safe.data, userId } }); const data = await prisma.webhook.create({ data: { ...safe.data, userId } });
const webhook = schemaWebhookPublic.parse(data); const webhook = schemaWebhookReadPublic.parse(data);
if (data) res.status(201).json({ webhook, message: "Webhook created successfully" }); if (data) res.status(201).json({ webhook, message: "Webhook created successfully" });
else else