Skip to content

Commit d5777ab

Browse files
committed
[server] Minor: Refactor StripeService.findCustomerByQuery to .findCustomerByAttributionId
1 parent 212b0a5 commit d5777ab

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

components/server/ee/src/user/stripe-service.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,15 @@ export class StripeService {
3636
}
3737

3838
async findCustomerByUserId(userId: string): Promise<Stripe.Customer | undefined> {
39-
return this.findCustomerByQuery(
40-
`metadata['${ATTRIBUTION_ID_METADATA_KEY}']:'${AttributionId.render({ kind: "user", userId })}'`,
41-
);
39+
return this.findCustomerByAttributionId(AttributionId.render({ kind: "user", userId }));
4240
}
4341

4442
async findCustomerByTeamId(teamId: string): Promise<Stripe.Customer | undefined> {
45-
return this.findCustomerByQuery(
46-
`metadata['${ATTRIBUTION_ID_METADATA_KEY}']:'${AttributionId.render({ kind: "team", teamId })}'`,
47-
);
43+
return this.findCustomerByAttributionId(AttributionId.render({ kind: "team", teamId }));
4844
}
4945

50-
async findCustomerByQuery(query: string): Promise<Stripe.Customer | undefined> {
46+
async findCustomerByAttributionId(attributionId: string): Promise<Stripe.Customer | undefined> {
47+
const query = `metadata['${ATTRIBUTION_ID_METADATA_KEY}']:'${attributionId}'`;
5148
const result = await this.getStripe().customers.search({ query });
5249
if (result.data.length > 1) {
5350
throw new Error(`Found more than one Stripe customer for query '${query}'`);
@@ -56,20 +53,21 @@ export class StripeService {
5653
}
5754

5855
async createCustomerForUser(user: User): Promise<Stripe.Customer> {
59-
if (await this.findCustomerByUserId(user.id)) {
56+
const attributionId = AttributionId.render({ kind: "user", userId: user.id });
57+
if (await this.findCustomerByAttributionId(attributionId)) {
6058
throw new Error(`A Stripe customer already exists for user '${user.id}'`);
6159
}
6260
// Create the customer in Stripe
6361
const customer = await this.getStripe().customers.create({
6462
email: User.getPrimaryEmail(user),
6563
name: User.getName(user),
6664
metadata: {
67-
[ATTRIBUTION_ID_METADATA_KEY]: AttributionId.render({ kind: "user", userId: user.id }),
65+
[ATTRIBUTION_ID_METADATA_KEY]: attributionId,
6866
},
6967
});
7068
// Wait for the customer to show up in Stripe search results before proceeding
7169
let attempts = 0;
72-
while (!(await this.findCustomerByUserId(user.id))) {
70+
while (!(await this.findCustomerByAttributionId(attributionId))) {
7371
await new Promise((resolve) => setTimeout(resolve, POLL_CREATED_CUSTOMER_INTERVAL_MS));
7472
if (++attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS) {
7573
throw new Error(`Could not confirm Stripe customer creation for user '${user.id}'`);
@@ -79,7 +77,8 @@ export class StripeService {
7977
}
8078

8179
async createCustomerForTeam(user: User, team: Team): Promise<Stripe.Customer> {
82-
if (await this.findCustomerByTeamId(team.id)) {
80+
const attributionId = AttributionId.render({ kind: "team", teamId: team.id });
81+
if (await this.findCustomerByAttributionId(attributionId)) {
8382
throw new Error(`A Stripe customer already exists for team '${team.id}'`);
8483
}
8584
// Create the customer in Stripe
@@ -88,12 +87,12 @@ export class StripeService {
8887
email: User.getPrimaryEmail(user),
8988
name: userName ? `${userName} (${team.name})` : team.name,
9089
metadata: {
91-
[ATTRIBUTION_ID_METADATA_KEY]: AttributionId.render({ kind: "team", teamId: team.id }),
90+
[ATTRIBUTION_ID_METADATA_KEY]: attributionId,
9291
},
9392
});
9493
// Wait for the customer to show up in Stripe search results before proceeding
9594
let attempts = 0;
96-
while (!(await this.findCustomerByTeamId(team.id))) {
95+
while (!(await this.findCustomerByAttributionId(attributionId))) {
9796
await new Promise((resolve) => setTimeout(resolve, POLL_CREATED_CUSTOMER_INTERVAL_MS));
9897
if (++attempts > POLL_CREATED_CUSTOMER_MAX_ATTEMPTS) {
9998
throw new Error(`Could not confirm Stripe customer creation for team '${team.id}'`);

0 commit comments

Comments
 (0)