* feat: Cal.ai Self Serve #2 * chore: fix import and remove logs * fix: update checkout session * fix: type errors and test * fix: imports * fix: type err * fix: type error * fix: tests * chore: save progress * fix: workflow flow * fix: workflow update bug * tests: add unit tests for retell ai webhoo * fix: status code * fix: test and delete bug * fix: add dynamic variables * fix: type err * chore: update unit test * fix: type error * chore: update default prompt * fix: type errors * fix: workflow permissions * fix: workflow page * fix: translations * feat: add call duration * chore: add booking uid * fix: button positioning * chore: update tests * chore: improvements * chore: some more improvements * refactor: improvements * refactor: code feedback * refactor: improvements * feat: enable credits for orgs (#23077) * Show credits UI for orgs * fix stripe callback url when buying credits * give orgs 20% credits * add test for calulating credits --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> * fix: types * fix: types * chore: error * fix: type error * fix: type error * chore: mock env * feat: add idempotency key to prevent double charging * chore: add userId and teamId * fix: skip inbound calls * chore: update tests * feat: add feature flag for voice agent * feat: finish test call and other improvements * chore: add alert * chore: update .env.example * chore: improvements * fix: update tests * refactor: remove un necessary * feat: add setup badge * chore: improvements * fix: use referene id * chore: improvements * fix: type error * fix: type * refactor: change pricing logic * refactor: update tests * fix: conflicts * fix: billing link for orgs * fix: types * refactor: move feature flag up * fix: alert and test call credit check * fix: update unit tests * fix: feedback * refactor: improvements * refactor: move handlers to separate files * fix: types * fix: missing import * fix: type * refactor: change general tools functions handling * refactor: use repository * refactor: improvements * fix: types * fix: type errorr * fix: auth check * feat: add creditFor * fix: update defualt prompt * fix: throw error on frontend * fix: update unit tests * fix: use deleteAllWorkflowReminders * refactor: add connect phone number * refactor: improvements * chore: translation * chore: update message * chore: translation * design improvements buy number dialog * add translation for error message * use translation key in error message * refactor: improve connect phone number tab * feat: support un saved workflow to tests * chore: remove un used * fix: remove un used * fix: remove un used * refactor: similify billing --------- Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
188 lines
6.2 KiB
TypeScript
188 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 agent = await PrismaAgentRepository.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 newNumber = await PrismaPhoneNumberRepository.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 PrismaAgentRepository.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;
|