Migrated CPO GetCdrs Endpoint
MODIFY
src/server/ocpi/OCPIServer.ts
src/server/ocpi/OCPIServer.ts
@@ -37,7 +37,7 @@ export default class OCPIServer {
this.expressApplication.use(Logging.traceExpressRequest.bind(this));
// Routers
this.expressApplication.use('/ocpi', new GlobalRouter().buildRoutes());
- // // New OCPI Services Instances
+
// const ocpiServices = new OCPIServices(this.ocpiRestConfig);
// // Register all services in express
// for (const ocpiService of ocpiServices.getOCPIServiceImplementations()) {

MODIFY
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/CPOService.ts
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/CPOService.ts
import AbstractOCPIService from '../../AbstractOCPIService';
-import CPOCdrsEndpoint from './cpo/CPOCdrsEndpoint';
import CPOCommandsEndpoint from './cpo/CPOCommandsEndpoint';
import CPOSessionsEndpoint from './cpo/CPOSessionsEndpoint';
import CPOTariffsEndpoint from './cpo/CPOTariffsEndpoint';
@@ -16,7 +15,6 @@ export default class CPOService extends AbstractOCPIService {
this.registerEndpoint(new CredentialsEndpoint(this));
this.registerEndpoint(new CPOCommandsEndpoint(this));
this.registerEndpoint(new CPOSessionsEndpoint(this));
- this.registerEndpoint(new CPOCdrsEndpoint(this));
this.registerEndpoint(new CPOTariffsEndpoint(this));
}
}

MODIFY
src/server/ocpi/router/cpo/CPORouter.ts
src/server/ocpi/router/cpo/CPORouter.ts
import { OCPIServerRoute, ServerAction } from '../../../../types/Server';
import express, { NextFunction, Request, Response } from 'express';
+import CPOCdrsRouterV211 from './V2.1.1/CPOCdrsRouterV211';
import CPOLocationsRouterV211 from './V2.1.1/CPOLocationsRouterV211';
import CPOTokensRouterV211 from './V2.1.1/CPOTokensRouterV211';
import CPOVersionsService from '../../service/cpo/CPOVersionsService';
@@ -30,6 +31,7 @@ export default class CPORouter {
this.router.use('/2.1.1', [
new CPOLocationsRouterV211().buildRoutes(),
new CPOTokensRouterV211().buildRoutes(),
+ new CPOCdrsRouterV211().buildRoutes(),
]);
}
}

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

MODIFY
src/server/ocpi/router/cpo/V2.1.1/CPOLocationsRouterV211.ts
src/server/ocpi/router/cpo/V2.1.1/CPOLocationsRouterV211.ts
@@ -13,11 +13,11 @@ export default class CPOLocationsRouterV211 {
}
public buildRoutes(): express.Router {
- this.buildRouteCpoLocations();
+ this.buildRouteLocations();
return this.router;
}
- protected buildRouteCpoLocations(): void {
+ protected buildRouteLocations(): void {
this.router.get(`/${OCPIServerRoute.OCPI_LOCATIONS}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(CPOLocationsService.handleGetLocations.bind(this), ServerAction.OCPI_CPO_GET_LOCATIONS, req, res, next);
});

RENAME
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/cpo/CPOCdrsEndpoint.ts
src/server/ocpi/service/cpo/v2.1.1/CPOCdrsService.ts
import { NextFunction, Request, Response } from 'express';
-import AbstractEndpoint from '../../AbstractEndpoint';
-import AbstractOCPIService from '../../../AbstractOCPIService';
import Constants from '../../../../../utils/Constants';
import { DataResult } from '../../../../../types/DataResult';
import { OCPICdr } from '../../../../../types/ocpi/OCPICdr';
-import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import { OCPIResponse } from '../../../../../types/ocpi/OCPIResponse';
import OCPIUtils from '../../../OCPIUtils';
+import OCPIUtilsService from '../../OCPIUtilsService';
+import { ServerAction } from '../../../../../types/Server';
import Tenant from '../../../../../types/Tenant';
import TransactionStorage from '../../../../../storage/mongodb/TransactionStorage';
import Utils from '../../../../../utils/Utils';
-export default class CPOCdrsEndpoint 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.getCdrsRequest(req, res, next, tenant);
- }
- }
-
- private async getCdrsRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
+export default class CPOCdrsService {
+ public static async handleGetCdrs(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant, ocpiEndpoint } = 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 cdrs
- const cdrs = await this.getAllCdrs(tenant, limit, offset,
+ const cdrs = await CPOCdrsService.getAllCdrs(tenant, limit, offset,
Utils.convertToDate(req.query.date_from), Utils.convertToDate(req.query.date_to));
// Set header
res.set({
@@ -37,16 +25,17 @@ export default class CPOCdrsEndpoint extends AbstractEndpoint {
'X-Limit': Constants.OCPI_RECORDS_LIMIT
});
// Return next link
- const nextUrl = OCPIUtils.buildNextUrl(req, this.getBaseUrl(req), offset, limit, cdrs.count);
+ const nextUrl = OCPIUtils.buildNextUrl(req, OCPIUtilsService.getBaseUrl(req), offset, limit, cdrs.count);
if (nextUrl) {
res.links({
next: nextUrl
});
}
- return OCPIUtils.success(cdrs.result);
+ res.json(OCPIUtils.success(cdrs.result));
+ next();
}
- private async getAllCdrs(tenant: Tenant, limit: number, skip: number, dateFrom?: Date, dateTo?: Date): Promise<DataResult<OCPICdr>> {
+ private static async getAllCdrs(tenant: Tenant, limit: number, skip: number, dateFrom?: Date, dateTo?: Date): Promise<DataResult<OCPICdr>> {
// Get all transactions
const transactions = await TransactionStorage.getTransactions(tenant,
{ issuer: true, ocpiCdrDateFrom: dateFrom, ocpiCdrDateTo: dateTo, ocpiCdrChecked: true },

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -155,6 +155,7 @@ export enum ServerAction {
OCPI_CPO_STOP_SESSION = 'OcpiCpoStopSession',
OCPI_CPO_CHECK_LOCATIONS = 'OcpiCpoCheckLocations',
OCPI_CPO_GET_LOCATIONS = 'OcpiCpoGetLocations',
+ OCPI_CPO_GET_CDRS = 'OcpiCpoGetCdrs',
OCPI_CPO_PUSH_EVSE_STATUSES = 'OcpiCpoPushEVSEStatuses',
OCPI_CPO_PUSH_CDRS = 'OcpiCpoPushCdrs',
OCPI_CPO_PULL_TOKENS = 'OcpiCpoPullTokens',
@@ -672,6 +673,7 @@ export enum RESTServerRoute {
export enum OCPIServerRoute {
OCPI_VERSIONS = 'versions',
OCPI_LOCATIONS = 'locations',
+ OCPI_CDRS = 'cdrs',
OCPI_TOKENS = 'tokens',
}

Migrated CPO GetCdrs Endpoint