feat(billing): update transfer to add sessions
MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
-import { BillingAccount, BillingAdditionalData, BillingInvoice, BillingInvoiceStatus, BillingSessionData, BillingTransfer } from '../../types/Billing';
+import { BillingAccount, BillingAdditionalData, BillingInvoice, BillingInvoiceStatus, BillingSessionData, BillingTransfer, BillingTransferSession } from '../../types/Billing';
import global, { DatabaseCount, FilterParams } from '../../types/GlobalType';
import Constants from '../../utils/Constants';
@@ -494,4 +494,20 @@ export default class BillingStorage {
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return transferMDB.count === 1 ? transferMDB.result[0] : null;
}
+
+ public static async updateTransferAdditionalData(tenant: Tenant, transfer: BillingTransfer, additionalData: BillingTransferSession[]): Promise<void> {
+ const startTime = Logging.traceDatabaseRequestStart();
+ DatabaseUtils.checkTenantObject(tenant);
+ // Preserve the previous list of sessions
+ const sessions: BillingTransferSession[] = transfer.sessions || [];
+ sessions.push(...additionalData);
+ // Set data
+ const updatedTransferMDB: any = {
+ sessions,
+ };
+ await global.database.getCollection(tenant.id, 'billingtransfers').findOneAndUpdate(
+ { '_id': DatabaseUtils.convertToObjectID(transfer.id) },
+ { $set: updatedTransferMDB });
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'updateTransferAdditionalData', startTime, updatedTransferMDB);
+ }
}

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
/* eslint-disable max-len */
-import { BillingAccount, BillingAccountStatus, BillingChargeInvoiceAction, BillingInvoiceStatus, BillingTransfer, BillingTransferStatus } from '../../src/types/Billing';
+import { BillingAccount, BillingAccountStatus, BillingChargeInvoiceAction, BillingInvoiceStatus, BillingTransfer } from '../../src/types/Billing';
import { BillingSettings, BillingSettingsType } from '../../src/types/Setting';
+import { BillingTransferFactory, BillingTransferSessionFactory } from '../factories/BillingFactory';
import chai, { expect } from 'chai';
import { BillingPeriodicOperationTaskConfig } from '../../src/types/TaskConfig';
import BillingStorage from '../../src/storage/mongodb/BillingStorage';
import BillingTestHelper from './BillingTestHelper';
-import { BillingTransferFactory } from '../factories/BillingFactory';
import CentralServerService from './client/CentralServerService';
import CompanyFactory from '../factories/CompanyFactory';
import Constants from '../../src/utils/Constants';
@@ -1249,12 +1249,26 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
BillingTransferFactory.build(),
BillingTransferFactory.build(),
];
-
const ids = await Promise.all(transfers.map(async (transfer) => BillingStorage.saveTransfer(billingTestHelper.tenantContext.getTenant(), transfer)));
const retrievedTransfers = await BillingStorage.getTransfers(billingTestHelper.tenantContext.getTenant(), {}, Constants.DB_PARAMS_MAX_LIMIT);
expect(retrievedTransfers.result.map((transfer) => transfer.id)).to.include.members(ids);
});
+
+ it('should update a draft billing transfer', async () => {
+ let transfer: BillingTransfer = BillingTransferFactory.build();
+ const transferID = await BillingStorage.saveTransfer(billingTestHelper.tenantContext.getTenant(), transfer);
+ expect(transferID).to.not.be.null;
+ transfer.id = transferID;
+
+ const nbSessions = transfer.sessions.length;
+ const sessions = [BillingTransferSessionFactory.build(), BillingTransferSessionFactory.build()];
+ await BillingStorage.updateTransferAdditionalData(billingTestHelper.tenantContext.getTenant(), transfer, sessions);
+
+ transfer = await BillingStorage.getTransferByID(billingTestHelper.tenantContext.getTenant(), transfer.id);
+ expect(transfer.sessions.length).to.be.eq(nbSessions + 2);
+ expect(transfer.sessions).to.include.deep.members(sessions);
+ });
});
});

MODIFY
test/factories/BillingFactory.ts
test/factories/BillingFactory.ts
@@ -15,12 +15,12 @@ export const BillingTransferFactory = Factory.define('billingtransfer')
invoiceExternalID: faker.datatype.uuid(),
}))
.attr('sessions', () =>
- faker.random.arrayElements().map(() => ({
- transactionID: faker.datatype.number(),
- amount: faker.datatype.number(),
- platformFee: {
+ [BillingTransferSessionFactory.build()]);
+
+export const BillingTransferSessionFactory = Factory.define('billingtransfersession')
+ .attr('transactionID', () => faker.datatype.number())
+ .attr('amount', () => faker.datatype.number())
+ .attr('platformFee', () => ({
flatFeePerSession: faker.datatype.number(),
percentage: faker.datatype.number(),
- }
- }))
- );
+ }));

feat(billing): update transfer to add sessions