billing - settings - remove obsolete methods + tests adapatation
MODIFY
src/integration/billing/BillingFactory.ts
src/integration/billing/BillingFactory.ts
import { BillingSettings, BillingSettingsType } from '../../types/Setting';
import BillingIntegration from './BillingIntegration';
+import BillingSettingStorage from '../../storage/mongodb/BillingSettingStorage';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
import SettingStorage from '../../storage/mongodb/SettingStorage';
@@ -20,7 +21,8 @@ export default class BillingFactory {
if (Utils.isTenantComponentActive(tenant, TenantComponents.PRICING) &&
Utils.isTenantComponentActive(tenant, TenantComponents.BILLING)) {
// Get the billing's settings
- const settings: BillingSettings = await SettingStorage.getBillingSettings(tenantID);
+ const allSettings: BillingSettings[] = await BillingSettingStorage.getBillingSettings(tenantID);
+ const settings: BillingSettings = allSettings[0];
if (settings) {
let billingIntegrationImpl = null;
switch (settings.type) {

MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -3,6 +3,7 @@ import FeatureToggles, { Feature } from '../../utils/FeatureToggles';
import User, { UserStatus } from '../../types/User';
import BackendError from '../../exception/BackendError';
+import BillingSettingStorage from '../../storage/mongodb/BillingSettingStorage';
import { BillingSettings } from '../../types/Setting';
import BillingStorage from '../../storage/mongodb/BillingStorage';
import Constants from '../../utils/Constants';
@@ -10,7 +11,6 @@ import Logging from '../../utils/Logging';
import NotificationHandler from '../../notification/NotificationHandler';
import { Request } from 'express';
import { ServerAction } from '../../types/Server';
-import SettingStorage from '../../storage/mongodb/SettingStorage';
import TenantStorage from '../../storage/mongodb/TenantStorage';
import Transaction from '../../types/Transaction';
import UserStorage from '../../storage/mongodb/UserStorage';
@@ -77,9 +77,8 @@ export default abstract class BillingIntegration {
'All the users are up to date'
);
// Update last synchronization
- const billingSettings = await SettingStorage.getBillingSettings(this.tenantID);
- billingSettings.billing.usersLastSynchronizedOn = new Date();
- await SettingStorage.saveBillingSettings(this.tenantID, billingSettings);
+ this.settings.billing.usersLastSynchronizedOn = new Date();
+ await BillingSettingStorage.saveBillingSetting(this.tenantID, this.settings);
// Result
return actionsDone;
}

MODIFY
src/storage/mongodb/SettingStorage.ts
src/storage/mongodb/SettingStorage.ts
@@ -259,71 +259,6 @@ export default class SettingStorage {
return smartChargingSettings;
}
- public static async saveBillingSettings(tenantID: string, billingSettingsToSave: BillingSettings): Promise<string> {
- const settings = await SettingStorage.getBillingSettings(tenantID);
- if (settings.type === BillingSettingsType.STRIPE) {
- if (!billingSettingsToSave.stripe.secretKey) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME,
- method: 'saveBillingSettings',
- message: 'One or several mandatory fields are missing'
- });
- }
- }
- // Build internal structure
- const settingsToSave = {
- id: billingSettingsToSave.id,
- identifier: billingSettingsToSave.identifier,
- sensitiveData: billingSettingsToSave.sensitiveData,
- backupSensitiveData: billingSettingsToSave.backupSensitiveData,
- lastChangedOn: new Date(),
- content: {
- billing: billingSettingsToSave.billing,
- stripe: billingSettingsToSave.stripe
- },
- } as SettingDB;
- // Save
- return this.saveSettings(tenantID, settingsToSave);
- }
-
- public static async getBillingSettings(tenantID: string): Promise<BillingSettings> {
- const billingSettings = {
- identifier: TenantComponents.BILLING,
- } as BillingSettings;
- const settings = await SettingStorage.getSettings(tenantID,
- { identifier: TenantComponents.BILLING },
- Constants.DB_PARAMS_MAX_LIMIT);
- if (settings && settings.count > 0 && settings.result[0].content) {
- const config = settings.result[0].content;
- billingSettings.id = settings.result[0].id;
- billingSettings.sensitiveData = settings.result[0].sensitiveData;
- billingSettings.backupSensitiveData = settings.result[0].backupSensitiveData;
- // Billing Common Properties
- if (config.billing) {
- const { isTransactionBillingActivated, immediateBillingAllowed, periodicBillingAllowed, taxID, usersLastSynchronizedOn } = config.billing;
- billingSettings.billing = {
- isTransactionBillingActivated,
- immediateBillingAllowed,
- periodicBillingAllowed,
- usersLastSynchronizedOn,
- taxID,
- };
- }
- // Billing Concrete Implementation Properties
- if (config.stripe) {
- billingSettings.type = BillingSettingsType.STRIPE;
- const { url, publicKey, secretKey } = config.stripe;
- billingSettings.stripe = {
- url,
- publicKey,
- secretKey,
- };
- }
- return billingSettings;
- }
- }
-
public static async getCryptoSettings(tenantID: string): Promise<CryptoSettings> {
// Get the Crypto Key settings
const settings = await SettingStorage.getSettings(tenantID,

MODIFY
test/api/context/BillingContext.ts
test/api/context/BillingContext.ts
@@ -2,10 +2,10 @@ import { BillingSettings, BillingSettingsType } from '../../../src/types/Setting
import BackendError from '../../../src/exception/BackendError';
import BillingFactory from '../../../src/integration/billing/BillingFactory';
+import BillingSettingStorage from '../../../src/storage/mongodb/BillingSettingStorage';
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';
@@ -67,11 +67,12 @@ export default class BillingContext {
private async saveBillingSettings(billingSettings: BillingSettings) {
// TODO - rethink that part
- const tenantBillingSettings = await SettingStorage.getBillingSettings(this.tenantContext.getTenant().id);
+ const allBillingSettings = await BillingSettingStorage.getBillingSettings(this.tenantContext.getTenant().id);
+ const tenantBillingSettings = allBillingSettings[0];
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.saveBillingSettings(this.tenantContext.getTenant().id, tenantBillingSettings);
+ await BillingSettingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
}
}

billing - settings - remove obsolete methods + tests adapatation