chore: refactor billing service v2 (#16805)

This commit is contained in:
Rajiv Sahal authored and GitHub committed 2024-09-30 13:56:21 +03:00
1 parent ad347cb59f
commit 26a1c749f7
2 files changed
+16 -25

No files matched your search

@@ -68,20 +68,14 @@ export class BillingController {
@Param("teamId") teamId: number,
@Body() input: SubscribeToPlanInput
): Promise<ApiResponse<SubscribeTeamToBillingResponseDto | undefined>> {
const { action, url } = await this.billingService.createSubscriptionForTeam(teamId, input.plan);
if (action === "redirect") {
return {
status: "success",
data: {
action: "redirect",
url,
},
};
}
const customerId = await this.billingService.createTeamBilling(teamId);
const url = await this.billingService.redirectToSubscribeCheckout(teamId, input.plan, customerId);
return {
status: "success",
data: {
url,
},
};
}
@@ -92,20 +86,13 @@ export class BillingController {
@Param("teamId") teamId: number,
@Body() input: SubscribeToPlanInput
): Promise<ApiResponse<SubscribeTeamToBillingResponseDto | undefined>> {
const { action, url } = await this.billingService.updateSubscriptionForTeam(teamId, input.plan);
if (action === "redirect") {
return {
status: "success",
data: {
action: "redirect",
url,
},
};
}
const url = await this.billingService.updateSubscriptionForTeam(teamId, input.plan);
return {
status: "success",
data: {
url,
},
};
}
@@ -47,7 +47,7 @@ export class BillingService implements OnModuleDestroy {
}
}
async createSubscriptionForTeam(teamId: number, plan: PlatformPlan) {
async createTeamBilling(teamId: number) {
const teamWithBilling = await this.teamsRepository.findByIdIncludeBilling(teamId);
let customerId = teamWithBilling?.platformBilling?.customerId;
@@ -60,6 +60,10 @@ export class BillingService implements OnModuleDestroy {
});
}
return customerId;
}
async redirectToSubscribeCheckout(teamId: number, plan: PlatformPlan, customerId?: string) {
const { url } = await this.stripeService.stripe.checkout.sessions.create({
customer: customerId,
line_items: [
@@ -90,7 +94,7 @@ export class BillingService implements OnModuleDestroy {
if (!url) throw new InternalServerErrorException("Failed to create Stripe session.");
return { action: "redirect", url };
return url;
}
async updateSubscriptionForTeam(teamId: number, plan: PlatformPlan) {
@@ -111,7 +115,7 @@ export class BillingService implements OnModuleDestroy {
if (!url) throw new InternalServerErrorException("Failed to create Stripe session.");
return { action: "redirect", url };
return url;
}
async setSubscriptionForTeam(teamId: number, subscriptionId: string, plan: PlatformPlan) {