pricing - Buil-in Pricing Integration
MODIFY
src/integration/pricing/PricingFactory.ts
src/integration/pricing/PricingFactory.ts
+import FeatureToggles, { Feature } from '../../utils/FeatureToggles';
import { PricingSetting, PricingSettingsType } from '../../types/Setting';
+import BuiltInPricingIntegration from './simple-pricing/BuiltInPricingIntegration';
import ConvergentChargingPricingIntegration from './export/convergent-charging';
import DummyPricingIntegration from './dummy/DummyPricingIntegration';
import PricingIntegration from './PricingIntegration';
@@ -27,7 +29,13 @@ export default class PricingFactory {
// Simple Pricing
case PricingSettingsType.SIMPLE:
// Simple Pricing implementation
+ if (FeatureToggles.isFeatureActive(Feature.PRICING_NEW_MODEL)) {
pricingIntegrationImpl = new SimplePricingIntegration(tenant, pricingSetting.simple);
+ } else {
+ // TODO - to be clarified - feature hidden behind a feature toggle for now!
+ // Do we need a dedicated PricingSettingsType?
+ pricingIntegrationImpl = new BuiltInPricingIntegration(tenant, pricingSetting.simple);
+ }
break;
default:
pricingIntegrationImpl = null;

ADD
src/integration/pricing/simple-pricing/BuiltInPricingIntegration.ts
+import { PricedConsumption, PricingSource } from '../../../types/Pricing';
+
+import Consumption from '../../../types/Consumption';
+import PricingIntegration from '../PricingIntegration';
+import { SimplePricingSetting } from '../../../types/Setting';
+import Tenant from '../../../types/Tenant';
+import Transaction from '../../../types/Transaction';
+import Utils from '../../../utils/Utils';
+
+export default class SimplePricingIntegration extends PricingIntegration<SimplePricingSetting> {
+ constructor(tenant: Tenant, readonly settings: SimplePricingSetting) {
+ super(tenant, settings);
+ }
+
+ public async startSession(transaction: Transaction, consumptionData: Consumption): Promise<PricedConsumption> {
+ return this.computePrice(transaction, consumptionData);
+ }
+
+ public async updateSession(transaction: Transaction, consumptionData: Consumption): Promise<PricedConsumption> {
+ return this.computePrice(transaction, consumptionData);
+ }
+
+ public async stopSession(transaction: Transaction, consumptionData: Consumption): Promise<PricedConsumption> {
+ return this.computePrice(transaction, consumptionData);
+ }
+
+ // eslint-disable-next-line @typescript-eslint/require-await
+ private async computePrice(transaction: Transaction, consumptionData: Consumption): Promise<PricedConsumption> {
+ let amount: number;
+ let roundedAmount: number;
+ if (consumptionData.consumptionWh && typeof consumptionData.consumptionWh === 'number') {
+ amount = Utils.computeSimplePrice(this.settings.price, consumptionData.consumptionWh);
+ roundedAmount = Utils.truncTo(amount, 2);
+ } else {
+ amount = 0;
+ roundedAmount = 0;
+ }
+ const pricedConsumption: PricedConsumption = {
+ pricingSource: PricingSource.SIMPLE,
+ amount: amount,
+ roundedAmount: roundedAmount,
+ currencyCode: this.settings.currency,
+ cumulatedAmount: transaction.currentCumulatedPrice ? Utils.createDecimal(transaction.currentCumulatedPrice).plus(amount).toNumber() : amount
+ };
+ return pricedConsumption;
+ }
+}

MODIFY
src/types/Transaction.ts
src/types/Transaction.ts
@@ -82,7 +82,7 @@ export default interface Transaction extends AbstractCurrentConsumption {
roundedPrice?: number;
priceUnit?: string;
pricingSource?: string;
- pricing?: PricingModel,
+ pricingModel?: PricingModel,
stateOfCharge: number;
timezone: string;
currentTimestamp?: Date;

MODIFY
src/utils/FeatureToggles.ts
src/utils/FeatureToggles.ts
@@ -4,7 +4,7 @@ export enum Feature {
BILLING_SYNC_USERS,
BILLING_CHECK_THRESHOLD_ON_STOP,
BILLING_PREVENT_CUSTOMER_DELETION,
- PRINCING_NEW_MODEL
+ PRICING_NEW_MODEL
}
export default class FeatureToggles {
@@ -13,7 +13,7 @@ export default class FeatureToggles {
// Feature.BILLING_SYNC_USERS, - When switched OFF the sync of the user should be implicit (LAZY mode)
Feature.BILLING_CHECK_THRESHOLD_ON_STOP,
Feature.BILLING_PREVENT_CUSTOMER_DELETION,
- Feature.PRINCING_NEW_MODEL
+ Feature.PRICING_NEW_MODEL
];
// Check whether the feature is active or not!

pricing - Buil-in Pricing Integration