* Add credits section to billing * create seperate router for credits * add stripe checkout session * schema changes + code improvements * rename to creditBalance * custom quantify input with error message * add checkout session completed webhook endpoint * fix typo * UI fixes * add payCredits handler * add error toast message * allow scheduling sms up as close to 15 minutes in the future * schedule at most 2 hours in advance * webhook to pay for sent sms * continued work on twilio callback * code clean up * further implementation for credit handling * add migration * object as param for scheduleSMS * object as param for sendSMS * fix TrpcSessionUser imports * fix imports * add db changes * add cron job for price setting * twilio status callback to create expense log * remove unused code * set up low credit balance email * fixes for buying credits * fixes in api/twilio/webhook * add test to save credits to credits balance * fix typos * add new helper function chargeCredits * expand twilioProvider * fix type errors * adjust tests * type errors * clean up * clean up * fix subscription active check * remove some user/org related code * more changes to remove user/org support * send emails seperatly to admins * fixes for team billing page * fix stripe success url * fixes to creating expense log * email imrovements and more * get monthly team price from stripe * fix import * fix monthly credits calculation * finsih low credit balance warning email * credit balance limit reached email * create CreditService * cancel SMS and send as email instead * add messageDispatcher * fix type error * fix type error * fix type error * fix import * fix unit test * clean up twilioProvider * clean up chckSmsPrices/route * add missing translations * add skeleton loader * add admin check to get handler * code clean up + fixes * improve scheduling with fallback * fix type error * add bookingUid to handleSendingSMS * add unit tests for creditService * add more tests to credit-service.test.ts * add test for cancelScheduledMessagesAndScheduleEmails * fix test and type error * add back resolve * fix empty resolve * adjust limitReachedAt logic * address mrge comment on styling * add getAdminMembership to repository * twilio/webhook clean up (feedback) * feedback - clean up * remove todo comment * clean up twilio/webhook * code clean up * add use client * add createOneTimeCheckout to stripe service * refactor repository pattern * small fixes + clean up * fix type error * add missing import * fix hasAvailableCredits for user * force-dynamic * rename credits to creditBalance * fix stripe import * remove not needed code * fix e2e tests * improve low balance warning email * dynamic-import CreditService * index.ts * fix e2e tests * remove dynamic import CreditService * Revert "remove dynamic import CreditService" This reverts commit e272978a7ff3fc5a04139e656c9f8d2c84a40dda. * no need to dynamic-import credit service * Revert "no need to dynamic-import credit service" This reverts commit ba5ae488d08979a65fb47b5d0722cda9f45d6ea0. * only select id in getAdminMembership * revert billing/package.json * fix type checks * fix type checks --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: hbjORbj <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Peer Richelsen <[email protected]>
337 lines
9.3 KiB
TypeScript
337 lines
9.3 KiB
TypeScript
import type { NextRequest } from "next/server";
|
|
import TwilioClient from "twilio";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import { checkSMSRateLimit } from "@calcom/lib/checkRateLimitAndThrowError";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { setTestSMS } from "@calcom/lib/testSMS";
|
|
import prisma from "@calcom/prisma";
|
|
import { SMSLockState } from "@calcom/prisma/enums";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["[twilioProvider]"] });
|
|
|
|
const testMode = process.env.NEXT_PUBLIC_IS_E2E || process.env.INTEGRATION_TEST_MODE || process.env.IS_E2E;
|
|
|
|
function createTwilioClient() {
|
|
if (process.env.TWILIO_SID && process.env.TWILIO_TOKEN && process.env.TWILIO_MESSAGING_SID) {
|
|
return TwilioClient(process.env.TWILIO_SID, process.env.TWILIO_TOKEN);
|
|
}
|
|
throw new Error("Twilio credentials are missing from the .env file");
|
|
}
|
|
|
|
function getDefaultSender(isWhatsapp = false) {
|
|
let defaultSender = process.env.TWILIO_PHONE_NUMBER;
|
|
if (isWhatsapp) {
|
|
defaultSender = `whatsapp:+${process.env.TWILIO_WHATSAPP_PHONE_NUMBER}`;
|
|
}
|
|
return defaultSender || "";
|
|
}
|
|
|
|
function getSMSNumber(phone: string, isWhatsapp = false) {
|
|
return isWhatsapp ? `whatsapp:${phone}` : phone;
|
|
}
|
|
|
|
export const sendSMS = async ({
|
|
phoneNumber,
|
|
body,
|
|
sender,
|
|
bookingUid,
|
|
userId,
|
|
teamId,
|
|
isWhatsapp = false,
|
|
}: {
|
|
phoneNumber: string;
|
|
body: string;
|
|
sender: string;
|
|
bookingUid?: string | null;
|
|
userId?: number | null;
|
|
teamId?: number | null;
|
|
isWhatsapp?: boolean;
|
|
}) => {
|
|
log.silly("sendSMS", JSON.stringify({ phoneNumber, body, sender, userId, teamId }));
|
|
|
|
const isSMSSendingLocked = await isLockedForSMSSending(userId, teamId);
|
|
|
|
if (isSMSSendingLocked) {
|
|
log.debug(`${teamId ? `Team id ${teamId} ` : `User id ${userId} `} is locked for SMS sending `);
|
|
return;
|
|
}
|
|
|
|
if (testMode) {
|
|
setTestSMS({
|
|
to: getSMSNumber(phoneNumber, isWhatsapp),
|
|
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
|
message: body,
|
|
});
|
|
console.log(
|
|
"Skipped sending SMS because process.env.NEXT_PUBLIC_IS_E2E or process.env.INTEGRATION_TEST_MODE is set. SMS are available in globalThis.testSMS"
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
const twilio = createTwilioClient();
|
|
|
|
if (!teamId && userId) {
|
|
await checkSMSRateLimit({
|
|
identifier: `sms:user:${userId}`,
|
|
rateLimitingType: "smsMonth",
|
|
});
|
|
}
|
|
|
|
const response = await twilio.messages.create({
|
|
body: body,
|
|
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
|
|
to: getSMSNumber(phoneNumber, isWhatsapp),
|
|
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
|
statusCallback: getStatusCallbackUrl(userId, teamId, bookingUid),
|
|
});
|
|
|
|
return response;
|
|
};
|
|
|
|
const getStatusCallbackUrl = (userId?: number | null, teamId?: number | null, bookingUid?: string | null) => {
|
|
const query = new URLSearchParams();
|
|
if (userId) query.append("userId", String(userId));
|
|
if (teamId) query.append("teamId", String(teamId));
|
|
if (bookingUid) query.append("bookingUid", bookingUid);
|
|
return `${WEBAPP_URL}/api/twilio/webhook${query.toString() ? `?${query.toString()}` : ""}`;
|
|
};
|
|
|
|
export const scheduleSMS = async ({
|
|
phoneNumber,
|
|
body,
|
|
scheduledDate,
|
|
sender,
|
|
bookingUid,
|
|
userId,
|
|
teamId,
|
|
isWhatsapp = false,
|
|
}: {
|
|
phoneNumber: string;
|
|
body: string;
|
|
scheduledDate: Date;
|
|
sender: string;
|
|
bookingUid?: string | null;
|
|
userId?: number | null;
|
|
teamId?: number | null;
|
|
isWhatsapp?: boolean;
|
|
}) => {
|
|
const isSMSSendingLocked = await isLockedForSMSSending(userId, teamId);
|
|
|
|
if (isSMSSendingLocked) {
|
|
log.debug(`${teamId ? `Team id ${teamId} ` : `User id ${userId} `} is locked for SMS sending `);
|
|
return;
|
|
}
|
|
|
|
if (testMode) {
|
|
setTestSMS({
|
|
to: getSMSNumber(phoneNumber, isWhatsapp),
|
|
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
|
message: body,
|
|
});
|
|
console.log(
|
|
"Skipped sending SMS because process.env.NEXT_PUBLIC_IS_E2E or process.env.INTEGRATION_TEST_MODE is set. SMS are available in globalThis.testSMS"
|
|
);
|
|
return { sid: uuidv4() };
|
|
}
|
|
|
|
const twilio = createTwilioClient();
|
|
|
|
if (!teamId && userId) {
|
|
await checkSMSRateLimit({
|
|
identifier: `sms:user:${userId}`,
|
|
rateLimitingType: "smsMonth",
|
|
});
|
|
}
|
|
const response = await twilio.messages.create({
|
|
body,
|
|
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
|
|
to: getSMSNumber(phoneNumber, isWhatsapp),
|
|
scheduleType: "fixed",
|
|
sendAt: scheduledDate,
|
|
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
|
statusCallback: getStatusCallbackUrl(userId, teamId, bookingUid),
|
|
});
|
|
|
|
return response;
|
|
};
|
|
|
|
export const cancelSMS = async (referenceId: string) => {
|
|
const twilio = createTwilioClient();
|
|
await twilio.messages(referenceId).update({ status: "canceled" });
|
|
};
|
|
|
|
export const sendVerificationCode = async (phoneNumber: string) => {
|
|
const twilio = createTwilioClient();
|
|
if (process.env.TWILIO_VERIFY_SID) {
|
|
await twilio.verify
|
|
.services(process.env.TWILIO_VERIFY_SID)
|
|
.verifications.create({ to: phoneNumber, channel: "sms" });
|
|
}
|
|
};
|
|
|
|
export const verifyNumber = async (phoneNumber: string, code: string) => {
|
|
const twilio = createTwilioClient();
|
|
if (process.env.TWILIO_VERIFY_SID) {
|
|
try {
|
|
const verification_check = await twilio.verify.v2
|
|
.services(process.env.TWILIO_VERIFY_SID)
|
|
.verificationChecks.create({ to: phoneNumber, code: code });
|
|
return verification_check.status;
|
|
} catch (e) {
|
|
return "failed";
|
|
}
|
|
}
|
|
};
|
|
|
|
export const getMessageBody = async (referenceId: string) => {
|
|
const twilio = createTwilioClient();
|
|
const message = await twilio.messages(referenceId).fetch();
|
|
return message.body;
|
|
};
|
|
|
|
async function isLockedForSMSSending(userId?: number | null, teamId?: number | null) {
|
|
if (teamId) {
|
|
const team = await prisma.team.findFirst({
|
|
where: {
|
|
id: teamId,
|
|
},
|
|
});
|
|
return team?.smsLockState === SMSLockState.LOCKED;
|
|
}
|
|
|
|
if (userId) {
|
|
const memberships = await prisma.membership.findMany({
|
|
where: {
|
|
userId: userId,
|
|
},
|
|
select: {
|
|
team: {
|
|
select: {
|
|
smsLockState: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const memberOfLockedTeam = memberships.find(
|
|
(membership) => membership.team.smsLockState === SMSLockState.LOCKED
|
|
);
|
|
|
|
if (!!memberOfLockedTeam) {
|
|
return true;
|
|
}
|
|
|
|
const user = await prisma.user.findFirst({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
return user?.smsLockState === SMSLockState.LOCKED;
|
|
}
|
|
}
|
|
|
|
export async function getCountryCodeForNumber(phoneNumber: string) {
|
|
const twilio = createTwilioClient();
|
|
const { countryCode } = await twilio.lookups.v2.phoneNumbers(phoneNumber).fetch();
|
|
return countryCode;
|
|
}
|
|
|
|
export async function getPriceForSMS(smsSid: string) {
|
|
const twilio = createTwilioClient();
|
|
const message = await twilio.messages(smsSid).fetch();
|
|
if (message.price == null || message.price === "null") return null;
|
|
return Math.abs(parseFloat(message.price));
|
|
}
|
|
|
|
export async function validateWebhookRequest({
|
|
requestUrl,
|
|
params,
|
|
signature,
|
|
}: {
|
|
requestUrl: string;
|
|
params: object;
|
|
signature: string;
|
|
}) {
|
|
if (!process.env.TWILIO_TOKEN) {
|
|
throw new Error("TWILIO_TOKEN is not set");
|
|
}
|
|
|
|
const isSignatureValid = TwilioClient.validateRequest(
|
|
process.env.TWILIO_TOKEN,
|
|
signature,
|
|
requestUrl,
|
|
params
|
|
);
|
|
return isSignatureValid;
|
|
}
|
|
|
|
export async function determineOptOutType(
|
|
req: NextRequest
|
|
): Promise<{ phoneNumber: string; optOutStatus: boolean } | { error: string }> {
|
|
const signature = req.headers.get("X-Twilio-Signature");
|
|
const formData = await req.formData();
|
|
const params = Object.fromEntries(formData.entries());
|
|
|
|
if (!signature) {
|
|
return { error: "Missing Twilio signature" };
|
|
}
|
|
|
|
const fullUrl = `${WEBAPP_URL}${req.nextUrl.pathname}`;
|
|
|
|
const isSignatureValid = await validateWebhookRequest({
|
|
requestUrl: fullUrl,
|
|
params: params,
|
|
signature: signature,
|
|
});
|
|
|
|
if (!isSignatureValid) {
|
|
return { error: "Invalid signature" };
|
|
}
|
|
|
|
const accountSid = params["AccountSid"]?.valueOf();
|
|
|
|
if (accountSid !== process.env.TWILIO_SID) {
|
|
return { error: "Invalid account SID" };
|
|
}
|
|
|
|
// Twilio returns phone numbers with a + prefix
|
|
const phoneNumberRaw = params["From"]?.valueOf();
|
|
|
|
if (!phoneNumberRaw) {
|
|
return { error: "No phone number to handle" };
|
|
}
|
|
|
|
const phoneNumber = phoneNumberRaw.toString();
|
|
|
|
if (!formData.get("OptOutType")) {
|
|
return { error: "No opt out message to handle" };
|
|
}
|
|
|
|
const optOutMessage = formData.get("OptOutType")?.valueOf();
|
|
|
|
if (optOutMessage !== "STOP" && optOutMessage !== "START") {
|
|
return { error: "Invalid opt out type" };
|
|
}
|
|
|
|
const optOutStatus = optOutMessage === "STOP" ? true : false;
|
|
|
|
return { phoneNumber, optOutStatus };
|
|
}
|
|
export async function deleteMultipleScheduledSMS(referenceIds: string[]) {
|
|
const twilio = createTwilioClient();
|
|
|
|
const pLimit = (await import("p-limit")).default;
|
|
const limit = pLimit(10);
|
|
|
|
await Promise.allSettled(
|
|
referenceIds.map((referenceId) => {
|
|
return limit(() => twilio.messages(referenceId).update({ status: "canceled" })).catch((error) => {
|
|
log.error(`Error canceling scheduled SMS with id ${referenceId}`, error);
|
|
});
|
|
})
|
|
);
|
|
}
|