Clean code + eslint
MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -36,10 +36,10 @@ export default class AssetService {
const filteredRequest = AssetValidator.getInstance().validateAssetGetConsumptionsReq(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.AssetID, MODULE_NAME,
'handleGetAssetConsumption', req.user);
- // Check action authorization before querying storage
- const staticAuthorizationAssetFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.READ_CONSUMPTION, {});
+ // Retrieve authorizations for current action attempt
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.READ_CONSUMPTION, {});
// Get asset
- const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.AssetID, { dynamicOnly: true }, staticAuthorizationAssetFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.AssetID, { dynamicOnly: true }, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${filteredRequest.AssetID}' cannot be retrieved`,
MODULE_NAME, 'handleGetAssetConsumption', req.user);
// Check dynamic auth
@@ -85,10 +85,10 @@ export default class AssetService {
const filteredRequest = AssetValidator.getInstance().validateAssetConsumptionCreateReq({ ...req.query, ...req.body });
UtilsService.assertIdIsProvided(action, filteredRequest.assetID, MODULE_NAME,
'handleCreateAssetConsumption', req.user);
- // Check static auth before querying storage
- const staticAuthorizationAssetFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.CREATE_CONSUMPTION, {});
+ // Retrieve authorizations for current action attempt
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.CREATE_CONSUMPTION, {});
// Get Asset
- const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.assetID, { withSiteArea: true }, staticAuthorizationAssetFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.assetID, { withSiteArea: true }, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${filteredRequest.assetID}' cannot be retrieved to create consumption`,
MODULE_NAME, 'handleCreateAssetConsumption', req.user);
// Check dynamic auth
@@ -217,11 +217,11 @@ export default class AssetService {
// Filter request
const assetID = AssetValidator.getInstance().validateAssetGetReq(req.query).ID;
UtilsService.assertIdIsProvided(action, assetID, MODULE_NAME, 'handleRetrieveConsumption', req.user);
- // Check static auth before querying storage
- const staticAuthorizationAssetFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
+ // Retrieve authorizations for current action attempt
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
req.tenant, req.user, Action.RETRIEVE_CONSUMPTION, {});
// Get
- const asset = await AssetService.getAssetFromStorage(req.tenant, assetID, { dynamicOnly: true, usesPushAPI: false }, staticAuthorizationAssetFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, assetID, { dynamicOnly: true, usesPushAPI: false }, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${assetID}' consumption cannot be retrieved`,
MODULE_NAME, 'handleRetrieveConsumption', req.user);
// Dynamic auth
@@ -309,11 +309,11 @@ export default class AssetService {
const filteredRequest = AssetValidator.getInstance().validateAssetGetReq(req.query);
// Check Mandatory fields
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleDeleteAsset', req.user);
- // Check static auth before querying storage
- const staticAuthorizationAssetFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
+ // Retrieve authorizations for current action attempt
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
req.tenant, req.user, Action.DELETE, filteredRequest);
// Get
- const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.ID, { withSiteArea: filteredRequest.WithSiteArea }, staticAuthorizationAssetFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.ID, { withSiteArea: filteredRequest.WithSiteArea }, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${filteredRequest.ID}' cannot be deleted`,
MODULE_NAME, 'handleDeleteAsset', req.user);
// Dynamic auth
@@ -342,10 +342,10 @@ export default class AssetService {
// ID is mandatory
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetAsset', req.user);
// Check action authorization before querying storage
- const staticAuthorizationAssetFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(
req.tenant, req.user, Action.READ, filteredRequest);
// Get it
- const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.ID, { withSiteArea: filteredRequest.WithSiteArea }, staticAuthorizationAssetFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.ID, { withSiteArea: filteredRequest.WithSiteArea }, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleGetAsset', req.user);
// Dynamic auth
@@ -424,7 +424,7 @@ export default class AssetService {
// Check request is valid
const filteredAssetRequest = AssetValidator.getInstance().validateAssetCreateReq(req.body);
UtilsService.checkIfAssetValid(filteredAssetRequest, req);
- // Get dynamic auth
+ // Check authorizations for current action attempt
await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.CREATE, {}, filteredAssetRequest);
// Check Site Area authorization
let siteArea: SiteArea = null;
@@ -462,8 +462,8 @@ export default class AssetService {
// Filter
const filteredRequest = AssetValidator.getInstance().validateAssetUpdateReq(req.body);
UtilsService.checkIfAssetValid(filteredRequest, req);
- // Check action authorization before querying storage
- const staticAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.UPDATE, {}, filteredRequest);
+ // Retrieve authorizations for current action attempt
+ const actionOnAssetAuthorizationFilter = await AuthorizationService.checkAndGetAssetAuthorizations(req.tenant, req.user, Action.UPDATE, {}, filteredRequest);
// Check Site Area authorization
let siteArea: SiteArea = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
@@ -471,7 +471,7 @@ export default class AssetService {
req.tenant, req.user, filteredRequest.siteAreaID, Action.UPDATE, action, filteredRequest, null, false);
}
// Get asset
- const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.id, {}, staticAuthorizationFilter);
+ const asset = await AssetService.getAssetFromStorage(req.tenant, filteredRequest.id, {}, actionOnAssetAuthorizationFilter);
UtilsService.assertObjectExists(action, asset, `Asset ID '${filteredRequest.id}' cannot be modified`,
MODULE_NAME, 'handleUpdateAsset', req.user);
// Check dynamic auth

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -294,8 +294,6 @@ export default class AuthorizationService {
asset.canUpdate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.UPDATE, authorizationFilter, {}, asset);
asset.canRetrieveConsumption = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.RETRIEVE_CONSUMPTION, authorizationFilter, {}, asset);
asset.canReadConsumption = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.READ_CONSUMPTION, authorizationFilter, {}, asset);
- // Not used in dashboard
- // asset.canCreateConsumption = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.CREATE_CONSUMPTION, authorizationFilter, {}, asset);
asset.canCheckConnection = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.CHECK_CONNECTION, authorizationFilter, {}, asset);
// Optimize data over the net
Utils.removeCanPropertiesWithFalseValue(asset);

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -200,7 +200,6 @@ export interface RegistrationTokenAuthorizationActions extends AuthorizationActi
export interface AssetAuthorizationActions extends AuthorizationActions {
canRetrieveConsumption?:boolean;
canReadConsumption?:boolean;
- canCreateConsumption?:boolean;
canCheckConnection?:boolean;
}

Clean code + eslint