* feat: lang support * fix: type errors * feat: select voice agent * refactor: address feedback * refactor: address feedback * refactor: missing import * fix: types * feat: add inbound calls * chore: formatting * chore * feat: finish inbound call * chore: formatting * fix: update bug * fix: types * refactor: Agent Configuration Sheet (#23930) * refactor: agent configuration sheet * chore: use default phone numbre * refactor: improvements * refactor: improvements * fix: types * fix: feedback * chore: * fix: feedback * fix: prompt * fix: review * fix: review * refactor: class * refactor: class * refactor: rename * Update apps/web/public/static/locales/en/common.json * Update apps/web/public/static/locales/en/common.json * chore: update set value * fix: remove index * fix: type error * fix: update tetss * fix: use logger * refactor: don't use static * fix: type * fix: schema * refactor: --------- Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { PrismaPhoneNumberRepository } from "@calcom/lib/server/repository/PrismaPhoneNumberRepository";
|
|
import prisma from "@calcom/prisma";
|
|
import { PhoneNumberSubscriptionStatus } from "@calcom/prisma/enums";
|
|
|
|
import type { SWHMap } from "./__handler";
|
|
import { HttpCode } from "./__handler";
|
|
|
|
type Data = SWHMap["customer.subscription.updated"]["data"];
|
|
|
|
const handler = async (data: Data) => {
|
|
const subscription = data.object;
|
|
|
|
if (!subscription.id) {
|
|
throw new HttpCode(400, "Subscription ID not found");
|
|
}
|
|
|
|
const phoneNumberRepo = new PrismaPhoneNumberRepository(prisma);
|
|
const phoneNumber = await phoneNumberRepo.findByStripeSubscriptionId({
|
|
stripeSubscriptionId: subscription.id,
|
|
});
|
|
|
|
if (!phoneNumber) {
|
|
throw new HttpCode(202, "Phone number not found");
|
|
}
|
|
|
|
return await handleCalAIPhoneNumberSubscriptionUpdate(subscription, phoneNumber);
|
|
};
|
|
|
|
type Subscription = Data["object"];
|
|
|
|
async function handleCalAIPhoneNumberSubscriptionUpdate(
|
|
subscription: Subscription,
|
|
phoneNumber: { id: number; phoneNumber: string }
|
|
) {
|
|
// Map Stripe subscription status to our enum
|
|
const statusMap: Record<string, PhoneNumberSubscriptionStatus> = {
|
|
active: PhoneNumberSubscriptionStatus.ACTIVE,
|
|
past_due: PhoneNumberSubscriptionStatus.PAST_DUE,
|
|
canceled: PhoneNumberSubscriptionStatus.CANCELLED,
|
|
incomplete: PhoneNumberSubscriptionStatus.INCOMPLETE,
|
|
incomplete_expired: PhoneNumberSubscriptionStatus.INCOMPLETE_EXPIRED,
|
|
trialing: PhoneNumberSubscriptionStatus.TRIALING,
|
|
unpaid: PhoneNumberSubscriptionStatus.UNPAID,
|
|
paused: PhoneNumberSubscriptionStatus.CANCELLED,
|
|
};
|
|
|
|
const subscriptionStatus = statusMap[subscription.status] || PhoneNumberSubscriptionStatus.UNPAID;
|
|
|
|
await prisma.calAiPhoneNumber.update({
|
|
where: {
|
|
id: phoneNumber.id,
|
|
},
|
|
data: {
|
|
subscriptionStatus,
|
|
},
|
|
});
|
|
|
|
return { success: true, subscriptionId: subscription.id, status: subscriptionStatus };
|
|
}
|
|
|
|
export default handler;
|