Skip to content

Commit

Permalink
fix: Await on event submit calls and check before creating stripe cus…
Browse files Browse the repository at this point in the history
…tomer

Signed-off-by: jay-dee7 <[email protected]>
  • Loading branch information
jay-dee7 committed Aug 6, 2024
1 parent 273ebb8 commit 87f82d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/controllers/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class AccountController {
customerEntity = userEntity.customer;
// this time user exists, so notify stripe account should be created.
if (process.env.STRIPE_ENABLED === 'true' && !customerEntity.paymentProviderId) {
eventTracker.submit({
await eventTracker.submit({
operation: OperationNameEnum.STRIPE_ACCOUNT_CREATE,
data: {
name: customerEntity.name,
Expand Down Expand Up @@ -362,7 +362,7 @@ export class AccountController {

// 10. Add the Stripe account to the Customer
if (process.env.STRIPE_ENABLED === 'true' && !customerEntity.paymentProviderId) {
eventTracker.submit({
await eventTracker.submit({
operation: OperationNameEnum.STRIPE_ACCOUNT_CREATE,
data: {
name: customerEntity.name,
Expand Down Expand Up @@ -538,7 +538,7 @@ export class AccountController {
}
// 5. Setup stripe account
if (process.env.STRIPE_ENABLED === 'true' && !customerEntity.paymentProviderId) {
eventTracker.submit({
await eventTracker.submit({
operation: OperationNameEnum.STRIPE_ACCOUNT_CREATE,
data: {
name: customerEntity.name,
Expand Down
52 changes: 30 additions & 22 deletions src/services/track/admin/account-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,43 @@ export class PortalAccountCreateSubmitter implements IObserver {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

try {
// Create a new Stripe account
const account = await stripe.customers.create({
name: data.name,
email: data.email,
const customer = await CustomerService.instance.customerRepository.findOne({
where: {
email: data.email,
},
});
if (account.lastResponse.statusCode !== StatusCodes.OK) {

let stripeCustomer: Stripe.Response<Stripe.Customer>;
if (customer && !customer.paymentProviderId) {
// Create a new Stripe account
stripeCustomer = await stripe.customers.create({
name: data.name,
email: data.email,
});
if (stripeCustomer.lastResponse.statusCode !== StatusCodes.OK) {
this.notify({
message: EventTracker.compileBasicNotification(
`Failed to create Stripe account with name: ${data.name}.`,
operation.operation
),
severity: 'error',
} as INotifyMessage);
return;
}
this.notify({
message: EventTracker.compileBasicNotification(
`Failed to create Stripe account with name: ${data.name}.`,
`Stripe account created with name: ${data.name}.`,
operation.operation
),
severity: 'error',
severity: 'info',
} as INotifyMessage);
return;
// Update the CaaS customer with the new Stripe account. Note, we're populating the "name" field from stripe's response.
await CustomerService.instance.update({
customerId: data.customerId,
name: data.name,
paymentProviderId: stripeCustomer.id,
});
}

// Update the CaaS customer with the new Stripe account. Note, we're populating the "name" field from stripe's response.
await CustomerService.instance.update({
customerId: data.customerId,
name: data.name,
paymentProviderId: account.id,
});
this.notify({
message: EventTracker.compileBasicNotification(
`Stripe account created with name: ${data.name}.`,
operation.operation
),
severity: 'info',
} as INotifyMessage);
} catch (error) {
this.notify({
message: EventTracker.compileBasicNotification(
Expand Down

0 comments on commit 87f82d1

Please sign in to comment.