Added companyID to Asset
MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -70,7 +70,8 @@ export default class AssetService {
// Validate request
const filteredRequest = AssetValidator.getInstance().validateAssetConsumptionCreateReq({ ...req.query, ...req.body });
// Check and get Asset
- const asset = await UtilsService.checkAndGetAssetAuthorization(req.tenant, req.user, filteredRequest.assetID, Action.CREATE_CONSUMPTION, action, null, { withSiteArea: true });
+ const asset = await UtilsService.checkAndGetAssetAuthorization(
+ req.tenant, req.user, filteredRequest.assetID, Action.CREATE_CONSUMPTION, action, null, { withSiteArea: true });
// Check if connection ID exists
if (!Utils.isNullOrUndefined(asset.connectionID)) {
throw new AppError({
@@ -400,11 +401,12 @@ export default class AssetService {
let siteArea: SiteArea = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredAssetRequest.siteAreaID) {
siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
- req.tenant, req.user, filteredAssetRequest.siteAreaID, Action.UPDATE, action, filteredAssetRequest, null, false);
+ req.tenant, req.user, filteredAssetRequest.siteAreaID, Action.UPDATE, action, filteredAssetRequest, { withSite: true }, false);
}
// Create asset
const newAsset: Asset = {
...filteredAssetRequest,
+ companyID: siteArea?.site ? siteArea.site.companyID : null,
siteID: siteArea ? siteArea.siteID : null,
issuer: true,
createdBy: { id: req.user.id },
@@ -437,14 +439,15 @@ export default class AssetService {
let siteArea: SiteArea = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
- req.tenant, req.user, filteredRequest.siteAreaID, Action.UPDATE, action, filteredRequest, null, false);
+ req.tenant, req.user, filteredRequest.siteAreaID, Action.UPDATE, action, filteredRequest, { withSite: true }, false);
}
// Check and get asset
const asset = await UtilsService.checkAndGetAssetAuthorization(req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest);
// Update Asset values and persist
asset.name = filteredRequest.name;
- asset.siteAreaID = filteredRequest.siteAreaID;
+ asset.companyID = siteArea?.site ? siteArea.site.companyID : null;
asset.siteID = siteArea ? siteArea.siteID : null;
+ asset.siteAreaID = filteredRequest.siteAreaID;
asset.assetType = filteredRequest.assetType;
asset.excludeFromSmartCharging = filteredRequest.excludeFromSmartCharging;
asset.variationThresholdPercent = filteredRequest.variationThresholdPercent;

MODIFY
src/storage/mongodb/AssetStorage.ts
src/storage/mongodb/AssetStorage.ts
@@ -45,8 +45,9 @@ export default class AssetStorage {
const assetMDB: any = {
_id: assetToSave.id ? DatabaseUtils.convertToObjectID(assetToSave.id) : new ObjectId(),
name: assetToSave.name,
- siteAreaID: DatabaseUtils.convertToObjectID(assetToSave.siteAreaID),
+ companyID: DatabaseUtils.convertToObjectID(assetToSave.companyID),
siteID: DatabaseUtils.convertToObjectID(assetToSave.siteID),
+ siteAreaID: DatabaseUtils.convertToObjectID(assetToSave.siteAreaID),
coordinates: Utils.hasValidGpsCoordinates(assetToSave.coordinates) ? assetToSave.coordinates.map(
(coordinate) => Utils.convertToFloat(coordinate)) : [],
assetType: assetToSave.assetType,

MODIFY
src/types/Asset.ts
src/types/Asset.ts
@@ -2,14 +2,17 @@ import Consumption, { AbstractCurrentConsumption } from './Consumption';
import { AssetAuthorizationActions } from './Authorization';
import CreatedUpdatedProps from './CreatedUpdatedProps';
+import Site from './Site';
import SiteArea from './SiteArea';
export default interface Asset extends CreatedUpdatedProps, AbstractCurrentConsumption, AssetAuthorizationActions {
id: string;
name: string;
+ companyID: string;
+ siteID?: string;
+ site?: Site;
siteAreaID: string;
siteArea?: SiteArea;
- siteID?: string;
assetType: AssetType;
fluctuationPercent: number;
staticValueWatt: number;

Added companyID to Asset