perf: move stripe to billing service out of app store (#20376)

* add more methods to billingService

* update profile handler

* stripe customer handler

* verify email

* move imports + move generic methods to billing service

* move to strip ebilling service class

* push changes to mocks

* fix enum

* fix typo

* correctly update customer metadata

* fix userId type to number

* fix types on billing services

* fix type errors due to return methods differing

* fix return types
This commit is contained in:
sean-brydon authored and GitHub committed 2025-03-31 09:20:27 +00:00
1 parent f15d347804
commit 5cd2ba1362
17 files changed
+116 -58

No files matched your search

@@ -1,4 +1,4 @@
import stripe from "@calcom/app-store/stripepayment/lib/server";
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billling-service";
import { prisma } from "@calcom/prisma";
import { userMetadata } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
@@ -16,11 +16,14 @@ export const stripeCustomerHandler = async ({ ctx }: StripeCustomerOptions) => {
user: { id: userId },
} = ctx;
const billingService = new StripeBillingService();
const user = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
email: true,
metadata: true,
},
});
@@ -33,7 +36,8 @@ export const stripeCustomerHandler = async ({ ctx }: StripeCustomerOptions) => {
let stripeCustomerId = metadata?.stripeCustomerId;
if (!stripeCustomerId) {
// Create stripe customer
const customer = await stripe.customers.create({
const customer = await billingService.createCustomer({
email: user.email,
metadata: {
userId: userId.toString(),
},
@@ -45,15 +49,15 @@ export const stripeCustomerHandler = async ({ ctx }: StripeCustomerOptions) => {
data: {
metadata: {
...metadata,
stripeCustomerId: customer.id,
stripeCustomerId: customer.stripeCustomerId,
},
},
});
stripeCustomerId = customer.id;
stripeCustomerId = customer.stripeCustomerId;
}
// Fetch stripe customer
const customer = await stripe.customers.retrieve(stripeCustomerId);
const customer = await billingService.getCustomer(stripeCustomerId);
if (customer.deleted) {
throw new TRPCError({ code: "BAD_REQUEST", message: "No stripe customer found" });
}
@@ -3,9 +3,9 @@ import { Prisma } from "@prisma/client";
import { keyBy } from "lodash";
import type { GetServerSidePropsContext, NextApiResponse } from "next";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import { getPremiumMonthlyPlanPriceId } from "@calcom/app-store/stripepayment/lib/utils";
import { sendChangeOfEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billling-service";
import { getFeatureFlag } from "@calcom/features/flags/server/utils";
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
import { HttpError } from "@calcom/lib/http-error";
@@ -37,6 +37,7 @@ type UpdateProfileOptions = {
export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions) => {
const { user } = ctx;
const billingService = new StripeBillingService();
const userMetadata = handleUserMetadata({ ctx, input });
const locale = input.locale || user.locale;
const emailVerification = await getFeatureFlag(prisma, "email-verification");
@@ -84,9 +85,9 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
throw new TRPCError({ code: "BAD_REQUEST", message: "User is not premium" });
}
const stripeSubscriptions = await stripe.subscriptions.list({ customer: stripeCustomerId });
const stripeSubscriptions = await billingService.getSubscriptions(stripeCustomerId);
if (!stripeSubscriptions || !stripeSubscriptions.data.length) {
if (!stripeSubscriptions || !stripeSubscriptions.length) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "No stripeSubscription found",
@@ -95,7 +96,7 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
// Iterate over subscriptions and look for premium product id and status active
// @TODO: iterate if stripeSubscriptions.hasMore is true
const isPremiumUsernameSubscriptionActive = stripeSubscriptions.data.some(
const isPremiumUsernameSubscriptionActive = stripeSubscriptions.some(
(subscription) =>
subscription.items.data[0].price.id === getPremiumMonthlyPlanPriceId() &&
subscription.status === "active"
@@ -284,12 +285,10 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
// Notify stripe about the change
if (updatedUser && updatedUser.metadata && hasKeyInMetadata(updatedUser, "stripeCustomerId")) {
const stripeCustomerId = `${updatedUser.metadata.stripeCustomerId}`;
await stripe.customers.update(stripeCustomerId, {
metadata: {
username: updatedUser.username,
email: updatedUser.email,
userId: updatedUser.id,
},
await billingService.updateCustomer({
customerId: stripeCustomerId,
email: updatedUser.email,
userId: updatedUser.id,
});
}