Change tenant id to object SiteAreaStorage
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -751,7 +751,7 @@ export default class Authorizations {
foundSiteArea = false;
} else if (!chargingStation.siteArea) {
chargingStation.siteArea = await SiteAreaStorage.getSiteArea(
- tenant.id, chargingStation.siteAreaID, { withSite: true });
+ tenant, chargingStation.siteAreaID, { withSite: true });
if (!chargingStation.siteArea) {
foundSiteArea = false;
}

MODIFY
src/client/ocpi/EmspOCPIClient.ts
src/client/ocpi/EmspOCPIClient.ts
@@ -345,7 +345,7 @@ export default class EmspOCPIClient extends OCPIClient {
}
const locationName = site.name + Constants.OCPI_SEPARATOR + location.id;
// Handle Site Area
- const siteAreas = await SiteAreaStorage.getSiteAreas(this.tenant.id,
+ const siteAreas = await SiteAreaStorage.getSiteAreas(this.tenant,
{ siteIDs: [site.id], name: locationName, issuer: false, withSite: true },
Constants.DB_PARAMS_SINGLE_RECORD);
let siteArea = !Utils.isEmptyArray(siteAreas.result) ? siteAreas.result[0] : null;
@@ -371,7 +371,7 @@ export default class EmspOCPIClient extends OCPIClient {
Utils.convertToFloat(location.coordinates.latitude)
];
}
- siteArea.id = await SiteAreaStorage.saveSiteArea(this.tenant.id, siteArea, false);
+ siteArea.id = await SiteAreaStorage.saveSiteArea(this.tenant, siteArea, false);
}
if (!Utils.isEmptyArray(location.evses)) {
for (const evse of location.evses) {

MODIFY
src/scheduler/tasks/CheckAndComputeSmartChargingTask.ts
src/scheduler/tasks/CheckAndComputeSmartChargingTask.ts
@@ -17,7 +17,7 @@ export default class CheckAndComputeSmartChargingTask extends SchedulerTask {
if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION) &&
Utils.isTenantComponentActive(tenant, TenantComponents.SMART_CHARGING)) {
// Get all site areas
- const siteAreas = await SiteAreaStorage.getSiteAreas(tenant.id,
+ const siteAreas = await SiteAreaStorage.getSiteAreas(tenant,
{ smartCharging: true },
Constants.DB_PARAMS_MAX_LIMIT);
// Get Site Area

MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -1596,7 +1596,7 @@ export default class OCPPService {
// Smart Charging must be active
if (Utils.isTenantComponentActive(tenant, TenantComponents.SMART_CHARGING)) {
// Get Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(tenant.id, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(tenant, chargingStation.siteAreaID);
if (siteArea && siteArea.smartCharging) {
const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea, 30);
if (siteAreaLock) {
@@ -1754,7 +1754,7 @@ export default class OCPPService {
newChargingStation.registrationStatus = RegistrationStatus.ACCEPTED;
// Assign to Site Area
if (token.siteAreaID) {
- const siteArea = await SiteAreaStorage.getSiteArea(tenant.id, token.siteAreaID, { withSite: true });
+ const siteArea = await SiteAreaStorage.getSiteArea(tenant, token.siteAreaID, { withSite: true });
if (siteArea) {
newChargingStation.companyID = siteArea.site?.companyID;
newChargingStation.siteID = siteArea.siteID;

MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -1070,7 +1070,7 @@ export default class OCPPUtils {
// Save Site Area max consumption
if (siteArea) {
siteArea.maximumPower = consumption.limitSiteAreaWatts;
- await SiteAreaStorage.saveSiteArea(tenant.id, siteArea);
+ await SiteAreaStorage.saveSiteArea(tenant, siteArea);
}
}
consumption.smartChargingActive = siteArea.smartCharging;
@@ -2188,7 +2188,7 @@ export default class OCPPUtils {
private static async setConnectorPhaseAssignment(tenant: Tenant, chargingStation: ChargingStation, connector: Connector, nrOfPhases?: number): Promise<void> {
const csNumberOfPhases = nrOfPhases ?? Utils.getNumberOfConnectedPhases(chargingStation, null, connector.connectorId);
if (chargingStation.siteAreaID) {
- const siteArea = await SiteAreaStorage.getSiteArea(tenant.id, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(tenant, chargingStation.siteAreaID);
// Phase Assignment to Grid has to be handled only for Site Area with 3 phases
if (siteArea.numberOfPhases === 3) {
switch (csNumberOfPhases) {

MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -527,7 +527,7 @@ export default class AssetService {
// Check Site Area
let siteArea: SiteArea = null;
if (filteredRequest.siteAreaID) {
- siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
+ siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.siteAreaID);
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleCreateAsset', req.user);
}
@@ -585,7 +585,7 @@ export default class AssetService {
// Check Site Area
let siteArea: SiteArea = null;
if (filteredRequest.siteAreaID) {
- siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
+ siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.siteAreaID);
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleUpdateAsset', req.user);
}

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -72,7 +72,7 @@ export default class ChargingStationService {
let siteArea: SiteArea = null;
// Check the Site Area
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
- siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID, { withSite: true });
+ siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.siteAreaID, { withSite: true });
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.siteAreaID}' does not exist.`,
MODULE_NAME, 'handleUpdateChargingStationParams', req.user);
}
@@ -372,7 +372,7 @@ export default class ChargingStationService {
let siteID = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
// Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, chargingStation.siteAreaID);
siteID = siteArea ? siteArea.siteID : null;
}
// Check Auth
@@ -547,7 +547,7 @@ export default class ChargingStationService {
const filteredRequest = ChargingStationValidator.getInstance().validateSmartChargingTriggerReq(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.SiteAreaID, MODULE_NAME, 'handleTriggerSmartCharging', req.user);
// Get Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.SiteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.SiteAreaID);
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.SiteAreaID}' does not exist`,
MODULE_NAME, 'handleTriggerSmartCharging', req.user);
// Check auth
@@ -681,7 +681,7 @@ export default class ChargingStationService {
let siteID = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
// Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, chargingStation.siteAreaID);
siteID = siteArea ? siteArea.siteID : null;
}
// Check Auth
@@ -793,7 +793,7 @@ export default class ChargingStationService {
let siteID = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
// Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, chargingStation.siteAreaID);
siteID = siteArea ? siteArea.siteID : null;
}
// Check auth
@@ -1662,7 +1662,7 @@ export default class ChargingStationService {
let siteID = null;
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
// Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, chargingStation.siteAreaID);
siteID = siteArea ? siteArea.siteID : null;
}
// Check Auth

MODIFY
src/server/rest/v1/service/RegistrationTokenService.ts
src/server/rest/v1/service/RegistrationTokenService.ts
@@ -27,7 +27,7 @@ export default class RegistrationTokenService {
// Check Auth
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
// Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.siteAreaID);
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleCreateRegistrationToken', req.user);
if (!await Authorizations.canCreateRegistrationToken(req.user, siteArea.siteID)) {
@@ -113,7 +113,7 @@ export default class RegistrationTokenService {
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
// Check Site Area if it's provided
if (filteredRequest.siteAreaID) {
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
+ const siteArea = await SiteAreaStorage.getSiteArea(req.tenant, filteredRequest.siteAreaID);
UtilsService.assertObjectExists(action, siteArea, `Site Area ID '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleUpdateRegistrationToken', req.user);
}

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -20,6 +20,7 @@ import SiteAreaSecurity from './security/SiteAreaSecurity';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import SmartChargingFactory from '../../../../integration/smart-charging/SmartChargingFactory';
import TenantComponents from '../../../../types/TenantComponents';
+import TenantStorage from '../../../../storage/mongodb/TenantStorage';
import Utils from '../../../../utils/Utils';
import UtilsService from './UtilsService';
import moment from 'moment';
@@ -42,9 +43,9 @@ export default class SiteAreaService {
req.tenant, req.user, siteArea, filteredRequest.assetIDs, action);
// Save
if (action === ServerAction.ADD_ASSET_TO_SITE_AREA) {
- await SiteAreaStorage.addAssetsToSiteArea(req.user.tenantID, siteArea, assets.map((asset) => asset.id));
+ await SiteAreaStorage.addAssetsToSiteArea(req.tenant, siteArea, assets.map((asset) => asset.id));
} else {
- await SiteAreaStorage.removeAssetsFromSiteArea(req.user.tenantID, filteredRequest.siteAreaID, assets.map((asset) => asset.id));
+ await SiteAreaStorage.removeAssetsFromSiteArea(req.tenant, filteredRequest.siteAreaID, assets.map((asset) => asset.id));
}
// Log
await Logging.logSecurityInfo({
@@ -95,10 +96,10 @@ export default class SiteAreaService {
// Save
if (action === ServerAction.ADD_CHARGING_STATIONS_TO_SITE_AREA) {
await SiteAreaStorage.addChargingStationsToSiteArea(
- req.user.tenantID, siteArea, chargingStations.map((chargingStation) => chargingStation.id));
+ req.tenant, siteArea, chargingStations.map((chargingStation) => chargingStation.id));
} else {
await SiteAreaStorage.removeChargingStationsFromSiteArea(
- req.user.tenantID, filteredRequest.siteAreaID, chargingStations.map((chargingStation) => chargingStation.id));
+ req.tenant, filteredRequest.siteAreaID, chargingStations.map((chargingStation) => chargingStation.id));
}
// Log
await Logging.logSecurityInfo({
@@ -124,7 +125,7 @@ export default class SiteAreaService {
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
req.tenant, req.user, siteAreaID, Action.DELETE, action);
// Delete
- await SiteAreaStorage.deleteSiteArea(req.user.tenantID, siteArea.id);
+ await SiteAreaStorage.deleteSiteArea(req.tenant, siteArea.id);
// Log
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
@@ -163,7 +164,8 @@ export default class SiteAreaService {
// Check mandatory fields
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetSiteAreaImage', req.user);
// Get it
- const siteAreaImage = await SiteAreaStorage.getSiteAreaImage(filteredRequest.TenantID, filteredRequest.ID);
+ const siteAreaImage = await SiteAreaStorage.getSiteAreaImage(
+ await TenantStorage.getTenant(filteredRequest.TenantID), filteredRequest.ID);
// Return
if (siteAreaImage?.image) {
let header = 'image';
@@ -196,7 +198,7 @@ export default class SiteAreaService {
return;
}
// Get the SiteAreas
- const siteAreas = await SiteAreaStorage.getSiteAreas(req.user.tenantID,
+ const siteAreas = await SiteAreaStorage.getSiteAreas(req.tenant,
{
issuer: filteredRequest.Issuer,
search: filteredRequest.Search,
@@ -300,7 +302,7 @@ export default class SiteAreaService {
createdOn: new Date()
} as SiteArea;
// Save
- newSiteArea.id = await SiteAreaStorage.saveSiteArea(req.user.tenantID, newSiteArea, true);
+ newSiteArea.id = await SiteAreaStorage.saveSiteArea(req.tenant, newSiteArea, true);
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handleCreateSiteArea',
@@ -367,7 +369,7 @@ export default class SiteAreaService {
siteArea.lastChangedBy = { 'id': req.user.id };
siteArea.lastChangedOn = new Date();
// Save
- await SiteAreaStorage.saveSiteArea(req.user.tenantID, siteArea, Utils.objectHasProperty(filteredRequest, 'image'));
+ await SiteAreaStorage.saveSiteArea(req.tenant, siteArea, Utils.objectHasProperty(filteredRequest, 'image'));
// Retrigger Smart Charging
if (filteredRequest.smartCharging) {
// FIXME: the lock acquisition can wait for 30s before timeout and the whole code execution timeout at 3s

MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -344,7 +344,7 @@ export default class TenantService {
if (filteredRequest.components && filteredRequest.components.smartCharging &&
tenant.components && tenant.components.smartCharging &&
!filteredRequest.components.smartCharging.active && tenant.components.smartCharging.active) {
- const siteAreas = await SiteAreaStorage.getSiteAreas(filteredRequest.id, { smartCharging: true }, Constants.DB_PARAMS_MAX_LIMIT);
+ const siteAreas = await SiteAreaStorage.getSiteAreas(tenant, { smartCharging: true }, Constants.DB_PARAMS_MAX_LIMIT);
if (siteAreas.count !== 0) {
throw new AppError({
source: Constants.CENTRAL_SERVER,

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -530,7 +530,7 @@ export default class UtilsService {
});
}
// Get SiteArea & check it exists
- const siteArea = await SiteAreaStorage.getSiteArea(tenant.id, siteAreaID,
+ const siteArea = await SiteAreaStorage.getSiteArea(tenant, siteAreaID,
{
...additionalFilters,
...authorizationFilter.filters,

MODIFY
src/storage/mongodb/SiteAreaStorage.ts
src/storage/mongodb/SiteAreaStorage.ts
@@ -7,22 +7,23 @@ import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
import { ObjectId } from 'mongodb';
import SiteArea from '../../types/SiteArea';
+import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'SiteAreaStorage';
export default class SiteAreaStorage {
- public static async addAssetsToSiteArea(tenantID: string, siteArea: SiteArea, assetIDs: string[]): Promise<void> {
+ public static async addAssetsToSiteArea(tenant: Tenant, siteArea: SiteArea, assetIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'addAssetsToSiteArea');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'addAssetsToSiteArea');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Site Area provided?
if (siteArea) {
// At least one Asset
if (assetIDs && assetIDs.length > 0) {
// Update all assets
- await global.database.getCollection<any>(tenantID, 'assets').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'assets').updateMany(
{ '_id': { $in: assetIDs.map((assetID) => DatabaseUtils.convertToObjectID(assetID)) } },
{
$set: {
@@ -33,20 +34,20 @@ export default class SiteAreaStorage {
}
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'addAssetsToSiteArea', uniqueTimerID, assetIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'addAssetsToSiteArea', uniqueTimerID, assetIDs);
}
- public static async removeAssetsFromSiteArea(tenantID: string, siteAreaID: string, assetIDs: string[]): Promise<void> {
+ public static async removeAssetsFromSiteArea(tenant: Tenant, siteAreaID: string, assetIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'removeAssetsFromSiteArea');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'removeAssetsFromSiteArea');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Site Area provided?
if (siteAreaID) {
// At least one Asset
if (assetIDs && assetIDs.length > 0) {
// Update all assets
- await global.database.getCollection<any>(tenantID, 'assets').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'assets').updateMany(
{ '_id': { $in: assetIDs.map((assetID) => DatabaseUtils.convertToObjectID(assetID)) } },
{
$set: {
@@ -57,28 +58,28 @@ export default class SiteAreaStorage {
}
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'removeAssetsFromSiteArea', uniqueTimerID, assetIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'removeAssetsFromSiteArea', uniqueTimerID, assetIDs);
}
- public static async getSiteAreaImage(tenantID: string, id: string): Promise<Image> {
+ public static async getSiteAreaImage(tenant: Tenant, id: string): Promise<Image> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getSiteAreaImage');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getSiteAreaImage');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Read DB
- const siteAreaImageMDB = await global.database.getCollection<{ _id: ObjectId; image: string }>(tenantID, 'siteareaimages')
+ const siteAreaImageMDB = await global.database.getCollection<{ _id: ObjectId; image: string }>(tenant.id, 'siteareaimages')
.findOne({ _id: DatabaseUtils.convertToObjectID(id) });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getSiteAreaImage', uniqueTimerID, siteAreaImageMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getSiteAreaImage', uniqueTimerID, siteAreaImageMDB);
return {
id: id, image: siteAreaImageMDB ? siteAreaImageMDB.image : null
};
}
- public static async getSiteArea(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID,
+ public static async getSiteArea(tenant: Tenant, id: string = Constants.UNKNOWN_OBJECT_ID,
params: { withSite?: boolean; withChargingStations?: boolean, withAvailableChargingStations?: boolean; withImage?: boolean } = {},
projectFields?: string[]): Promise<SiteArea> {
- const siteAreasMDB = await SiteAreaStorage.getSiteAreas(tenantID, {
+ const siteAreasMDB = await SiteAreaStorage.getSiteAreas(tenant, {
siteAreaIDs: [id],
withSite: params.withSite,
withChargingStations: params.withChargingStations,
@@ -88,11 +89,11 @@ export default class SiteAreaStorage {
return siteAreasMDB.count === 1 ? siteAreasMDB.result[0] : null;
}
- public static async saveSiteArea(tenantID: string, siteAreaToSave: SiteArea, saveImage = false): Promise<string> {
+ public static async saveSiteArea(tenant: Tenant, siteAreaToSave: SiteArea, saveImage = false): Promise<string> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveSiteArea');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveSiteArea');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Set
const siteAreaMDB: any = {
_id: !siteAreaToSave.id ? new ObjectId() : DatabaseUtils.convertToObjectID(siteAreaToSave.id),
@@ -121,20 +122,20 @@ export default class SiteAreaStorage {
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(siteAreaMDB, siteAreaToSave);
// Modify
- await global.database.getCollection<SiteArea>(tenantID, 'siteareas').findOneAndUpdate(
+ await global.database.getCollection<SiteArea>(tenant.id, 'siteareas').findOneAndUpdate(
{ _id: siteAreaMDB._id },
{ $set: siteAreaMDB },
{ upsert: true, returnDocument: 'after' }
);
if (saveImage) {
- await SiteAreaStorage.saveSiteAreaImage(tenantID, siteAreaMDB._id.toString(), siteAreaToSave.image);
+ await SiteAreaStorage.saveSiteAreaImage(tenant, siteAreaMDB._id.toString(), siteAreaToSave.image);
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveSiteArea', uniqueTimerID, siteAreaMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveSiteArea', uniqueTimerID, siteAreaMDB);
return siteAreaMDB._id.toString();
}
- public static async getSiteAreas(tenantID: string,
+ public static async getSiteAreas(tenant: Tenant,
params: {
siteAreaIDs?: string[]; search?: string; siteIDs?: string[]; companyIDs?: string[]; withSite?: boolean; issuer?: boolean; name?: string;
withChargingStations?: boolean; withOnlyChargingStations?: boolean; withAvailableChargingStations?: boolean;
@@ -142,9 +143,9 @@ export default class SiteAreaStorage {
} = {},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<SiteArea>> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getSiteAreas');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getSiteAreas');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Clone before updating the values
dbParams = Utils.cloneObject(dbParams);
// Check Limit
@@ -190,7 +191,7 @@ export default class SiteAreaStorage {
// Company
if (!Utils.isEmptyArray(params.companyIDs)) {
DatabaseUtils.pushSiteLookupInAggregation({
- tenantID, aggregation, localField: 'siteID', foreignField: '_id',
+ tenantID: tenant.id, aggregation, localField: 'siteID', foreignField: '_id',
asField: 'site', oneToOneCardinality: true
});
filters['site.companyID'] = {
@@ -219,13 +220,13 @@ export default class SiteAreaStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const siteAreasCountMDB = await global.database.getCollection<DataResult<SiteArea>>(tenantID, 'siteareas')
+ const siteAreasCountMDB = await global.database.getCollection<DataResult<SiteArea>>(tenant.id, 'siteareas')
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
// Return only the count
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getSiteAreas', uniqueTimerID, siteAreasCountMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getSiteAreas', uniqueTimerID, siteAreasCountMDB);
return {
count: (siteAreasCountMDB.length > 0 ? siteAreasCountMDB[0].count : 0),
result: []
@@ -255,14 +256,14 @@ export default class SiteAreaStorage {
// Sites
if (params.withSite) {
DatabaseUtils.pushSiteLookupInAggregation({
- tenantID, aggregation, localField: 'siteID', foreignField: '_id',
+ tenantID: tenant.id, aggregation, localField: 'siteID', foreignField: '_id',
asField: 'site', oneToOneCardinality: true
});
}
// Charging Stations
if (params.withChargingStations || params.withOnlyChargingStations || params.withAvailableChargingStations) {
DatabaseUtils.pushChargingStationLookupInAggregation({
- tenantID, aggregation, localField: '_id', foreignField: 'siteAreaID',
+ tenantID: tenant.id, aggregation, localField: '_id', foreignField: 'siteAreaID',
asField: 'chargingStations'
});
}
@@ -274,7 +275,7 @@ export default class SiteAreaStorage {
$concat: [
`${Utils.buildRestServerURL()}/client/util/SiteAreaImage?ID=`,
{ $toString: '$_id' },
- `&TenantID=${tenantID}&LastChangedOn=`,
+ `&TenantID=${tenant.id}&LastChangedOn=`,
{ $toString: '$lastChangedOn' }
]
}
@@ -284,7 +285,7 @@ export default class SiteAreaStorage {
// Convert Object ID to string
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'siteID');
// Add Last Changed / Created
- DatabaseUtils.pushCreatedLastChangedInAggregation(tenantID, aggregation);
+ DatabaseUtils.pushCreatedLastChangedInAggregation(tenant.id, aggregation);
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Project
@@ -294,7 +295,7 @@ export default class SiteAreaStorage {
'chargingStations.deleted', 'chargingStations.cannotChargeInParallel', 'chargingStations.public', 'chargingStations.inactive']);
}
// Read DB
- const siteAreasMDB = await global.database.getCollection<SiteArea>(tenantID, 'siteareas')
+ const siteAreasMDB = await global.database.getCollection<SiteArea>(tenant.id, 'siteareas')
.aggregate(aggregation, {
allowDiskUse: true
})
@@ -324,7 +325,7 @@ export default class SiteAreaStorage {
}
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getSiteAreas', uniqueTimerID, siteAreasMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getSiteAreas', uniqueTimerID, siteAreasMDB);
// Ok
return {
projectedFields: projectFields,
@@ -334,17 +335,17 @@ export default class SiteAreaStorage {
};
}
- public static async addChargingStationsToSiteArea(tenantID: string, siteArea: SiteArea, chargingStationIDs: string[]): Promise<void> {
+ public static async addChargingStationsToSiteArea(tenant: Tenant, siteArea: SiteArea, chargingStationIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'addChargingStationsToSiteArea');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'addChargingStationsToSiteArea');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Site provided?
if (siteArea) {
// At least one ChargingStation
if (chargingStationIDs && chargingStationIDs.length > 0) {
// Update all chargers
- await global.database.getCollection<any>(tenantID, 'chargingstations').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'chargingstations').updateMany(
{ '_id': { $in: chargingStationIDs } },
{
$set: {
@@ -356,20 +357,20 @@ export default class SiteAreaStorage {
}
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'addChargingStationsToSiteArea', uniqueTimerID, chargingStationIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'addChargingStationsToSiteArea', uniqueTimerID, chargingStationIDs);
}
- public static async removeChargingStationsFromSiteArea(tenantID: string, siteAreaID: string, chargingStationIDs: string[]): Promise<void> {
+ public static async removeChargingStationsFromSiteArea(tenant: Tenant, siteAreaID: string, chargingStationIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'removeChargingStationsFromSiteArea');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'removeChargingStationsFromSiteArea');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Site provided?
if (siteAreaID) {
// At least one ChargingStation
if (chargingStationIDs && chargingStationIDs.length > 0) {
// Update all chargers
- await global.database.getCollection<any>(tenantID, 'chargingstations').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'chargingstations').updateMany(
{ '_id': { $in: chargingStationIDs } },
{
$set: {
@@ -381,67 +382,67 @@ export default class SiteAreaStorage {
}
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'removeChargingStationsFromSiteArea', uniqueTimerID, chargingStationIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'removeChargingStationsFromSiteArea', uniqueTimerID, chargingStationIDs);
}
- public static async deleteSiteArea(tenantID: string, id: string): Promise<void> {
- await SiteAreaStorage.deleteSiteAreas(tenantID, [id]);
+ public static async deleteSiteArea(tenant: Tenant, id: string): Promise<void> {
+ await SiteAreaStorage.deleteSiteAreas(tenant, [id]);
}
- public static async deleteSiteAreas(tenantID: string, siteAreaIDs: string[]): Promise<void> {
+ public static async deleteSiteAreas(tenant: Tenant, siteAreaIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteSiteAreas');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteSiteAreas');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Remove Charging Station's Site Area
- await global.database.getCollection<any>(tenantID, 'chargingstations').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'chargingstations').updateMany(
{ siteAreaID: { $in: siteAreaIDs.map((ID) => DatabaseUtils.convertToObjectID(ID)) } },
{ $set: { siteAreaID: null } }
);
// Remove Asset's Site Area
- await global.database.getCollection<any>(tenantID, 'assets').updateMany(
+ await global.database.getCollection<any>(tenant.id, 'assets').updateMany(
{ siteAreaID: { $in: siteAreaIDs.map((ID) => DatabaseUtils.convertToObjectID(ID)) } },
{ $set: { siteAreaID: null } }
);
// Delete SiteArea
- await global.database.getCollection<any>(tenantID, 'siteareas').deleteMany(
+ await global.database.getCollection<any>(tenant.id, 'siteareas').deleteMany(
{ '_id': { $in: siteAreaIDs.map((ID) => DatabaseUtils.convertToObjectID(ID)) } }
);
// Delete Image
- await global.database.getCollection<any>(tenantID, 'sitesareaimages').deleteMany(
+ await global.database.getCollection<any>(tenant.id, 'sitesareaimages').deleteMany(
{ '_id': { $in: siteAreaIDs.map((ID) => DatabaseUtils.convertToObjectID(ID)) } }
);
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteSiteAreas', uniqueTimerID, siteAreaIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteSiteAreas', uniqueTimerID, siteAreaIDs);
}
- public static async deleteSiteAreasFromSites(tenantID: string, siteIDs: string[]): Promise<void> {
+ public static async deleteSiteAreasFromSites(tenant: Tenant, siteIDs: string[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteSiteAreasFromSites');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteSiteAreasFromSites');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Find site areas to delete
- const siteareas: string[] = (await global.database.getCollection<any>(tenantID, 'siteareas')
+ const siteareas: string[] = (await global.database.getCollection<any>(tenant.id, 'siteareas')
.find({ siteID: { $in: siteIDs.map((id) => DatabaseUtils.convertToObjectID(id)) } })
.project({ _id: 1 }).toArray()).map((idWrapper): string => idWrapper._id.toString());
// Delete site areas
- await SiteAreaStorage.deleteSiteAreas(tenantID, siteareas);
+ await SiteAreaStorage.deleteSiteAreas(tenant, siteareas);
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteSiteAreasFromSites', uniqueTimerID, siteIDs);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteSiteAreasFromSites', uniqueTimerID, siteIDs);
}
- private static async saveSiteAreaImage(tenantID: string, siteAreaID: string, siteAreaImageToSave: string): Promise<void> {
+ private static async saveSiteAreaImage(tenant: Tenant, siteAreaID: string, siteAreaImageToSave: string): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveSiteAreaImage');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveSiteAreaImage');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Modify
- await global.database.getCollection<any>(tenantID, 'siteareaimages').findOneAndUpdate(
+ await global.database.getCollection<any>(tenant.id, 'siteareaimages').findOneAndUpdate(
{ '_id': DatabaseUtils.convertToObjectID(siteAreaID) },
{ $set: { image: siteAreaImageToSave } },
{ upsert: true, returnDocument: 'after' }
);
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveSiteAreaImage', uniqueTimerID, siteAreaImageToSave);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveSiteAreaImage', uniqueTimerID, siteAreaImageToSave);
}
}

MODIFY
src/storage/mongodb/SiteStorage.ts
src/storage/mongodb/SiteStorage.ts
@@ -508,7 +508,7 @@ export default class SiteStorage {
// Check Tenant
DatabaseUtils.checkTenantObject(tenant);
// Delete all Site Areas
- await SiteAreaStorage.deleteSiteAreasFromSites(tenant.id, ids);
+ await SiteAreaStorage.deleteSiteAreasFromSites(tenant, ids);
// Convert
const cids: ObjectId[] = ids.map((id) => DatabaseUtils.convertToObjectID(id));
// Delete Site
@@ -536,7 +536,7 @@ export default class SiteStorage {
.toArray())
.map((site): string => site._id.toString());
// Delete all Site Areas
- await SiteAreaStorage.deleteSiteAreasFromSites(tenant.id, siteIDs);
+ await SiteAreaStorage.deleteSiteAreasFromSites(tenant, siteIDs);
// Delete Sites
await SiteStorage.deleteSites(tenant, siteIDs);
// Debug

MODIFY
test/api/context/ContextBuilder.ts
test/api/context/ContextBuilder.ts
@@ -303,8 +303,8 @@ export default class ContextBuilder {
siteAreaTemplate.numberOfPhases = siteAreaDef.numberOfPhases;
siteAreaTemplate.voltage = siteAreaDef.voltage;
console.log(`${buildTenant.id} (${buildTenant.name}) - Site Area '${siteAreaTemplate.name}'`);
- const sireAreaID = await SiteAreaStorage.saveSiteArea(buildTenant.id, siteAreaTemplate);
- const siteAreaModel = await SiteAreaStorage.getSiteArea(buildTenant.id, sireAreaID);
+ const sireAreaID = await SiteAreaStorage.saveSiteArea(buildTenant, siteAreaTemplate);
+ const siteAreaModel = await SiteAreaStorage.getSiteArea(buildTenant, sireAreaID);
const siteAreaContext = siteContext.addSiteArea(siteAreaModel);
const relevantCS = ContextDefinition.TENANT_CHARGING_STATION_LIST.filter(
(chargingStation) => chargingStation.siteAreaNames && chargingStation.siteAreaNames.includes(siteAreaModel.name) === true);

Change tenant id to object SiteAreaStorage