pricing - some logs
MODIFY
src/integration/pricing/PricingEngine.ts
src/integration/pricing/PricingEngine.ts
@@ -5,6 +5,7 @@ import ChargingStation from '../../types/ChargingStation';
import Constants from '../../utils/Constants';
import Consumption from '../../types/Consumption';
import ConsumptionPricer from './ConsumptionPricer';
+import PricingHelper from './PricingHelper';
import PricingStorage from '../../storage/mongodb/PricingStorage';
import Tenant from '../../types/Tenant';
import Transaction from '../../types/Transaction';
@@ -33,6 +34,10 @@ export default class PricingEngine {
},
pricingDefinitions
};
+ await PricingHelper.logInfo(tenant, transaction, {
+ message: `Pricing context has been resolved - ${pricingDefinitions.length} pricing definitions have been found`,
+ detailedMessages: { resolvedPricingModel }
+ });
return Promise.resolve(resolvedPricingModel);
}
@@ -56,6 +61,10 @@ export default class PricingEngine {
const actualPricingDefinitions = pricingDefinitions.filter((pricingDefinition) =>
PricingEngine.checkStaticRestrictions(pricingDefinition, transaction, chargingStation)
);
+ await PricingHelper.logInfo(tenant, transaction, {
+ method: 'getPricingDefinitions4Entity',
+ message: `Pricing context resolution - ${actualPricingDefinitions.length || 0} pricing definitions found for entity '${entityID}'`
+ });
return actualPricingDefinitions || [];
}

MODIFY
src/integration/pricing/PricingHelper.ts
src/integration/pricing/PricingHelper.ts
+/* eslint-disable @typescript-eslint/member-ordering */
/* eslint-disable max-len */
import { DimensionType, PricedConsumptionData, PricedDimensionData, PricingDimensions } from '../../types/Pricing';
import Decimal from 'decimal.js';
+import { Log } from '../../types/Log';
+import Logging from '../../utils/Logging';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import Transaction from '../../types/Transaction';
import Utils from '../../utils/Utils';
export default class PricingHelper {
@@ -61,4 +67,34 @@ export default class PricingHelper {
accumulatedData[dimensionType].roundedAmount = Utils.createDecimal(accumulatedData[dimensionType].roundedAmount).plus(pricedData[dimensionType].roundedAmount).toNumber();
}
}
+
+ public static async logInfo(tenant: Tenant, transaction: Transaction, params: Partial<Log>): Promise<void> {
+ const commonProperties = PricingHelper.getLogCommonProperties(tenant, transaction);
+ await Logging.logInfo({
+ ...commonProperties,
+ ...params,
+ } as Log);
+ }
+
+ public static async logWarning(tenant: Tenant, transaction: Transaction, params: Partial<Log>): Promise<void> {
+ const commonProperties = PricingHelper.getLogCommonProperties(tenant, transaction);
+ await Logging.logWarning({
+ ...commonProperties,
+ ...params,
+ } as Log);
+ }
+
+ public static getLogCommonProperties(tenant: Tenant, transaction: Transaction): Partial<Log> {
+ return {
+ tenantID: tenant.id,
+ siteID: transaction.siteID,
+ siteAreaID: transaction.siteAreaID,
+ companyID: transaction.companyID,
+ chargingStationID: transaction.chargeBoxID,
+ source: transaction.chargeBoxID,
+ actionOnUser: transaction.user,
+ action: ServerAction.PRICING,
+ };
+ }
+
}

MODIFY
src/integration/pricing/simple-pricing/BuiltInPricingIntegration.ts
src/integration/pricing/simple-pricing/BuiltInPricingIntegration.ts
@@ -18,15 +18,27 @@ export default class BuiltInPricingIntegration extends PricingIntegration<Simple
}
public async startSession(transaction: Transaction, consumptionData: Consumption, chargingStation: ChargingStation): Promise<PricedConsumption> {
- return this.computePrice(transaction, consumptionData, chargingStation);
+ const pricedConsumption = await this.computePrice(transaction, consumptionData, chargingStation);
+ await PricingHelper.logInfo(this.tenant, transaction, {
+ message: `Session START - Transaction: ${transaction.id} - Accumulated amount: ${pricedConsumption.cumulatedRoundedAmount} ${pricedConsumption.currencyCode}`,
+ });
+ return pricedConsumption;
}
public async updateSession(transaction: Transaction, consumptionData: Consumption, chargingStation: ChargingStation): Promise<PricedConsumption> {
- return this.computePrice(transaction, consumptionData, chargingStation);
+ const pricedConsumption = await this.computePrice(transaction, consumptionData, chargingStation);
+ // await PricingHelper.logInfo(this.tenant, transaction, {
+ // message: `Session UPDATE - Transaction: ${transaction.id} - Accumulated amount: ${pricedConsumption.cumulatedRoundedAmount} ${pricedConsumption.currencyCode}`,
+ // });
+ return pricedConsumption;
}
public async stopSession(transaction: Transaction, consumptionData: Consumption, chargingStation: ChargingStation): Promise<PricedConsumption> {
- return this.computePrice(transaction, consumptionData, chargingStation);
+ const pricedConsumption = await this.computePrice(transaction, consumptionData, chargingStation);
+ await PricingHelper.logInfo(this.tenant, transaction, {
+ message: `Session STOP - Transaction: ${transaction.id} - Accumulated amount: ${pricedConsumption.cumulatedRoundedAmount} ${pricedConsumption.currencyCode}`,
+ });
+ return pricedConsumption;
}
private async computePrice(transaction: Transaction, consumptionData: Consumption, chargingStation: ChargingStation): Promise<PricedConsumption> {
@@ -37,19 +49,19 @@ export default class BuiltInPricingIntegration extends PricingIntegration<Simple
}
const pricingConsumptionData = PricingEngine.priceConsumption(this.tenant, pricingModel, consumptionData);
const { flatFee, energy, chargingTime, parkingTime } = pricingConsumptionData;
+ const currencyCode = this.settings.currency;
const amount = Utils.createDecimal(flatFee?.amount || 0).plus(energy?.amount || 0).plus(chargingTime?.amount || 0).plus(parkingTime?.amount || 0).toNumber();
const roundedAmount = Utils.createDecimal(flatFee?.roundedAmount || 0).plus(energy?.roundedAmount || 0).plus(chargingTime?.roundedAmount || 0).plus(parkingTime?.roundedAmount || 0).toNumber();
// Sum all dimensions
const pricedConsumption: PricedConsumption = {
pricingSource: PricingSource.SIMPLE,
pricingModel,
- amount: amount,
- roundedAmount: roundedAmount,
- currencyCode: this.settings.currency,
+ amount,
+ roundedAmount,
+ currencyCode,
cumulatedAmount: 0,
cumulatedRoundedAmount: 0,
};
-
if (!FeatureToggles.isFeatureActive(Feature.PRICING_NEW_MODEL)) {
throw new Error('Unexpected situation - this layer should not be called in that context');
} else {
@@ -57,7 +69,6 @@ export default class BuiltInPricingIntegration extends PricingIntegration<Simple
const { cumulatedAmount, cumulatedRoundedAmount } = this.computeCumulatedAmount(pricingModel);
pricedConsumption.cumulatedAmount = cumulatedAmount;
pricedConsumption.cumulatedRoundedAmount = cumulatedRoundedAmount;
-
}
return Promise.resolve(pricedConsumption);
}
@@ -76,6 +87,10 @@ export default class BuiltInPricingIntegration extends PricingIntegration<Simple
const resolvedPricingModel: ResolvedPricingModel = await PricingEngine.resolvePricingContext(tenant, transaction, chargingStation);
if (!resolvedPricingModel.pricingDefinitions?.length) {
resolvedPricingModel.pricingDefinitions = [ this.getDefaultPricingDefinition() ];
+ await PricingHelper.logWarning(tenant, transaction, {
+ message: 'No pricing definition found - Simple Pricing logic will be used as a fallback',
+ detailedMessages: { resolvedPricingModel }
+ });
}
return resolvedPricingModel;
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -438,6 +438,7 @@ export enum ServerAction {
BILLING_WEB_HOOK = 'BillingWebHook',
BILLING_TEST_DATA_CLEANUP = 'BillingTestDataCleanup',
+ PRICING = 'Pricing',
PRICING_DEFINITION = 'PricingDefinition',
PRICING_DEFINITIONS = 'PricingDefinitions',
PRICING_DEFINITION_CREATE = 'PricingDefinitionCreate',

pricing - some logs