billing - BillingStorage and BillingSettingStorage merged
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 BillingStorage from '../../storage/mongodb/BillingStorage';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
import StripeBillingIntegration from './stripe/StripeBillingIntegration';
@@ -20,7 +20,7 @@ export default class BillingFactory {
if (Utils.isTenantComponentActive(tenant, TenantComponents.PRICING) &&
Utils.isTenantComponentActive(tenant, TenantComponents.BILLING)) {
// Get the billing's settings
- const allSettings: BillingSettings[] = await BillingSettingStorage.getBillingSettings(tenantID);
+ const allSettings: BillingSettings[] = await BillingStorage.getBillingSettings(tenantID);
const settings: BillingSettings = allSettings[0];
if (settings) {
let billingIntegrationImpl = null;

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

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -9,7 +9,6 @@ import Authorizations from '../../../../authorization/Authorizations';
import BackendError from '../../../../exception/BackendError';
import BillingFactory from '../../../../integration/billing/BillingFactory';
import BillingSecurity from './security/BillingSecurity';
-import BillingSettingStorage from '../../../../storage/mongodb/BillingSettingStorage';
import { BillingSettings } from '../../../../types/Setting';
import BillingStorage from '../../../../storage/mongodb/BillingStorage';
import Constants from '../../../../utils/Constants';
@@ -755,7 +754,7 @@ export default class BillingService {
module: MODULE_NAME, method: 'handleGetBillingSetting',
});
}
- const allBillingSettings: BillingSettings[] = await BillingSettingStorage.getBillingSettings(req.user.tenantID);
+ const allBillingSettings: BillingSettings[] = await BillingStorage.getBillingSettings(req.user.tenantID);
if (allBillingSettings) {
allBillingSettings.map((billingSettings) => BillingService.hashSensitiveData(req.user.tenantID, billingSettings));
res.json(allBillingSettings);
@@ -775,7 +774,7 @@ export default class BillingService {
});
}
const settingID = req.params.id;
- const billingSettings: BillingSettings = await BillingSettingStorage.getBillingSetting(req.user.tenantID, settingID);
+ const billingSettings: BillingSettings = await BillingStorage.getBillingSetting(req.user.tenantID, settingID);
if (billingSettings) {
// For trouble shooting only
// _.set(billingSettings, 'sk-enc', billingSettings.stripe.secretKey);
@@ -803,7 +802,7 @@ export default class BillingService {
});
}
// Load previous settings
- const billingSettings = await BillingSettingStorage.getBillingSetting(req.user.tenantID, newBillingProperties.id);
+ const billingSettings = await BillingStorage.getBillingSetting(req.user.tenantID, newBillingProperties.id);
await BillingService.alterSensitiveData(req.user.tenantID, billingSettings, newBillingProperties);
// Billing properties to preserve
const { isTransactionBillingActivated, usersLastSynchronizedOn } = billingSettings.billing;
@@ -821,7 +820,7 @@ export default class BillingService {
billingSettings.lastChangedBy = { 'id': req.user.id };
billingSettings.lastChangedOn = new Date();
// Let's save it
- const id = await BillingSettingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
+ const id = await BillingStorage.saveBillingSetting(req.user.tenantID, billingSettings);
if (!id) {
res.sendStatus(StatusCodes.NOT_FOUND);
} else {
@@ -833,10 +832,10 @@ export default class BillingService {
public static async handleActivateBillingSetting(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// TODO - sanitize
const settingID = req.params.id;
- const currentBillingSettings = await BillingSettingStorage.getBillingSetting(req.user.tenantID, settingID);
+ const currentBillingSettings = await BillingStorage.getBillingSetting(req.user.tenantID, settingID);
// TODO - check prerequisites - activation should not be possible when settings are inconsistent
currentBillingSettings.billing.isTransactionBillingActivated = true;
- const id = await BillingSettingStorage.saveBillingSetting(req.user.tenantID, currentBillingSettings);
+ const id = await BillingStorage.saveBillingSetting(req.user.tenantID, currentBillingSettings);
if (!id) {
res.sendStatus(StatusCodes.NOT_FOUND);
} else {

DELETE
src/storage/mongodb/BillingSettingStorage.ts
-import { BillingSettings, BillingSettingsType, SettingDB } from '../../types/Setting';
-
-import Constants from '../../utils/Constants';
-import SettingStorage from './SettingStorage';
-import TenantComponents from '../../types/TenantComponents';
-
-const MODULE_NAME = 'BillingSettingStorage';
-
-export default class BillingSettingStorage {
-
- public static async getBillingSettings(tenantID: string): Promise<BillingSettings[]> {
- const settings = await SettingStorage.getSettings(tenantID, { identifier: TenantComponents.BILLING }, Constants.DB_PARAMS_SINGLE_RECORD);
- if (settings && settings.count > 0) {
- const allBillingSettings: BillingSettings[] = settings.result.map((setting) => BillingSettingStorage.convertToBillingSettings(setting));
- return allBillingSettings;
- }
- return null;
- }
-
- public static async getBillingSetting(tenantID: string, settingID: string): Promise<BillingSettings> {
- const settings = await SettingStorage.getSettings(tenantID, { settingID }, Constants.DB_PARAMS_SINGLE_RECORD);
- if (settings?.result?.[0]?.content) {
- const setting: SettingDB = settings.result[0];
- return BillingSettingStorage.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, category,
- createdBy, createdOn, lastChangedBy, lastChangedOn,
- content: {
- type,
- billing,
- stripe,
- },
- };
- return SettingStorage.saveSettings(tenantID, setting);
- }
-
- private static convertToBillingSettings(setting: SettingDB): BillingSettings {
- const { id, sensitiveData, backupSensitiveData, category } = setting;
- const { createdBy, createdOn, lastChangedBy, lastChangedOn } = setting;
- const { content } = setting;
-
- let billingSettings: BillingSettings = null;
- if (content.type === BillingSettingsType.STRIPE) {
- billingSettings = {
- identifier: TenantComponents.BILLING,
- type: BillingSettingsType.STRIPE,
- id, sensitiveData, backupSensitiveData, category,
- createdBy, createdOn, lastChangedBy, lastChangedOn,
- billing: content.billing,
- stripe: content.stripe
- };
- }
- return billingSettings;
- }
-}

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';
@@ -7,6 +8,8 @@ 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';
@@ -234,4 +237,61 @@ export default class BillingStorage {
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteInvoice', uniqueTimerID, { id });
}
+
+ // ------------------------------------------------------------------------------------------
+ // BILLING SETTINGS
+ // ------------------------------------------------------------------------------------------
+ public static async getBillingSettings(tenantID: string): Promise<BillingSettings[]> {
+ const settings = await SettingStorage.getSettings(tenantID, { identifier: TenantComponents.BILLING }, Constants.DB_PARAMS_SINGLE_RECORD);
+ if (settings && settings.count > 0) {
+ const allBillingSettings: BillingSettings[] = settings.result.map((setting) => BillingStorage.convertToBillingSettings(setting));
+ return allBillingSettings;
+ }
+ return null;
+ }
+
+ public static async getBillingSetting(tenantID: string, settingID: string): Promise<BillingSettings> {
+ const settings = await SettingStorage.getSettings(tenantID, { settingID }, Constants.DB_PARAMS_SINGLE_RECORD);
+ if (settings?.result?.[0]?.content) {
+ const setting: SettingDB = settings.result[0];
+ 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, category,
+ createdBy, createdOn, lastChangedBy, lastChangedOn,
+ content: {
+ type,
+ billing,
+ stripe,
+ },
+ };
+ return SettingStorage.saveSettings(tenantID, setting);
+ }
+
+ private static convertToBillingSettings(setting: SettingDB): BillingSettings {
+ const { id, sensitiveData, backupSensitiveData, category } = setting;
+ const { createdBy, createdOn, lastChangedBy, lastChangedOn } = setting;
+ const { content } = setting;
+
+ let billingSettings: BillingSettings = null;
+ if (content.type === BillingSettingsType.STRIPE) {
+ billingSettings = {
+ identifier: TenantComponents.BILLING,
+ type: BillingSettingsType.STRIPE,
+ id, sensitiveData, backupSensitiveData, category,
+ createdBy, createdOn, lastChangedBy, lastChangedOn,
+ billing: content.billing,
+ stripe: content.stripe
+ };
+ }
+ return billingSettings;
+ }
+
}

MODIFY
test/api/context/BillingContext.ts
test/api/context/BillingContext.ts
@@ -2,7 +2,7 @@ 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 BillingStorage from '../../../src/storage/mongodb/BillingStorage';
import Constants from '../../../src/utils/Constants';
import ContextDefinition from './ContextDefinition';
import Cypher from '../../../src/utils/Cypher';
@@ -67,12 +67,12 @@ export default class BillingContext {
private async saveBillingSettings(billingSettings: BillingSettings) {
// TODO - rethink that part
- const allBillingSettings = await BillingSettingStorage.getBillingSettings(this.tenantContext.getTenant().id);
+ const allBillingSettings = await BillingStorage.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 BillingSettingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
+ await BillingStorage.saveBillingSetting(this.tenantContext.getTenant().id, tenantBillingSettings);
}
}

billing - BillingStorage and BillingSettingStorage merged