Merge pull request #96 from calcom/fix/webhook-create

Fix/webhook create
This commit is contained in:
Syed Ali Shahbaz
2022-05-25 14:30:36 +05:30
committed by GitHub
2 changed files with 24 additions and 32 deletions
+11 -19
View File
@@ -12,29 +12,21 @@ const schemaWebhookBaseBodyParams = Webhook.pick({
payloadTemplate: true,
}).partial();
// const schemaWebhookCreateParams = z
// .object({
// id: z.string(),
// subscriberUrl: z.string(),
// })
// .strict();
export const schemaWebhookCreateParams = z
.object({
userId: z.number().or(z.string()).optional(),
eventTypeId: z.number().or(z.string()).optional(),
eventTriggers: z.any.optional(),
active: z.boolean().optional(),
subscriberUrl: z.string(),
payloadTemplate: z.string().optional(),
})
.strict();
export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(
z.object({
id: z.string(),
subscriberUrl: z.string(),
})
schemaWebhookCreateParams
);
// const schemaWebhookEditParams = z
// .object({
// payloadTemplate: z.string().optional(),
// /** @todo: don't use any here and validate eventTriggers proper */
// eventTriggers: z.any(),
// subscriberUrl: z.string().optional(),
// })
// .strict();
export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge(
z.object({
payloadTemplate: z.string().optional(),
+13 -13
View File
@@ -55,19 +55,19 @@ async function createOrlistAllWebhooks(
* 401:
* description: Authorization information is missing or invalid.
*/
// const safe = schemaWebhookCreateBodyParams.safeParse(body);
// if (!safe.success) {
// res.status(400).json({ message: "Invalid request body" });
// return;
// }
// const data = await prisma.webhook.create({ data: { ...safe.data, userId } });
// if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" });
// else
// (error: Error) =>
// res.status(400).json({
// message: "Could not create new webhook",
// error,
// });
const safe = schemaWebhookCreateBodyParams.safeParse(body);
if (!safe.success) {
res.status(400).json({ message: "Invalid request body" });
return;
}
const data = await prisma.webhook.create({ data: { ...safe.data, userId } });
if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new webhook",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}