|
| 1 | +import advancedFormat from "dayjs/plugin/advancedFormat"; |
| 2 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +import dayjs from "@calcom/dayjs"; |
| 6 | +import { fetcher } from "@calcom/lib/retellAIFetcher"; |
| 7 | +import { defaultHandler } from "@calcom/lib/server"; |
| 8 | +import prisma from "@calcom/prisma"; |
| 9 | +import { getRetellLLMSchema } from "@calcom/prisma/zod-utils"; |
| 10 | +import type { TGetRetellLLMSchema } from "@calcom/prisma/zod-utils"; |
| 11 | +import { getAvailableSlots } from "@calcom/trpc/server/routers/viewer/slots/util"; |
| 12 | + |
| 13 | +dayjs.extend(advancedFormat); |
| 14 | + |
| 15 | +const schema = z.object({ |
| 16 | + llm_id: z.string(), |
| 17 | + from_number: z.string(), |
| 18 | + to_number: z.string(), |
| 19 | +}); |
| 20 | + |
| 21 | +const getEventTypeIdFromRetellLLM = ( |
| 22 | + generalTools: TGetRetellLLMSchema["general_tools"] |
| 23 | +): number | undefined => { |
| 24 | + const generalTool = generalTools.find((tool) => !!tool.event_type_id && !!tool.timezone); |
| 25 | + |
| 26 | + return generalTool?.event_type_id; |
| 27 | +}; |
| 28 | + |
| 29 | +async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 30 | + const response = schema.safeParse(req.body); |
| 31 | + |
| 32 | + if (!response.success) { |
| 33 | + return res.status(400).send({ |
| 34 | + message: "Invalid Payload", |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + const body = response.data; |
| 39 | + |
| 40 | + const retellLLM = await fetcher(`/get-retell-llm/${body.llm_id}`).then(getRetellLLMSchema.parse); |
| 41 | + |
| 42 | + const eventTypeId = getEventTypeIdFromRetellLLM(retellLLM.general_tools); |
| 43 | + |
| 44 | + if (!eventTypeId) return res.status(404).json({ message: "eventTypeId not found" }); |
| 45 | + |
| 46 | + const eventType = await prisma.eventType.findUnique({ |
| 47 | + where: { |
| 48 | + id: eventTypeId, |
| 49 | + }, |
| 50 | + select: { |
| 51 | + id: true, |
| 52 | + teamId: true, |
| 53 | + team: { |
| 54 | + select: { |
| 55 | + parent: { |
| 56 | + select: { |
| 57 | + slug: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + if (!eventType) return res.status(404).json({ message: "eventType not found id" }); |
| 66 | + |
| 67 | + const now = dayjs(); |
| 68 | + |
| 69 | + const startTime = now.startOf("month").toISOString(); |
| 70 | + const endTime = now.add(2, "month").endOf("month").toISOString(); |
| 71 | + const orgSlug = eventType?.team?.parent?.slug ?? undefined; |
| 72 | + |
| 73 | + const availableSlots = await getAvailableSlots({ |
| 74 | + input: { |
| 75 | + startTime, |
| 76 | + endTime, |
| 77 | + eventTypeId, |
| 78 | + isTeamEvent: !!eventType?.teamId, |
| 79 | + orgSlug, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const firstAvailableDate = Object.keys(availableSlots.slots)[0]; |
| 84 | + const firstSlot = availableSlots?.slots?.[firstAvailableDate]?.[0]?.time; |
| 85 | + |
| 86 | + return res.status(200).json({ |
| 87 | + next_available: firstSlot ? dayjs.utc(firstSlot).format("dddd [the] Do [at] h:mma [GMT]") : undefined, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +export default defaultHandler({ |
| 92 | + POST: Promise.resolve({ default: handler }), |
| 93 | +}); |
0 commit comments