Add CompanID to ChargingStations
MODIFY
src/assets/server/rest/v1/schemas/chargingstation/chargingstations-get.json
src/assets/server/rest/v1/schemas/chargingstation/chargingstations-get.json
"SiteID": {
"$ref": "common.json#/definitions/ids"
},
+ "CompanyID": {
+ "$ref": "common.json#/definitions/ids"
+ },
"WithSite": {
"type": "boolean",
"sanitize": "mongo"

MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
+import AddCompanyIDPropertToChargingStationsTask from './tasks/AddCompanyIDPropertToChargingStationsTask';
import Constants from '../utils/Constants';
import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
@@ -86,6 +87,7 @@ export default class MigrationHandler {
private static createMigrationTasks(): MigrationTask[] {
const currentMigrationTasks: MigrationTask[] = [];
currentMigrationTasks.push(new RemoveDuplicateTagVisualIDsTask());
+ currentMigrationTasks.push(new AddCompanyIDPropertToChargingStationsTask());
return currentMigrationTasks;
}

ADD
src/migration/tasks/AddCompanyIDPropertToChargingStationsTask.ts
+import ChargingStationStorage from '../../storage/mongodb/ChargingStationStorage';
+import Constants from '../../utils/Constants';
+import DatabaseUtils from '../../storage/mongodb/DatabaseUtils';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Site from '../../types/Site';
+import SiteStorage from '../../storage/mongodb/SiteStorage';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AddCompanyIDPropertToChargingStationsTask';
+
+export default class AddCompanyIDPropertToChargingStationsTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ let updated = 0;
+ let site: Site;
+ const chargingStations = await ChargingStationStorage.getChargingStations(tenant.id, {
+ }, Constants.DB_PARAMS_MAX_LIMIT, ['id', 'siteID']);
+ if (chargingStations.count > 0) {
+ for (const chargingStation of chargingStations.result) {
+ site = await SiteStorage.getSite(tenant, chargingStation.siteID, {}, ['companyID']);
+ if (site && site.companyID) {
+ await global.database.getCollection<any>(tenant.id, 'chargingstations').updateOne(
+ { _id: chargingStation.id },
+ {
+ $set: { companyID: DatabaseUtils.convertToObjectID(site.companyID) }
+ }
+ );
+ updated++;
+ }
+ }
+ }
+ // Log in the default tenant
+ if (updated > 0) {
+ await Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ module: MODULE_NAME, method: 'migrateTenant',
+ action: ServerAction.MIGRATION,
+ message: `${updated} Charging station(s) company have been added in Tenant ${Utils.buildTenantName(tenant)}`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AddCompanyIDPropertToChargingStationsTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -1391,6 +1391,7 @@ export default class ChargingStationService {
issuer: filteredRequest.Issuer,
siteIDs: siteIDs,
siteAreaIDs: filteredRequest.SiteAreaID ? filteredRequest.SiteAreaID.split('|') : null,
+ companyIDs: filteredRequest.CompanyID ? filteredRequest.CompanyID.split('|') : null,
includeDeleted: filteredRequest.IncludeDeleted,
locCoordinates: filteredRequest.LocCoordinates,
locMaxDistanceMeters: filteredRequest.LocMaxDistanceMeters,

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -206,6 +206,7 @@ export default class SiteAreaService {
locCoordinates: filteredRequest.LocCoordinates,
locMaxDistanceMeters: filteredRequest.LocMaxDistanceMeters,
siteIDs: (filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
+ companyIDs: (filteredRequest.CompanyID ? filteredRequest.CompanyID.split('|') : null),
...authorizationSiteAreasFilter.filters
},
{

MODIFY
src/server/rest/v1/service/security/SiteAreaSecurity.ts
src/server/rest/v1/service/security/SiteAreaSecurity.ts
@@ -47,7 +47,8 @@ export default class SiteAreaSecurity {
WithSite: !request.WithSite ? false : UtilsSecurity.filterBoolean(request.WithSite),
WithChargeBoxes: !request.WithChargeBoxes ? false : UtilsSecurity.filterBoolean(request.WithChargeBoxes),
WithAvailableChargers: !request.WithAvailableChargers ? false : UtilsSecurity.filterBoolean(request.WithAvailableChargers),
- SiteID: sanitize(request.SiteID)
+ SiteID: sanitize(request.SiteID),
+ CompanyID: sanitize(request.CompanyID)
} as HttpSiteAreasRequest;
if (Utils.objectHasProperty(request, 'Issuer')) {
filteredRequest.Issuer = UtilsSecurity.filterBoolean(request.Issuer);

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -175,7 +175,7 @@ export default class ChargingStationStorage {
search?: string; chargingStationIDs?: string[]; chargingStationSerialNumbers?: string[]; siteAreaIDs?: string[]; withNoSiteArea?: boolean;
connectorStatuses?: string[]; connectorTypes?: string[]; statusChangedBefore?: Date;
ocpiEvseUid?: string; ocpiEvseID?: string; ocpiLocationID?: string; oicpEvseID?: string;
- siteIDs?: string[]; withSite?: boolean; includeDeleted?: boolean; offlineSince?: Date; issuer?: boolean;
+ siteIDs?: string[]; companyIDs?: string[]; withSite?: boolean; includeDeleted?: boolean; offlineSince?: Date; issuer?: boolean;
locCoordinates?: number[]; locMaxDistanceMeters?: number; public?: boolean;
},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<ChargingStation>> {
@@ -314,6 +314,11 @@ export default class ChargingStationStorage {
// Query by siteID
filters.siteID = { $in: params.siteIDs.map((id) => DatabaseUtils.convertToObjectID(id)) };
}
+ // Check Company ID
+ if (!Utils.isEmptyArray(params.companyIDs)) {
+ // Query by siteID
+ filters.companyID = { $in: params.companyIDs.map((id) => DatabaseUtils.convertToObjectID(id)) };
+ }
// Date before provided
if (params.statusChangedBefore && moment(params.statusChangedBefore).isValid()) {
aggregation.push({

MODIFY
src/storage/mongodb/SiteAreaStorage.ts
src/storage/mongodb/SiteAreaStorage.ts
@@ -136,7 +136,7 @@ export default class SiteAreaStorage {
public static async getSiteAreas(tenantID: string,
params: {
- siteAreaIDs?: string[]; search?: string; siteIDs?: string[]; withSite?: boolean; issuer?: boolean; name?: string;
+ siteAreaIDs?: string[]; search?: string; siteIDs?: string[];companyIDs?: string[]; withSite?: boolean; issuer?: boolean; name?: string;
withChargingStations?: boolean; withOnlyChargingStations?: boolean; withAvailableChargingStations?: boolean;
locCoordinates?: number[]; locMaxDistanceMeters?: number; smartCharging?: boolean; withImage?: boolean;
} = {},
@@ -187,6 +187,17 @@ export default class SiteAreaStorage {
$in: params.siteIDs.map((siteID) => DatabaseUtils.convertToObjectID(siteID))
};
}
+ // Company
+ if (!Utils.isEmptyArray(params.companyIDs)) {
+ DatabaseUtils.pushSiteLookupInAggregation({
+ tenantID, aggregation, localField: 'siteID', foreignField: '_id',
+ asField: 'site', oneToOneCardinality: true
+ });
+ filters['site.companyID'] = {
+ $in: params.companyIDs.map((companyID) => companyID)
+ };
+ params.withSite = false;
+ }
if (Utils.objectHasProperty(params, 'issuer') && Utils.isBoolean(params.issuer)) {
filters.issuer = params.issuer;
}

MODIFY
src/types/ChargingStation.ts
src/types/ChargingStation.ts
@@ -21,6 +21,7 @@ export default interface ChargingStation extends CreatedUpdatedProps {
public: boolean;
siteAreaID?: string;
siteID?: string;
+ companyID?: string;
chargePointSerialNumber: string;
chargePointModel: string;
chargeBoxSerialNumber: string;

MODIFY
src/types/requests/HttpChargingStationRequest.ts
src/types/requests/HttpChargingStationRequest.ts
@@ -38,6 +38,7 @@ export interface HttpChargingStationsRequest extends HttpDatabaseRequest {
ConnectorType?: string;
ChargingStationID?: string;
SiteID?: string;
+ CompanyID?: string;
WithSite?: boolean;
SiteAreaID?: string;
IncludeDeleted?: boolean;

MODIFY
src/types/requests/HttpSiteAreaRequest.ts
src/types/requests/HttpSiteAreaRequest.ts
@@ -11,6 +11,7 @@ export interface HttpSiteAreasRequest extends HttpDatabaseRequest {
Issuer: boolean;
Search: string;
SiteID?: string;
+ CompanyID?: string;
WithSite?: boolean;
WithChargeBoxes?: boolean;
WithAvailableChargers: boolean;

Add CompanID to ChargingStations