billing settings - moved methods to SettingStorage
MODIFY
src/integration/billing/BillingFactory.ts
src/integration/billing/BillingFactory.ts
-import { BillingSettings, BillingSettingsType } from '../../types/Setting';
-
import BillingIntegration from './BillingIntegration';
-import BillingStorage from '../../storage/mongodb/BillingStorage';
+import { BillingSettingsType } from '../../types/Setting';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
+import SettingStorage from '../../storage/mongodb/SettingStorage';
import StripeBillingIntegration from './stripe/StripeBillingIntegration';
import Tenant from '../../types/Tenant';
import TenantComponents from '../../types/TenantComponents';
@@ -20,7 +19,7 @@ export default class BillingFactory {
if (Utils.isTenantComponentActive(tenant, TenantComponents.PRICING) &&
Utils.isTenantComponentActive(tenant, TenantComponents.BILLING)) {
// Get the billing's settings
- const settings = await BillingStorage.getBillingSetting(tenantID);
+ const settings = await SettingStorage.getBillingSetting(tenantID);
if (settings) {
let billingIntegrationImpl = null;
switch (settings.type) {

MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -10,6 +10,7 @@ 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,7 +78,7 @@ export default abstract class BillingIntegration {
);
// Update last synchronization
this.settings.billing.usersLastSynchronizedOn = new Date();
- await BillingStorage.saveBillingSetting(this.tenantID, this.settings);
+ await SettingStorage.saveBillingSetting(this.tenantID, this.settings);
// Result
return actionsDone;
}

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -19,6 +19,7 @@ import LockingHelper from '../../../../locking/LockingHelper';
import LockingManager from '../../../../locking/LockingManager';
import Logging from '../../../../utils/Logging';
import { ServerAction } from '../../../../types/Server';
+import SettingStorage from '../../../../storage/mongodb/SettingStorage';
import { StatusCodes } from 'http-status-codes';
import TenantComponents from '../../../../types/TenantComponents';
import TransactionStorage from '../../../../storage/mongodb/TransactionStorage';
@@ -758,7 +759,7 @@ export default class BillingService {
module: MODULE_NAME, method: 'handleGetBillingSetting',
});
}
- const billingSettings: BillingSettings = await BillingStorage.getBillingSetting(req.user.tenantID);
+ const billingSettings: BillingSettings = await SettingStorage.getBillingSetting(req.user.tenantID);
if (!billingSettings) {
res.sendStatus(HTTPError.OBJECT_DOES_NOT_EXIST_ERROR);
next();
@@ -786,7 +787,7 @@ export default class BillingService {
// Check
const newBillingProperties = BillingValidator.getInstance().validateUpdateBillingSetting({ ...req.params, ...req.body });
// Load previous settings
- const billingSettings = await BillingStorage.getBillingSetting(req.user.tenantID);
+ const billingSettings = await SettingStorage.getBillingSetting(req.user.tenantID);
if (!billingSettings) {
res.sendStatus(HTTPError.OBJECT_DOES_NOT_EXIST_ERROR);
next();
@@ -851,7 +852,7 @@ export default class BillingService {
billingSettings.lastChangedBy = { 'id': req.user.id };
billingSettings.lastChangedOn = new Date();
// Let's save the new settings
- await BillingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
+ await SettingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
// Post-process the activation of the billing feature
if (postponeTransactionBillingActivation) {
// Check
@@ -859,7 +860,7 @@ export default class BillingService {
// Well - everything was Ok, activation is possible
billingSettings.billing.isTransactionBillingActivated = true;
// Save it again now that we are sure
- await BillingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
+ await SettingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
}
// Ok
res.json(Constants.REST_RESPONSE_SUCCESS);

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
import { BillingAdditionalData, BillingInvoice, BillingInvoiceStatus, BillingSessionData } from '../../types/Billing';
-import { BillingSettings, BillingSettingsType, SettingDB } from '../../types/Setting';
import global, { FilterParams } from '../../types/GlobalType';
import Constants from '../../utils/Constants';
@@ -8,8 +7,6 @@ import DatabaseUtils from './DatabaseUtils';
import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
import { ObjectID } from 'mongodb';
-import SettingStorage from './SettingStorage';
-import TenantComponents from '../../types/TenantComponents';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'BillingStorage';
@@ -237,58 +234,4 @@ export default class BillingStorage {
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteInvoice', uniqueTimerID, { id });
}
-
- public static async getBillingSetting(tenantID: string): Promise<BillingSettings> {
- // Get BILLING Settings by Identifier
- const setting = await SettingStorage.getSettingByIdentifier(tenantID, TenantComponents.BILLING);
- if (setting) {
- return BillingStorage.convertToBillingSettings(setting);
- }
- return null;
- }
-
- public static async saveBillingSetting(tenantID: string, billingSettings: BillingSettings): Promise<string> {
- const { id, identifier, sensitiveData, backupSensitiveData, category } = billingSettings;
- const { createdBy, createdOn, lastChangedBy, lastChangedOn } = billingSettings;
- const { type, billing, stripe } = billingSettings;
- const setting: SettingDB = {
- id, identifier, sensitiveData, backupSensitiveData,
- content: {
- type,
- billing,
- },
- category, createdBy, createdOn, lastChangedBy, lastChangedOn,
- };
- if (billingSettings.type === BillingSettingsType.STRIPE) {
- setting.sensitiveData = [ 'content.stripe.secretKey' ];
- setting.content.stripe = stripe;
- }
- return SettingStorage.saveSettings(tenantID, setting);
- }
-
- private static convertToBillingSettings(setting: SettingDB): BillingSettings {
- const { id, backupSensitiveData, category } = setting;
- const { createdBy, createdOn, lastChangedBy, lastChangedOn } = setting;
- const { content } = setting;
- const billingSettings: BillingSettings = {
- id,
- identifier: TenantComponents.BILLING,
- type: content.type as BillingSettingsType,
- backupSensitiveData,
- billing: content.billing,
- category,
- createdBy,
- createdOn,
- lastChangedBy,
- lastChangedOn,
- };
- switch (content.type) {
- // Only STRIPE so far
- case BillingSettingsType.STRIPE:
- billingSettings.stripe = content.stripe;
- billingSettings.sensitiveData = [ 'stripe.secretKey' ];
- break;
- }
- return billingSettings;
- }
}

MODIFY
src/storage/mongodb/SettingStorage.ts
src/storage/mongodb/SettingStorage.ts
@@ -418,4 +418,58 @@ export default class SettingStorage {
// Save
await SettingStorage.saveSettings(tenantID, settingsToSave);
}
+
+ public static async getBillingSetting(tenantID: string): Promise<BillingSettings> {
+ // Get BILLING Settings by Identifier
+ const setting = await SettingStorage.getSettingByIdentifier(tenantID, TenantComponents.BILLING);
+ if (setting) {
+ return SettingStorage.convertToBillingSettings(setting);
+ }
+ return null;
+ }
+
+ public static async saveBillingSetting(tenantID: string, billingSettings: BillingSettings): Promise<string> {
+ const { id, identifier, sensitiveData, backupSensitiveData, category } = billingSettings;
+ const { createdBy, createdOn, lastChangedBy, lastChangedOn } = billingSettings;
+ const { type, billing, stripe } = billingSettings;
+ const setting: SettingDB = {
+ id, identifier, sensitiveData, backupSensitiveData,
+ content: {
+ type,
+ billing,
+ },
+ category, createdBy, createdOn, lastChangedBy, lastChangedOn,
+ };
+ if (billingSettings.type === BillingSettingsType.STRIPE) {
+ setting.sensitiveData = [ 'content.stripe.secretKey' ];
+ setting.content.stripe = stripe;
+ }
+ return SettingStorage.saveSettings(tenantID, setting);
+ }
+
+ private static convertToBillingSettings(setting: SettingDB): BillingSettings {
+ const { id, backupSensitiveData, category } = setting;
+ const { createdBy, createdOn, lastChangedBy, lastChangedOn } = setting;
+ const { content } = setting;
+ const billingSettings: BillingSettings = {
+ id,
+ identifier: TenantComponents.BILLING,
+ type: content.type as BillingSettingsType,
+ backupSensitiveData,
+ billing: content.billing,
+ category,
+ createdBy,
+ createdOn,
+ lastChangedBy,
+ lastChangedOn,
+ };
+ switch (content.type) {
+ // Only STRIPE so far
+ case BillingSettingsType.STRIPE:
+ billingSettings.stripe = content.stripe;
+ billingSettings.sensitiveData = [ 'stripe.secretKey' ];
+ break;
+ }
+ return billingSettings;
+ }
}

MODIFY
test/api/context/BillingContext.ts
test/api/context/BillingContext.ts
@@ -6,6 +6,7 @@ import BillingStorage from '../../../src/storage/mongodb/BillingStorage';
import Constants from '../../../src/utils/Constants';
import ContextDefinition from './ContextDefinition';
import Cypher from '../../../src/utils/Cypher';
+import SettingStorage from '../../storage/mongodb/SettingStorage';
import TenantComponents from '../../../src/types/TenantComponents';
import TenantContext from './TenantContext';
import User from '../../../src/types/User';
@@ -66,11 +67,11 @@ export default class BillingContext {
}
private async saveBillingSettings(billingSettings: BillingSettings) {
- const tenantBillingSettings = await BillingStorage.getBillingSetting(this.tenantContext.getTenant().id);
+ 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 BillingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
+ await SettingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
}
}

billing settings - moved methods to SettingStorage