pricing - entityID / entityType
MODIFY
src/integration/pricing/PricingEngine.ts
src/integration/pricing/PricingEngine.ts
@@ -52,8 +52,8 @@ export default class PricingEngine {
static async getPricingModel4Entity(tenant: Tenant, entityID: string): Promise<PricingModel> {
if (entityID) {
- const contextIDs = [ entityID ];
- const pricingModelResults = await PricingStorage.getPricingModels(tenant, { contextIDs }, { limit: 1, skip: 0, sort: { createdOn: -1 } });
+ const entityIDs = [ entityID ];
+ const pricingModelResults = await PricingStorage.getPricingModels(tenant, { entityIDs }, { limit: 1, skip: 0, sort: { createdOn: -1 } });
if (pricingModelResults.count > 0) {
// ---------------------------------------------------------------------
// TODO - First implementation: we simply return the latest created one

MODIFY
src/server/rest/v1/service/PricingService.ts
src/server/rest/v1/service/PricingService.ts
@@ -147,7 +147,8 @@ export default class PricingService {
const pricingModel = await UtilsService.checkAndGetPricingModelAuthorization(
req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest as PricingModel);
// Update
- pricingModel.contextID = filteredRequest.contextID;
+ pricingModel.entityID = filteredRequest.entityID;
+ pricingModel.entityType = filteredRequest.entityType;
pricingModel.pricingDefinitions = filteredRequest.pricingDefinitions;
pricingModel.lastChangedBy = { 'id': req.user.id };
pricingModel.lastChangedOn = new Date();

MODIFY
src/server/rest/v1/service/security/PricingSecurity.ts
src/server/rest/v1/service/security/PricingSecurity.ts
@@ -39,7 +39,8 @@ export default class PricingSecurity {
public static _filterPricingModelRequest(request: any): Partial<PricingModel> {
const filteredRequest = {
- contextID: sanitize(request.contextID),
+ entityID: sanitize(request.entityID),
+ entityType: sanitize(request.entityType),
// TODO - not yet sanitized properly
pricingDefinitions: sanitize(request.pricingDefinitions)
} as Partial<PricingModel>;

MODIFY
src/storage/mongodb/PricingStorage.ts
src/storage/mongodb/PricingStorage.ts
@@ -21,7 +21,8 @@ export default class PricingStorage {
DatabaseUtils.checkTenantObject(tenant);
const pricingModelMDB = {
_id: pricingModel.id ? DatabaseUtils.convertToObjectID(pricingModel.id) : new ObjectId(),
- contextID: pricingModel.contextID,
+ entityID: pricingModel.entityID,
+ entityType: pricingModel.entityType,
pricingDefinitions: pricingModel.pricingDefinitions, // TODO - check here some data consistency
};
// Check Created/Last Changed By
@@ -52,10 +53,10 @@ export default class PricingStorage {
}
public static async getPricingModel(tenant: Tenant, id: string,
- params: { contextIDs?: string[]; } = {}, projectFields?: string[]): Promise<PricingModel> {
+ params: { entityIDs?: string[]; } = {}, projectFields?: string[]): Promise<PricingModel> {
const pricingModelMDB = await PricingStorage.getPricingModels(tenant, {
pricingModelIDs: [id],
- contextIDs: params.contextIDs,
+ entityIDs: params.entityIDs,
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return pricingModelMDB.count === 1 ? pricingModelMDB.result[0] : null;
}
@@ -63,7 +64,7 @@ export default class PricingStorage {
public static async getPricingModels(tenant: Tenant,
params: {
pricingModelIDs?: string[],
- contextIDs?: string[];
+ entityIDs?: string[];
},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<PricingModel>> {
const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getPricingModels');
@@ -85,8 +86,8 @@ export default class PricingStorage {
};
}
// Context IDs
- if (!Utils.isEmptyArray(params.contextIDs)) {
- filters.contextID = { $in: params.contextIDs };
+ if (!Utils.isEmptyArray(params.entityIDs)) {
+ filters.contextID = { $in: params.entityIDs };
}
// Remove deleted
filters.deleted = { '$ne': true };

MODIFY
src/types/Pricing.ts
src/types/Pricing.ts
@@ -3,7 +3,8 @@ import CreatedUpdatedProps from './CreatedUpdatedProps';
export default interface PricingModel extends CreatedUpdatedProps, AuthorizationActions {
id: string;
- contextID: string; // id of the entity the pricing definition belongs to!
+ entityID: string; // id of the entity the pricing definition belongs to!
+ entityType: string; // Type of the entity this model belongs to
pricingDefinitions: PricingDefinition[];
}

MODIFY
test/api/BillingStripeTestData.ts
test/api/BillingStripeTestData.ts
@@ -433,7 +433,8 @@ export default class StripeIntegrationTestData {
public async checkPricingModel(): Promise<void> {
const initialPricingModel: Partial<PricingModel> = {
- contextID: null, // a pricing model for the tenant
+ entityID: null, // a pricing model for the tenant
+ entityType: null,
pricingDefinitions: []
};
let response = await this.adminUserService.pricingApi.createPricingModel(initialPricingModel);

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -2,7 +2,7 @@ import AsyncTask, { AsyncTaskStatus } from '../../src/types/AsyncTask';
import { BillingChargeInvoiceAction, BillingDataTransactionStop, BillingInvoiceStatus, BillingStatus, BillingUser } from '../../src/types/Billing';
import { BillingSettings, BillingSettingsType, SettingDB } from '../../src/types/Setting';
import FeatureToggles, { Feature } from '../../src/utils/FeatureToggles';
-import PricingModel, { PricingDefinition, PricingDimension, PricingRestriction } from '../../src/types/Pricing';
+import PricingModel, { PricingDefinition } from '../../src/types/Pricing';
import chai, { assert, expect } from 'chai';
import AsyncTaskStorage from '../../src/storage/mongodb/AsyncTaskStorage';
@@ -13,6 +13,7 @@ import ContextDefinition from './context/ContextDefinition';
import ContextProvider from './context/ContextProvider';
import Cypher from '../../src/utils/Cypher';
import { DataResult } from '../../src/types/DataResult';
+import { Entity } from '../../src/types/Authorization';
import Factory from '../factories/Factory';
import MongoDBStorage from '../../src/storage/mongodb/MongoDBStorage';
import { ObjectId } from 'mongodb';
@@ -257,7 +258,8 @@ class TestData {
public async createTariff4Company(companyID: string): Promise<void> {
const initialPricingModel: Partial<PricingModel> = {
- contextID: companyID, // a pricing model for the company
+ entityID: companyID, // a pricing model for the Company
+ entityType: Entity.COMPANY,
pricingDefinitions: []
};
let response = await this.adminUserService.pricingApi.createPricingModel(initialPricingModel);
@@ -303,7 +305,8 @@ class TestData {
public async createTariff4Site(siteID: string): Promise<void> {
const initialPricingModel: Partial<PricingModel> = {
- contextID: siteID, // a pricing model for the site
+ entityID: siteID, // a pricing model for the site
+ entityType: Entity.SITE,
pricingDefinitions: []
};
let response = await this.adminUserService.pricingApi.createPricingModel(initialPricingModel);

pricing - entityID / entityType