Migrated EMSP Cdrs endpoint
MODIFY
src/async-task/tasks/ocpi/OCPIPullCdrsAsyncTask.ts
src/async-task/tasks/ocpi/OCPIPullCdrsAsyncTask.ts
@@ -35,7 +35,7 @@ export default class OCPIPullCdrsAsyncTask extends AbstractAsyncTask {
}
}
} catch (error) {
- await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_CDRS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_CDR, error);
}
}
}

MODIFY
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/EMSPService.ts
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/EMSPService.ts
import AbstractOCPIService from '../../AbstractOCPIService';
-import EMSPCdrsEndpoint from './emsp/EMSPCdrsEndpoint';
import EMSPCommandsEndpoint from './emsp/EMSPCommandsEndpoint';
import EMSPTariffsEndpoint from './emsp/EMSPTariffsEndpoint';
import OCPIServiceConfiguration from '../../../../types/configuration/OCPIServiceConfiguration';
@@ -10,7 +9,6 @@ export default class EMSPService extends AbstractOCPIService {
public constructor(ocpiRestConfig: OCPIServiceConfiguration) {
super(ocpiRestConfig, EMSPService.PATH, EMSPService.VERSION);
- this.registerEndpoint(new EMSPCdrsEndpoint(this));
this.registerEndpoint(new EMSPCommandsEndpoint(this));
this.registerEndpoint(new EMSPTariffsEndpoint(this));
}

DELETE
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/emsp/EMSPCdrsEndpoint.ts
-import { NextFunction, Request, Response } from 'express';
-
-import AbstractEndpoint from '../../AbstractEndpoint';
-import AbstractOCPIService from '../../../AbstractOCPIService';
-import AppError from '../../../../../exception/AppError';
-import { HTTPError } from '../../../../../types/HTTPError';
-import { OCPICdr } from '../../../../../types/ocpi/OCPICdr';
-import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import { OCPIResponse } from '../../../../../types/ocpi/OCPIResponse';
-import { OCPIStatusCode } from '../../../../../types/ocpi/OCPIStatusCode';
-import OCPIUtils from '../../../OCPIUtils';
-import OCPIUtilsService from '../../../service/OCPIUtilsService';
-import { ServerAction } from '../../../../../types/Server';
-import Tenant from '../../../../../types/Tenant';
-import Transaction from '../../../../../types/Transaction';
-import TransactionStorage from '../../../../../storage/mongodb/TransactionStorage';
-
-const MODULE_NAME = 'EMSPCdrsEndpoint';
-
-export default class EMSPCdrsEndpoint extends AbstractEndpoint {
- public constructor(ocpiService: AbstractOCPIService) {
- super(ocpiService, 'cdrs');
- }
-
- public async process(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
- switch (req.method) {
- case 'GET':
- return this.getCdrRequest(req, res, next, tenant);
- case 'POST':
- return this.postCdrRequest(req, res, next, tenant);
- }
- }
-
- private async getCdrRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
- const urlSegment = req.path.substring(1).split('/');
- // Remove action
- urlSegment.shift();
- // Get filters
- const id = urlSegment.shift();
- if (!id) {
- throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_CDRS,
- module: MODULE_NAME, method: 'getCdrRequest',
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Missing request parameters',
- ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
- });
- }
- const transaction: Transaction = await TransactionStorage.getOCPITransactionBySessionID(tenant, id);
- if (!transaction) {
- throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_CDRS,
- module: MODULE_NAME, method: 'getCdrRequest',
- errorCode: HTTPError.GENERAL_ERROR,
- message: `No Transaction found for CDR ID '${id}'`,
- ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
- });
- }
- if (!transaction.ocpiData?.cdr) {
- throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_CDRS,
- module: MODULE_NAME, method: 'getCdrRequest',
- errorCode: HTTPError.GENERAL_ERROR,
- message: `No CDR found in Transaction ID '${transaction.id}'`,
- ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR,
- detailedMessages: { transaction }
- });
- }
- return OCPIUtils.success(transaction.ocpiData.cdr);
- }
-
- private async postCdrRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
- const cdr: OCPICdr = req.body as OCPICdr;
- await OCPIUtilsService.processEmspCdr(tenant, cdr, ServerAction.OCPI_EMSP_CREATE_CDR);
- res.setHeader('Location', OCPIUtils.buildLocationUrl(req, this.getBaseUrl(req), cdr.id));
- return OCPIUtils.success({});
- }
-}

MODIFY
src/server/ocpi/router/emsp/EMSPRouter.ts
src/server/ocpi/router/emsp/EMSPRouter.ts
@@ -3,6 +3,7 @@ import { OCPIServerRoute, ServerAction } from '../../../../types/Server';
import express, { NextFunction, Request, Response } from 'express';
import CPOEMSPCredentialsRouterV211 from '../common/V2.1.1/CPOEMSPCredentialsRouterV211';
+import EMSPCdrsRouterV211 from './V2.1.1/EMSPCdrsRouterV211';
import EMSPLocationsRouterV211 from './V2.1.1/EMSPLocationsRouterV211';
import EMSPSessionsRouterV211 from './V2.1.1/EMSPSessionsRouterV211';
import EMSPTokensRouterV211 from './V2.1.1/EMSPTokensRouterV211';
@@ -33,6 +34,7 @@ export default class EMSPRouter {
new EMSPTokensRouterV211().buildRoutes(),
new EMSPLocationsRouterV211().buildRoutes(),
new EMSPSessionsRouterV211().buildRoutes(),
+ new EMSPCdrsRouterV211().buildRoutes(),
new CPOEMSPCredentialsRouterV211().buildRoutes(),
]);
}

ADD
src/server/ocpi/router/emsp/V2.1.1/EMSPCdrsRouterV211.ts
+/* eslint-disable @typescript-eslint/no-misused-promises */
+import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
+import express, { NextFunction, Request, Response } from 'express';
+
+import EMSPCdrsService from '../../../service/emsp/v2.1.1/EMSPCdrsService';
+import RouterUtils from '../../../../../utils/RouterUtils';
+
+export default class EMSPCdrsRouterV211 {
+ private router: express.Router;
+
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteGetCdr();
+ this.buildRoutePostCdr();
+ return this.router;
+ }
+
+ protected buildRouteGetCdr(): void {
+ this.router.get(`/${OCPIServerRoute.OCPI_CDRS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPCdrsService.handleGetCdr.bind(this), ServerAction.OCPI_EMSP_GET_CDR, req, res, next);
+ });
+ }
+
+ protected buildRoutePostCdr(): void {
+ this.router.post(`/${OCPIServerRoute.OCPI_CDRS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPCdrsService.handlePostCdr.bind(this), ServerAction.OCPI_EMSP_CREATE_CDR, req, res, next);
+ });
+ }
+}

MODIFY
src/server/ocpi/router/emsp/V2.1.1/EMSPSessionsRouterV211.ts
src/server/ocpi/router/emsp/V2.1.1/EMSPSessionsRouterV211.ts
-import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
/* eslint-disable @typescript-eslint/no-misused-promises */
+import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
import express, { NextFunction, Request, Response } from 'express';
import EMSPSessionsService from '../../../service/emsp/v2.1.1/EMSPSessionsService';

ADD
src/server/ocpi/service/emsp/v2.1.1/EMSPCdrsService.ts
+import { NextFunction, Request, Response } from 'express';
+
+import AppError from '../../../../../exception/AppError';
+import { HTTPError } from '../../../../../types/HTTPError';
+import { OCPICdr } from '../../../../../types/ocpi/OCPICdr';
+import { OCPIStatusCode } from '../../../../../types/ocpi/OCPIStatusCode';
+import OCPIUtils from '../../../OCPIUtils';
+import OCPIUtilsService from '../../OCPIUtilsService';
+import { ServerAction } from '../../../../../types/Server';
+import Transaction from '../../../../../types/Transaction';
+import TransactionStorage from '../../../../../storage/mongodb/TransactionStorage';
+
+const MODULE_NAME = 'EMSPCdrsService';
+
+export default class EMSPCdrsService {
+ public static async handleGetCdr(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
+ const urlSegment = req.path.substring(1).split('/');
+ // Remove action
+ urlSegment.shift();
+ // Get filters
+ const id = urlSegment.shift();
+ if (!id) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleGetCdr', action,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: 'Missing request parameters',
+ ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
+ });
+ }
+ const transaction: Transaction = await TransactionStorage.getOCPITransactionBySessionID(tenant, id);
+ if (!transaction) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleGetCdr', action,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `No Transaction found for CDR ID '${id}'`,
+ ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
+ });
+ }
+ if (!transaction.ocpiData?.cdr) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleGetCdr', action,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `No CDR found in Transaction ID '${transaction.id}'`,
+ ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR,
+ detailedMessages: { transaction }
+ });
+ }
+ res.json(OCPIUtils.success(transaction.ocpiData.cdr));
+ next();
+ }
+
+ public static async handlePostCdr(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
+ const cdr: OCPICdr = req.body as OCPICdr;
+ await OCPIUtilsService.processEmspCdr(tenant, cdr, action);
+ res.setHeader('Location', OCPIUtils.buildLocationUrl(req, OCPIUtilsService.getBaseUrl(req), cdr.id));
+ res.json(OCPIUtils.success({}));
+ next();
+ }
+}

MODIFY
src/server/rest/v1/router/api/OCPIEndpointRouter.ts
src/server/rest/v1/router/api/OCPIEndpointRouter.ts
@@ -71,7 +71,7 @@ export default class OCPIEndpointRouter {
private buildRouteOcpiEndpointPullCDRs(): void {
this.router.put(`/${RESTServerRoute.REST_OCPI_ENDPOINT_PULL_CDRS}`, async (req: Request, res: Response, next: NextFunction) => {
req.body.id = req.params.id;
- await RouterUtils.handleServerAction(OCPIEndpointService.handlePullCdrsEndpoint.bind(this), ServerAction.OCPI_EMSP_GET_CDRS, req, res, next);
+ await RouterUtils.handleServerAction(OCPIEndpointService.handlePullCdrsEndpoint.bind(this), ServerAction.OCPI_EMSP_GET_CDR, req, res, next);
});
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -174,6 +174,7 @@ export enum ServerAction {
OCPI_EMSP_START_SESSION = 'OcpiEmspStartSession',
OCPI_EMSP_STOP_SESSION = 'OcpiEmspStopSession',
OCPI_EMSP_UPDATE_TOKENS = 'OcpiEmspUpdateTokens',
+ OCPI_EMSP_GET_CDR = 'OcpiEmspGetCdr',
OCPI_EMSP_GET_CDRS = 'OcpiEmspGetCdrs',
OCPI_EMSP_GET_TOKENS = 'OcpiEmspGetTokens',
OCPI_EMSP_CREATE_CDR = 'OcpiEmspCreateCdr',

Migrated EMSP Cdrs endpoint