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

MODIFY
src/scheduler/tasks/ocpi/OCPIPullSessionsTask.ts
src/scheduler/tasks/ocpi/OCPIPullSessionsTask.ts
@@ -29,7 +29,7 @@ export default class OCPIPullSessionsTask extends TenantSchedulerTask {
}
} catch (error) {
// Log error
- await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_SESSIONS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_SESSION, error);
}
}
@@ -43,7 +43,7 @@ export default class OCPIPullSessionsTask extends TenantSchedulerTask {
await Logging.logDebug({
tenantID: tenant.id,
module: MODULE_NAME, method: 'processOCPIEndpoint',
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
+ action: ServerAction.OCPI_EMSP_GET_SESSION,
message: `The OCPI endpoint '${ocpiEndpoint.name}' is not registered. Skipping the OCPI endpoint.`
});
return;
@@ -52,7 +52,7 @@ export default class OCPIPullSessionsTask extends TenantSchedulerTask {
await Logging.logDebug({
tenantID: tenant.id,
module: MODULE_NAME, method: 'processOCPIEndpoint',
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
+ action: ServerAction.OCPI_EMSP_GET_SESSION,
message: `The OCPI endpoint '${ocpiEndpoint.name}' is inactive.`
});
return;
@@ -60,7 +60,7 @@ export default class OCPIPullSessionsTask extends TenantSchedulerTask {
await Logging.logInfo({
tenantID: tenant.id,
module: MODULE_NAME, method: 'processOCPIEndpoint',
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
+ action: ServerAction.OCPI_EMSP_GET_SESSION,
message: `Pull of Sessions for endpoint '${ocpiEndpoint.name}' is being processed`
});
// Build OCPI Client
@@ -70,13 +70,13 @@ export default class OCPIPullSessionsTask extends TenantSchedulerTask {
await Logging.logInfo({
tenantID: tenant.id,
module: MODULE_NAME, method: 'processOCPIEndpoint',
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
+ action: ServerAction.OCPI_EMSP_GET_SESSION,
message: `Pull of Sessions for endpoint '${ocpiEndpoint.name}' is completed`,
detailedMessages: { result }
});
} catch (error) {
// Log error
- await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_SESSIONS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_EMSP_GET_SESSION, error);
} finally {
// Release the lock
await LockingManager.release(ocpiLock);

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 EMSPSessionsEndpoint from './emsp/EMSPSessionsEndpoint';
import EMSPTariffsEndpoint from './emsp/EMSPTariffsEndpoint';
import OCPIServiceConfiguration from '../../../../types/configuration/OCPIServiceConfiguration';
@@ -11,7 +10,6 @@ export default class EMSPService extends AbstractOCPIService {
public constructor(ocpiRestConfig: OCPIServiceConfiguration) {
super(ocpiRestConfig, EMSPService.PATH, EMSPService.VERSION);
- this.registerEndpoint(new EMSPSessionsEndpoint(this));
this.registerEndpoint(new EMSPCdrsEndpoint(this));
this.registerEndpoint(new EMSPCommandsEndpoint(this));
this.registerEndpoint(new EMSPTariffsEndpoint(this));

MODIFY
src/server/ocpi/router/emsp/EMSPRouter.ts
src/server/ocpi/router/emsp/EMSPRouter.ts
@@ -4,6 +4,7 @@ import express, { NextFunction, Request, Response } from 'express';
import CPOEMSPCredentialsRouterV211 from '../common/V2.1.1/CPOEMSPCredentialsRouterV211';
import EMSPLocationsRouterV211 from './V2.1.1/EMSPLocationsRouterV211';
+import EMSPSessionsRouterV211 from './V2.1.1/EMSPSessionsRouterV211';
import EMSPTokensRouterV211 from './V2.1.1/EMSPTokensRouterV211';
import EMSPVersionsService from '../../service/emsp/EMSPVersionsService';
import RouterUtils from '../../../../utils/RouterUtils';
@@ -31,6 +32,7 @@ export default class EMSPRouter {
this.router.use('/2.1.1', [
new EMSPTokensRouterV211().buildRoutes(),
new EMSPLocationsRouterV211().buildRoutes(),
+ new EMSPSessionsRouterV211().buildRoutes(),
new CPOEMSPCredentialsRouterV211().buildRoutes(),
]);
}

ADD
src/server/ocpi/router/emsp/V2.1.1/EMSPSessionsRouterV211.ts
+import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
+/* eslint-disable @typescript-eslint/no-misused-promises */
+import express, { NextFunction, Request, Response } from 'express';
+
+import EMSPSessionsService from '../../../service/emsp/v2.1.1/EMSPSessionsService';
+import RouterUtils from '../../../../../utils/RouterUtils';
+
+export default class EMSPSessionsRouterV211 {
+ private router: express.Router;
+
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteGetSession();
+ this.buildRoutePatchSession();
+ this.buildRoutePutSession();
+ return this.router;
+ }
+
+ protected buildRouteGetSession(): void {
+ this.router.get(`/${OCPIServerRoute.OCPI_SESSIONS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPSessionsService.handleGetSession.bind(this), ServerAction.OCPI_EMSP_GET_SESSION, req, res, next);
+ });
+ }
+
+ protected buildRoutePatchSession(): void {
+ this.router.patch(`/${OCPIServerRoute.OCPI_SESSIONS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPSessionsService.handlePatchSession.bind(this), ServerAction.OCPI_EMSP_UPDATE_SESSION, req, res, next);
+ });
+ }
+
+ protected buildRoutePutSession(): void {
+ this.router.put(`/${OCPIServerRoute.OCPI_SESSIONS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPSessionsService.handlePutSession.bind(this), ServerAction.OCPI_EMSP_UPDATE_SESSION, req, res, next);
+ });
+ }
+}

RENAME
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/emsp/EMSPSessionsEndpoint.ts
src/server/ocpi/service/emsp/v2.1.1/EMSPSessionsService.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 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 '../../../service/OCPIUtilsService';
+import OCPIUtilsService from '../../OCPIUtilsService';
import { ServerAction } from '../../../../../types/Server';
import { StatusCodes } from 'http-status-codes';
-import Tenant from '../../../../../types/Tenant';
import Transaction from '../../../../../types/Transaction';
import TransactionStorage from '../../../../../storage/mongodb/TransactionStorage';
import _ from 'lodash';
-const MODULE_NAME = 'EMSPSessionsEndpoint';
+const MODULE_NAME = 'EMSPSessionsService';
-export default class EMSPSessionsEndpoint 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.getSessionRequest(req, res, next, tenant);
- case 'PATCH':
- return this.patchSessionRequest(req, res, next, tenant);
- case 'PUT':
- return this.putSessionRequest(req, res, next, tenant);
- }
- }
-
- private async getSessionRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
+export default class EMSPSessionsService {
+ public static async handleGetSession(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
const urlSegment = req.path.substring(1).split('/');
// Remove action
urlSegment.shift();
@@ -45,8 +26,7 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
const sessionId = urlSegment.shift();
if (!countryCode || !partyId || !sessionId) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
- module: MODULE_NAME, method: 'getSessionRequest',
+ module: MODULE_NAME, method: 'handleGetSession', action,
errorCode: StatusCodes.BAD_REQUEST,
message: 'Missing request parameters',
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
@@ -55,17 +35,18 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
const transaction: Transaction = await TransactionStorage.getOCPITransactionBySessionID(tenant, sessionId);
if (!transaction) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
- module: MODULE_NAME, method: 'getSessionRequest',
+ module: MODULE_NAME, method: 'handleGetSession', action,
errorCode: HTTPError.GENERAL_ERROR,
message: `No Transaction found for OCPI Session ID ${sessionId}`,
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
});
}
- return OCPIUtils.success(transaction.ocpiData.session);
+ res.json(OCPIUtils.success(transaction.ocpiData.session));
+ next();
}
- private async putSessionRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
+ public static async handlePutSession(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
const urlSegment = req.path.substring(1).split('/');
// Remove action
urlSegment.shift();
@@ -75,8 +56,7 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
const sessionID = urlSegment.shift();
if (!countryCode || !partyID || !sessionID) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_UPDATE_SESSION,
- module: MODULE_NAME, method: 'putSessionRequest',
+ module: MODULE_NAME, method: 'handlePutSession', action,
errorCode: StatusCodes.BAD_REQUEST,
message: 'Missing request parameters',
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
@@ -87,8 +67,7 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
session.id = sessionID;
} else if (session.id !== sessionID) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_UPDATE_SESSION,
- module: MODULE_NAME, method: 'putSessionRequest',
+ module: MODULE_NAME, method: 'handlePutSession', action,
errorCode: StatusCodes.BAD_REQUEST,
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR,
message: `Transaction ID '${session.id}' mismatch in URL`,
@@ -96,10 +75,12 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
});
}
await OCPIUtilsService.processEmspTransactionFromSession(tenant, session, ServerAction.OCPI_EMSP_UPDATE_SESSION);
- return OCPIUtils.success({});
+ res.json(OCPIUtils.success({}));
+ next();
}
- private async patchSessionRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant): Promise<OCPIResponse> {
+ public static async handlePatchSession(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
const urlSegment = req.path.substring(1).split('/');
// Remove action
urlSegment.shift();
@@ -109,8 +90,7 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
const sessionID = urlSegment.shift();
if (!countryCode || !partyID || !sessionID) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_UPDATE_SESSION,
- module: MODULE_NAME, method: 'patchSessionRequest',
+ module: MODULE_NAME, method: 'handlePatchSession', action,
errorCode: HTTPError.GENERAL_ERROR,
message: 'Missing request parameters',
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
@@ -119,8 +99,7 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
const transaction = await TransactionStorage.getOCPITransactionBySessionID(tenant, sessionID);
if (!transaction) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_UPDATE_SESSION,
- module: MODULE_NAME, method: 'patchSessionRequest',
+ module: MODULE_NAME, method: 'handlePatchSession', action,
errorCode: HTTPError.GENERAL_ERROR,
message: `Transaction not found with OCPI Session ID '${sessionID}'`,
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
@@ -130,7 +109,8 @@ export default class EMSPSessionsEndpoint extends AbstractEndpoint {
_.merge(transaction.ocpiData.session, req.body);
// Update
await OCPIUtilsService.processEmspTransactionFromSession(tenant, transaction.ocpiData.session, ServerAction.OCPI_EMSP_UPDATE_SESSION, transaction);
- return OCPIUtils.success({});
+ res.json(OCPIUtils.success({}));
+ next();
}
}

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

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -309,7 +309,7 @@ export default class OCPIEndpointService {
const pullSessionsLock = await LockingHelper.createOCPIPullSessionsLock(req.tenant.id, ocpiEndpoint);
if (!pullSessionsLock) {
throw new AppError({
- action: ServerAction.OCPI_EMSP_GET_SESSIONS,
+ action: ServerAction.OCPI_EMSP_GET_SESSION,
errorCode: HTTPError.CANNOT_ACQUIRE_LOCK,
message: 'Error in pulling the OCPI Sessions: cannot acquire the lock',
module: MODULE_NAME, method: 'handlePullSessionsEndpoint',

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -169,6 +169,7 @@ export enum ServerAction {
OCPI_EMSP_UPDATE_LOCATION = 'OcpiEmspUpdateLocation',
OCPI_EMSP_GET_LOCATIONS = 'OcpiEmspGetLocations',
OCPI_EMSP_GET_SESSIONS = 'OcpiEmspGetSessions',
+ OCPI_EMSP_GET_SESSION = 'OcpiEmspGetSession',
OCPI_EMSP_UPDATE_SESSION = 'OcpiEmspUpdateSession',
OCPI_EMSP_START_SESSION = 'OcpiEmspStartSession',
OCPI_EMSP_STOP_SESSION = 'OcpiEmspStopSession',

Migrated EMSP Session endpoint