Migrated CPO Sessions endpoint
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 CPOSessionsEndpoint from './cpo/CPOSessionsEndpoint';
import CredentialsEndpoint from './credentials/CredentialsEndpoint';
import OCPIServiceConfiguration from '../../../../types/configuration/OCPIServiceConfiguration';
@@ -10,7 +9,6 @@ export default class CPOService extends AbstractOCPIService {
public constructor(ocpiRestConfig: OCPIServiceConfiguration) {
super(ocpiRestConfig, CPOService.PATH, CPOService.VERSION);
// Register Endpoints
- this.registerEndpoint(new CPOSessionsEndpoint(this));
this.registerEndpoint(new CredentialsEndpoint(this));
}
}

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 CPOSessionsRouterV211 from './V2.1.1/CPOSessionsRouterV211';
import CPOTariffsRouterV211 from './V2.1.1/CPOTariffsRouterV211';
import CPOTokensRouterV211 from './V2.1.1/CPOTokensRouterV211';
import CPOVersionsService from '../../service/cpo/CPOVersionsService';
@@ -36,6 +37,7 @@ export default class CPORouter {
new CPOCdrsRouterV211().buildRoutes(),
new CPOCommandsRouterV211().buildRoutes(),
new CPOTariffsRouterV211().buildRoutes(),
+ new CPOSessionsRouterV211().buildRoutes(),
]);
}
}

MODIFY
src/server/ocpi/router/cpo/V2.1.1/CPOCommandsRouterV211.ts
src/server/ocpi/router/cpo/V2.1.1/CPOCommandsRouterV211.ts
@@ -18,7 +18,7 @@ export default class CPOCommandsRouterV211 {
}
protected buildRouteCommands(): void {
- this.router.post(`/${OCPIServerRoute.OCPI_COMMANDS}/*`, async (req: Request, res: Response, next: NextFunction) => {
+ this.router.post(`/${OCPIServerRoute.OCPI_COMMANDS}*`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(CPOCommandsService.handleCommands.bind(this), ServerAction.OCPI_CPO_COMMAND, req, res, next);
});
}

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

MODIFY
src/server/ocpi/router/cpo/V2.1.1/CPOTokensRouterV211.ts
src/server/ocpi/router/cpo/V2.1.1/CPOTokensRouterV211.ts
@@ -20,19 +20,19 @@ export default class CPOTokensRouterV211 {
}
protected buildRouteGetTokens(): void {
- this.router.get(`/${OCPIServerRoute.OCPI_TOKENS}/*`, async (req: Request, res: Response, next: NextFunction) => {
+ this.router.get(`/${OCPIServerRoute.OCPI_TOKENS}*`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(CPOTokensService.handleGetToken.bind(this), ServerAction.OCPI_CPO_GET_TOKEN, req, res, next);
});
}
protected buildRoutePutTokens(): void {
- this.router.put(`/${OCPIServerRoute.OCPI_TOKENS}/*`, async (req: Request, res: Response, next: NextFunction) => {
+ this.router.put(`/${OCPIServerRoute.OCPI_TOKENS}*`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(CPOTokensService.handlePutToken.bind(this), ServerAction.OCPI_CPO_PUT_TOKEN, req, res, next);
});
}
protected buildRoutePatchTokens(): void {
- this.router.patch(`/${OCPIServerRoute.OCPI_TOKENS}/*`, async (req: Request, res: Response, next: NextFunction) => {
+ this.router.patch(`/${OCPIServerRoute.OCPI_TOKENS}*`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(CPOTokensService.handlePatchToken.bind(this), ServerAction.OCPI_CPO_PATCH_TOKEN, req, res, next);
});
}

RENAME
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/cpo/CPOSessionsEndpoint.ts
src/server/ocpi/service/cpo/v2.1.1/CPOSessionsService.ts
import { NextFunction, Request, Response } from 'express';
-import AbstractEndpoint from '../../AbstractEndpoint';
-import AbstractOCPIService from '../../../AbstractOCPIService';
import AppError from '../../../../../exception/AppError';
import Constants from '../../../../../utils/Constants';
import { DataResult } from '../../../../../types/DataResult';
-import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import { OCPIResponse } from '../../../../../types/ocpi/OCPIResponse';
import { OCPISession } from '../../../../../types/ocpi/OCPISession';
import { OCPIStatusCode } from '../../../../../types/ocpi/OCPIStatusCode';
import OCPIUtils from '../../../OCPIUtils';
+import OCPIUtilsService from '../../OCPIUtilsService';
import { ServerAction } from '../../../../../types/Server';
import { StatusCodes } from 'http-status-codes';
import Tenant from '../../../../../types/Tenant';
import TransactionStorage from '../../../../../storage/mongodb/TransactionStorage';
import Utils from '../../../../../utils/Utils';
-const MODULE_NAME = 'CPOSessionsEndpoint';
+const MODULE_NAME = 'CPOSessionsService';
-export default class CPOSessionsEndpoint extends AbstractEndpoint {
- public constructor(ocpiService: AbstractOCPIService) {
- super(ocpiService, 'sessions');
- }
-
- public async process(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
- switch (req.method) {
- case 'GET':
- return this.getSessionsRequest(req, res, next, tenant);
- }
- }
-
- private async getSessionsRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
+export default class CPOSessionsService {
+ public static async handleGetSessions(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Split URL Segments
// /ocpi/cpo/2.0/sessions/?date_from=xxx&date_to=yyy
// date_from required
// date_to optional
// offset optional
// limit optional
+ 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;
if (!req.query.date_from) {
throw new AppError({
- action: ServerAction.OCPI_CPO_PULL_SESSIONS,
- module: MODULE_NAME, method: 'getSessionsRequest',
+ module: MODULE_NAME, method: 'handleGetSessions', action,
errorCode: StatusCodes.BAD_REQUEST,
message: 'Missing \'date_from\' parameter',
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
});
}
// Get all sessions
- const sessions = await this.getAllSessions(tenant, limit, offset,
+ const sessions = await CPOSessionsService.getAllSessions(tenant, limit, offset,
Utils.convertToDate(req.query.date_from), Utils.convertToDate(req.query.date_to));
// Set header
res.set({
@@ -58,16 +44,17 @@ export default class CPOSessionsEndpoint extends AbstractEndpoint {
'X-Limit': Constants.OCPI_RECORDS_LIMIT
});
// Return next link
- const nextUrl = OCPIUtils.buildNextUrl(req, this.getBaseUrl(req), offset, limit, sessions.count);
+ const nextUrl = OCPIUtils.buildNextUrl(req, OCPIUtilsService.getBaseUrl(req), offset, limit, sessions.count);
if (nextUrl) {
res.links({
next: nextUrl
});
}
- return OCPIUtils.success(sessions.result);
+ res.json(OCPIUtils.success(sessions.result));
+ next();
}
- private async getAllSessions(tenant: Tenant, limit: number, skip: number, dateFrom?: Date, dateTo?: Date): Promise<DataResult<OCPISession>> {
+ private static async getAllSessions(tenant: Tenant, limit: number, skip: number, dateFrom?: Date, dateTo?: Date): Promise<DataResult<OCPISession>> {
// Result
const sessions: OCPISession[] = [];
// Get all transactions

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -149,7 +149,7 @@ export enum ServerAction {
OCPI_CPO_PATCH_STATUS = 'OcpiCpoPatchStatus',
OCPI_CPO_CHECK_CDRS = 'OcpiCpoCheckCdrs',
OCPI_CPO_CHECK_SESSIONS = 'OcpiCpoCheckSessions',
- OCPI_CPO_PULL_SESSIONS = 'OcpiCpoPullSessions',
+ OCPI_CPO_GET_SESSIONS = 'OcpiCpoGetSessions',
OCPI_CPO_PUSH_SESSIONS = 'OcpiCpoPushSessions',
OCPI_CPO_START_SESSION = 'OcpiCpoStartSession',
OCPI_CPO_STOP_SESSION = 'OcpiCpoStopSession',
@@ -677,6 +677,7 @@ export enum OCPIServerRoute {
OCPI_COMMANDS = 'commands',
OCPI_TOKENS = 'tokens',
OCPI_TARIFFS = 'tariffs',
+ OCPI_SESSIONS = 'sessions',
}
export enum ServerProtocol {

Migrated CPO Sessions endpoint