Files
calendar/packages/features/ee/billing/api/webhook/_checkout.session.completed.ts
T
aa48e72c12 feat: inbound calls in cal ai (#23890)
* 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>
2025-10-09 11:40:37 +01:00

190 lines
6.2 KiB
TypeScript

import { createDefaultAIPhoneServiceProvider } from "@calcom/features/calAIPhone";
import stripe from "@calcom/features/ee/payments/server/stripe";
import { PrismaAgentRepository } from "@calcom/lib/server/repository/PrismaAgentRepository";
import { PrismaPhoneNumberRepository } from "@calcom/lib/server/repository/PrismaPhoneNumberRepository";
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
import prisma from "@calcom/prisma";
import { PhoneNumberSubscriptionStatus } from "@calcom/prisma/enums";
import { CHECKOUT_SESSION_TYPES } from "../../constants";
import type { SWHMap } from "./__handler";
import { HttpCode } from "./__handler";
const handler = async (data: SWHMap["checkout.session.completed"]["data"]) => {
const session = data.object;
if (session.metadata?.type === CHECKOUT_SESSION_TYPES.PHONE_NUMBER_SUBSCRIPTION) {
return await handleCalAIPhoneNumberSubscription(session);
}
// Handle credit purchases (existing logic)
if (!session.amount_total) {
throw new HttpCode(400, "Missing required payment details");
}
const teamId = session.metadata?.teamId ? Number(session.metadata.teamId) : undefined;
const userId = session.metadata?.userId ? Number(session.metadata.userId) : undefined;
if (!teamId && !userId) {
throw new HttpCode(400, "Team id and user id are missing, but at least one is 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({ userId, teamId, nrOfCredits });
return { success: true };
};
async function saveToCreditBalance({
userId,
teamId,
nrOfCredits,
}: {
userId?: number;
teamId?: number;
nrOfCredits: number;
}) {
const creditBalance = await CreditsRepository.findCreditBalance({ teamId, userId });
let creditBalanceId = creditBalance?.id;
if (creditBalance) {
await CreditsRepository.updateCreditBalance({
id: creditBalance.id,
data: { additionalCredits: { increment: nrOfCredits }, limitReachedAt: null, warningSentAt: null },
});
} else {
const newCreditBalance = await CreditsRepository.createCreditBalance({
teamId: teamId,
userId: !teamId ? userId : undefined,
additionalCredits: nrOfCredits,
});
creditBalanceId = newCreditBalance.id;
}
if (creditBalanceId) {
await CreditsRepository.createCreditPurchaseLog({
credits: nrOfCredits,
creditBalanceId,
});
}
}
async function handleCalAIPhoneNumberSubscription(
session: SWHMap["checkout.session.completed"]["data"]["object"]
) {
const userId = session.metadata?.userId ? parseInt(session.metadata.userId, 10) : null;
const teamId = session.metadata?.teamId ? parseInt(session.metadata.teamId, 10) : null;
const agentId = session.metadata?.agentId || null;
if (!userId || !session.subscription) {
console.error("Missing required data for phone number subscription", {
userId,
hasSubscription: !!session.subscription,
});
throw new HttpCode(400, "Missing required data for phone number subscription");
}
if (!agentId || agentId?.trim() === "") {
console.error("Missing agentId for phone number subscription", {
userId,
teamId,
});
throw new HttpCode(400, "Missing agentId for phone number subscription");
}
const agentRepo = new PrismaAgentRepository(prisma);
const agent = await agentRepo.findByIdWithUserAccess({
agentId,
userId,
teamId: teamId ?? undefined,
});
if (!agent) {
console.error("Agent not found or user does not have access", { agentId, userId });
throw new HttpCode(404, "Agent not found or user does not have access to it");
}
const aiService = createDefaultAIPhoneServiceProvider();
const calAIPhoneNumber = await aiService.createPhoneNumber({
nickname: `userId:${userId}${teamId ? `-teamId:${teamId}` : ""}-${Date.now()}`,
});
if (!calAIPhoneNumber?.phone_number) {
console.error("Failed to create phone number - invalid response from Retell");
throw new HttpCode(500, "Failed to create phone number - invalid response");
}
const subscriptionId =
typeof session.subscription === "string" ? session.subscription : session.subscription?.id;
if (!subscriptionId) {
console.error("Invalid subscription data", { subscription: session.subscription });
throw new HttpCode(400, "Invalid subscription data");
}
const phoneNumberRepo = new PrismaPhoneNumberRepository(prisma);
const newNumber = await phoneNumberRepo.createPhoneNumber({
userId,
teamId: teamId ?? undefined,
phoneNumber: calAIPhoneNumber.phone_number,
provider: calAIPhoneNumber.provider,
stripeCustomerId: session.customer as string,
stripeSubscriptionId: subscriptionId,
subscriptionStatus: PhoneNumberSubscriptionStatus.ACTIVE,
providerPhoneNumberId: calAIPhoneNumber.phone_number,
});
try {
console.log("Attempting to link agent to phone number:", { agentId, phoneNumberId: newNumber.id });
const agent = await agentRepo.findByIdWithUserAccess({
agentId,
userId,
});
if (!agent) {
console.error("Agent not found or user does not have access", { agentId, userId });
throw new HttpCode(404, "Agent not found or user does not have access to it");
}
console.log("Found agent:", { agentId: agent.id, providerAgentId: agent.providerAgentId });
// Assign agent to the new number via Retell API
await aiService.updatePhoneNumber(calAIPhoneNumber.phone_number, {
outbound_agent_id: agent.providerAgentId,
});
// Link the new number to the agent in our database
await prisma.calAiPhoneNumber.update({
where: { id: newNumber.id },
data: {
outboundAgent: {
connect: { id: agentId },
},
},
});
console.log("Phone number successfully linked to agent");
} catch (error) {
console.error("Agent linking error details:", {
error,
agentId,
phoneNumber: calAIPhoneNumber.phone_number,
userId,
});
}
return { success: true, phoneNumber: newNumber.phoneNumber };
}
export default handler;