Skip to content

Commit

Permalink
fix subscription create workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Sep 3, 2024
1 parent e632f45 commit 83094ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/services/api/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export class CustomerService {
customerEntity
);
await PaymentAccountService.instance.create(CheqdNetwork.Testnet, true, customerEntity, key);
return {
customerId: customerEntity.customerId,
name: customerEntity.name,
};
return customerEntity;
}

public async update(customer: UpdateCustomerEntity) {
Expand Down
56 changes: 30 additions & 26 deletions src/services/track/admin/subscription-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,40 @@ export class SubscriptionSubmitter implements IObserver {
this.stripe.customers.retrieve(data.paymentProviderId),
]);
if (!customer) {
const whereClause: FindOptionsWhere<CustomerEntity>[] = [{ paymentProviderId: data.paymentProviderId }];
// we add an additional "OR" check in case that a customer was created locally with email and no paymentProviderId
if (!stripeCustomer.deleted && stripeCustomer.email) {
whereClause.push({ email: stripeCustomer.email });
}

const customers = await CustomerService.instance.customerRepository.find({
where: whereClause,
where: { paymentProviderId: data.paymentProviderId },
});
if (customers.length === 0) {
this.notify({
message: EventTracker.compileBasicNotification(
`Customer not found for Cheqd Studio, creating new customer record with paymentProviderId: ${data.paymentProviderId}`,
operation.operation
),
severity: 'info',
});

// we add an additional "OR" check in case that a customer was created locally with email and no paymentProviderId
if (!stripeCustomer.deleted && stripeCustomer.email) {
const customerName = stripeCustomer.name ?? stripeCustomer.email;
const customer = await CustomerService.instance.create(
customerName,
stripeCustomer.email,
undefined,
data.paymentProviderId
);
customers.push(customer as CustomerEntity);
const customerWithoutPaymentProviderId =
await CustomerService.instance.customerRepository.findOne({
where: { email: stripeCustomer.email },
});

if (!customerWithoutPaymentProviderId) {
this.notify({
message: EventTracker.compileBasicNotification(
`Customer not found for Cheqd Studio, creating new customer record with paymentProviderId: ${data.paymentProviderId}`,
operation.operation
),
severity: 'info',
});

const customerName = stripeCustomer.name ?? stripeCustomer.email;
customers.push(
await CustomerService.instance.create(
customerName,
stripeCustomer.email,
undefined,
data.paymentProviderId
)
);
} else {
customers.push(customerWithoutPaymentProviderId);
}
}
}

if (customers.length !== 1) {
} else if (customers.length !== 1) {
this.notify({
message: EventTracker.compileBasicNotification(
`Only one Stripe account should be associated with CaaS customer. Stripe accountId: ${data.paymentProviderId}.`,
Expand All @@ -191,6 +194,7 @@ export class SubscriptionSubmitter implements IObserver {
severity: 'error',
});
}

customer = customers[0];
}

Expand Down

0 comments on commit 83094ed

Please sign in to comment.