Added create/update/getConsumption/assign assets/chargingstations schemas
ADD
src/assets/server/rest/v1/schemas/site-area/site-area-assign-assets.json
+{
+ "$id": "site-area-assign-assets",
+ "title": "Assign assets to a Site Area Request",
+ "type": "object",
+ "properties": {
+ "siteAreaID": {
+ "$ref": "common#/definitions/id"
+ },
+ "assetIDs": {
+ "type": "array",
+ "items": {
+ "$ref": "common#/definitions/id"
+ }
+ }
+ },
+ "required": [
+ "siteAreaID",
+ "assetIDs"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site-area/site-area-assign-charging-stations.json
+{
+ "$id": "site-area-assign-charging-stations",
+ "title": "Assign charging stations to a Site Area Request",
+ "type": "object",
+ "properties": {
+ "siteAreaID": {
+ "$ref": "common#/definitions/id"
+ },
+ "chargingStationIDs": {
+ "type": "array",
+ "items": {
+ "$ref": "common#/definitions/string-id"
+ }
+ }
+ },
+ "required": [
+ "siteAreaID",
+ "chargingStationIDs"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site-area/site-area-create.json
+{
+ "$id": "site-area-create",
+ "title": "Create Site Area Request",
+ "type": "object",
+ "properties": {
+ "name": {
+ "$ref": "site-area#/definitions/name"
+ },
+ "address": {
+ "$ref": "site-area#/definitions/address"
+ },
+ "maximumPower": {
+ "$ref": "site-area#/definitions/maximumPower"
+ },
+ "numberOfPhases": {
+ "$ref": "site-area#/definitions/numberOfPhases"
+ },
+ "voltage": {
+ "$ref": "site-area#/definitions/voltage"
+ },
+ "smartCharging": {
+ "$ref": "site-area#/definitions/smartCharging"
+ },
+ "accessControl": {
+ "$ref": "site-area#/definitions/accessControl"
+ },
+ "siteID": {
+ "$ref": "site-area#/definitions/siteID"
+ },
+ "image": {
+ "$ref": "site-area#/definitions/image"
+ }
+ },
+ "required": [
+ "name",
+ "siteID"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site-area/site-area-get-consumption.json
+{
+ "$id": "site-area-get",
+ "title": "Get Site Area Request",
+ "type": "object",
+ "properties": {
+ "SiteAreaID": {
+ "$ref": "common#/definitions/id"
+ },
+ "StartDate": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "EndDate": {
+ "type": "string",
+ "format": "date-time"
+ }
+ },
+ "required": [
+ "SiteAreaID",
+ "StartDate",
+ "EndDate"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site-area/site-area-update.json
+{
+ "$id": "site-area-update",
+ "title": "Update Site Area Request",
+ "type": "object",
+ "properties": {
+ "id": {
+ "$ref": "site-area#/definitions/id"
+ },
+ "name": {
+ "$ref": "site-area#/definitions/name"
+ },
+ "address": {
+ "$ref": "site-area#/definitions/address"
+ },
+ "maximumPower": {
+ "$ref": "site-area#/definitions/maximumPower"
+ },
+ "numberOfPhases": {
+ "$ref": "site-area#/definitions/numberOfPhases"
+ },
+ "voltage": {
+ "$ref": "site-area#/definitions/voltage"
+ },
+ "smartCharging": {
+ "$ref": "site-area#/definitions/smartCharging"
+ },
+ "accessControl": {
+ "$ref": "site-area#/definitions/accessControl"
+ },
+ "siteID": {
+ "$ref": "site-area#/definitions/siteID"
+ },
+ "image": {
+ "$ref": "site-area#/definitions/image"
+ }
+ },
+ "required": [
+ "id",
+ "name",
+ "siteID"
+ ]
+}

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -34,7 +34,7 @@ export default class SiteAreaService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ASSET,
Action.UPDATE, Entity.ASSET, MODULE_NAME, 'handleAssignAssetsToSiteArea');
// Filter request
- const filteredRequest = SiteAreaSecurity.filterAssignAssetsToSiteAreaRequest(req.body);
+ const filteredRequest = SiteAreaValidator.getInstance().validateSiteAreaAssignAssetsReq(req.body);
// Check and Get Site Area
const authAction = action === ServerAction.ADD_ASSET_TO_SITE_AREA ? Action.ASSIGN_ASSETS_TO_SITE_AREA : Action.UNASSIGN_ASSETS_TO_SITE_AREA;
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
@@ -67,7 +67,7 @@ export default class SiteAreaService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.CHARGING_STATION, MODULE_NAME, 'handleAssignChargingStationsToSiteArea');
// Filter request
- const filteredRequest = SiteAreaSecurity.filterAssignChargingStationsToSiteAreaRequest(req.body);
+ const filteredRequest = SiteAreaValidator.getInstance().validateSiteAreaAssignChargingStationsReq(req.body);
// Check and Get Site Area
const authAction = action === ServerAction.ADD_CHARGING_STATIONS_TO_SITE_AREA ? Action.ASSIGN_CHARGING_STATIONS_TO_SITE_AREA : Action.UNASSIGN_CHARGING_STATIONS_TO_SITE_AREA;
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
@@ -241,17 +241,8 @@ export default class SiteAreaService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.LIST, Entity.SITE_AREA, MODULE_NAME, 'handleGetSiteAreaConsumption');
// Filter request
- const filteredRequest = SiteAreaSecurity.filterSiteAreaConsumptionRequest(req.query);
+ const filteredRequest = SiteAreaValidator.getInstance().validateSiteAreaGetConsumptionReq(req.query);
// Check dates
- if (!filteredRequest.StartDate || !filteredRequest.EndDate) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Start date and end date must be provided',
- module: MODULE_NAME, method: 'handleGetSiteAreaConsumption',
- user: req.user,
- action: action
- });
- }
if (filteredRequest.StartDate && filteredRequest.EndDate &&
moment(filteredRequest.StartDate).isAfter(moment(filteredRequest.EndDate))) {
throw new AppError({
@@ -283,12 +274,12 @@ export default class SiteAreaService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.CREATE, Entity.SITE_AREA, MODULE_NAME, 'handleCreateSiteArea');
// Filter request
- const filteredRequest = SiteAreaSecurity.filterSiteAreaCreateRequest(req.body);
+ const filteredRequest = SiteAreaValidator.getInstance().validateSiteAreaCreateReq(req.body);
// Check request data is valid
UtilsService.checkIfSiteAreaValid(filteredRequest, req);
// Check auth
const authorizationFilters = await AuthorizationService.checkAndGetSiteAreaAuthorizations(req.tenant, req.user,
- {}, Action.CREATE, filteredRequest as SiteArea);
+ {}, Action.CREATE, filteredRequest);
if (!authorizationFilters.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -327,12 +318,12 @@ export default class SiteAreaService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE_AREA, MODULE_NAME, 'handleUpdateSiteArea');
// Filter request
- const filteredRequest = SiteAreaSecurity.filterSiteAreaUpdateRequest(req.body);
+ const filteredRequest = SiteAreaValidator.getInstance().validateSiteAreaUpdateReq(req.body);
// Check Mandatory fields
UtilsService.checkIfSiteAreaValid(filteredRequest, req);
// Check and Get SiteArea
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
- req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest as SiteArea, {
+ req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest, {
withChargingStations: true,
}, false, true);
// Check Site auth

MODIFY
src/server/rest/v1/validator/SiteAreaValidator.ts
src/server/rest/v1/validator/SiteAreaValidator.ts
-import { HttpSiteAreaImageRequest, HttpSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
+import { HttpAssignAssetsToSiteAreaRequest, HttpAssignChargingStationToSiteAreaRequest, HttpSiteAreaConsumptionsRequest, HttpSiteAreaImageRequest, HttpSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from '../../../../validator/SchemaValidator';
+import SiteArea from '../../../../types/SiteArea';
import fs from 'fs';
import global from '../../../../types/GlobalType';
@@ -10,6 +11,11 @@ export default class SiteAreaValidator extends SchemaValidator {
private siteAreasGet: Schema;
private siteAreaGet: Schema;
private siteAreaGetImage: Schema;
+ private siteAreaCreate: Schema;
+ private siteAreaUpdate: Schema;
+ private siteAreaAssignChargingStations: Schema;
+ private siteAreaAssignAssets: Schema;
+ private siteAreaGetConsumption: Schema;
private constructor() {
@@ -17,6 +23,11 @@ export default class SiteAreaValidator extends SchemaValidator {
this.siteAreasGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-areas-get.json`, 'utf8'));
this.siteAreaGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-get.json`, 'utf8'));
this.siteAreaGetImage = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-get-image.json`, 'utf8'));
+ this.siteAreaCreate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-create.json`, 'utf8'));
+ this.siteAreaUpdate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-update.json`, 'utf8'));
+ this.siteAreaAssignChargingStations = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-assign-charging-stations.json`, 'utf8'));
+ this.siteAreaAssignAssets = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-assign-assets.json`, 'utf8'));
+ this.siteAreaGetConsumption = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site-area/site-area-get-consumption.json`, 'utf8'));
}
public static getInstance(): SiteAreaValidator {
@@ -37,4 +48,24 @@ export default class SiteAreaValidator extends SchemaValidator {
public validateSiteAreaGetImageReq(data: Record<string, unknown>): HttpSiteAreaImageRequest {
return this.validate(this.siteAreaGetImage, data);
}
+
+ public validateSiteAreaCreateReq(data: Record<string, unknown>): SiteArea {
+ return this.validate(this.siteAreaCreate, data);
+ }
+
+ public validateSiteAreaUpdateReq(data: Record<string, unknown>): SiteArea {
+ return this.validate(this.siteAreaUpdate, data);
+ }
+
+ public validateSiteAreaAssignChargingStationsReq(data: Record<string, unknown>): HttpAssignChargingStationToSiteAreaRequest {
+ return this.validate(this.siteAreaAssignChargingStations, data);
+ }
+
+ public validateSiteAreaAssignAssetsReq(data: Record<string, unknown>): HttpAssignAssetsToSiteAreaRequest {
+ return this.validate(this.siteAreaAssignAssets, data);
+ }
+
+ public validateSiteAreaGetConsumptionReq(data: Record<string, unknown>): HttpSiteAreaConsumptionsRequest {
+ return this.validate(this.siteAreaGetConsumption, data);
+ }
}

Added create/update/getConsumption/assign assets/chargingstations schemas