billing - settings - REST end points (draft)
MODIFY
src/server/rest/v1/router/api/BillingSettingRouter.ts
src/server/rest/v1/router/api/BillingSettingRouter.ts
@@ -30,7 +30,7 @@ export default class SettingRouter {
protected buildRouteBillingSetting(): void {
this.router.get(`/${ServerRoute.REST_BILLING_SETTING}`, (req: Request, res: Response, next: NextFunction) => {
- void RouterUtils.handleServerAction(BillingSettingService.handleUpdateBillingSetting.bind(this), ServerAction.SETTINGS, req, res, next);
+ void RouterUtils.handleServerAction(BillingSettingService.handleGetBillingSetting.bind(this), ServerAction.SETTINGS, req, res, next);
});
}

MODIFY
src/server/rest/v1/service/BillingSettingService.ts
src/server/rest/v1/service/BillingSettingService.ts
@@ -14,9 +14,21 @@ const MODULE_NAME = 'BillingSettingService';
export default class BillingSettingService {
- // eslint-disable-next-line @typescript-eslint/require-await
public static async handleGetBillingSettings(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
- res.sendStatus(StatusCodes.NOT_IMPLEMENTED);
+ if (!await Authorizations.canListSettings(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: req.user,
+ action: Action.READ, entity: Entity.SETTING,
+ module: MODULE_NAME, method: 'handleGetBillingSetting',
+ });
+ }
+ const billingSettings: BillingSettings = await BillingSettingStorage.getBillingSettings(req.user.tenantID, true);
+ if (billingSettings) {
+ res.json(billingSettings);
+ } else {
+ res.sendStatus(StatusCodes.NOT_FOUND);
+ }
next();
}
@@ -29,8 +41,13 @@ export default class BillingSettingService {
module: MODULE_NAME, method: 'handleGetBillingSetting',
});
}
- const billingSettings: BillingSettings = await BillingSettingStorage.getBillingSettings(req.user.tenantID, true);
+ const settingID = req.params.id;
+ const billingSettings: BillingSettings = await BillingSettingStorage.getBillingSetting(req.user.tenantID, settingID, true);
+ if (billingSettings) {
res.json(billingSettings);
+ } else {
+ res.sendStatus(StatusCodes.NOT_FOUND);
+ }
next();
}
@@ -43,11 +60,11 @@ export default class BillingSettingService {
module: MODULE_NAME, method: 'handleUpdateBillingSetting',
});
}
- res.json(StatusCodes.NOT_IMPLEMENTED);
+ res.sendStatus(StatusCodes.NOT_IMPLEMENTED);
// const billingSettings: BillingSettings = null;
// await BillingSettingStorage.saveBillingSettings(req.user.tenantID, billingSettings);
// Ok
- res.json(Constants.REST_RESPONSE_SUCCESS);
+ // res.json(Constants.REST_RESPONSE_SUCCESS);
next();
}

MODIFY
src/storage/mongodb/BillingSettingStorage.ts
src/storage/mongodb/BillingSettingStorage.ts
@@ -9,24 +9,16 @@ import TenantComponents from '../../types/TenantComponents';
const MODULE_NAME = 'BillingSettingStorage';
export default class BillingSettingStorage {
- public static async getBillingSettings(tenantID: string, encryptSensitiveData = false): Promise<BillingSettings> {
- const billingSettings = {
- identifier: TenantComponents.BILLING,
- } as BillingSettings;
- const settings = await SettingStorage.getSettings(tenantID, { identifier: TenantComponents.BILLING }, Constants.DB_PARAMS_SINGLE_RECORD);
+ public static alterBillingSettings(billingSettings: BillingSettings, setting: SettingDB, encryptSensitiveData: boolean): BillingSettings {
// Encrypt Sensitive Data
if (encryptSensitiveData) {
- for (const setting of settings.result) {
- // Hash sensitive data before being sent to the front end
Cypher.hashSensitiveDataInJSON(setting);
}
- }
- // TODO - To be clarified - Here we only consider the first one
- 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;
+ // Extract the data
+ const config = setting.content;
+ billingSettings.id = setting.id;
+ billingSettings.sensitiveData = setting.sensitiveData;
+ billingSettings.backupSensitiveData = setting.backupSensitiveData;
// Billing Common Properties
if (config.billing) {
const { isTransactionBillingActivated, immediateBillingAllowed, periodicBillingAllowed, taxID, usersLastSynchronizedOn } = config.billing;
@@ -50,6 +42,29 @@ export default class BillingSettingStorage {
}
return billingSettings;
}
+
+ public static async getBillingSettings(tenantID: string, encryptSensitiveData = false): Promise<BillingSettings> {
+ const settings = await SettingStorage.getSettings(tenantID, { identifier: TenantComponents.BILLING }, Constants.DB_PARAMS_SINGLE_RECORD);
+ if (settings?.result?.[0]?.content) {
+ const billingSettings = {
+ identifier: TenantComponents.BILLING,
+ } as BillingSettings;
+ BillingSettingStorage.alterBillingSettings(billingSettings, settings.result[0], encryptSensitiveData);
+ return billingSettings;
+ }
+ return null;
+ }
+
+ public static async getBillingSetting(tenantID: string, settingID: string, encryptSensitiveData = false): Promise<BillingSettings> {
+ const settings = await SettingStorage.getSettings(tenantID, { settingID }, Constants.DB_PARAMS_SINGLE_RECORD);
+ if (settings?.result?.[0]?.content) {
+ const billingSettings = {
+ identifier: TenantComponents.BILLING,
+ } as BillingSettings;
+ BillingSettingStorage.alterBillingSettings(billingSettings, settings.result[0], encryptSensitiveData);
+ return billingSettings;
+ }
+ return null;
}
public static async saveBillingSettings(tenantID: string, billingSettingsToSave: BillingSettings): Promise<string> {

billing - settings - REST end points (draft)