feat(billing): save pending subaccounts in database
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -1720,8 +1720,9 @@ export default class StripeBillingIntegration extends BillingIntegration {
}
return {
- id: subAccount.id,
- activationLink: activationLink.url
+ accountID: subAccount.id,
+ activationLink: activationLink.url,
+ pending: true
};
}
}

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -496,6 +496,9 @@ export default class BillingService {
MODULE_NAME, 'handleCreateSubAccount', req.user);
// Create the sub account
const subAccount = await billingImpl.createSubAccount();
+ subAccount.userID = user.id;
+ // Save the sub account
+ subAccount.id = await BillingStorage.saveSubAccount(req.tenant, subAccount);
// Notify the user by mail
void NotificationHandler.sendBillingSubAccountCreationLink(req.tenant, Utils.generateUUID(), user, { onboardingLink: subAccount.activationLink });
res.status(StatusCodes.CREATED).json(subAccount);

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
-import { BillingAdditionalData, BillingInvoice, BillingInvoiceStatus, BillingSessionData } from '../../types/Billing';
+import { BillingAccount, BillingAdditionalData, BillingInvoice, BillingInvoiceStatus, BillingSessionData } from '../../types/Billing';
import global, { DatabaseCount, FilterParams } from '../../types/GlobalType';
import Constants from '../../utils/Constants';
@@ -216,4 +216,24 @@ export default class BillingStorage {
.findOneAndDelete({ 'invoiceID': id });
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'deleteInvoiceByInvoiceID', startTime, { id });
}
+
+ public static async saveSubAccount(tenant: Tenant, subAccount: BillingAccount): Promise<string> {
+ const startTime = Logging.traceDatabaseRequestStart();
+ // Build Request
+ // Properties to save
+ const subAccountMDB: any = {
+ _id: subAccount.id ? DatabaseUtils.convertToObjectID(subAccount.id) : new ObjectId(),
+ accountID: subAccount.accountID,
+ pending: subAccount.pending,
+ userID: DatabaseUtils.convertToObjectID(subAccount.userID)
+ };
+ // Modify and return the modified document
+ await global.database.getCollection<any>(tenant.id, 'billingsubaccounts').findOneAndUpdate(
+ { _id: subAccountMDB._id },
+ { $set: subAccountMDB },
+ { upsert: true, returnDocument: 'after' }
+ );
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'saveInvoice', startTime, subAccountMDB);
+ return subAccountMDB._id.toString();
+ }
}

MODIFY
src/types/Billing.ts
src/types/Billing.ts
@@ -154,7 +154,10 @@ export interface BillingAdditionalData {
}
export interface BillingAccount {
- id: string;
+ id?: string;
+ accountID: string;
activationLink?: string;
+ userID?: string;
+ pending: boolean;
}

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -90,7 +90,9 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
it('Should create a sub-account with its associated activation link', async () => {
const subAccount = await stripeTestHelper.createSubAccount();
expect(subAccount.id).to.exist;
+ expect(subAccount.accountID).to.exist;
expect(subAccount.activationLink).to.include('https://connect.stripe.com/setup/s/');
+ expect(subAccount.pending).to.be.true;
});
});

feat(billing): save pending subaccounts in database