refactor: team and org billing portal service (#24497)
* refactor: team and org billing portal service * fix: implement coderabbit feedback * fix: move core logic to base service * remove unused comments
This commit is contained in:
1 parent
b2839830c8
commit
1dade81299
3 files changed
+23
-44
No files matched your search
@@ -6,6 +6,8 @@ import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import stripe from "../../server";
|
||||
|
||||
@@ -62,6 +64,23 @@ export abstract class BillingPortalService {
|
||||
return safeRedirectUrl || defaultUrl;
|
||||
}
|
||||
|
||||
protected getValidatedTeamSubscriptionId(metadata: Prisma.JsonValue) {
|
||||
const teamMetadataParsed = teamMetadataSchema.safeParse(metadata);
|
||||
|
||||
if (!teamMetadataParsed.success || !teamMetadataParsed.data?.subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return teamMetadataParsed.data.subscriptionId;
|
||||
}
|
||||
|
||||
protected getValidatedTeamSubscriptionIdForPlatform(subscriptionId?: string | null) {
|
||||
if (!subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
return subscriptionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to check permissions - implemented by subclasses
|
||||
*/
|
||||
|
||||
+2
-22
@@ -1,7 +1,5 @@
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { getSubscriptionFromId } from "../../subscriptions";
|
||||
import { BillingPortalService } from "../base/BillingPortalService";
|
||||
@@ -24,24 +22,6 @@ export class OrganizationBillingPortalService extends BillingPortalService {
|
||||
});
|
||||
}
|
||||
|
||||
async getValidatedTeamSubscriptionId(metadata: Prisma.JsonValue) {
|
||||
const teamMetadataParsed = teamMetadataSchema.safeParse(metadata);
|
||||
|
||||
if (!teamMetadataParsed.success || !teamMetadataParsed.data?.subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return teamMetadataParsed.data.subscriptionId;
|
||||
}
|
||||
|
||||
async getValidatedTeamSubscriptionIdForPlatform(subscriptionId?: string | null) {
|
||||
if (!subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return subscriptionId;
|
||||
}
|
||||
|
||||
async getCustomerId(teamId: number): Promise<string | null> {
|
||||
const log = logger.getSubLogger({ prefix: ["OrganizationBillingPortalService", "getCustomerId"] });
|
||||
|
||||
@@ -51,14 +31,14 @@ export class OrganizationBillingPortalService extends BillingPortalService {
|
||||
let teamSubscriptionId = "";
|
||||
|
||||
if (team.isPlatform) {
|
||||
const subscriptionId = await this.getValidatedTeamSubscriptionIdForPlatform(
|
||||
const subscriptionId = this.getValidatedTeamSubscriptionIdForPlatform(
|
||||
team.platformBilling?.subscriptionId
|
||||
);
|
||||
|
||||
if (!subscriptionId) return null;
|
||||
teamSubscriptionId = subscriptionId;
|
||||
} else {
|
||||
const subscriptionId = await this.getValidatedTeamSubscriptionId(team.metadata);
|
||||
const subscriptionId = this.getValidatedTeamSubscriptionId(team.metadata);
|
||||
|
||||
if (!subscriptionId) return null;
|
||||
teamSubscriptionId = subscriptionId;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { getSubscriptionFromId } from "../../subscriptions";
|
||||
import { BillingPortalService } from "../base/BillingPortalService";
|
||||
@@ -19,24 +17,6 @@ export class TeamBillingPortalService extends BillingPortalService {
|
||||
});
|
||||
}
|
||||
|
||||
async getValidatedTeamSubscriptionId(metadata: Prisma.JsonValue) {
|
||||
const teamMetadataParsed = teamMetadataSchema.safeParse(metadata);
|
||||
|
||||
if (!teamMetadataParsed.success || !teamMetadataParsed.data?.subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return teamMetadataParsed.data.subscriptionId;
|
||||
}
|
||||
|
||||
async getValidatedTeamSubscriptionIdForPlatform(subscriptionId?: string | null) {
|
||||
if (!subscriptionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return subscriptionId;
|
||||
}
|
||||
|
||||
async getCustomerId(teamId: number): Promise<string | null> {
|
||||
const log = logger.getSubLogger({ prefix: ["TeamBillingPortalService", "getCustomerId"] });
|
||||
|
||||
@@ -46,14 +26,14 @@ export class TeamBillingPortalService extends BillingPortalService {
|
||||
let teamSubscriptionId = "";
|
||||
|
||||
if (team.isPlatform) {
|
||||
const subscriptionId = await this.getValidatedTeamSubscriptionIdForPlatform(
|
||||
const subscriptionId = this.getValidatedTeamSubscriptionIdForPlatform(
|
||||
team.platformBilling?.subscriptionId
|
||||
);
|
||||
|
||||
if (!subscriptionId) return null;
|
||||
teamSubscriptionId = subscriptionId;
|
||||
} else {
|
||||
const subscriptionId = await this.getValidatedTeamSubscriptionId(team.metadata);
|
||||
const subscriptionId = this.getValidatedTeamSubscriptionId(team.metadata);
|
||||
|
||||
if (!subscriptionId) return null;
|
||||
teamSubscriptionId = subscriptionId;
|
||||
|
||||
Reference in new issue
Block a user