chore: change timezone format and update webhook (#14433)

* chore: change timezone format

* chore: set guest email and guest company to null

* feat: also check states to get id

* chore: improve code

* chore: fix type erro

* chore: update zod schema
This commit is contained in:
Udit Takkar
2024-04-08 15:26:31 +05:30
committed by GitHub
parent 67262910cd
commit 74dc46f8b2
4 changed files with 57 additions and 12 deletions
@@ -19,11 +19,27 @@ const schema = z.object({
});
const getEventTypeIdFromRetellLLM = (
generalTools: TGetRetellLLMSchema["general_tools"]
): number | undefined => {
const generalTool = generalTools.find((tool) => !!tool.event_type_id && !!tool.timezone);
retellLLM: TGetRetellLLMSchema
): { eventTypeId: number | undefined; timezone: string | undefined } => {
const { general_tools, states } = retellLLM;
return generalTool?.event_type_id;
const generalTool = general_tools.find((tool) => tool.event_type_id && tool.timezone);
if (generalTool) {
return { eventTypeId: generalTool.event_type_id, timezone: generalTool.timezone };
}
// If no general tool found, search in states
if (states) {
for (const state of states) {
const tool = state.tools.find((tool) => tool.event_type_id && tool.timezone);
if (tool) {
return { eventTypeId: tool.event_type_id, timezone: tool.timezone };
}
}
}
return { eventTypeId: undefined, timezone: undefined };
};
async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -39,9 +55,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const retellLLM = await fetcher(`/get-retell-llm/${body.llm_id}`).then(getRetellLLMSchema.parse);
const eventTypeId = getEventTypeIdFromRetellLLM(retellLLM.general_tools);
const { eventTypeId, timezone } = getEventTypeIdFromRetellLLM(retellLLM);
if (!eventTypeId) return res.status(404).json({ message: "eventTypeId not found" });
if (!eventTypeId || !timezone)
return res.status(404).json({ message: "eventTypeId or Timezone not found" });
const eventType = await prisma.eventType.findUnique({
where: {
@@ -84,7 +101,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const firstSlot = availableSlots?.slots?.[firstAvailableDate]?.[0]?.time;
return res.status(200).json({
next_available: firstSlot ? dayjs.utc(firstSlot).format("dddd [the] Do [at] h:mma [GMT]") : undefined,
next_available: firstSlot
? dayjs.utc(firstSlot).tz(timezone).format(`dddd [the] Do [at] h:mma [${timezone} timezone]`)
: undefined,
});
}
+22 -2
View File
@@ -702,8 +702,8 @@ export const AIPhoneSettingSchema = z.object({
guestName: z.string().trim().min(1, {
message: "Please enter Guest Name",
}),
guestEmail: z.string().email().optional(),
guestCompany: z.string().optional(),
guestEmail: z.string().email().nullable().optional(),
guestCompany: z.string().nullable().optional(),
generalPrompt: z.string().trim().min(1, {
message: "Please enter prompt",
}),
@@ -731,6 +731,26 @@ export const getRetellLLMSchema = z
})
.passthrough()
),
states: z
.array(
z
.object({
name: z.string(),
tools: z.array(
z
.object({
name: z.string(),
type: z.string(),
cal_api_key: z.string().optional(),
event_type_id: z.number().optional(),
timezone: z.string().optional(),
})
.passthrough()
),
})
.passthrough()
)
.nullable(),
})
.passthrough();
@@ -16,8 +16,8 @@ export const EventTypeUpdateInput = _EventTypeModel
yourPhoneNumber: z.string().default(""),
numberToCall: z.string().default(""),
guestName: z.string().default(""),
guestEmail: z.string().default(""),
guestCompany: z.string().default(""),
guestEmail: z.string().nullable().default(null),
guestCompany: z.string().nullable().default(null),
})
.optional(),
calAiPhoneScript: z.string().optional(),
@@ -372,9 +372,15 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
where: {
eventTypeId: id,
},
update: aiPhoneCallConfig,
update: {
...aiPhoneCallConfig,
guestEmail: !!aiPhoneCallConfig?.guestEmail ? aiPhoneCallConfig.guestEmail : null,
guestCompany: !!aiPhoneCallConfig?.guestCompany ? aiPhoneCallConfig.guestCompany : null,
},
create: {
...aiPhoneCallConfig,
guestEmail: !!aiPhoneCallConfig?.guestEmail ? aiPhoneCallConfig.guestEmail : null,
guestCompany: !!aiPhoneCallConfig?.guestCompany ? aiPhoneCallConfig.guestCompany : null,
eventTypeId: id,
},
});