Migrated CPO Pricing endpoint
MODIFY
src/server/ocpi/OCPIUtils.ts
src/server/ocpi/OCPIUtils.ts
@@ -430,9 +430,17 @@ export default class OCPIUtils {
const tariff = {} as OCPITariff;
tariff.id = '1';
tariff.currency = simplePricingSetting.currency;
- tariff.elements[0].price_components[0].type = OCPITariffDimensionType.TIME;
- tariff.elements[0].price_components[0].price = simplePricingSetting.price;
- tariff.elements[0].price_components[0].step_size = 60;
+ tariff.elements = [
+ {
+ price_components: [
+ {
+ type: OCPITariffDimensionType.TIME,
+ price: simplePricingSetting.price,
+ step_size: 60,
+ }
+ ]
+ }
+ ];
tariff.last_updated = simplePricingSetting.last_updated;
return tariff;
}

MODIFY
src/server/ocpi/router/cpo/CPORouter.ts
src/server/ocpi/router/cpo/CPORouter.ts
@@ -5,6 +5,7 @@ import express, { NextFunction, Request, Response } from 'express';
import CPOCdrsRouterV211 from './V2.1.1/CPOCdrsRouterV211';
import CPOCommandsRouterV211 from './V2.1.1/CPOCommandsRouterV211';
import CPOLocationsRouterV211 from './V2.1.1/CPOLocationsRouterV211';
+import CPOTariffsRouterV211 from './V2.1.1/CPOTariffsRouterV211';
import CPOTokensRouterV211 from './V2.1.1/CPOTokensRouterV211';
import CPOVersionsService from '../../service/cpo/CPOVersionsService';
import RouterUtils from '../../../../utils/RouterUtils';
@@ -34,6 +35,7 @@ export default class CPORouter {
new CPOTokensRouterV211().buildRoutes(),
new CPOCdrsRouterV211().buildRoutes(),
new CPOCommandsRouterV211().buildRoutes(),
+ new CPOTariffsRouterV211().buildRoutes(),
]);
}
}

ADD
src/server/ocpi/router/cpo/V2.1.1/CPOTariffsRouterV211.ts
+import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
+/* eslint-disable @typescript-eslint/no-misused-promises */
+import express, { NextFunction, Request, Response } from 'express';
+
+import CPOTariffsService from '../../../service/cpo/v2.1.1/CPOTariffsService';
+import RouterUtils from '../../../../../utils/RouterUtils';
+
+export default class CPOTariffsRouterV211 {
+ private router: express.Router;
+
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteTariffs();
+ return this.router;
+ }
+
+ protected buildRouteTariffs(): void {
+ this.router.get(`/${OCPIServerRoute.OCPI_TARIFFS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(CPOTariffsService.handleGetTariffs.bind(this), ServerAction.OCPI_CPO_GET_TARIFFS, req, res, next);
+ });
+ }
+}

ADD
src/server/ocpi/service/cpo/v2.1.1/CPOTariffsService.ts
+import { NextFunction, Request, Response } from 'express';
+import { OCPITariff, OCPITariffDimensionType } from '../../../../../types/ocpi/OCPITariff';
+import { PricingSettings, PricingSettingsType } from '../../../../../types/Setting';
+
+import AppError from '../../../../../exception/AppError';
+import Constants from '../../../../../utils/Constants';
+import { DataResult } from '../../../../../types/DataResult';
+import { HTTPError } from '../../../../../types/HTTPError';
+import { OCPIStatusCode } from '../../../../../types/ocpi/OCPIStatusCode';
+import OCPIUtils from '../../../OCPIUtils';
+import OCPIUtilsService from '../../OCPIUtilsService';
+import { ServerAction } from '../../../../../types/Server';
+import SettingStorage from '../../../../../storage/mongodb/SettingStorage';
+import Tenant from '../../../../../types/Tenant';
+import Utils from '../../../../../utils/Utils';
+
+const MODULE_NAME = 'CPOTariffsService';
+
+export default class CPOTariffsService {
+ public static async handleGetTariffs(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
+ // Get query parameters
+ const offset = (req.query.offset) ? Utils.convertToInt(req.query.offset) : 0;
+ const limit = (req.query.limit && Utils.convertToInt(req.query.limit) < Constants.OCPI_RECORDS_LIMIT) ? Utils.convertToInt(req.query.limit) : Constants.OCPI_RECORDS_LIMIT;
+ // Get all tariffs
+ const tariffs = await CPOTariffsService.getAllTariffs(tenant, limit, offset, Utils.convertToDate(req.query.date_from), Utils.convertToDate(req.query.date_to));
+ if (tariffs.count === 0) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleGetTariffs', action,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: 'No OCPI Tariffs found',
+ ocpiError: OCPIStatusCode.CODE_3000_GENERIC_SERVER_ERROR
+ });
+ }
+ // Set header
+ res.set({
+ 'X-Total-Count': tariffs.count,
+ 'X-Limit': Constants.OCPI_RECORDS_LIMIT
+ });
+ // Return next link
+ const nextUrl = OCPIUtils.buildNextUrl(req, OCPIUtilsService.getBaseUrl(req), offset, limit, tariffs.count);
+ if (nextUrl) {
+ res.links({
+ next: nextUrl
+ });
+ }
+ res.json(OCPIUtils.success(tariffs.result));
+ next();
+ }
+
+ private static async getAllTariffs(tenant: Tenant, limit: number, skip: number, dateFrom?: Date, dateTo?: Date): Promise<DataResult<OCPITariff>> {
+ // Result
+ const tariffs: OCPITariff[] = [];
+ let tariff: OCPITariff;
+ if (tenant.components?.pricing?.active) {
+ // Get simple pricing settings
+ const pricingSettings = await SettingStorage.getPricingSettings(tenant, limit, skip, dateFrom, dateTo);
+ if (pricingSettings.type === PricingSettingsType.SIMPLE && pricingSettings.simple) {
+ tariff = OCPIUtils.convertSimplePricingSetting2OCPITariff(pricingSettings.simple);
+ if (tariff.currency && tariff.elements[0].price_components[0].price > 0) {
+ tariffs.push(tariff);
+ } else if (tariff.currency && tariff.elements[0].price_components[0].price === 0) {
+ tariff = this.convertPricingSettings2ZeroFlatTariff(pricingSettings);
+ tariffs.push(tariff);
+ }
+ }
+ }
+ return {
+ count: tariffs.length,
+ result: tariffs
+ };
+ }
+
+ private static convertPricingSettings2ZeroFlatTariff(pricingSettings: PricingSettings): OCPITariff {
+ let tariff: OCPITariff;
+ tariff.id = '1';
+ tariff.elements = [
+ {
+ price_components: [
+ {
+ type: OCPITariffDimensionType.FLAT,
+ price: 0,
+ step_size: 0,
+ }
+ ]
+ }
+ ];
+ switch (pricingSettings.type) {
+ case PricingSettingsType.SIMPLE:
+ tariff.currency = pricingSettings.simple.currency;
+ tariff.last_updated = pricingSettings.simple.last_updated;
+ break;
+ default:
+ tariff.currency = 'EUR';
+ tariff.last_updated = new Date();
+ break;
+ }
+ return tariff;
+ }
+}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -676,6 +676,7 @@ export enum OCPIServerRoute {
OCPI_CDRS = 'cdrs',
OCPI_COMMANDS = 'commands',
OCPI_TOKENS = 'tokens',
+ OCPI_TARIFFS = 'tariffs',
}
export enum ServerProtocol {

Migrated CPO Pricing endpoint