Optimized OCPI Service
MODIFY
src/client/ocpi/CpoOCPIClient.ts
src/client/ocpi/CpoOCPIClient.ts
@@ -565,7 +565,6 @@ export default class CpoOCPIClient extends OCPIClient {
total: 0,
logs: [],
objectIDsInFailure: [],
- objectIDsInSuccess: []
};
// Perfs trace
const startTime = new Date().getTime();
@@ -578,7 +577,6 @@ export default class CpoOCPIClient extends OCPIClient {
try {
if (await this.checkSession(transaction)) {
result.success++;
- result.objectIDsInSuccess.push(String(transaction.id));
} else {
result.failure++;
result.objectIDsInFailure.push(String(transaction.id));
@@ -612,7 +610,6 @@ export default class CpoOCPIClient extends OCPIClient {
total: 0,
logs: [],
objectIDsInFailure: [],
- objectIDsInSuccess: []
};
// Perfs trace
const startTime = new Date().getTime();
@@ -630,7 +627,6 @@ export default class CpoOCPIClient extends OCPIClient {
try {
if (await this.checkLocation(location)) {
result.success++;
- result.objectIDsInSuccess.push(String(location.id));
} else {
result.failure++;
result.objectIDsInFailure.push(String(location.id));
@@ -664,7 +660,6 @@ export default class CpoOCPIClient extends OCPIClient {
total: 0,
logs: [],
objectIDsInFailure: [],
- objectIDsInSuccess: []
};
// Perfs trace
const startTime = new Date().getTime();
@@ -676,7 +671,6 @@ export default class CpoOCPIClient extends OCPIClient {
try {
if (await this.checkCdr(transaction)) {
result.success++;
- result.objectIDsInSuccess.push(String(transaction.id));
} else {
result.failure++;
result.objectIDsInFailure.push(String(transaction.id));
@@ -712,7 +706,6 @@ export default class CpoOCPIClient extends OCPIClient {
total: 0,
logs: [],
objectIDsInFailure: [],
- objectIDsInSuccess: []
};
// Perfs trace
const startTime = new Date().getTime();
@@ -754,7 +747,6 @@ export default class CpoOCPIClient extends OCPIClient {
try {
await this.patchEVSEStatus(evse.chargeBoxId, location.id, evse.uid, evse.status);
result.success++;
- result.objectIDsInSuccess.push(evse.chargeBoxId);
} catch (error) {
result.failure++;
result.objectIDsInFailure.push(evse.chargeBoxId);

MODIFY
src/client/ocpi/EmspOCPIClient.ts
src/client/ocpi/EmspOCPIClient.ts
@@ -57,7 +57,6 @@ export default class EmspOCPIClient extends OCPIClient {
total: 0,
logs: [],
objectIDsInFailure: [],
- objectIDsInSuccess: []
};
// Perfs trace
const startTime = new Date().getTime();
@@ -70,7 +69,6 @@ export default class EmspOCPIClient extends OCPIClient {
try {
await this.pushToken(token);
result.success++;
- result.objectIDsInSuccess.push(token.uid);
} catch (error) {
result.failure++;
result.objectIDsInFailure.push(token.uid);
@@ -88,7 +86,6 @@ export default class EmspOCPIClient extends OCPIClient {
'failureNbr': result.failure,
'totalNbr': result.total,
'tokenIDsInFailure': _.uniq(result.objectIDsInFailure),
- 'tokenIDsInSuccess': _.uniq(result.objectIDsInSuccess)
};
} else {
this.ocpiEndpoint.lastPatchJobResult = {
@@ -96,7 +93,6 @@ export default class EmspOCPIClient extends OCPIClient {
'failureNbr': 0,
'totalNbr': 0,
'tokenIDsInFailure': [],
- 'tokenIDsInSuccess': []
};
}
// Save

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -79,11 +79,14 @@ export default class OCPIEndpointService {
});
}
// Get it
- const ocpiEndpoint = await OCPIEndpointStorage.getOcpiEndpoint(req.user.tenantID, endpointID);
+ const ocpiEndpoint = await OCPIEndpointStorage.getOcpiEndpoint(req.user.tenantID, endpointID,
+ [
+ 'id', 'name', 'role', 'baseUrl', 'countryCode', 'partyId', 'version', 'status', 'patchJobStatus', 'localToken', 'token',
+ 'patchJobResult.successNbr', 'patchJobResult.failureNbr', 'patchJobResult.totalNbr'
+ ]);
UtilsService.assertObjectExists(action, ocpiEndpoint, `OCPIEndpoint with ID '${endpointID}' does not exist`,
MODULE_NAME, 'handleGetOcpiEndpoint', req.user);
- // Return
- res.json(OCPIEndpointSecurity.filterOcpiEndpointResponse(ocpiEndpoint, req.user));
+ res.json(ocpiEndpoint);
next();
}
@@ -100,6 +103,11 @@ export default class OCPIEndpointService {
module: MODULE_NAME, method: 'handleGetOcpiEndpoints'
});
}
+ // Check User
+ let userProject: string[] = [];
+ if (Authorizations.canListUsers(req.user)) {
+ userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
+ }
// Filter
const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointsRequest(req.query);
// Get all ocpiendpoints
@@ -111,9 +119,13 @@ export default class OCPIEndpointService {
skip: filteredRequest.Skip,
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
- });
- OCPIEndpointSecurity.filterOcpiEndpointsResponse(ocpiEndpoints, req.user);
- // Return
+ },
+ [
+ 'id', 'name', 'role', 'baseUrl', 'countryCode', 'partyId', 'version', 'status', 'lastChangedOn', 'lastPatchJobOn',
+ 'backgroundPatchJob', 'localToken', 'token',
+ 'lastPatchJobResult.successNbr', 'lastPatchJobResult.failureNbr', 'lastPatchJobResult.totalNbr',
+ ...userProject
+ ]);
res.json(ocpiEndpoints);
next();
}

MODIFY
src/server/rest/v1/service/security/OCPIEndpointSecurity.ts
src/server/rest/v1/service/security/OCPIEndpointSecurity.ts
-import Authorizations from '../../../../../authorization/Authorizations';
-import { DataResult } from '../../../../../types/DataResult';
import { HttpOCPIEndpointsRequest } from '../../../../../types/requests/HttpOCPIEndpointRequest';
import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import UserToken from '../../../../../types/UserToken';
import UtilsSecurity from './UtilsSecurity';
import sanitize from 'mongo-sanitize';
@@ -101,49 +98,5 @@ export default class OCPIEndpointSecurity {
filteredRequest.backgroundPatchJob = sanitize(request.backgroundPatchJob);
return filteredRequest;
}
-
- static filterOcpiEndpointResponse(ocpiEndpoint: OCPIEndpoint, loggedUser: UserToken): Partial<OCPIEndpoint> {
- let filteredOcpiEndpoint;
-
- if (!ocpiEndpoint) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadOcpiEndpoint(loggedUser)) {
- // Admin?
- if (Authorizations.isAdmin(loggedUser)) {
- // Yes: set all params
- filteredOcpiEndpoint = ocpiEndpoint;
- } else {
- // Set only necessary info
- return null;
- }
- // Created By / Last Changed By
- UtilsSecurity.filterCreatedAndLastChanged(
- filteredOcpiEndpoint, ocpiEndpoint, loggedUser);
- }
- return filteredOcpiEndpoint;
- }
-
- static filterOcpiEndpointsResponse(ocpiEndpoints: DataResult<OCPIEndpoint>, loggedUser): void {
- const filteredOcpiEndpoints = [];
-
- if (!ocpiEndpoints || !ocpiEndpoints.result) {
- return;
- }
- if (!Authorizations.canListOcpiEndpoints(loggedUser)) {
- return;
- }
- for (const ocpiEndPoint of ocpiEndpoints.result) {
- // Filter
- const filteredOcpiEndpoint = OCPIEndpointSecurity.filterOcpiEndpointResponse(ocpiEndPoint, loggedUser);
- // Ok?
- if (filteredOcpiEndpoint) {
- // Add
- filteredOcpiEndpoints.push(filteredOcpiEndpoint);
- }
- }
- ocpiEndpoints.result = filteredOcpiEndpoints;
- }
}

MODIFY
src/storage/mongodb/OCPIEndpointStorage.ts
src/storage/mongodb/OCPIEndpointStorage.ts
@@ -13,13 +13,15 @@ import Utils from '../../utils/Utils';
const MODULE_NAME = 'OCPIEndpointStorage';
export default class OCPIEndpointStorage {
- static async getOcpiEndpoint(tenantID: string, id: string): Promise<OCPIEndpoint> {
- const endpointsMDB = await OCPIEndpointStorage.getOcpiEndpoints(tenantID, { ocpiEndpointIDs: [id] }, Constants.DB_PARAMS_SINGLE_RECORD);
+ static async getOcpiEndpoint(tenantID: string, id: string, projectFields?: string[]): Promise<OCPIEndpoint> {
+ const endpointsMDB = await OCPIEndpointStorage.getOcpiEndpoints(
+ tenantID, { ocpiEndpointIDs: [id] }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return endpointsMDB.count === 1 ? endpointsMDB.result[0] : null;
}
- static async getOcpiEndpointByLocalToken(tenantID: string, token: string): Promise<OCPIEndpoint> {
- const endpointsMDB = await OCPIEndpointStorage.getOcpiEndpoints(tenantID, { localToken: token }, Constants.DB_PARAMS_SINGLE_RECORD);
+ static async getOcpiEndpointByLocalToken(tenantID: string, token: string, projectFields?: string[]): Promise<OCPIEndpoint> {
+ const endpointsMDB = await OCPIEndpointStorage.getOcpiEndpoints(
+ tenantID, { localToken: token }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return endpointsMDB.count === 1 ? endpointsMDB.result[0] : null;
}
@@ -77,7 +79,9 @@ export default class OCPIEndpointStorage {
}
// Delegate
- static async getOcpiEndpoints(tenantID: string, params: { search?: string; role?: string; ocpiEndpointIDs?: string[]; localToken?: string }, dbParams: DbParams): Promise<DataResult<OCPIEndpoint>> {
+ static async getOcpiEndpoints(tenantID: string,
+ params: { search?: string; role?: string; ocpiEndpointIDs?: string[]; localToken?: string },
+ dbParams: DbParams, projectFields?: string[]): Promise<DataResult<OCPIEndpoint>> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getOcpiEndpoints');
// Check Tenant
@@ -152,6 +156,8 @@ export default class OCPIEndpointStorage {
aggregation.push({
$limit: dbParams.limit
});
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
const ocpiEndpointsMDB = await global.database.getCollection<any>(tenantID, 'ocpiendpoints')
.aggregate(aggregation, { collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 } })

MODIFY
src/types/ocpi/OCPIEndpoint.ts
src/types/ocpi/OCPIEndpoint.ts
@@ -24,6 +24,5 @@ export default interface OCPIEndpoint extends CreatedUpdatedProps {
chargeBoxIDsInFailure?: string[];
chargeBoxIDsInSuccess?: string[];
tokenIDsInFailure?: string[];
- tokenIDsInSuccess?: string[];
};
}

MODIFY
src/types/ocpi/OCPIResult.ts
src/types/ocpi/OCPIResult.ts
@@ -4,5 +4,4 @@ export interface OCPIResult {
total: number;
logs: string[];
objectIDsInFailure?: string[];
- objectIDsInSuccess?: string[];
}

Optimized OCPI Service