Fixed SiteIDs in Token management + clean the code
MODIFY
src/server/rest/service/RegistrationTokenService.ts
src/server/rest/service/RegistrationTokenService.ts
@@ -7,7 +7,6 @@ import AppAuthError from '../../../exception/AppAuthError';
import AppError from '../../../exception/AppError';
import Authorizations from '../../../authorization/Authorizations';
import Constants from '../../../utils/Constants';
-import DbParams from '../../../types/database/DbParams';
import Logging from '../../../utils/Logging';
import RegistrationToken from '../../../types/RegistrationToken';
import RegistrationTokenSecurity from './security/RegistrationTokenSecurity';
@@ -22,8 +21,7 @@ import moment from 'moment';
const MODULE_NAME = 'RegistrationTokenService';
export default class RegistrationTokenService {
- static async handleCreateRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction) {
- try {
+ static async handleCreateRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
const filteredRequest = RegistrationTokenSecurity.filterRegistrationTokenCreateRequest(req.body);
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
@@ -36,10 +34,8 @@ export default class RegistrationTokenService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.CREATE,
- entity: Entity.TOKEN,
- module: MODULE_NAME,
- method: 'handleCreateRegistrationToken'
+ action: Action.CREATE, entity: Entity.TOKEN,
+ module: MODULE_NAME, method: 'handleCreateRegistrationToken'
});
}
} else if (!Authorizations.canCreateRegistrationToken(req.user, null)) {
@@ -47,24 +43,19 @@ export default class RegistrationTokenService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.CREATE,
- entity: Entity.TOKEN,
- module: MODULE_NAME,
- method: 'handleCreateRegistrationToken'
+ action: Action.CREATE, entity: Entity.TOKEN,
+ module: MODULE_NAME, method: 'handleCreateRegistrationToken'
});
}
-
if (!filteredRequest.description) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.GENERAL_ERROR,
message: 'The description must be provided',
- module: MODULE_NAME,
- method: 'handleCreateRegistrationToken',
+ module: MODULE_NAME, method: 'handleCreateRegistrationToken',
user: req.user
});
}
-
const registrationToken: RegistrationToken = {
siteAreaID: filteredRequest.siteAreaID,
description: filteredRequest.description,
@@ -80,14 +71,9 @@ export default class RegistrationTokenService {
// Ok
res.json(RegistrationTokenSecurity.filterRegistrationTokenResponse(registrationToken, req.user));
next();
- } catch (error) {
- // Log
- Logging.logActionExceptionMessageAndSendResponse(action, error, req, res, next);
- }
}
- static async handleDeleteRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction) {
- try {
+ static async handleDeleteRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
const tokenID = RegistrationTokenSecurity.filterRegistrationTokenByIDRequest(req.query);
// Check Mandatory fields
if (!tokenID) {
@@ -95,8 +81,7 @@ export default class RegistrationTokenService {
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.GENERAL_ERROR,
message: 'Registration Token\'s ID must be provided',
- module: MODULE_NAME,
- method: 'handleDeleteRegistrationToken',
+ module: MODULE_NAME, method: 'handleDeleteRegistrationToken',
user: req.user
});
}
@@ -105,10 +90,8 @@ export default class RegistrationTokenService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.DELETE,
- entity: Entity.TOKEN,
- module: MODULE_NAME,
- method: 'handleDeleteRegistrationToken',
+ action: Action.DELETE, entity: Entity.TOKEN,
+ module: MODULE_NAME, method: 'handleDeleteRegistrationToken',
value: tokenID
});
}
@@ -128,14 +111,9 @@ export default class RegistrationTokenService {
// Ok
res.json(Constants.REST_RESPONSE_SUCCESS);
next();
- } catch (error) {
- // Log
- Logging.logActionExceptionMessageAndSendResponse(action, error, req, res, next);
- }
}
- static async handleRevokeRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction) {
- try {
+ static async handleRevokeRegistrationToken(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
const tokenID = RegistrationTokenSecurity.filterRegistrationTokenByIDRequest(req.query);
// Check Mandatory fields
if (!tokenID) {
@@ -143,8 +121,7 @@ export default class RegistrationTokenService {
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.GENERAL_ERROR,
message: 'Registration Token\'s ID must be provided',
- module: MODULE_NAME,
- method: 'handleRevokeRegistrationToken',
+ module: MODULE_NAME, method: 'handleRevokeRegistrationToken',
user: req.user
});
}
@@ -153,10 +130,8 @@ export default class RegistrationTokenService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.UPDATE,
- entity: Entity.TOKEN,
- module: MODULE_NAME,
- method: 'handleRevokeRegistrationToken',
+ action: Action.UPDATE, entity: Entity.TOKEN,
+ module: MODULE_NAME, method: 'handleRevokeRegistrationToken',
value: tokenID
});
}
@@ -179,44 +154,34 @@ export default class RegistrationTokenService {
// Ok
res.json(Constants.REST_RESPONSE_SUCCESS);
next();
- } catch (error) {
- // Log
- Logging.logActionExceptionMessageAndSendResponse(action, error, req, res, next);
- }
}
- static async handleGetRegistrationTokens(action: ServerAction, req: Request, res: Response, next: NextFunction) {
- try {
+ static async handleGetRegistrationTokens(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check auth
if (!Authorizations.canListRegistrationTokens(req.user)) {
// Not Authorized!
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.LIST,
- entity: Entity.TOKENS,
- module: MODULE_NAME,
- method: 'handleGetRegistrationTokens'
+ action: Action.LIST, entity: Entity.TOKENS,
+ module: MODULE_NAME, method: 'handleGetRegistrationTokens'
});
}
const filteredRequest = RegistrationTokenSecurity.filterRegistrationTokensRequest(req.query);
-
- const params = {
- siteAreaID: filteredRequest.siteAreaID
- };
-
- if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
- params['siteIDs'] = Authorizations.getAuthorizedSiteAdminIDs(req.user, null);
- }
-
- const dbParams: DbParams = {
+ // Get the tokens
+ const registrationTokens = await RegistrationTokenStorage.getRegistrationTokens(req.user.tenantID,
+ {
+ siteAreaID: filteredRequest.siteAreaID,
+ siteIDs: Authorizations.getAuthorizedSiteAdminIDs(req.user, null),
+ },
+ {
limit: filteredRequest.Limit,
skip: filteredRequest.Skip,
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
- };
-
- const registrationTokens = await RegistrationTokenStorage.getRegistrationTokens(req.user.tenantID, params, dbParams);
+ }
+ );
+ // Build OCPP URL
registrationTokens.result.forEach((registrationToken) => {
registrationToken.ocpp15SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_15, OCPPProtocol.SOAP, registrationToken.id);
registrationToken.ocpp16SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.SOAP, registrationToken.id);
@@ -227,8 +192,5 @@ export default class RegistrationTokenService {
RegistrationTokenSecurity.filterRegistrationTokensResponse(registrationTokens, req.user);
res.json(registrationTokens);
next();
- } catch (error) {
- Logging.logActionExceptionMessageAndSendResponse(action, error, req, res, next);
- }
}
}

MODIFY
src/storage/mongodb/RegistrationTokenStorage.ts
src/storage/mongodb/RegistrationTokenStorage.ts
@@ -38,7 +38,7 @@ export default class RegistrationTokenStorage {
}
static async getRegistrationTokens(tenantID: string,
- params: { id?: string; siteIDs?: string; siteAreaID?: string } = {}, dbParams: DbParams):
+ params: { id?: string; siteIDs?: string[]; siteAreaID?: string } = {}, dbParams: DbParams):
Promise<DataResult<RegistrationToken>> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getRegistrationTokens');
@@ -48,7 +48,6 @@ export default class RegistrationTokenStorage {
const limit = Utils.checkRecordLimit(dbParams.limit);
// Check Skip
const skip = Utils.checkRecordSkip(dbParams.skip);
-
// Set the filters
const filters: any = {};
// Build filter
@@ -59,34 +58,24 @@ export default class RegistrationTokenStorage {
if (params.id) {
filters._id = Utils.convertToObjectID(params.id);
}
-
if (!Utils.isEmptyArray(params.siteIDs)) {
// Build filter
filters['siteArea.siteID'] = {
- $in: params.siteIDs
+ $in: params.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
};
}
-
// Create Aggregation
const aggregation = [];
-
- DatabaseUtils.pushSiteAreaLookupInAggregation(
- {
- tenantID,
- aggregation,
- localField: 'siteAreaID',
- foreignField: '_id',
- asField: 'siteArea',
- oneToOneCardinality: true
+ DatabaseUtils.pushSiteAreaLookupInAggregation({
+ tenantID, aggregation, localField: 'siteAreaID', foreignField: '_id',
+ asField: 'siteArea', oneToOneCardinality: true
});
-
// Filters
if (filters) {
aggregation.push({
$match: filters
});
}
-
// Limit records?
if (!dbParams.onlyRecordCount) {
// Always limit the nbr of record to avoid perfs issues
@@ -106,7 +95,6 @@ export default class RegistrationTokenStorage {
}
// Remove the limit
aggregation.pop();
-
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
@@ -124,12 +112,10 @@ export default class RegistrationTokenStorage {
aggregation.push({
$limit: (limit > 0 && limit < Constants.DB_RECORD_COUNT_CEIL) ? limit : Constants.DB_RECORD_COUNT_CEIL
});
-
// Read DB
const registrationTokens = await global.database.getCollection<any>(tenantID, 'registrationtokens')
.aggregate(aggregation, { collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 }, allowDiskUse: true })
.toArray();
-
// Debug
Logging.traceEnd(MODULE_NAME, 'getRegistrationTokens', uniqueTimerID,
{ params, limit: dbParams.limit, skip: dbParams.skip, sort: dbParams.sort });

Fixed SiteIDs in Token management + clean the code