feat: add POST end point to mark calls as no-shows (#15690)
* feat: add POST end point to mark calls as no-shows * fix: type * feat: add v2 endpoint * wip * chore: type err * chore: add permission * chore: remove packae * chore: remove v1 API endpoint * fix: endpoint * feat: add new version * chore: change route * chore: error msg * fix: message * fix: type err * chore: output * fix: type err * chore: bump libraries to 0.0.17 * chore: add noShowHost --------- Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: Morgan <[email protected]> Co-authored-by: Morgan Vernay <[email protected]>
This commit is contained in:
11 files changed
+294
-133
No files matched your search
@@ -85,3 +85,7 @@ export type BookingResponse = Awaited<
|
||||
export type InstantBookingResponse = Awaited<
|
||||
ReturnType<typeof import("@calcom/features/instant-meeting/handleInstantMeeting").default>
|
||||
>;
|
||||
|
||||
export type MarkNoShowResponse = Awaited<
|
||||
ReturnType<typeof import("@calcom/features/handleMarkNoShow").default>
|
||||
>;
|
||||
@@ -0,0 +1,155 @@
|
||||
import { WebhookService } from "@calcom/features/webhooks/lib/WebhookService";
|
||||
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { WebhookTriggerEvents } from "@calcom/prisma/client";
|
||||
import type { TNoShowInputSchema } from "@calcom/trpc/server/routers/publicViewer/noShow.schema";
|
||||
|
||||
const getResultPayload = async (attendees: { email: string; noShow: boolean }[]) => {
|
||||
if (attendees.length === 1) {
|
||||
const [attendee] = attendees;
|
||||
return {
|
||||
messageKey: attendee.noShow ? "x_marked_as_no_show" : "x_unmarked_as_no_show",
|
||||
attendees: [attendee],
|
||||
};
|
||||
}
|
||||
return {
|
||||
messageKey: "no_show_updated",
|
||||
attendees: attendees,
|
||||
};
|
||||
};
|
||||
|
||||
type ResponsePayload = {
|
||||
attendees: { email: string; noShow: boolean }[];
|
||||
noShowHost: boolean;
|
||||
message: string;
|
||||
messageKey: string | undefined;
|
||||
};
|
||||
|
||||
const logFailedResults = (results: PromiseSettledResult<any>[]) => {
|
||||
const failed = results.filter((x) => x.status === "rejected") as PromiseRejectedResult[];
|
||||
if (failed.length < 1) return;
|
||||
const failedMessage = failed.map((r) => r.reason);
|
||||
console.error("Failed to update no-show status", failedMessage.join(","));
|
||||
};
|
||||
|
||||
const handleMarkNoShow = async ({ bookingUid, attendees, noShowHost }: TNoShowInputSchema) => {
|
||||
const responsePayload: ResponsePayload = {
|
||||
attendees: [],
|
||||
noShowHost: false,
|
||||
message: "Failed to update no-show status",
|
||||
messageKey: undefined,
|
||||
};
|
||||
try {
|
||||
const attendeeEmails = attendees?.map((attendee) => attendee.email) || [];
|
||||
if (attendees && attendeeEmails.length > 0) {
|
||||
const allAttendees = await prisma.attendee.findMany({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
booking: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
email: {
|
||||
in: attendeeEmails,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
|
||||
const allAttendeesMap = allAttendees.reduce((acc, attendee) => {
|
||||
acc[attendee.email] = attendee;
|
||||
return acc;
|
||||
}, {} as Record<string, { id: number; email: string }>);
|
||||
|
||||
const updatePromises = attendees.map((attendee) => {
|
||||
const attendeeToUpdate = allAttendeesMap[attendee.email];
|
||||
if (!attendeeToUpdate) return;
|
||||
return prisma.attendee.update({
|
||||
where: { id: attendeeToUpdate.id },
|
||||
data: { noShow: attendee.noShow },
|
||||
});
|
||||
});
|
||||
|
||||
const results = await Promise.allSettled(updatePromises);
|
||||
logFailedResults(results);
|
||||
|
||||
const _attendees = results
|
||||
.filter((x) => x.status === "fulfilled")
|
||||
.map((x) => (x as PromiseFulfilledResult<{ noShow: boolean; email: string }>).value)
|
||||
.map((x) => ({ email: x.email, noShow: x.noShow }));
|
||||
|
||||
const payload = await getResultPayload(_attendees);
|
||||
|
||||
const booking = await prisma.booking.findUnique({
|
||||
where: { uid: bookingUid },
|
||||
select: {
|
||||
id: true,
|
||||
eventType: {
|
||||
select: {
|
||||
id: true,
|
||||
teamId: true,
|
||||
userId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const orgId = await getOrgIdFromMemberOrTeamId({
|
||||
memberId: booking?.eventType?.userId,
|
||||
teamId: booking?.eventType?.teamId,
|
||||
});
|
||||
const webhooks = await new WebhookService({
|
||||
teamId: booking?.eventType?.teamId,
|
||||
userId: booking?.eventType?.userId,
|
||||
eventTypeId: booking?.eventType?.id,
|
||||
orgId,
|
||||
triggerEvent: WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED,
|
||||
});
|
||||
|
||||
const t = await getTranslation("en", "common");
|
||||
const message = t(payload.messageKey, { x: payload.attendees[0]?.email || "User" });
|
||||
|
||||
await webhooks.sendPayload({
|
||||
...payload,
|
||||
/** We send webhook message pre-translated, on client we already handle this */
|
||||
// @ts-expect-error payload is too booking specific, we need to refactor this
|
||||
message,
|
||||
bookingUid,
|
||||
bookingId: booking?.id,
|
||||
});
|
||||
|
||||
responsePayload["attendees"] = payload.attendees;
|
||||
responsePayload["message"] = message;
|
||||
responsePayload["messageKey"] = payload.messageKey;
|
||||
}
|
||||
|
||||
if (noShowHost) {
|
||||
await prisma.booking.update({
|
||||
where: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
data: {
|
||||
noShowHost: true,
|
||||
},
|
||||
});
|
||||
|
||||
responsePayload["noShowHost"] = true;
|
||||
responsePayload["message"] = "No-show status updated";
|
||||
}
|
||||
|
||||
return responsePayload;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
logger.error(error.message);
|
||||
}
|
||||
return { message: "Failed to update no-show status" };
|
||||
}
|
||||
};
|
||||
|
||||
export default handleMarkNoShow;
|
||||
@@ -4,6 +4,7 @@ import getBookingInfo from "@calcom/features/bookings/lib/getBookingInfo";
|
||||
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
|
||||
import * as newBookingMethods from "@calcom/features/bookings/lib/handleNewBooking";
|
||||
import { getPublicEvent } from "@calcom/features/eventtypes/lib/getPublicEvent";
|
||||
import handleMarkNoShow from "@calcom/features/handleMarkNoShow";
|
||||
import * as instantMeetingMethods from "@calcom/features/instant-meeting/handleInstantMeeting";
|
||||
import getAllUserBookings from "@calcom/lib/bookings/getAllUserBookings";
|
||||
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
||||
@@ -41,6 +42,8 @@ export { handleNewBooking };
|
||||
const handleInstantMeeting = instantMeetingMethods.default;
|
||||
export { handleInstantMeeting };
|
||||
|
||||
export { handleMarkNoShow };
|
||||
|
||||
export { getAvailableSlots };
|
||||
export type AvailableSlotsType = Awaited<ReturnType<typeof getAvailableSlots>>;
|
||||
export { handleNewRecurringBooking } from "@calcom/features/bookings/lib/handleNewRecurringBooking";
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import { WebhookService } from "@calcom/features/webhooks/lib/WebhookService";
|
||||
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { WebhookTriggerEvents } from "@calcom/prisma/client";
|
||||
import handleMarkNoShow from "@calcom/features/handleMarkNoShow";
|
||||
|
||||
import type { TNoShowInputSchema } from "./noShow.schema";
|
||||
|
||||
@@ -11,118 +6,10 @@ type NoShowOptions = {
|
||||
input: TNoShowInputSchema;
|
||||
};
|
||||
|
||||
const getResultPayload = async (attendees: { email: string; noShow: boolean }[]) => {
|
||||
if (attendees.length === 1) {
|
||||
const [attendee] = attendees;
|
||||
return {
|
||||
message: attendee.noShow ? "x_marked_as_no_show" : "x_unmarked_as_no_show",
|
||||
attendees: [attendee],
|
||||
};
|
||||
}
|
||||
return { message: "no_show_updated", attendees: attendees };
|
||||
};
|
||||
|
||||
const logFailedResults = (results: PromiseSettledResult<any>[]) => {
|
||||
const failed = results.filter((x) => x.status === "rejected") as PromiseRejectedResult[];
|
||||
if (failed.length < 1) return;
|
||||
const failedMessage = failed.map((r) => r.reason);
|
||||
console.error("Failed to update no-show status", failedMessage.join(","));
|
||||
};
|
||||
|
||||
export const noShowHandler = async ({ input }: NoShowOptions) => {
|
||||
const { bookingUid, attendees } = input;
|
||||
|
||||
try {
|
||||
const attendeeEmails = attendees?.map((attendee) => attendee.email) || [];
|
||||
if (attendees && attendeeEmails.length > 0) {
|
||||
const allAttendees = await prisma.attendee.findMany({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
booking: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
email: {
|
||||
in: attendeeEmails,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
const allAttendeesMap = allAttendees.reduce((acc, attendee) => {
|
||||
acc[attendee.email] = attendee;
|
||||
return acc;
|
||||
}, {} as Record<string, { id: number; email: string }>);
|
||||
const updatePromises = attendees.map((attendee) => {
|
||||
const attendeeToUpdate = allAttendeesMap[attendee.email];
|
||||
if (!attendeeToUpdate) return;
|
||||
return prisma.attendee.update({
|
||||
where: { id: attendeeToUpdate.id },
|
||||
data: { noShow: attendee.noShow },
|
||||
});
|
||||
});
|
||||
const results = await Promise.allSettled(updatePromises);
|
||||
logFailedResults(results);
|
||||
const _attendees = results
|
||||
.filter((x) => x.status === "fulfilled")
|
||||
.map((x) => (x as PromiseFulfilledResult<{ noShow: boolean; email: string }>).value)
|
||||
.map((x) => ({ email: x.email, noShow: x.noShow }));
|
||||
const payload = await getResultPayload(_attendees);
|
||||
const booking = await prisma.booking.findUnique({
|
||||
where: { uid: bookingUid },
|
||||
select: {
|
||||
id: true,
|
||||
eventType: {
|
||||
select: {
|
||||
id: true,
|
||||
teamId: true,
|
||||
userId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const orgId = await getOrgIdFromMemberOrTeamId({
|
||||
memberId: booking?.eventType?.userId,
|
||||
teamId: booking?.eventType?.teamId,
|
||||
});
|
||||
const webhooks = await new WebhookService({
|
||||
teamId: booking?.eventType?.teamId,
|
||||
userId: booking?.eventType?.userId,
|
||||
eventTypeId: booking?.eventType?.id,
|
||||
orgId,
|
||||
triggerEvent: WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED,
|
||||
});
|
||||
|
||||
const t = await getTranslation("en", "common");
|
||||
await webhooks.sendPayload({
|
||||
...payload,
|
||||
/** We send webhook message pre-translated, on client we already handle this */
|
||||
// @ts-expect-error payload is too booking specific, we need to refactor this
|
||||
message: t(payload.message, { x: payload.attendees[0]?.email || "User" }),
|
||||
bookingUid,
|
||||
bookingId: booking?.id,
|
||||
});
|
||||
return payload;
|
||||
}
|
||||
await prisma.booking.update({
|
||||
where: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
data: {
|
||||
noShowHost: true,
|
||||
},
|
||||
});
|
||||
return { message: "No-show status updated", noShowHost: true };
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
logger.error(error.message);
|
||||
}
|
||||
return { message: "Failed to update no-show status" };
|
||||
}
|
||||
return handleMarkNoShow({ bookingUid, attendees });
|
||||
};
|
||||
|
||||
export default noShowHandler;
|
||||
@@ -1,15 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZNoShowInputSchema = z.object({
|
||||
bookingUid: z.string(),
|
||||
attendees: z
|
||||
.array(
|
||||
z.object({
|
||||
email: z.string(),
|
||||
noShow: z.boolean(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
});
|
||||
export const ZNoShowInputSchema = z
|
||||
.object({
|
||||
bookingUid: z.string(),
|
||||
attendees: z
|
||||
.array(
|
||||
z.object({
|
||||
email: z.string(),
|
||||
noShow: z.boolean(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
noShowHost: z.boolean().optional(),
|
||||
})
|
||||
.refine(
|
||||
(data) => {
|
||||
return (data.attendees && data.attendees.length > 0) || data.noShowHost !== undefined;
|
||||
},
|
||||
{
|
||||
message: "At least one of 'attendees' or 'noShowHost' must be provided",
|
||||
path: ["attendees", "noShowHost"],
|
||||
}
|
||||
);
|
||||
|
||||
export type TNoShowInputSchema = z.infer<typeof ZNoShowInputSchema>;
|
||||
Reference in new issue
Block a user