Optimized Charger Token Registration
MODIFY
src/server/rest/v1/service/RegistrationTokenService.ts
src/server/rest/v1/service/RegistrationTokenService.ts
@@ -73,7 +73,7 @@ export default class RegistrationTokenService {
registrationToken.ocpp16SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.SOAP, registrationToken.id);
registrationToken.ocpp16JSONUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.JSON, registrationToken.id);
// Ok
- res.json(RegistrationTokenSecurity.filterRegistrationTokenResponse(registrationToken, req.user));
+ res.json(Constants.REST_RESPONSE_SUCCESS);
next();
}
@@ -124,7 +124,7 @@ export default class RegistrationTokenService {
registrationToken.ocpp16SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.SOAP, registrationToken.id);
registrationToken.ocpp16JSONUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.JSON, registrationToken.id);
// Ok
- res.json(RegistrationTokenSecurity.filterRegistrationTokenResponse(registrationToken, req.user));
+ res.json(Constants.REST_RESPONSE_SUCCESS);
next();
}
@@ -218,7 +218,8 @@ export default class RegistrationTokenService {
skip: filteredRequest.Skip,
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
- }
+ },
+ [ 'id', 'status', 'description', 'createdOn', 'lastChangedOn', 'expirationDate', 'revocationDate', 'siteAreaID', 'siteArea.name' ]
);
// Build OCPP URLs
registrationTokens.result.forEach((registrationToken) => {
@@ -227,8 +228,6 @@ export default class RegistrationTokenService {
registrationToken.ocpp16JSONUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.JSON, registrationToken.id);
return registrationToken;
});
- // Filter
- RegistrationTokenSecurity.filterRegistrationTokensResponse(registrationTokens, req.user);
// Ok
res.json(registrationTokens);
next();

MODIFY
src/server/rest/v1/service/security/RegistrationTokenSecurity.ts
src/server/rest/v1/service/security/RegistrationTokenSecurity.ts
-import Authorizations from '../../../../../authorization/Authorizations';
-import { DataResult } from '../../../../../types/DataResult';
import { HttpRegistrationTokensRequest } from '../../../../../types/requests/HttpRegistrationToken';
import RegistrationToken from '../../../../../types/RegistrationToken';
-import UserToken from '../../../../../types/UserToken';
import UtilsSecurity from './UtilsSecurity';
import sanitize from 'mongo-sanitize';
@@ -45,27 +42,4 @@ export default class RegistrationTokenSecurity {
UtilsSecurity.filterSort(request, filteredRequest);
return filteredRequest as HttpRegistrationTokensRequest;
}
-
- static filterRegistrationTokensResponse(registrationTokens: DataResult<RegistrationToken>, loggedUser: UserToken): void {
- const filteredTokens = [];
- if (!registrationTokens.result) {
- return null;
- }
- for (const registrationToken of registrationTokens.result) {
- // Filter
- const filteredToken = RegistrationTokenSecurity.filterRegistrationTokenResponse(registrationToken, loggedUser);
- if (filteredToken) {
- filteredTokens.push(filteredToken);
- }
- }
- registrationTokens.result = filteredTokens;
- }
-
- static filterRegistrationTokenResponse(registrationToken: RegistrationToken, loggedUser: UserToken): RegistrationToken {
- if (registrationToken || Authorizations.canReadRegistrationToken(loggedUser,
- registrationToken.siteArea ? registrationToken.siteArea.siteID : null)) {
- return registrationToken;
- }
- return null;
- }
}

MODIFY
src/storage/mongodb/RegistrationTokenStorage.ts
src/storage/mongodb/RegistrationTokenStorage.ts
@@ -39,7 +39,7 @@ export default class RegistrationTokenStorage {
}
static async getRegistrationTokens(tenantID: string,
- params: { tokenIDs?: string[]; siteIDs?: string[]; siteAreaID?: string } = {}, dbParams: DbParams):
+ params: { tokenIDs?: string[]; siteIDs?: string[]; siteAreaID?: string } = {}, dbParams: DbParams, projectFields?: string[]):
Promise<DataResult<RegistrationToken>> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getRegistrationTokens');
@@ -119,6 +119,8 @@ export default class RegistrationTokenStorage {
aggregation.push({
$limit: (dbParams.limit > 0 && dbParams.limit < Constants.DB_RECORD_COUNT_CEIL) ? dbParams.limit : Constants.DB_RECORD_COUNT_CEIL
});
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
const registrationTokens = await global.database.getCollection<any>(tenantID, 'registrationtokens')
.aggregate(aggregation, { collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 }, allowDiskUse: true })

Optimized Charger Token Registration