feat: Credit System (SMS) (#20126)
* 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]>
This commit is contained in:
45 files changed
+2481
-133
No files matched your search
+10
-1
@@ -20,12 +20,21 @@ import {
|
||||
import { getMockRequestDataForBooking } from "@calcom/web/test/utils/bookingScenario/getMockRequestDataForBooking";
|
||||
import { setupAndTeardown } from "@calcom/web/test/utils/bookingScenario/setupAndTeardown";
|
||||
|
||||
import { describe, beforeEach } from "vitest";
|
||||
import { describe, beforeEach, vi } from "vitest";
|
||||
|
||||
import { resetTestSMS } from "@calcom/lib/testSMS";
|
||||
import { SMSLockState, SchedulingType } from "@calcom/prisma/enums";
|
||||
import { test } from "@calcom/web/test/fixtures/fixtures";
|
||||
|
||||
vi.mock("@calcom/lib/constants", async () => {
|
||||
const actual = await vi.importActual<typeof import("@calcom/lib/constants")>("@calcom/lib/constants");
|
||||
|
||||
return {
|
||||
...actual,
|
||||
IS_SMS_CREDITS_ENABLED: false,
|
||||
};
|
||||
});
|
||||
|
||||
// Local test runs sometime gets too slow
|
||||
const timeout = process.env.CI ? 5000 : 20000;
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import stripe from "@calcom/features/ee/payments/server/stripe";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type { SWHMap } from "./__handler";
|
||||
import { HttpCode } from "./__handler";
|
||||
|
||||
const handler = async (data: SWHMap["checkout.session.completed"]["data"]) => {
|
||||
const session = data.object;
|
||||
if (!session.amount_total) {
|
||||
throw new HttpCode(400, "Missing required payment details");
|
||||
}
|
||||
|
||||
const teamId = session.metadata?.teamId ? Number(session.metadata.teamId) : null;
|
||||
|
||||
if (!teamId) {
|
||||
throw new HttpCode(400, "Team id missing but required");
|
||||
}
|
||||
|
||||
const lineItems = await stripe.checkout.sessions.listLineItems(session.id);
|
||||
const priceId = lineItems.data[0]?.price?.id;
|
||||
const nrOfCredits = lineItems.data[0]?.quantity ?? 0;
|
||||
|
||||
if (!priceId || priceId !== process.env.NEXT_PUBLIC_STRIPE_CREDITS_PRICE_ID || !nrOfCredits) {
|
||||
throw new HttpCode(400, "Invalid price ID");
|
||||
}
|
||||
|
||||
await saveToCreditBalance({ teamId, nrOfCredits });
|
||||
|
||||
return { success: true };
|
||||
};
|
||||
|
||||
async function saveToCreditBalance({ teamId, nrOfCredits }: { teamId: number; nrOfCredits: number }) {
|
||||
const creditBalance = await prisma.creditBalance.findUnique({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (creditBalance) {
|
||||
await prisma.creditBalance.update({
|
||||
where: {
|
||||
id: creditBalance.id,
|
||||
},
|
||||
data: { additionalCredits: { increment: nrOfCredits }, limitReachedAt: null, warningSentAt: null },
|
||||
});
|
||||
return;
|
||||
}
|
||||
await prisma.creditBalance.create({
|
||||
data: {
|
||||
teamId: teamId,
|
||||
additionalCredits: nrOfCredits,
|
||||
},
|
||||
});
|
||||
}
|
||||
export default handler;
|
||||
@@ -8,6 +8,7 @@ const handlers = {
|
||||
"payment_intent.succeeded": () => import("./_payment_intent.succeeded"),
|
||||
"customer.subscription.deleted": () => import("./_customer.subscription.deleted"),
|
||||
"invoice.paid": () => import("./_invoice.paid"),
|
||||
"checkout.session.completed": () => import("./_checkout.session.completed"),
|
||||
};
|
||||
|
||||
export default defaultHandler({
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import * as EmailManager from "@calcom/emails/email-manager";
|
||||
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
|
||||
import { MembershipRepository } from "@calcom/lib/server/repository/membership";
|
||||
import { TeamRepository } from "@calcom/lib/server/repository/team";
|
||||
import { CreditType } from "@calcom/prisma/enums";
|
||||
|
||||
import { CreditService } from "./credit-service";
|
||||
|
||||
vi.mock("@calcom/lib/constants", async () => {
|
||||
const actual = (await vi.importActual("@calcom/lib/constants")) as typeof import("@calcom/lib/constants");
|
||||
return {
|
||||
...actual,
|
||||
IS_SMS_CREDITS_ENABLED: true,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@calcom/lib/server/repository/credits");
|
||||
vi.mock("@calcom/lib/server/repository/membership");
|
||||
vi.mock("@calcom/lib/server/repository/team");
|
||||
vi.mock("@calcom/emails/email-manager");
|
||||
vi.mock("../workflows/lib/reminders/reminderScheduler", () => ({
|
||||
cancelScheduledMessagesAndScheduleEmails: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("CreditService", () => {
|
||||
let creditService: CreditService;
|
||||
|
||||
beforeEach(() => {
|
||||
creditService = new CreditService();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("hasAvailableCredits", () => {
|
||||
it("should return true if team has not yet reached limit", async () => {
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
const noLimitReached = await creditService.hasAvailableCredits({ teamId: 1 });
|
||||
expect(noLimitReached).toBe(true);
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: dayjs().subtract(1, "month").toDate(),
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
const limitReachedLastMonth = await creditService.hasAvailableCredits({ teamId: 1 });
|
||||
expect(limitReachedLastMonth).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if team limit reached this month", async () => {
|
||||
vi.setSystemTime(new Date("2024-06-20T11:59:59Z"));
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: dayjs().subtract(1, "week").toDate(),
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
const result = await creditService.hasAvailableCredits({ teamId: 1 });
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTeamWithAvailableCredits", () => {
|
||||
it("should return team with available credits", async () => {
|
||||
vi.mocked(MembershipRepository.findAllAcceptedMemberships).mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
teamId: 1,
|
||||
userId: 1,
|
||||
role: "MEMBER",
|
||||
accepted: true,
|
||||
},
|
||||
]);
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
const result = await creditService.getTeamWithAvailableCredits(1);
|
||||
expect(result).toEqual({
|
||||
teamId: 1,
|
||||
availableCredits: 0,
|
||||
creditType: CreditType.ADDITIONAL,
|
||||
});
|
||||
});
|
||||
|
||||
it("should return first team if no team has available credits", async () => {
|
||||
vi.mocked(MembershipRepository.findAllAcceptedMemberships).mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
teamId: 1,
|
||||
userId: 1,
|
||||
role: "MEMBER",
|
||||
accepted: true,
|
||||
},
|
||||
]);
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: new Date(),
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
const result = await creditService.getTeamWithAvailableCredits(1);
|
||||
expect(result).toEqual({
|
||||
teamId: 1,
|
||||
availableCredits: 0,
|
||||
creditType: CreditType.ADDITIONAL,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("chargeCredits", () => {
|
||||
it("should create expense log and send low balance warning email", async () => {
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 10,
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
vi.mocked(TeamRepository.findTeamWithAdmins).mockResolvedValue({
|
||||
id: 1,
|
||||
name: "team-name",
|
||||
members: [
|
||||
{
|
||||
user: {
|
||||
name: "admin",
|
||||
email: "[email protected]",
|
||||
locale: "en",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
vi.spyOn(EmailManager, "sendCreditBalanceLowWarningEmails").mockResolvedValue();
|
||||
|
||||
vi.spyOn(CreditService.prototype, "getAllCreditsForTeam").mockResolvedValue({
|
||||
totalMonthlyCredits: 500,
|
||||
totalRemainingMonthlyCredits: 20,
|
||||
additionalCredits: 60,
|
||||
});
|
||||
|
||||
await creditService.chargeCredits({
|
||||
teamId: 1,
|
||||
credits: 5,
|
||||
bookingUid: "booking-123",
|
||||
smsSid: "sms-123",
|
||||
});
|
||||
|
||||
expect(CreditsRepository.createCreditExpenseLog).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
bookingUid: "booking-123",
|
||||
creditBalanceId: "1",
|
||||
creditType: CreditType.MONTHLY,
|
||||
credits: 5,
|
||||
smsSid: "sms-123",
|
||||
})
|
||||
);
|
||||
|
||||
expect(EmailManager.sendCreditBalanceLowWarningEmails).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should create expense log and send limit reached email", async () => {
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
vi.mocked(TeamRepository.findTeamWithAdmins).mockResolvedValue({
|
||||
id: 1,
|
||||
name: "team-name",
|
||||
members: [
|
||||
{
|
||||
user: {
|
||||
name: "admin",
|
||||
email: "[email protected]",
|
||||
locale: "en",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
vi.spyOn(EmailManager, "sendCreditBalanceLimitReachedEmails").mockResolvedValue();
|
||||
|
||||
vi.spyOn(CreditService.prototype, "getAllCreditsForTeam").mockResolvedValue({
|
||||
totalMonthlyCredits: 500,
|
||||
totalRemainingMonthlyCredits: -1,
|
||||
additionalCredits: 0,
|
||||
});
|
||||
|
||||
await creditService.chargeCredits({
|
||||
teamId: 1,
|
||||
credits: 5,
|
||||
bookingUid: "booking-123",
|
||||
smsSid: "sms-123",
|
||||
});
|
||||
|
||||
expect(CreditsRepository.createCreditExpenseLog).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
bookingUid: "booking-123",
|
||||
creditBalanceId: "1",
|
||||
creditType: CreditType.ADDITIONAL,
|
||||
credits: 5,
|
||||
smsSid: "sms-123",
|
||||
})
|
||||
);
|
||||
|
||||
expect(EmailManager.sendCreditBalanceLimitReachedEmails).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTeamToCharge", () => {
|
||||
it("should return team with remaining credits when teamId is provided", async () => {
|
||||
vi.spyOn(CreditService.prototype, "getAllCreditsForTeam").mockResolvedValue({
|
||||
totalMonthlyCredits: 500,
|
||||
totalRemainingMonthlyCredits: 100,
|
||||
additionalCredits: 50,
|
||||
});
|
||||
|
||||
const result = await creditService.getTeamToCharge({
|
||||
credits: 50,
|
||||
teamId: 1,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
teamId: 1,
|
||||
remainingCredits: 100,
|
||||
creditType: CreditType.MONTHLY,
|
||||
});
|
||||
});
|
||||
|
||||
it("should use additional credits when monthly credits are out", async () => {
|
||||
vi.spyOn(CreditService.prototype, "getAllCreditsForTeam").mockResolvedValue({
|
||||
totalMonthlyCredits: 500,
|
||||
totalRemainingMonthlyCredits: 0,
|
||||
additionalCredits: 50,
|
||||
});
|
||||
|
||||
const result = await creditService.getTeamToCharge({
|
||||
credits: 30,
|
||||
teamId: 1,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
teamId: 1,
|
||||
remainingCredits: 20,
|
||||
creditType: CreditType.ADDITIONAL,
|
||||
});
|
||||
});
|
||||
|
||||
it("should return team with available credits when userId is provided", async () => {
|
||||
vi.mocked(MembershipRepository.findAllAcceptedMemberships).mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
teamId: 1,
|
||||
userId: 1,
|
||||
role: "MEMBER",
|
||||
accepted: true,
|
||||
},
|
||||
]);
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditBalance).mockResolvedValue({
|
||||
id: "1",
|
||||
additionalCredits: 0,
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
});
|
||||
|
||||
vi.spyOn(CreditService.prototype, "getAllCreditsForTeam").mockResolvedValue({
|
||||
totalMonthlyCredits: 500,
|
||||
totalRemainingMonthlyCredits: 100,
|
||||
additionalCredits: 50,
|
||||
});
|
||||
|
||||
const result = await creditService.getTeamToCharge({
|
||||
credits: 50,
|
||||
userId: 1,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
teamId: 1,
|
||||
availableCredits: 150,
|
||||
creditType: CreditType.MONTHLY,
|
||||
remainingCredits: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,363 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import {
|
||||
sendCreditBalanceLimitReachedEmails,
|
||||
sendCreditBalanceLowWarningEmails,
|
||||
} from "@calcom/emails/email-manager";
|
||||
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billling-service";
|
||||
import { InternalTeamBilling } from "@calcom/features/ee/billing/teams/internal-team-billing";
|
||||
import { cancelScheduledMessagesAndScheduleEmails } from "@calcom/features/ee/workflows/lib/reminders/reminderScheduler";
|
||||
import { IS_SMS_CREDITS_ENABLED } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
|
||||
import { MembershipRepository } from "@calcom/lib/server/repository/membership";
|
||||
import { TeamRepository } from "@calcom/lib/server/repository/team";
|
||||
import { CreditType } from "@calcom/prisma/enums";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[CreditService]"] });
|
||||
|
||||
export class CreditService {
|
||||
async chargeCredits({
|
||||
userId,
|
||||
teamId,
|
||||
credits,
|
||||
bookingUid,
|
||||
smsSid,
|
||||
}: {
|
||||
userId?: number;
|
||||
teamId?: number;
|
||||
credits: number | null;
|
||||
bookingUid?: string;
|
||||
smsSid?: string;
|
||||
}) {
|
||||
let teamToCharge = credits === 0 && teamId ? teamId : null;
|
||||
let creditType: CreditType = CreditType.ADDITIONAL;
|
||||
let remainingCredits;
|
||||
if (credits !== 0) {
|
||||
const result = await this.getTeamToCharge({
|
||||
credits: credits ?? 1, // if we don't have exact credits, we check for at east 1 credit available
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
teamToCharge = result?.teamId ?? null;
|
||||
creditType = result?.creditType ?? creditType;
|
||||
remainingCredits = result?.remainingCredits;
|
||||
}
|
||||
|
||||
if (!teamToCharge) {
|
||||
log.error("No team or user found to charge. No credit expense log created");
|
||||
return null;
|
||||
}
|
||||
|
||||
await this.createExpenseLog({
|
||||
bookingUid,
|
||||
smsSid,
|
||||
teamId: teamToCharge,
|
||||
credits,
|
||||
creditType,
|
||||
});
|
||||
|
||||
if (credits) {
|
||||
await this.handleLowCreditBalance({
|
||||
teamId: teamToCharge,
|
||||
remainingCredits,
|
||||
});
|
||||
}
|
||||
|
||||
return teamToCharge;
|
||||
}
|
||||
|
||||
async hasAvailableCredits({ userId, teamId }: { userId?: number | null; teamId?: number | null }) {
|
||||
if (!IS_SMS_CREDITS_ENABLED) return true;
|
||||
|
||||
if (teamId) {
|
||||
const creditBalance = await CreditsRepository.findCreditBalance({ teamId });
|
||||
|
||||
const limitReached =
|
||||
creditBalance?.limitReachedAt &&
|
||||
dayjs(creditBalance.limitReachedAt).isAfter(dayjs().startOf("month"));
|
||||
|
||||
if (!limitReached) return true;
|
||||
|
||||
// check if team is still out of credits
|
||||
const teamCredits = await this.getAllCreditsForTeam(teamId);
|
||||
const availableCredits = teamCredits.totalRemainingMonthlyCredits + teamCredits.additionalCredits;
|
||||
|
||||
if (availableCredits > 0) {
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
teamId,
|
||||
data: {
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
const team = await this.getTeamWithAvailableCredits(userId);
|
||||
return team.availableCredits > 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async getTeamWithAvailableCredits(userId: number) {
|
||||
const memberships = await MembershipRepository.findAllAcceptedMemberships(userId);
|
||||
|
||||
//check if user is member of team that has available credits
|
||||
for (const membership of memberships) {
|
||||
const creditBalance = await CreditsRepository.findCreditBalance({ teamId: membership.teamId });
|
||||
|
||||
const allCredits = await this.getAllCreditsForTeam(membership.teamId);
|
||||
const limitReached =
|
||||
creditBalance?.limitReachedAt &&
|
||||
dayjs(creditBalance.limitReachedAt).isAfter(dayjs().startOf("month"));
|
||||
|
||||
const availableCredits = allCredits.totalRemainingMonthlyCredits + allCredits.additionalCredits;
|
||||
|
||||
if (!limitReached || availableCredits > 0) {
|
||||
if (limitReached) {
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
teamId: membership.teamId,
|
||||
data: {
|
||||
limitReachedAt: null,
|
||||
warningSentAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
return {
|
||||
teamId: membership.teamId,
|
||||
availableCredits,
|
||||
creditType:
|
||||
allCredits.totalRemainingMonthlyCredits > 0 ? CreditType.MONTHLY : CreditType.ADDITIONAL,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
teamId: memberships[0].teamId,
|
||||
availableCredits: 0,
|
||||
creditType: CreditType.ADDITIONAL,
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
always returns a team, even if all teams are out of credits
|
||||
*/
|
||||
async getTeamToCharge({
|
||||
credits,
|
||||
userId,
|
||||
teamId,
|
||||
}: {
|
||||
credits: number;
|
||||
userId?: number | null;
|
||||
teamId?: number | null;
|
||||
}) {
|
||||
if (teamId) {
|
||||
const teamCredits = await this.getAllCreditsForTeam(teamId);
|
||||
const remaningMonthlyCredits =
|
||||
teamCredits.totalRemainingMonthlyCredits > 0 ? teamCredits.totalRemainingMonthlyCredits : 0;
|
||||
return {
|
||||
teamId,
|
||||
remainingCredits: remaningMonthlyCredits + teamCredits.additionalCredits - credits,
|
||||
creditType: remaningMonthlyCredits > 0 ? CreditType.MONTHLY : CreditType.ADDITIONAL,
|
||||
};
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
const team = await this.getTeamWithAvailableCredits(userId);
|
||||
return { ...team, remainingCredits: team.availableCredits - credits };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async createExpenseLog(props: {
|
||||
bookingUid?: string;
|
||||
smsSid?: string;
|
||||
teamId: number;
|
||||
credits: number | null;
|
||||
creditType: CreditType;
|
||||
}) {
|
||||
const { credits, creditType, bookingUid, smsSid, teamId } = props;
|
||||
let creditBalance: { id: string; additionalCredits: number } | null =
|
||||
await CreditsRepository.findCreditBalance({ teamId });
|
||||
|
||||
if (!creditBalance) {
|
||||
creditBalance = await CreditsRepository.createCreditBalance({
|
||||
teamId,
|
||||
});
|
||||
}
|
||||
|
||||
if (credits && creditType === CreditType.ADDITIONAL) {
|
||||
const decrementValue =
|
||||
credits <= creditBalance.additionalCredits ? credits : creditBalance.additionalCredits;
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
id: creditBalance.id,
|
||||
data: {
|
||||
additionalCredits: {
|
||||
decrement: decrementValue,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (creditBalance) {
|
||||
// also track logs with undefined credits (will be set on the cron job)
|
||||
await CreditsRepository.createCreditExpenseLog({
|
||||
creditBalanceId: creditBalance.id,
|
||||
credits,
|
||||
creditType,
|
||||
date: new Date(),
|
||||
bookingUid,
|
||||
smsSid,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Called when we know the exact amount of credits to be charged:
|
||||
- Sets `limitReachedAt` and `warningSentAt`
|
||||
- Sends warning email if balance is low
|
||||
- Sends limit reached email
|
||||
- cancels all already scheduled SMS (from the next two hours)
|
||||
*/
|
||||
async handleLowCreditBalance({
|
||||
teamId,
|
||||
remainingCredits = 0,
|
||||
}: {
|
||||
teamId: number;
|
||||
remainingCredits?: number;
|
||||
}) {
|
||||
const { totalMonthlyCredits } = await this.getAllCreditsForTeam(teamId);
|
||||
const warningLimit = totalMonthlyCredits * 0.2;
|
||||
if (remainingCredits < warningLimit) {
|
||||
const creditBalance = await CreditsRepository.findCreditBalance({ teamId });
|
||||
|
||||
if (
|
||||
creditBalance?.limitReachedAt &&
|
||||
dayjs(creditBalance?.limitReachedAt).isAfter(dayjs().startOf("month"))
|
||||
) {
|
||||
return; // team has already reached limit this month
|
||||
}
|
||||
|
||||
const team = await TeamRepository.findTeamWithAdmins(teamId);
|
||||
|
||||
if (!team) {
|
||||
log.error("Team not found to send warning email");
|
||||
return;
|
||||
}
|
||||
|
||||
if (remainingCredits <= 0) {
|
||||
await sendCreditBalanceLimitReachedEmails({
|
||||
team: {
|
||||
id: teamId,
|
||||
name: team.name,
|
||||
adminAndOwners: await Promise.all(
|
||||
team.members.map(async (member) => ({
|
||||
name: member.user.name ?? "",
|
||||
email: member.user.email,
|
||||
t: await getTranslation(member.user.locale ?? "en", "common"),
|
||||
}))
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
teamId,
|
||||
data: {
|
||||
limitReachedAt: new Date(),
|
||||
warningSentAt: null,
|
||||
},
|
||||
});
|
||||
await cancelScheduledMessagesAndScheduleEmails(teamId);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
creditBalance?.warningSentAt &&
|
||||
dayjs(creditBalance?.warningSentAt).isAfter(dayjs().startOf("month"))
|
||||
) {
|
||||
return; // team has already sent warning email this month
|
||||
}
|
||||
|
||||
// team balance below 20% of total monthly credits
|
||||
await sendCreditBalanceLowWarningEmails({
|
||||
balance: remainingCredits,
|
||||
team: {
|
||||
id: teamId,
|
||||
name: team.name,
|
||||
adminAndOwners: await Promise.all(
|
||||
team.members.map(async (member) => ({
|
||||
name: member.user.name ?? "",
|
||||
email: member.user.email,
|
||||
t: await getTranslation(member.user.locale ?? "en", "common"),
|
||||
}))
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
teamId,
|
||||
data: {
|
||||
warningSentAt: new Date(),
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await CreditsRepository.updateCreditBalance({
|
||||
teamId,
|
||||
data: {
|
||||
warningSentAt: null,
|
||||
limitReachedAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getMonthlyCredits(teamId: number) {
|
||||
const team = await TeamRepository.findTeamWithMembers(teamId);
|
||||
|
||||
if (!team) return 0;
|
||||
|
||||
let totalMonthlyCredits = 0;
|
||||
|
||||
const teamBillingService = new InternalTeamBilling(team);
|
||||
const subscriptionStatus = await teamBillingService.getSubscriptionStatus();
|
||||
|
||||
if (subscriptionStatus !== "active" && subscriptionStatus !== "past_due") {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const activeMembers = team.members.filter((member) => member.accepted).length;
|
||||
|
||||
const billingService = new StripeBillingService();
|
||||
|
||||
const teamMonthlyPrice = await billingService.getPrice(process.env.STRIPE_TEAM_MONTHLY_PRICE_ID || "");
|
||||
const pricePerSeat = teamMonthlyPrice.unit_amount ?? 0;
|
||||
totalMonthlyCredits = (activeMembers * pricePerSeat) / 2;
|
||||
|
||||
return totalMonthlyCredits;
|
||||
}
|
||||
|
||||
calculateCreditsFromPrice(price: number) {
|
||||
const twilioPrice = price;
|
||||
const priceWithMarkUp = twilioPrice * 1.8;
|
||||
const credits = Math.ceil(priceWithMarkUp * 100);
|
||||
return credits || null;
|
||||
}
|
||||
|
||||
async getAllCreditsForTeam(teamId: number) {
|
||||
const creditBalance = await CreditsRepository.findCreditBalanceWithExpenseLogs({ teamId });
|
||||
|
||||
const totalMonthlyCredits = await this.getMonthlyCredits(teamId);
|
||||
const totalMonthlyCreditsUsed =
|
||||
creditBalance?.expenseLogs.reduce((sum, log) => sum + (log?.credits ?? 0), 0) || 0;
|
||||
|
||||
return {
|
||||
totalMonthlyCredits,
|
||||
totalRemainingMonthlyCredits: Math.max(totalMonthlyCredits - totalMonthlyCreditsUsed, 0),
|
||||
additionalCredits: creditBalance?.additionalCredits ?? 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,29 @@ export class StripeBillingService implements BillingService {
|
||||
};
|
||||
}
|
||||
|
||||
async createOneTimeCheckout(args: {
|
||||
priceId: string;
|
||||
quantity: number;
|
||||
successUrl: string;
|
||||
cancelUrl: string;
|
||||
metadata?: Record<string, string>;
|
||||
}) {
|
||||
const { priceId, quantity, successUrl, cancelUrl, metadata } = args;
|
||||
|
||||
const session = await this.stripe.checkout.sessions.create({
|
||||
line_items: [{ price: priceId, quantity }],
|
||||
mode: "payment",
|
||||
success_url: successUrl,
|
||||
cancel_url: cancelUrl,
|
||||
metadata: metadata,
|
||||
});
|
||||
|
||||
return {
|
||||
checkoutUrl: session.url,
|
||||
sessionId: session.id,
|
||||
};
|
||||
}
|
||||
|
||||
async createSubscriptionCheckout(args: Parameters<BillingService["createSubscriptionCheckout"]>[0]) {
|
||||
const {
|
||||
customerId,
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
/* Schedule any workflow reminder that falls within 7 days for SMS */
|
||||
/* Schedule any workflow reminder that falls within the next 2 hours for SMS */
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { bulkShortenLinks } from "@calcom/ee/workflows/lib/reminders/utils";
|
||||
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
|
||||
import { isAttendeeAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import { scheduleSmsOrFallbackEmail } from "@calcom/features/ee/workflows/lib/reminders/messageDispatcher";
|
||||
import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@calcom/prisma/enums";
|
||||
@@ -14,7 +17,6 @@ import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
import { getSenderId } from "../lib/alphanumericSenderIdSupport";
|
||||
import type { PartialWorkflowReminder } from "../lib/getWorkflowReminders";
|
||||
import { select } from "../lib/getWorkflowReminders";
|
||||
import * as twilio from "../lib/reminders/providers/twilioProvider";
|
||||
import type { VariablesType } from "../lib/reminders/templates/customTemplate";
|
||||
import customTemplate from "../lib/reminders/templates/customTemplate";
|
||||
import smsReminderTemplate from "../lib/reminders/templates/smsReminderTemplate";
|
||||
@@ -52,7 +54,7 @@ export async function handler(req: NextRequest) {
|
||||
method: WorkflowMethods.SMS,
|
||||
scheduled: false,
|
||||
scheduledDate: {
|
||||
lte: dayjs().add(7, "day").toISOString(),
|
||||
lte: dayjs().add(2, "hour").toISOString(),
|
||||
},
|
||||
},
|
||||
select: {
|
||||
@@ -178,25 +180,45 @@ export async function handler(req: NextRequest) {
|
||||
message = await WorkflowOptOutService.addOptOutMessage(message, locale || "en");
|
||||
}
|
||||
|
||||
const scheduledSMS = await twilio.scheduleSMS(
|
||||
sendTo,
|
||||
message,
|
||||
reminder.scheduledDate,
|
||||
senderID,
|
||||
userId,
|
||||
teamId
|
||||
);
|
||||
const scheduledNotification = await scheduleSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: sendTo,
|
||||
body: message,
|
||||
scheduledDate: reminder.scheduledDate,
|
||||
sender: senderID,
|
||||
bookingUid: reminder.booking.uid,
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
fallbackData:
|
||||
reminder.workflowStep.action && isAttendeeAction(reminder.workflowStep.action)
|
||||
? {
|
||||
email: reminder.booking.attendees[0].email,
|
||||
t: await getTranslation(locale || "en", "common"),
|
||||
replyTo: reminder.booking?.user?.email ?? "",
|
||||
workflowStepId: reminder.workflowStep.id,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
if (scheduledSMS) {
|
||||
await prisma.workflowReminder.update({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
data: {
|
||||
scheduled: true,
|
||||
referenceId: scheduledSMS.sid,
|
||||
},
|
||||
});
|
||||
if (scheduledNotification) {
|
||||
if (scheduledNotification.sid) {
|
||||
await prisma.workflowReminder.update({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
data: {
|
||||
scheduled: true,
|
||||
referenceId: scheduledNotification.sid,
|
||||
},
|
||||
});
|
||||
} else if (scheduledNotification.emailReminderId) {
|
||||
await prisma.workflowReminder.delete({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await prisma.workflowReminder.update({
|
||||
where: {
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
/* Schedule any workflow reminder that falls within 7 days for WHATSAPP */
|
||||
/* Schedule any workflow reminder that falls within the next 2 hours for WHATSAPP */
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { WorkflowActions, WorkflowMethods } from "@calcom/prisma/enums";
|
||||
|
||||
import { getWhatsappTemplateFunction } from "../lib/actionHelperFunctions";
|
||||
import { getWhatsappTemplateFunction, isAttendeeAction } from "../lib/actionHelperFunctions";
|
||||
import type { PartialWorkflowReminder } from "../lib/getWorkflowReminders";
|
||||
import { select } from "../lib/getWorkflowReminders";
|
||||
import * as twilio from "../lib/reminders/providers/twilioProvider";
|
||||
import { scheduleSmsOrFallbackEmail } from "../lib/reminders/messageDispatcher";
|
||||
|
||||
export async function handler(req: NextRequest) {
|
||||
const apiKey = req.headers.get("authorization") || req.nextUrl.searchParams.get("apiKey");
|
||||
@@ -35,7 +36,7 @@ export async function handler(req: NextRequest) {
|
||||
method: WorkflowMethods.WHATSAPP,
|
||||
scheduled: false,
|
||||
scheduledDate: {
|
||||
lte: dayjs().add(7, "day").toISOString(),
|
||||
lte: dayjs().add(2, "hour").toISOString(),
|
||||
},
|
||||
},
|
||||
select,
|
||||
@@ -87,25 +88,51 @@ export async function handler(req: NextRequest) {
|
||||
);
|
||||
|
||||
if (message?.length && message?.length > 0 && sendTo) {
|
||||
const scheduledSMS = await twilio.scheduleSMS(
|
||||
sendTo,
|
||||
message,
|
||||
reminder.scheduledDate,
|
||||
"",
|
||||
userId,
|
||||
teamId,
|
||||
true
|
||||
);
|
||||
const scheduledNotification = await scheduleSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: sendTo,
|
||||
body: message,
|
||||
scheduledDate: reminder.scheduledDate,
|
||||
sender: "",
|
||||
bookingUid: reminder.booking.uid,
|
||||
userId,
|
||||
teamId,
|
||||
isWhatsapp: true,
|
||||
},
|
||||
fallbackData:
|
||||
reminder.workflowStep.action && isAttendeeAction(reminder.workflowStep.action)
|
||||
? {
|
||||
email: reminder.booking.attendees[0].email,
|
||||
t: await getTranslation(reminder.booking.attendees[0].locale || "en", "common"),
|
||||
replyTo: reminder.booking?.user?.email ?? "",
|
||||
workflowStepId: reminder.workflowStep.id,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
if (scheduledSMS) {
|
||||
await prisma.workflowReminder.update({
|
||||
if (scheduledNotification) {
|
||||
if (scheduledNotification.sid) {
|
||||
await prisma.workflowReminder.update({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
data: {
|
||||
scheduled: true,
|
||||
referenceId: scheduledNotification.sid,
|
||||
},
|
||||
});
|
||||
} else if (scheduledNotification.emailReminderId) {
|
||||
await prisma.workflowReminder.delete({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await prisma.workflowReminder.delete({
|
||||
where: {
|
||||
id: reminder.id,
|
||||
},
|
||||
data: {
|
||||
scheduled: true,
|
||||
referenceId: scheduledSMS.sid,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { TFunction } from "i18next";
|
||||
|
||||
import { sendOrScheduleWorkflowEmails } from "@calcom/features/ee/workflows/lib/reminders/providers/emailProvider";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { WorkflowMethods } from "@calcom/prisma/enums";
|
||||
|
||||
import * as twilio from "./providers/twilioProvider";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[reminderScheduler]"] });
|
||||
|
||||
export async function sendSmsOrFallbackEmail(props: {
|
||||
twilioData: {
|
||||
phoneNumber: string;
|
||||
body: string;
|
||||
sender: string;
|
||||
bookingUid?: string | null;
|
||||
userId?: number | null;
|
||||
teamId?: number | null;
|
||||
isWhatsapp?: boolean;
|
||||
};
|
||||
fallbackData?: {
|
||||
email: string;
|
||||
t: TFunction;
|
||||
replyTo: string;
|
||||
};
|
||||
}) {
|
||||
const { userId, teamId } = props.twilioData;
|
||||
const { CreditService } = await import("@calcom/features/ee/billing/credit-service");
|
||||
|
||||
const creditService = new CreditService();
|
||||
|
||||
const hasCredits = await creditService.hasAvailableCredits({ userId, teamId });
|
||||
|
||||
if (!hasCredits) {
|
||||
const { fallbackData, twilioData } = props;
|
||||
if (fallbackData) {
|
||||
await sendOrScheduleWorkflowEmails({
|
||||
to: [fallbackData.email],
|
||||
subject: fallbackData.t("notification_about_your_booking"),
|
||||
html: twilioData.body,
|
||||
replyTo: fallbackData.replyTo,
|
||||
});
|
||||
}
|
||||
|
||||
log.debug(
|
||||
`SMS not sent because ${teamId ? `Team id ${teamId} ` : `User id ${userId} `} has no available credits`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await twilio.sendSMS(props.twilioData);
|
||||
}
|
||||
|
||||
export async function scheduleSmsOrFallbackEmail(props: {
|
||||
twilioData: {
|
||||
phoneNumber: string;
|
||||
body: string;
|
||||
scheduledDate: Date;
|
||||
sender: string;
|
||||
bookingUid?: string | null;
|
||||
userId?: number | null;
|
||||
teamId?: number | null;
|
||||
isWhatsapp?: boolean;
|
||||
};
|
||||
fallbackData?: {
|
||||
email: string;
|
||||
t: TFunction;
|
||||
replyTo: string;
|
||||
workflowStepId?: number;
|
||||
};
|
||||
}) {
|
||||
const { userId, teamId } = props.twilioData;
|
||||
const { CreditService } = await import("@calcom/features/ee/billing/credit-service");
|
||||
const creditService = new CreditService();
|
||||
|
||||
const hasCredits = await creditService.hasAvailableCredits({ userId, teamId });
|
||||
|
||||
if (!hasCredits) {
|
||||
const { fallbackData, twilioData } = props;
|
||||
if (fallbackData) {
|
||||
const reminder = await prisma.workflowReminder.create({
|
||||
data: {
|
||||
bookingUid: twilioData.bookingUid,
|
||||
workflowStepId: fallbackData.workflowStepId,
|
||||
method: WorkflowMethods.EMAIL,
|
||||
scheduledDate: twilioData.scheduledDate,
|
||||
scheduled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await sendOrScheduleWorkflowEmails({
|
||||
to: [fallbackData.email],
|
||||
subject: fallbackData.t("notification_about_your_booking"),
|
||||
html: twilioData.body,
|
||||
replyTo: fallbackData.replyTo,
|
||||
sendAt: twilioData.scheduledDate,
|
||||
referenceUid: reminder.uuid || undefined,
|
||||
});
|
||||
return { emailReminderId: reminder.id, sid: null };
|
||||
}
|
||||
|
||||
log.debug(
|
||||
`SMS not sent because ${teamId ? `Team id ${teamId} ` : `User id ${userId} `} has no available credits`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const scheduledSMS = await twilio.scheduleSMS(props.twilioData);
|
||||
return scheduledSMS?.sid ? { emailReminderId: null, sid: scheduledSMS.sid } : null;
|
||||
}
|
||||
@@ -20,26 +20,35 @@ function createTwilioClient() {
|
||||
throw new Error("Twilio credentials are missing from the .env file");
|
||||
}
|
||||
|
||||
function getDefaultSender(whatsapp = false) {
|
||||
function getDefaultSender(isWhatsapp = false) {
|
||||
let defaultSender = process.env.TWILIO_PHONE_NUMBER;
|
||||
if (whatsapp) {
|
||||
if (isWhatsapp) {
|
||||
defaultSender = `whatsapp:+${process.env.TWILIO_WHATSAPP_PHONE_NUMBER}`;
|
||||
}
|
||||
return defaultSender || "";
|
||||
}
|
||||
|
||||
function getSMSNumber(phone: string, whatsapp = false) {
|
||||
return whatsapp ? `whatsapp:${phone}` : phone;
|
||||
function getSMSNumber(phone: string, isWhatsapp = false) {
|
||||
return isWhatsapp ? `whatsapp:${phone}` : phone;
|
||||
}
|
||||
|
||||
export const sendSMS = async (
|
||||
phoneNumber: string,
|
||||
body: string,
|
||||
sender: string,
|
||||
userId?: number | null,
|
||||
teamId?: number | null,
|
||||
whatsapp = false
|
||||
) => {
|
||||
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);
|
||||
@@ -51,8 +60,8 @@ export const sendSMS = async (
|
||||
|
||||
if (testMode) {
|
||||
setTestSMS({
|
||||
to: getSMSNumber(phoneNumber, whatsapp),
|
||||
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
|
||||
to: getSMSNumber(phoneNumber, isWhatsapp),
|
||||
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
||||
message: body,
|
||||
});
|
||||
console.log(
|
||||
@@ -74,22 +83,41 @@ export const sendSMS = async (
|
||||
const response = await twilio.messages.create({
|
||||
body: body,
|
||||
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
|
||||
to: getSMSNumber(phoneNumber, whatsapp),
|
||||
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
|
||||
to: getSMSNumber(phoneNumber, isWhatsapp),
|
||||
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
||||
statusCallback: getStatusCallbackUrl(userId, teamId, bookingUid),
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
export const scheduleSMS = async (
|
||||
phoneNumber: string,
|
||||
body: string,
|
||||
scheduledDate: Date,
|
||||
sender: string,
|
||||
userId?: number | null,
|
||||
teamId?: number | null,
|
||||
whatsapp = false
|
||||
) => {
|
||||
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) {
|
||||
@@ -99,8 +127,8 @@ export const scheduleSMS = async (
|
||||
|
||||
if (testMode) {
|
||||
setTestSMS({
|
||||
to: getSMSNumber(phoneNumber, whatsapp),
|
||||
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
|
||||
to: getSMSNumber(phoneNumber, isWhatsapp),
|
||||
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
||||
message: body,
|
||||
});
|
||||
console.log(
|
||||
@@ -117,14 +145,14 @@ export const scheduleSMS = async (
|
||||
rateLimitingType: "smsMonth",
|
||||
});
|
||||
}
|
||||
|
||||
const response = await twilio.messages.create({
|
||||
body: body,
|
||||
body,
|
||||
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
|
||||
to: getSMSNumber(phoneNumber, whatsapp),
|
||||
to: getSMSNumber(phoneNumber, isWhatsapp),
|
||||
scheduleType: "fixed",
|
||||
sendAt: scheduledDate,
|
||||
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
|
||||
from: isWhatsapp ? getDefaultSender(isWhatsapp) : sender || getDefaultSender(),
|
||||
statusCallback: getStatusCallbackUrl(userId, teamId, bookingUid),
|
||||
});
|
||||
|
||||
return response;
|
||||
@@ -158,6 +186,12 @@ export const verifyNumber = async (phoneNumber: string, code: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
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({
|
||||
@@ -199,6 +233,19 @@ async function isLockedForSMSSending(userId?: number | null, teamId?: number | n
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import prismaMock from "../../../../../../tests/libs/__mocks__/prismaMock";
|
||||
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
|
||||
import { WorkflowMethods } from "@calcom/prisma/enums";
|
||||
|
||||
import { sendOrScheduleWorkflowEmails } from "./providers/emailProvider";
|
||||
import * as twilioProvider from "./providers/twilioProvider";
|
||||
import { cancelScheduledMessagesAndScheduleEmails } from "./reminderScheduler";
|
||||
|
||||
vi.mock("@calcom/features/ee/workflows/lib/reminders/providers/twilioProvider", () => ({
|
||||
cancelSMS: vi.fn(),
|
||||
getMessageBody: vi.fn().mockResolvedValue("Test message body"),
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/features/ee/workflows/lib/reminders/providers/emailProvider", () => ({
|
||||
sendOrScheduleWorkflowEmails: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("reminderScheduler", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("cancelScheduledMessagesAndScheduleEmails", () => {
|
||||
it("should cancel SMS messages and schedule emails for team", async () => {
|
||||
prismaMock.membership.findMany.mockResolvedValue([]);
|
||||
|
||||
const mockScheduledMessages = [
|
||||
{
|
||||
id: 1,
|
||||
referenceId: "sms-123",
|
||||
workflowStep: {
|
||||
action: "SMS_ATTENDEE",
|
||||
},
|
||||
scheduledDate: new Date(),
|
||||
uuid: "uuid-123",
|
||||
booking: {
|
||||
attendees: [
|
||||
{
|
||||
email: "[email protected]",
|
||||
locale: "en",
|
||||
},
|
||||
],
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
prismaMock.workflowReminder.findMany.mockResolvedValue(mockScheduledMessages);
|
||||
|
||||
prismaMock.workflowReminder.updateMany.mockResolvedValue({ count: 1 });
|
||||
|
||||
await cancelScheduledMessagesAndScheduleEmails(1);
|
||||
|
||||
expect(twilioProvider.cancelSMS).toHaveBeenCalledWith("sms-123");
|
||||
|
||||
expect(sendOrScheduleWorkflowEmails).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
to: ["[email protected]"],
|
||||
replyTo: "[email protected]",
|
||||
referenceUid: "uuid-123",
|
||||
})
|
||||
);
|
||||
|
||||
expect(prismaMock.workflowReminder.updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: {
|
||||
in: [1],
|
||||
},
|
||||
},
|
||||
data: {
|
||||
method: WorkflowMethods.EMAIL,
|
||||
referenceId: null,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,24 @@
|
||||
import {
|
||||
isAttendeeAction,
|
||||
isSMSAction,
|
||||
isSMSOrWhatsappAction,
|
||||
isWhatsappAction,
|
||||
} from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import { sendOrScheduleWorkflowEmails } from "@calcom/features/ee/workflows/lib/reminders/providers/emailProvider";
|
||||
import * as twilio from "@calcom/features/ee/workflows/lib/reminders/providers/twilioProvider";
|
||||
import type { Workflow, WorkflowStep } from "@calcom/features/ee/workflows/lib/types";
|
||||
import { checkSMSRateLimit } from "@calcom/lib/checkRateLimitAndThrowError";
|
||||
import { SENDER_NAME } from "@calcom/lib/constants";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { SchedulingType, WorkflowActions, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
||||
import { SchedulingType } from "@calcom/prisma/enums";
|
||||
import { WorkflowActions, WorkflowMethods, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||
|
||||
import { scheduleEmailReminder } from "./emailReminderManager";
|
||||
import type { ScheduleTextReminderAction } from "./smsReminderManager";
|
||||
import { scheduleSMSReminder } from "./smsReminderManager";
|
||||
import type { ScheduleTextReminderAction } from "./smsReminderManager";
|
||||
import { scheduleWhatsappReminder } from "./whatsappReminderManager";
|
||||
|
||||
export type ExtendedCalendarEvent = Omit<CalendarEvent, "bookerUrl"> & {
|
||||
@@ -251,9 +256,123 @@ const _sendCancelledReminders = async (args: SendCancelledRemindersArgs) => {
|
||||
}
|
||||
};
|
||||
|
||||
const _cancelScheduledMessagesAndScheduleEmails = async (teamId: number) => {
|
||||
const teamMembers = await prisma.membership.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
const { CreditService } = await import("@calcom/features/ee/billing/credit-service");
|
||||
|
||||
const creditService = new CreditService();
|
||||
|
||||
const membersWithNoCredits = (
|
||||
await Promise.all(
|
||||
teamMembers.map(async (member) => {
|
||||
const hasCredits = await creditService.hasAvailableCredits({ userId: member.userId });
|
||||
return { member, hasCredits };
|
||||
})
|
||||
)
|
||||
)
|
||||
.filter(({ hasCredits }) => !hasCredits)
|
||||
.map(({ member }) => member);
|
||||
|
||||
const scheduledMessages = await prisma.workflowReminder.findMany({
|
||||
where: {
|
||||
workflowStep: {
|
||||
workflow: {
|
||||
OR: [
|
||||
{
|
||||
userId: {
|
||||
in: membersWithNoCredits.map((member) => member.userId),
|
||||
},
|
||||
},
|
||||
{
|
||||
teamId,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
scheduled: true,
|
||||
OR: [{ cancelled: false }, { cancelled: null }],
|
||||
referenceId: {
|
||||
not: null,
|
||||
},
|
||||
method: {
|
||||
in: [WorkflowMethods.SMS, WorkflowMethods.WHATSAPP],
|
||||
},
|
||||
},
|
||||
select: {
|
||||
referenceId: true,
|
||||
workflowStep: {
|
||||
select: {
|
||||
action: true,
|
||||
},
|
||||
},
|
||||
scheduledDate: true,
|
||||
uuid: true,
|
||||
id: true,
|
||||
booking: {
|
||||
select: {
|
||||
attendees: {
|
||||
select: {
|
||||
email: true,
|
||||
locale: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.allSettled(scheduledMessages.map((msg) => twilio.cancelSMS(msg.referenceId ?? "")));
|
||||
|
||||
await Promise.allSettled(
|
||||
scheduledMessages.map(async (msg) => {
|
||||
if (msg.workflowStep?.action && isAttendeeAction(msg.workflowStep.action)) {
|
||||
const messageBody = await twilio.getMessageBody(msg.referenceId ?? "");
|
||||
const sendTo = msg.booking?.attendees?.[0];
|
||||
|
||||
if (sendTo) {
|
||||
const t = await getTranslation(sendTo.locale ?? "en", "common");
|
||||
await sendOrScheduleWorkflowEmails({
|
||||
to: [sendTo.email],
|
||||
subject: t("notification_about_your_booking"),
|
||||
html: messageBody,
|
||||
replyTo: msg.booking?.user?.email ?? "",
|
||||
sendAt: msg.scheduledDate,
|
||||
referenceUid: msg.uuid || undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
await prisma.workflowReminder.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: scheduledMessages.map((msg) => msg.id),
|
||||
},
|
||||
},
|
||||
data: {
|
||||
method: WorkflowMethods.EMAIL,
|
||||
referenceId: null,
|
||||
},
|
||||
});
|
||||
};
|
||||
// Export functions wrapped with withReporting
|
||||
export const scheduleWorkflowReminders = withReporting(
|
||||
_scheduleWorkflowReminders,
|
||||
"scheduleWorkflowReminders"
|
||||
);
|
||||
export const sendCancelledReminders = withReporting(_sendCancelledReminders, "sendCancelledReminders");
|
||||
export const cancelScheduledMessagesAndScheduleEmails = withReporting(
|
||||
_cancelScheduledMessagesAndScheduleEmails,
|
||||
"cancelScheduledMessagesAndScheduleEmails"
|
||||
);
|
||||
@@ -3,6 +3,7 @@ import { bulkShortenLinks } from "@calcom/ee/workflows/lib/reminders/utils";
|
||||
import { SENDER_ID, WEBSITE_URL } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import type { TimeFormat } from "@calcom/lib/timeFormat";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import prisma from "@calcom/prisma";
|
||||
@@ -12,10 +13,12 @@ import { WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
||||
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { CalEventResponses, RecurringEvent } from "@calcom/types/Calendar";
|
||||
|
||||
import { isAttendeeAction } from "../actionHelperFunctions";
|
||||
import { getSenderId } from "../alphanumericSenderIdSupport";
|
||||
import { WorkflowOptOutContactRepository } from "../repository/workflowOptOutContact";
|
||||
import { WorkflowOptOutService } from "../service/workflowOptOutService";
|
||||
import type { ScheduleReminderArgs } from "./emailReminderManager";
|
||||
import { scheduleSmsOrFallbackEmail, sendSmsOrFallbackEmail } from "./messageDispatcher";
|
||||
import * as twilio from "./providers/twilioProvider";
|
||||
import type { VariablesType } from "./templates/customTemplate";
|
||||
import customTemplate from "./templates/customTemplate";
|
||||
@@ -230,7 +233,23 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
triggerEvent === WorkflowTriggerEvents.RESCHEDULE_EVENT
|
||||
) {
|
||||
try {
|
||||
await twilio.sendSMS(reminderPhone, smsMessage, senderID, userId, teamId);
|
||||
await sendSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: reminderPhone,
|
||||
body: smsMessage,
|
||||
sender: senderID,
|
||||
bookingUid: evt.uid,
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
fallbackData: isAttendeeAction(action)
|
||||
? {
|
||||
email: evt.attendees[0].email,
|
||||
t: await getTranslation(evt.attendees[0].language.locale, "common"),
|
||||
replyTo: evt.organizer.email,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
log.error(`Error sending SMS with error ${error}`);
|
||||
}
|
||||
@@ -239,22 +258,33 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) &&
|
||||
scheduledDate
|
||||
) {
|
||||
// Can only schedule at least 60 minutes in advance and at most 7 days in advance
|
||||
// schedule at least 15 minutes in advance and at most 2 hours in advance
|
||||
if (
|
||||
currentDate.isBefore(scheduledDate.subtract(1, "hour")) &&
|
||||
!scheduledDate.isAfter(currentDate.add(7, "day"))
|
||||
currentDate.isBefore(scheduledDate.subtract(15, "minute")) &&
|
||||
!scheduledDate.isAfter(currentDate.add(2, "hour"))
|
||||
) {
|
||||
try {
|
||||
const scheduledSMS = await twilio.scheduleSMS(
|
||||
reminderPhone,
|
||||
smsMessage,
|
||||
scheduledDate.toDate(),
|
||||
senderID,
|
||||
userId,
|
||||
teamId
|
||||
);
|
||||
const scheduledNotification = await scheduleSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: reminderPhone,
|
||||
body: smsMessage,
|
||||
scheduledDate: scheduledDate.toDate(),
|
||||
sender: senderID,
|
||||
bookingUid: evt.uid,
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
fallbackData: isAttendeeAction(action)
|
||||
? {
|
||||
email: evt.attendees[0].email,
|
||||
t: await getTranslation(evt.attendees[0].language.locale, "common"),
|
||||
replyTo: evt.organizer.email,
|
||||
workflowStepId,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
if (scheduledSMS) {
|
||||
if (scheduledNotification?.sid) {
|
||||
await prisma.workflowReminder.create({
|
||||
data: {
|
||||
bookingUid: uid,
|
||||
@@ -262,7 +292,7 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
method: WorkflowMethods.SMS,
|
||||
scheduledDate: scheduledDate.toDate(),
|
||||
scheduled: true,
|
||||
referenceId: scheduledSMS.sid,
|
||||
referenceId: scheduledNotification.sid,
|
||||
seatReferenceId: seatReferenceUid,
|
||||
},
|
||||
});
|
||||
@@ -270,8 +300,8 @@ export const scheduleSMSReminder = async (args: ScheduleTextReminderArgs) => {
|
||||
} catch (error) {
|
||||
log.error(`Error scheduling SMS with error ${error}`);
|
||||
}
|
||||
} else if (scheduledDate.isAfter(currentDate.add(7, "day"))) {
|
||||
// Write to DB and send to CRON if scheduled reminder date is past 7 days
|
||||
} else if (scheduledDate.isAfter(currentDate.add(2, "hour"))) {
|
||||
// Write to DB and send to CRON if scheduled reminder date is past 2 hours from now
|
||||
await prisma.workflowReminder.create({
|
||||
data: {
|
||||
bookingUid: uid,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import prisma from "@calcom/prisma";
|
||||
import {
|
||||
WorkflowTriggerEvents,
|
||||
@@ -8,7 +9,8 @@ import {
|
||||
WorkflowMethods,
|
||||
} from "@calcom/prisma/enums";
|
||||
|
||||
import * as twilio from "./providers/twilioProvider";
|
||||
import { isAttendeeAction } from "../actionHelperFunctions";
|
||||
import { scheduleSmsOrFallbackEmail, sendSmsOrFallbackEmail } from "./messageDispatcher";
|
||||
import type { ScheduleTextReminderArgs, timeUnitLowerCase } from "./smsReminderManager";
|
||||
import { deleteScheduledSMSReminder } from "./smsReminderManager";
|
||||
import {
|
||||
@@ -159,7 +161,24 @@ export const scheduleWhatsappReminder = async (args: ScheduleTextReminderArgs) =
|
||||
triggerEvent === WorkflowTriggerEvents.RESCHEDULE_EVENT
|
||||
) {
|
||||
try {
|
||||
await twilio.sendSMS(reminderPhone, textMessage, "", userId, teamId, true);
|
||||
await sendSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: reminderPhone,
|
||||
body: textMessage,
|
||||
sender: "",
|
||||
bookingUid: evt.uid,
|
||||
userId,
|
||||
teamId,
|
||||
isWhatsapp: true,
|
||||
},
|
||||
fallbackData: isAttendeeAction(action)
|
||||
? {
|
||||
email: evt.attendees[0].email,
|
||||
t: await getTranslation(evt.attendees[0].language.locale ?? "en", "common"),
|
||||
replyTo: evt.organizer.email,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Error sending WHATSAPP with error ${error}`);
|
||||
}
|
||||
@@ -168,23 +187,34 @@ export const scheduleWhatsappReminder = async (args: ScheduleTextReminderArgs) =
|
||||
triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) &&
|
||||
scheduledDate
|
||||
) {
|
||||
// Can only schedule at least 60 minutes in advance and at most 7 days in advance
|
||||
// schedule at least 15 minutes in advance and at most 2 hours in advance
|
||||
if (
|
||||
currentDate.isBefore(scheduledDate.subtract(1, "hour")) &&
|
||||
!scheduledDate.isAfter(currentDate.add(7, "day"))
|
||||
currentDate.isBefore(scheduledDate.subtract(15, "minute")) &&
|
||||
!scheduledDate.isAfter(currentDate.add(2, "hour"))
|
||||
) {
|
||||
try {
|
||||
const scheduledWHATSAPP = await twilio.scheduleSMS(
|
||||
reminderPhone,
|
||||
textMessage,
|
||||
scheduledDate.toDate(),
|
||||
"",
|
||||
userId,
|
||||
teamId,
|
||||
true
|
||||
);
|
||||
const scheduledNotification = await scheduleSmsOrFallbackEmail({
|
||||
twilioData: {
|
||||
phoneNumber: reminderPhone,
|
||||
body: textMessage,
|
||||
scheduledDate: scheduledDate.toDate(),
|
||||
sender: "",
|
||||
bookingUid: evt.uid ?? "",
|
||||
userId,
|
||||
teamId,
|
||||
isWhatsapp: true,
|
||||
},
|
||||
fallbackData: isAttendeeAction(action)
|
||||
? {
|
||||
email: evt.attendees[0].email,
|
||||
t: await getTranslation(evt.attendees[0].language.locale ?? "en", "common"),
|
||||
replyTo: evt.organizer.email,
|
||||
workflowStepId,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
if (scheduledWHATSAPP) {
|
||||
if (scheduledNotification?.sid) {
|
||||
await prisma.workflowReminder.create({
|
||||
data: {
|
||||
bookingUid: uid,
|
||||
@@ -192,7 +222,7 @@ export const scheduleWhatsappReminder = async (args: ScheduleTextReminderArgs) =
|
||||
method: WorkflowMethods.WHATSAPP,
|
||||
scheduledDate: scheduledDate.toDate(),
|
||||
scheduled: true,
|
||||
referenceId: scheduledWHATSAPP.sid,
|
||||
referenceId: scheduledNotification.sid,
|
||||
seatReferenceId: seatReferenceUid,
|
||||
},
|
||||
});
|
||||
@@ -200,8 +230,8 @@ export const scheduleWhatsappReminder = async (args: ScheduleTextReminderArgs) =
|
||||
} catch (error) {
|
||||
console.log(`Error scheduling WHATSAPP with error ${error}`);
|
||||
}
|
||||
} else if (scheduledDate.isAfter(currentDate.add(7, "day"))) {
|
||||
// Write to DB and send to CRON if scheduled reminder date is past 7 days
|
||||
} else if (scheduledDate.isAfter(currentDate.add(2, "hour"))) {
|
||||
// Write to DB and send to CRON if scheduled reminder date is past 2 hours from now
|
||||
await prisma.workflowReminder.create({
|
||||
data: {
|
||||
bookingUid: uid,
|
||||
|
||||
@@ -89,15 +89,15 @@ const mockEventTypes = [
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
//2024-05-20T11:59:59Z
|
||||
const mockBookings = [
|
||||
{
|
||||
uid: "jK7Rf8iYsOpmQUw9hB1vZxP",
|
||||
eventTypeId: 1,
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-05-20T14:00:00.000Z`,
|
||||
endTime: `2024-05-20T14:30:00.000Z`,
|
||||
startTime: `2024-05-20T09:00:00.000Z`,
|
||||
endTime: `2024-05-20T09:15:00.000Z`,
|
||||
attendees: [{ email: "[email protected]", locale: "en" }],
|
||||
},
|
||||
{
|
||||
@@ -105,8 +105,8 @@ const mockBookings = [
|
||||
eventTypeId: 1,
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-05-20T14:30:00.000Z`,
|
||||
endTime: `2024-05-20T15:00:00.000Z`,
|
||||
startTime: `2024-05-20T09:15:00.000Z`,
|
||||
endTime: `2024-05-20T09:30:00.000Z`,
|
||||
attendees: [{ email: "[email protected]", locale: "en" }],
|
||||
},
|
||||
{
|
||||
@@ -241,6 +241,14 @@ async function createWorkflowRemindersAndTasksForWorkflow(workflowName: string)
|
||||
return workflow;
|
||||
}
|
||||
|
||||
vi.mock("@calcom/lib/constants", async () => {
|
||||
const actual = (await vi.importActual("@calcom/lib/constants")) as typeof import("@calcom/lib/constants");
|
||||
return {
|
||||
...actual,
|
||||
IS_SMS_CREDITS_ENABLED: false,
|
||||
};
|
||||
});
|
||||
|
||||
describe("deleteRemindersOfActiveOnIds", () => {
|
||||
test("should delete all reminders and tasks from removed event types", async ({}) => {
|
||||
const organizer = getOrganizer({
|
||||
@@ -510,8 +518,8 @@ describe("scheduleBookingReminders", () => {
|
||||
);
|
||||
|
||||
const expectedScheduledDates = [
|
||||
new Date("2024-05-20T13:00:00.000"),
|
||||
new Date("2024-05-20T13:30:00.000Z"),
|
||||
new Date("2024-05-20T08:00:00.000"),
|
||||
new Date("2024-05-20T08:15:00.000Z"),
|
||||
new Date("2024-06-01T03:30:00.000Z"),
|
||||
new Date("2024-06-02T03:30:00.000Z"),
|
||||
];
|
||||
@@ -596,8 +604,8 @@ describe("scheduleBookingReminders", () => {
|
||||
);
|
||||
|
||||
const expectedScheduledDates = [
|
||||
new Date("2024-05-20T15:30:00.000"),
|
||||
new Date("2024-05-20T16:00:00.000Z"),
|
||||
new Date("2024-05-20T10:15:00.000"),
|
||||
new Date("2024-05-20T10:30:00.000Z"),
|
||||
new Date("2024-06-01T06:00:00.000Z"),
|
||||
new Date("2024-06-02T06:00:00.000Z"),
|
||||
];
|
||||
@@ -702,13 +710,13 @@ describe("scheduleBookingReminders", () => {
|
||||
expectSMSWorkflowToBeTriggered({
|
||||
sms,
|
||||
toNumber: "000",
|
||||
includedString: "2024 May 20 at 7:30pm Asia/Kolkata",
|
||||
includedString: "2024 May 20 at 2:30pm Asia/Kolkata",
|
||||
});
|
||||
|
||||
expectSMSWorkflowToBeTriggered({
|
||||
sms,
|
||||
toNumber: "000",
|
||||
includedString: "2024 May 20 at 8:00pm Asia/Kolkata",
|
||||
includedString: "2024 May 20 at 2:45pm Asia/Kolkata",
|
||||
});
|
||||
|
||||
// sms are too far in future
|
||||
@@ -736,8 +744,8 @@ describe("scheduleBookingReminders", () => {
|
||||
);
|
||||
|
||||
const expectedScheduledDates = [
|
||||
new Date("2024-05-20T17:30:00.000"),
|
||||
new Date("2024-05-20T18:00:00.000Z"),
|
||||
new Date("2024-05-20T12:15:00.000"),
|
||||
new Date("2024-05-20T12:30:00.000Z"),
|
||||
new Date("2024-06-01T08:00:00.000Z"),
|
||||
new Date("2024-06-02T08:00:00.000Z"),
|
||||
];
|
||||
|
||||
Reference in new issue
Block a user