Files
cal.diy/packages/app-store/zapier/api/subscriptions/listBookings.ts
T
0746b1e372 Fix Zapier sample data and payload (#5241)
* fix zapier example and payload

* fix type error

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-10-28 09:09:15 +00:00

81 lines
2.1 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { getHumanReadableLocationValue } from "@calcom/core/location";
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
import { defaultHandler, defaultResponder, getTranslation } from "@calcom/lib/server";
import prisma from "@calcom/prisma";
async function handler(req: NextApiRequest, res: NextApiResponse) {
const apiKey = req.query.apiKey as string;
if (!apiKey) {
return res.status(401).json({ message: "No API key provided" });
}
const validKey = await findValidApiKey(apiKey, "zapier");
if (!validKey) {
return res.status(401).json({ message: "API key not valid" });
}
try {
const bookings = await prisma.booking.findMany({
take: 3,
where: {
userId: validKey.userId,
},
select: {
title: true,
description: true,
customInputs: true,
startTime: true,
endTime: true,
location: true,
cancellationReason: true,
status: true,
user: {
select: {
username: true,
name: true,
email: true,
timeZone: true,
locale: true,
},
},
eventType: {
select: {
title: true,
description: true,
requiresConfirmation: true,
price: true,
currency: true,
length: true,
},
},
attendees: {
select: {
name: true,
email: true,
timeZone: true,
},
},
},
});
const t = await getTranslation(bookings[0].user?.locale ?? "en", "common");
const updatedBookings = bookings.map((booking) => {
return { ...booking, location: getHumanReadableLocationValue(booking.location || "", t) };
});
res.status(201).json(updatedBookings);
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Unable to get bookings." });
}
}
export default defaultHandler({
GET: Promise.resolve({ default: defaultResponder(handler) }),
});