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
.object({
uid: z.string(),
userId: z.number(),
eventTypeId: z.number(),
title: z.string(),
startTime: z.date(),
endTime: z.date(),
startTime: z.date().or(z.string()),
endTime: z.date().or(z.string()),
})
.strict();

View File

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

View File

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

View File

@ -7,7 +7,7 @@ import { DailyEventReferenceResponse, DailyEventReferencesResponse } from "@lib/
import {
schemaDailyEventReferenceCreateBodyParams,
schemaDailyEventReferenceReadPublic,
} from "@lib/validations/daily-event-reference";
} from "@lib/validations/event-reference";
async function createOrlistAllDailyEventReferences(
{ method, body, userId }: NextApiRequest,
@ -21,7 +21,8 @@ async function createOrlistAllDailyEventReferences(
* @swagger
* /event-references:
* get:
* summary: Find all daily event reference
* summary: Find all event reference
* operationId: listEventReferences
* tags:
* - event-references
* responses:
@ -30,7 +31,7 @@ async function createOrlistAllDailyEventReferences(
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No daily event references were found
* description: No event references were found
*/
const data = await prisma.dailyEventReference.findMany({
where: { bookingId: { in: userBookingIds } },
@ -50,12 +51,13 @@ async function createOrlistAllDailyEventReferences(
* @swagger
* /event-references:
* post:
* summary: Creates a new daily event reference
* summary: Creates a new event reference
* operationId: addEventReference
* tags:
* - event-references
* responses:
* 201:
* description: OK, daily event reference created
* description: OK, event reference created
* 400:
* description: Bad request. DailyEventReference body is invalid.
* 401:
@ -72,7 +74,7 @@ async function createOrlistAllDailyEventReferences(
else
(error: Error) =>
res.status(400).json({
message: "Could not create new daily event reference",
message: "Could not create new event reference",
error,
});
} 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 type { WebhookResponse } from "@lib/types";
import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { schemaQueryIdAsString } from "@lib/validations/shared/queryIdString";
import { schemaWebhookEditBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook";
export async function WebhookById(
{ method, query, body, userId }: NextApiRequest,
res: NextApiResponse<WebhookResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeQuery = schemaQueryIdAsString.safeParse(query);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const data = await prisma.webhook.findMany({ where: { userId } });
const userWebhooks = data.map((webhook) => webhook.id);
@ -50,8 +47,8 @@ export async function WebhookById(
case "GET":
await prisma.webhook
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaWebhookPublic.parse(data))
.then((hook) => res.status(200).json({ hook }))
.then((data) => schemaWebhookReadPublic.parse(data))
.then((webhook) => res.status(200).json({ webhook }))
.catch((error: Error) =>
res.status(404).json({
message: `Webhook with id: ${safeQuery.data.id} not found`,
@ -86,14 +83,14 @@ export async function WebhookById(
* description: Authorization information is missing or invalid.
*/
case "PATCH":
const safeBody = schemaWebhookBodyParams.safeParse(body);
const safeBody = schemaWebhookEditBodyParams.safeParse(body);
if (!safeBody.success) {
throw new Error("Invalid request body");
}
await prisma.webhook
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.then((data) => schemaWebhookPublic.parse(data))
.then((hook) => res.status(200).json({ hook }))
.then((data) => schemaWebhookReadPublic.parse(data))
.then((webhook) => res.status(200).json({ webhook }))
.catch((error: Error) =>
res.status(404).json({
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 { WebhookResponse, WebhooksResponse } from "@lib/types";
import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook";
import { schemaWebhookCreateBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook";
async function createOrlistAllWebhooks(
{ method, body, userId }: NextApiRequest,
@ -28,8 +28,8 @@ async function createOrlistAllWebhooks(
* 404:
* description: No webhooks were found
*/
const data = await prisma.webhooks.findMany({ where: { userId } });
const webhooks = data.map((webhook) => schemaWebhookPublic.parse(webhook));
const data = await prisma.webhook.findMany({ where: { userId } });
const webhooks = data.map((webhook) => schemaWebhookReadPublic.parse(webhook));
if (webhooks) res.status(200).json({ webhooks });
else
(error: Error) =>
@ -55,11 +55,11 @@ async function createOrlistAllWebhooks(
* 401:
* 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");
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" });
else