billing - changed billing context initialization
MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -417,6 +417,12 @@ describe('Billing Service', function() {
testData.chargingStationContext = testData.siteAreaContext.getChargingStationContext(ContextDefinition.CHARGING_STATION_CONTEXTS.ASSIGNED_OCPP16);
// Initialize the Billing module
testData.billingImpl = await testData.setBillingSystemValidCredentials();
+ // Make sure the required users are in sync
+ const adminUser: User = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ const basicUser: User = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ // Synchronize at least these 2 users - this creates a customer on the STRIPE side
+ await testData.billingImpl.forceSynchronizeUser(adminUser);
+ await testData.billingImpl.forceSynchronizeUser(basicUser);
});
describe('Where admin user (essential)', () => {

DELETE
test/api/context/BillingContext.ts
-import { BillingSettings, BillingSettingsType } from '../../../src/types/Setting';
-
-import BackendError from '../../../src/exception/BackendError';
-import BillingFactory from '../../../src/integration/billing/BillingFactory';
-import Constants from '../../../src/utils/Constants';
-import ContextDefinition from './ContextDefinition';
-import Cypher from '../../../src/utils/Cypher';
-import SettingStorage from '../../../src/storage/mongodb/SettingStorage';
-import TenantComponents from '../../../src/types/TenantComponents';
-import TenantContext from './TenantContext';
-import User from '../../../src/types/User';
-import config from '../../config';
-
-export default class BillingContext {
-
- private tenantContext: TenantContext;
-
- constructor(tenantContext: TenantContext) {
- this.tenantContext = tenantContext;
- }
-
- private static getUnitTestBillingSettings(): BillingSettings {
- const billingProperties = {
- isTransactionBillingActivated: config.get('billing.isTransactionBillingActivated'),
- immediateBillingAllowed: config.get('billing.immediateBillingAllowed'),
- periodicBillingAllowed: config.get('billing.periodicBillingAllowed'),
- taxID: config.get('billing.taxID')
- };
- const stripeProperties = {
- url: config.get('stripe.url'),
- publicKey: config.get('stripe.publicKey'),
- secretKey: config.get('stripe.secretKey'),
- };
- return {
- identifier: TenantComponents.BILLING,
- type: BillingSettingsType.STRIPE,
- billing: billingProperties,
- stripe: stripeProperties,
- };
- }
-
- public async createTestData(): Promise<void> {
- const settings = BillingContext.getUnitTestBillingSettings();
- const skip = (!settings.stripe.secretKey);
- if (skip) {
- // Skip billing context generation if no settings are provided
- return;
- }
- await this.saveBillingSettings(BillingContext.getUnitTestBillingSettings());
- const tenant = this.tenantContext.getTenant();
- const billingImpl = await BillingFactory.getBillingImpl(tenant);
- if (!billingImpl) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- message: 'Billing service is not configured',
- method: 'createTestData',
- module: 'BillingContext'
- });
- }
- // Create Users
- const adminUser: User = this.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
- const basicUser: User = this.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
- // Synchronize at least these 2 users - this creates a customer on the STRIPE side
- await billingImpl.synchronizeUser(adminUser);
- await billingImpl.synchronizeUser(basicUser);
- }
-
- private async saveBillingSettings(billingSettings: BillingSettings) {
- const tenantBillingSettings = await SettingStorage.getBillingSetting(this.tenantContext.getTenant().id);
- tenantBillingSettings.billing = billingSettings.billing;
- tenantBillingSettings.stripe = billingSettings.stripe;
- tenantBillingSettings.sensitiveData = ['content.stripe.secretKey'];
- tenantBillingSettings.stripe.secretKey = await Cypher.encrypt(this.tenantContext.getTenant().id, billingSettings.stripe.secretKey);
- await SettingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
- }
-}

MODIFY
test/api/context/ContextBuilder.ts
test/api/context/ContextBuilder.ts
@@ -2,7 +2,6 @@ import ContextDefinition, { TenantDefinition } from './ContextDefinition';
import { SettingDB, SettingDBContent } from '../../../src/types/Setting';
import AssetStorage from '../../../src/storage/mongodb/AssetStorage';
-import BillingContext from './BillingContext';
import CentralServerService from '../client/CentralServerService';
import ChargingStation from '../../../src/types/ChargingStation';
import CompanyStorage from '../../../src/storage/mongodb/CompanyStorage';
@@ -385,7 +384,6 @@ export default class ContextBuilder {
newTenantContext.addSiteContext(siteContext);
// Create transaction/session data for a specific tenants:
const statisticContext = new StatisticsContext(newTenantContext);
- const billingContext = new BillingContext(newTenantContext);
switch (tenantContextDef.tenantName) {
case ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS:
console.log(`${buildTenant.id} (${buildTenant.name}) - Transactions - Site Area '${ContextDefinition.SITE_CONTEXTS.SITE_BASIC}-${ContextDefinition.SITE_AREA_CONTEXTS.WITH_ACL}'`);
@@ -395,9 +393,6 @@ export default class ContextBuilder {
console.log(`${buildTenant.id} (${buildTenant.name}) - Transactions - Unassigned Charging Stations`);
await statisticContext.createTestData(ContextDefinition.SITE_CONTEXTS.NO_SITE, ContextDefinition.SITE_AREA_CONTEXTS.NO_SITE);
break;
- case ContextDefinition.TENANT_CONTEXTS.TENANT_BILLING:
- console.log(`${buildTenant.id} (${buildTenant.name}) - Invoices`);
- await billingContext.createTestData();
}
return newTenantContext;
}

billing - changed billing context initialization