bp - test - i'm lost
MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -502,7 +502,9 @@ export default class BillingService {
const billingAccount: BillingAccount = {
businessOwnerID: user.id,
status: BillingAccountStatus.IDLE,
- accountExternalID: null
+ accountExternalID: null,
+ createdBy: { id: req.user.id },
+ createdOn: new Date()
};
// Save the account
billingAccount.id = await BillingStorage.saveAccount(req.tenant, billingAccount);
@@ -620,9 +622,16 @@ export default class BillingService {
const user = await UserStorage.getUser(req.tenant, billingAccount.businessOwnerID);
UtilsService.assertObjectExists(action, user, `User ID '${billingAccount.businessOwnerID}' does not exist`, MODULE_NAME, 'handleOnboardAccount', req.user);
// Trigger the creation of the connected account
- billingAccount = { ...billingAccount, ...await billingImpl.createConnectedAccount() };
- // Activate and save the account
- billingAccount.status = BillingAccountStatus.PENDING;
+ const connectedAccount = await billingImpl.createConnectedAccount();
+ // Update account properties
+ billingAccount = {
+ ...billingAccount,
+ ...connectedAccount,
+ status: BillingAccountStatus.PENDING,
+ lastChangedBy: { 'id': req.user.id },
+ lastChangedOn: new Date()
+ };
+ // Save the account
await BillingStorage.saveAccount(req.tenant, billingAccount);
// Notify the user
const notificationData : BillingAccountCreationLinkNotification = {
@@ -684,7 +693,7 @@ export default class BillingService {
});
}
// Get the transfer
- const transfer = await BillingStorage.getTransferByID(req.tenant, filteredRequest.ID);
+ let transfer = await BillingStorage.getTransferByID(req.tenant, filteredRequest.ID);
UtilsService.assertObjectExists(action, transfer, `Transfer ID '${filteredRequest.ID}' does not exist`, MODULE_NAME, 'handleFinalizeTransfer', req.user);
// Check if the transfer is in draft status
if (transfer.status !== BillingTransferStatus.DRAFT) {
@@ -704,13 +713,18 @@ export default class BillingService {
UtilsService.assertObjectExists(action, user, `User ID '${billingAccount.businessOwnerID}' does not exist`, MODULE_NAME, 'handleSendTransferInvoice', req.user);
// Generate the invoice with a fee per session
const invoice = await billingImpl.billPlatformFee(transfer, user);
- // Let's keep track of the invoice data
- transfer.invoice = invoice;
- // We now know how much we can transfer to the sub-account!
- const amountIncludingTaxes = invoice.totalAmount;
- transfer.transferAmount = Utils.createDecimal(transfer.totalAmount).minus(amountIncludingTaxes).toNumber();
- // Update the transfer status
- transfer.status = BillingTransferStatus.FINALIZED;
+ // Funds to transfer
+ const transferAmount = Utils.createDecimal(transfer.totalAmount).minus(invoice.totalAmount).toNumber();
+ // Update transfer properties
+ transfer = {
+ ...transfer,
+ status: BillingTransferStatus.FINALIZED,
+ transferAmount,
+ invoice, // Keep track of some invoice data
+ lastChangedBy: { id: req.user.id },
+ lastChangedOn: new Date()
+ };
+ // Save transfer
await BillingStorage.saveTransfer(req.tenant, transfer);
res.json(Constants.REST_RESPONSE_SUCCESS);
next();
@@ -735,7 +749,7 @@ export default class BillingService {
});
}
// Get the transfer
- const transfer = await BillingStorage.getTransferByID(req.tenant, filteredRequest.ID);
+ let transfer = await BillingStorage.getTransferByID(req.tenant, filteredRequest.ID);
UtilsService.assertObjectExists(action, transfer, `Transfer ID '${filteredRequest.ID}' does not exist`, MODULE_NAME, 'handleSendTransfer', req.user);
// Check if the transfer is in draft status
if (transfer.status !== BillingTransferStatus.FINALIZED) {
@@ -754,10 +768,17 @@ export default class BillingService {
const user = await UserStorage.getUser(req.tenant, billingAccount.businessOwnerID);
UtilsService.assertObjectExists(action, user, `User ID '${billingAccount.businessOwnerID}' does not exist`, MODULE_NAME, 'handleSendTransfer', req.user);
// Send the funds
- transfer.transferExternalID = await billingImpl.sendTransfer(transfer, user);
- transfer.status = BillingTransferStatus.TRANSFERRED;
+ const transferExternalID = await billingImpl.sendTransfer(transfer, user);
+ // Update transfer properties
+ transfer = {
+ ...transfer,
+ transferExternalID,
+ status: BillingTransferStatus.TRANSFERRED,
+ lastChangedBy: { id: req.user.id },
+ lastChangedOn: new Date()
+ };
+ // Save transfer
await BillingStorage.saveTransfer(req.tenant, transfer);
- // TODO - send an email notification with the invoice
res.json(Constants.REST_RESPONSE_SUCCESS);
next();
}

MODIFY
test/api/BillingPlatformTest.ts
test/api/BillingPlatformTest.ts
@@ -38,16 +38,20 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
describe('Billing Platform (utbillingplatform)', () => {
beforeAll(async () => {
await billingTestHelper.initialize(ContextDefinition.TENANT_CONTEXTS.TENANT_BILLING_PLATFORM);
+ // Initialize the Billing module
+ await billingTestHelper.setBillingSystemValidCredentials();
});
describe('Where the admin user', () => {
+
+ describe('Connected Accounts', () => {
+
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
// Set the admin as the current user context
billingTestHelper.setCurrentUserContextAsAdmin();
});
- describe('Connected Accounts', () => {
it('should create an account in an IDLE state and list it', async () => {
const accountID = await billingTestHelper.createBillingAccount();
// List accounts
@@ -181,6 +185,12 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
describe('Transfers', () => {
+ // eslint-disable-next-line @typescript-eslint/require-await
+ beforeAll(async () => {
+ // Set the admin as the current user context
+ billingTestHelper.setCurrentUserContextAsAdmin();
+ });
+
it('should list transfers', async () => {
const transfer = BillingTransferFactory.build();
const transferID = await BillingStorage.saveTransfer(billingTestHelper.getTenant(), transfer);
@@ -232,7 +242,12 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
});
describe('Invoicing', () => {
+
+
+ // eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
+ // Set the admin as the current user context
+ billingTestHelper.setCurrentUserContextAsAdmin();
// Initialize the charging station context
await billingTestHelper.initContext2TestConnectedAccounts();
});
@@ -313,10 +328,16 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
beforeAll(async () => {
await billingTestHelper.setBillingSystemValidCredentials();
- billingTestHelper.setCurrentUserContextAsBasic();
});
describe('Connected Accounts', () => {
+
+ // eslint-disable-next-line @typescript-eslint/require-await
+ beforeAll(async () => {
+ // Set the basic user as the current user context
+ billingTestHelper.setCurrentUserContextAsBasic();
+ });
+
it('should not be able to create a account', async () => {
const response = await billingTestHelper.getCurrentUserService().billingApi.createBillingAccount({
businessOwnerID: billingTestHelper.getCurrentUserContext().id
@@ -351,6 +372,13 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
});
describe('Transfers', () => {
+
+ // eslint-disable-next-line @typescript-eslint/require-await
+ beforeAll(async () => {
+ // Set the basic user as the current user context
+ billingTestHelper.setCurrentUserContextAsBasic();
+ });
+
it('should not be able to list transfers', async () => {
const transfersResponse = await billingTestHelper.getCurrentUserService().billingApi.readTransfers({});
expect(transfersResponse.status).to.be.eq(StatusCodes.FORBIDDEN);

MODIFY
test/api/BillingTestHelper.ts
test/api/BillingTestHelper.ts
@@ -935,7 +935,7 @@ export default class BillingTestHelper {
// Send the activation link
const billingAccountOnboardResponse = await this.getCurrentUserService().billingApi.onboardBillingAccount(accountID);
expect(billingAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- let response = await this.getCurrentUserService().billingApi.activateBillingAccount({ accountID, TenantID: this.tenantContext.getTenant().id });
+ let response = await this.getCurrentUserService().billingApi.activateBillingAccount({ accountID, TenantID: this.getTenantID() });
expect(response.status).to.be.eq(StatusCodes.OK);
expect(response.data?.id).to.be.eq(accountID);
response = await this.getCurrentUserService().billingApi.readBillingAccount(accountID);

bp - test - i'm lost