feat: shorten sms links using dub (#17925)
* feat: add getshortenLinks function * feat: use shortenLinks in SMS Worflows * chore: use getShortenLink and cleanup getShortenLinks * fix: Duplicate keys error --------- Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Alex van Andel <me@alexvanandel.com>
This commit is contained in:
co-authored by
Peer Richelsen
Alex van Andel
parent
9b661b75f1
commit
a7727b5ca6
@@ -2,6 +2,7 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { getShortenLink } from "@calcom/ee/workflows/lib/reminders/utils";
|
||||
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
|
||||
import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
|
||||
import { defaultHandler } from "@calcom/lib/server";
|
||||
@@ -120,6 +121,33 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
reminder.booking.eventType?.team?.parentId ?? organizerOrganizationId ?? null
|
||||
);
|
||||
|
||||
const urls = {
|
||||
meetingUrl: bookingMetadataSchema.parse(reminder.booking?.metadata || {})?.videoCallUrl || "",
|
||||
cancelLink: `${bookerUrl}/booking/${reminder.booking.uid}?cancel=true` || "",
|
||||
rescheduleLink: `${bookerUrl}/reschedule/${reminder.booking.uid}` || "",
|
||||
};
|
||||
|
||||
const [meetingUrl, cancelLink, rescheduleLink] = await Promise.allSettled([
|
||||
getShortenLink(urls.meetingUrl),
|
||||
getShortenLink(urls.cancelLink),
|
||||
getShortenLink(urls.rescheduleLink),
|
||||
]).then((results) => {
|
||||
return results.map((result) => {
|
||||
let finalResult = "";
|
||||
|
||||
if (result.status === "fulfilled") {
|
||||
const v = result.value;
|
||||
if (typeof v === "string") {
|
||||
finalResult = v;
|
||||
} else {
|
||||
finalResult = v.shortLink;
|
||||
}
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
});
|
||||
});
|
||||
|
||||
const variables: VariablesType = {
|
||||
eventName: reminder.booking?.eventType?.title,
|
||||
organizerName: reminder.booking?.user?.name || "",
|
||||
@@ -131,9 +159,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
location: reminder.booking?.location || "",
|
||||
additionalNotes: reminder.booking?.description,
|
||||
responses: responses,
|
||||
meetingUrl: bookingMetadataSchema.parse(reminder.booking?.metadata || {})?.videoCallUrl,
|
||||
cancelLink: `${bookerUrl}/booking/${reminder.booking.uid}?cancel=true`,
|
||||
rescheduleLink: `${bookerUrl}/reschedule/${reminder.booking.uid}`,
|
||||
meetingUrl,
|
||||
cancelLink,
|
||||
rescheduleLink,
|
||||
attendeeTimezone: reminder.booking.attendees[0].timeZone,
|
||||
eventTimeInAttendeeTimezone: dayjs(reminder.booking.startTime).tz(
|
||||
reminder.booking.attendees[0].timeZone
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { getShortenLink } from "@calcom/ee/workflows/lib/reminders/utils";
|
||||
import { SENDER_ID, WEBSITE_URL } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { TimeFormat } from "@calcom/lib/timeFormat";
|
||||
@@ -146,6 +147,33 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
let smsMessage = message;
|
||||
|
||||
if (smsMessage) {
|
||||
const urls = {
|
||||
meetingUrl: bookingMetadataSchema.parse(evt.metadata || {})?.videoCallUrl || "",
|
||||
cancelLink: `${evt.bookerUrl ?? WEBSITE_URL}/booking/${evt.uid}?cancel=true`,
|
||||
rescheduleLink: `${evt.bookerUrl ?? WEBSITE_URL}/reschedule/${evt.uid}`,
|
||||
};
|
||||
|
||||
const [meetingUrl, cancelLink, rescheduleLink] = await Promise.allSettled([
|
||||
getShortenLink(urls.meetingUrl),
|
||||
getShortenLink(urls.cancelLink),
|
||||
getShortenLink(urls.rescheduleLink),
|
||||
]).then((results) => {
|
||||
return results.map((result) => {
|
||||
let finalResult = "";
|
||||
|
||||
if (result.status === "fulfilled") {
|
||||
const v = result.value;
|
||||
if (typeof v === "string") {
|
||||
finalResult = v;
|
||||
} else {
|
||||
finalResult = v.shortLink;
|
||||
}
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
});
|
||||
});
|
||||
|
||||
const variables: VariablesType = {
|
||||
eventName: evt.title,
|
||||
organizerName: evt.organizer.name,
|
||||
@@ -159,10 +187,10 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
location: evt.location,
|
||||
additionalNotes: evt.additionalNotes,
|
||||
responses: evt.responses,
|
||||
meetingUrl: bookingMetadataSchema.parse(evt.metadata || {})?.videoCallUrl,
|
||||
cancelLink: `${evt.bookerUrl ?? WEBSITE_URL}/booking/${evt.uid}?cancel=true`,
|
||||
meetingUrl,
|
||||
cancelLink,
|
||||
rescheduleLink,
|
||||
cancelReason: evt.cancellationReason,
|
||||
rescheduleLink: `${evt.bookerUrl ?? WEBSITE_URL}/reschedule/${evt.uid}`,
|
||||
rescheduleReason: evt.rescheduleReason,
|
||||
attendeeTimezone: evt.attendees[0].timeZone,
|
||||
eventTimeInAttendeeTimezone: dayjs(evt.startTime).tz(evt.attendees[0].timeZone),
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { dub } from "@calcom/features/auth/lib/dub";
|
||||
|
||||
export const getShortenLink = (link: string) => {
|
||||
// don't hit dub with with empty string
|
||||
if (!link.length) {
|
||||
const pr: Promise<string> = new Promise((resolve) => resolve(link));
|
||||
return pr;
|
||||
} else {
|
||||
return dub.links.create({
|
||||
url: link,
|
||||
domain: "sms.cal.com",
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user