Optimized Asset management
MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -43,7 +43,9 @@ export default class AssetService {
});
}
// Get it
- const asset = await AssetStorage.getAsset(req.user.tenantID, filteredRequest.AssetID);
+ const asset = await AssetStorage.getAsset(req.user.tenantID, filteredRequest.AssetID, {},
+ [ 'id', 'name' ]
+ );
UtilsService.assertObjectExists(action, asset, `Asset with ID '${filteredRequest.AssetID}' does not exist`,
MODULE_NAME, 'handleGetAssetConsumption', req.user);
// Check dates
@@ -74,9 +76,11 @@ export default class AssetService {
assetID: filteredRequest.AssetID,
startDate: filteredRequest.StartDate,
endDate: filteredRequest.EndDate
- });
+ }, [ 'startedAt', 'instantWatts', 'instantAmps', 'limitWatts', 'limitAmps' ]);
+ // Assign
+ asset.values = consumptions;
// Return
- res.json(AssetSecurity.filterAssetConsumptionResponse(asset, consumptions, req.user));
+ res.json(asset);
next();
}
@@ -225,14 +229,8 @@ export default class AssetService {
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
},
+ [ 'id', 'name', 'errorCodeDetails', 'errorCode' ]
);
- // Filter
- AssetSecurity.filterAssetsResponse(assets, req.user);
- // Limit to 100
- if (assets.result.length > 100) {
- assets.result.length = 100;
- }
- // Return
res.json(assets);
next();
}
@@ -300,11 +298,7 @@ export default class AssetService {
{ withSiteArea: filteredRequest.WithSiteArea });
UtilsService.assertObjectExists(action, asset, `Asset with ID '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleGetAsset', req.user);
- // Return
- res.json(
- // Filter
- AssetSecurity.filterAssetResponse(asset, req.user)
- );
+ res.json(asset);
next();
}
@@ -331,7 +325,6 @@ export default class AssetService {
// Check
UtilsService.assertObjectExists(action, assetImage, `Asset with ID '${assetID}' does not exist`,
MODULE_NAME, 'handleGetAssetImage', req.user);
- // Return
res.json({ id: assetImage.id, image: assetImage.image });
next();
}
@@ -361,11 +354,11 @@ export default class AssetService {
dynamicOnly: filteredRequest.DynamicOnly,
},
{ limit: filteredRequest.Limit, skip: filteredRequest.Skip, sort: filteredRequest.Sort, onlyRecordCount: filteredRequest.OnlyRecordCount },
- [ 'id', 'name', 'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.siteID', 'assetType', 'coordinates', 'dynamicAsset', 'connectionID', 'meterID', 'currentInstantWatts']
+ [
+ 'id', 'name', 'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.siteID', 'assetType', 'coordinates',
+ 'dynamicAsset', 'connectionID', 'meterID', 'currentInstantWatts'
+ ]
);
- // Filter
- AssetSecurity.filterAssetsResponse(assets, req.user);
- // Return
res.json(assets);
next();
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -612,9 +612,7 @@ export default class UserService {
action: action
});
}
- // Ok
res.json(
- // Filter
UserSecurity.filterUserResponse(user, req.user)
);
next();

MODIFY
src/server/rest/v1/service/security/AssetSecurity.ts
src/server/rest/v1/service/security/AssetSecurity.ts
@@ -48,41 +48,6 @@ export default class AssetSecurity {
return AssetSecurity._filterAssetRequest(request);
}
- public static filterAssetResponse(asset: Asset, loggedUser: UserToken): Asset {
- let filteredAsset: Asset;
- if (!asset) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadAsset(loggedUser)) {
- // Admin?
- if (Authorizations.isAdmin(loggedUser)) {
- // Yes: set all params
- filteredAsset = asset;
- } else {
- // Set only necessary info
- filteredAsset = {} as Asset;
- filteredAsset.id = asset.id;
- filteredAsset.name = asset.name;
- filteredAsset.siteAreaID = asset.siteAreaID;
- filteredAsset.assetType = asset.assetType;
- filteredAsset.coordinates = asset.coordinates;
- filteredAsset.image = asset.image;
- filteredAsset.dynamicAsset = asset.dynamicAsset;
- filteredAsset.connectionID = asset.connectionID;
- filteredAsset.meterID = asset.meterID;
- filteredAsset.currentInstantWatts = asset.currentInstantWatts;
- if (asset.siteArea) {
- filteredAsset.siteArea = SiteAreaSecurity.filterSiteAreaResponse(asset.siteArea, loggedUser);
- }
- }
- // Created By / Last Changed By
- UtilsSecurity.filterCreatedAndLastChanged(
- filteredAsset, asset, loggedUser);
- }
- return filteredAsset;
- }
-
public static filterAssetConsumptionRequest(request: any): HttpAssetConsumptionRequest {
return {
AssetID: sanitize(request.AssetID),
@@ -91,52 +56,6 @@ export default class AssetSecurity {
};
}
- public static filterAssetConsumptionResponse(asset: Asset, consumptions: Consumption[], loggedUser: UserToken): Asset {
- asset.values = [];
- if (!consumptions) {
- consumptions = [];
- }
- const filteredAsset = this.filterAssetResponse(asset, loggedUser);
- if (consumptions.length === 0) {
- filteredAsset.values = [];
- return filteredAsset;
- }
- // Clean
- filteredAsset.values = consumptions.map((consumption) => ({
- date: consumption.startedAt,
- instantWatts: consumption.instantWatts,
- instantAmps: consumption.instantAmps,
- limitWatts: consumption.limitWatts,
- limitAmps: consumption.limitAmps,
- }));
- // Add the last point (duration of the last consumption)
- if (consumptions.length > 0) {
- filteredAsset.values.push({
- ...filteredAsset.values[filteredAsset.values.length - 1],
- date: consumptions[consumptions.length - 1].endedAt,
- });
- }
- return filteredAsset;
- }
-
- public static filterAssetsResponse(assets: DataResult<Asset>, loggedUser: UserToken): void {
- const filteredAssets = [];
- if (!assets.result) {
- return null;
- }
- if (!Authorizations.canListAssets(loggedUser)) {
- return null;
- }
- for (const asset of assets.result) {
- // Add
- const filteredAsset = AssetSecurity.filterAssetResponse(asset, loggedUser);
- if (filteredAsset) {
- filteredAssets.push(filteredAsset);
- }
- }
- assets.result = filteredAssets;
- }
-
private static _filterAssetRequest(request: any): Partial<Asset> {
const filteredRequest: Partial<Asset> = {};
filteredRequest.name = sanitize(request.name),

MODIFY
src/storage/mongodb/AssetStorage.ts
src/storage/mongodb/AssetStorage.ts
@@ -14,11 +14,11 @@ const MODULE_NAME = 'AssetStorage';
export default class AssetStorage {
public static async getAsset(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID,
- params: { withSiteArea?: boolean} = {}): Promise<Asset> {
+ params: { withSiteArea?: boolean} = {}, projectFields?: string[]): Promise<Asset> {
const assetsMDB = await AssetStorage.getAssets(tenantID, {
assetIDs: [id],
withSiteArea: params.withSiteArea
- }, Constants.DB_PARAMS_SINGLE_RECORD);
+ }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return assetsMDB.count === 1 ? assetsMDB.result[0] : null;
}

MODIFY
src/types/Asset.ts
src/types/Asset.ts
-import { AbstractCurrentConsumption } from './Consumption';
+import Consumption, { AbstractCurrentConsumption } from './Consumption';
+
import CreatedUpdatedProps from './CreatedUpdatedProps';
import SiteArea from './SiteArea';
@@ -14,15 +15,7 @@ export default interface Asset extends CreatedUpdatedProps, AbstractCurrentConsu
dynamicAsset: boolean;
connectionID?: string;
meterID?: string;
- values: AssetConsumption[],
-}
-
-export interface AssetConsumption {
- date: Date;
- instantWatts: number;
- instantAmps: number;
- limitWatts: number;
- limitAmps: number;
+ values: Consumption[],
}
export enum SchneiderProperty {

Optimized Asset management