Skip to content

Commit e837238

Browse files
committed
added getStripeCustomerId
1 parent a5b9a3d commit e837238

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

packages/altair-api/src/auth/user/user.service.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,35 @@ export class UserService {
143143
});
144144
}
145145

146-
async getBillingUrl(userId: string, returnUrl?: string) {
146+
async getStripeCustomerId(userId: string) {
147147
const user = await this.getUser(userId);
148-
let customerId = user.stripeCustomerId;
149148

150-
if (!customerId) {
151-
const res = await this.stripeService.connectOrCreateCustomer(user.email);
152-
customerId = res.id;
149+
if (user.stripeCustomerId) {
150+
return user.stripeCustomerId;
153151
}
154152

153+
// retrieve customer ID from Stripe
154+
const res = await this.stripeService.connectOrCreateCustomer(user.email);
155+
const customerId = res.id;
156+
157+
// update user with customer ID
158+
await this.prisma.user.update({
159+
where: {
160+
id: userId,
161+
},
162+
data: {
163+
stripeCustomerId: customerId,
164+
},
165+
});
166+
167+
return customerId;
168+
}
169+
170+
async getBillingUrl(userId: string, returnUrl?: string) {
171+
const customerId = await this.getStripeCustomerId(userId);
172+
155173
const session = await this.stripeService.createBillingSession(
156-
user.stripeCustomerId,
174+
customerId,
157175
returnUrl
158176
);
159177

0 commit comments

Comments
 (0)