Added statistics validation schemas
ADD
src/assets/server/rest/v1/schemas/statistic/statistics-export.json
+{
+ "$id": "statistic-export",
+ "title": "Export statistics Request",
+ "type": "object",
+ "properties": {
+ "DataType": {
+ "type": "string",
+ "enum": [
+ "Consumption",
+ "Usage",
+ "Inactivity",
+ "Transactions",
+ "Pricing"
+ ],
+ "sanitize": "mongo"
+ },
+ "DataCategory": {
+ "type": "string",
+ "enum": [
+ "C",
+ "U"
+ ],
+ "sanitize": "mongo"
+ },
+ "DataScope": {
+ "type": "string",
+ "enum": [
+ "year",
+ "month"
+ ],
+ "sanitize": "mongo"
+ }
+ }
+}

ADD
src/assets/server/rest/v1/schemas/statistic/statistics-get.json
+{
+ "$id": "statistic",
+ "title": "Get statistics Request",
+ "type": "object",
+ "properties": {
+ "Year": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "StartDateTime": {
+ "type": "string",
+ "format": "date-time",
+ "sanitize": "mongo"
+ },
+ "EndDateTime": {
+ "type": "string",
+ "format": "date-time",
+ "sanitize": "mongo"
+ },
+ "SiteAreaID": {
+ "$ref": "common#/definitions/ids"
+ },
+ "ChargingStationID": {
+ "$ref": "common#/definitions/string-ids"
+ },
+ "UserID": {
+ "$ref": "common#/definitions/ids"
+ },
+ "SiteID": {
+ "$ref": "common#/definitions/ids"
+ }
+ }
+}

MODIFY
src/server/rest/v1/service/StatisticService.ts
src/server/rest/v1/service/StatisticService.ts
@@ -9,8 +9,8 @@ import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpStatisticsRequest from '../../../../types/requests/HttpStatisticRequest';
import { ServerAction } from '../../../../types/Server';
-import StatisticSecurity from './security/StatisticSecurity';
import StatisticsStorage from '../../../../storage/mongodb/StatisticsStorage';
+import StatisticsValidator from '../validator/StatisticsValidator';
import { TenantComponents } from '../../../../types/Tenant';
import UserToken from '../../../../types/UserToken';
import Utils from '../../../../utils/Utils';
@@ -34,7 +34,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -63,7 +63,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -92,7 +92,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -121,7 +121,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -150,7 +150,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -179,7 +179,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -208,7 +208,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -237,7 +237,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -266,7 +266,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -295,7 +295,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsGet(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
@@ -324,7 +324,7 @@ export default class StatisticService {
});
}
// Filter
- const filteredRequest = StatisticSecurity.filterExportStatisticsRequest(req.query);
+ const filteredRequest = StatisticsValidator.getInstance().validateStatisticsExport(req.query);
// Build filter
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Decisions
@@ -389,16 +389,16 @@ export default class StatisticService {
filter.endDateTime = filteredRequest.EndDateTime;
}
// Site
- if (filteredRequest.SiteIDs) {
- filter.siteIDs = filteredRequest.SiteIDs;
+ if (filteredRequest.SiteID) {
+ filter.siteIDs = filteredRequest.SiteID.split('|');
}
// Site Area
- if (filteredRequest.SiteAreaIDs) {
- filter.siteAreaIDs = filteredRequest.SiteAreaIDs;
+ if (filteredRequest.SiteAreaID) {
+ filter.siteAreaIDs = filteredRequest.SiteAreaID.split('|');
}
// Charge Box
- if (filteredRequest.ChargingStationIDs) {
- filter.chargeBoxIDs = filteredRequest.ChargingStationIDs;
+ if (filteredRequest.ChargingStationID) {
+ filter.chargeBoxIDs = filteredRequest.ChargingStationID.split('|');
}
// User
if (Authorizations.isBasic(loggedUser)) {
@@ -409,8 +409,8 @@ export default class StatisticService {
// Only for current user
filter.userIDs = [loggedUser.id];
}
- } else if (!Authorizations.isBasic(loggedUser) && filteredRequest.UserIDs) {
- filter.userIDs = filteredRequest.UserIDs;
+ } else if (!Authorizations.isBasic(loggedUser) && filteredRequest.UserID) {
+ filter.userIDs = filteredRequest.UserID.split('|');
}
return filter;
}

DELETE
src/server/rest/v1/service/security/StatisticSecurity.ts
-import HttpStatisticsRequest, { HttpMetricsStatisticsRequest } from '../../../../../types/requests/HttpStatisticRequest';
-
-import Utils from '../../../../../utils/Utils';
-import sanitize from 'mongo-sanitize';
-
-export default class StatisticSecurity {
- static filterStatisticsRequest(request: any): HttpStatisticsRequest {
- return {
- Year: Utils.convertToInt(sanitize(request.Year)),
- StartDateTime: request.StartDateTime ? sanitize(request.StartDateTime) : null,
- EndDateTime: request.EndDateTime ? sanitize(request.EndDateTime) : null,
- SiteAreaIDs: request.SiteAreaID ? sanitize(request.SiteAreaID).split('|') : null,
- ChargingStationIDs: request.ChargingStationID ? sanitize(request.ChargingStationID).split('|') : null,
- UserIDs: request.UserID ? sanitize(request.UserID).split('|') : null,
- SiteIDs: request.SiteID ? sanitize(request.SiteID).split('|') : null
- };
- }
-
- static filterMetricsStatisticsRequest(request: any): HttpMetricsStatisticsRequest {
- return { PeriodInMonth: request.PeriodInMonth ? sanitize(request.PeriodInMonth) : null };
- }
-
- static filterExportStatisticsRequest(request: any): HttpStatisticsRequest {
- return { ...StatisticSecurity.filterStatisticsRequest(request),
- DataType: sanitize(request.DataType),
- DataCategory: sanitize(request.DataCategory),
- DataScope: sanitize(request.DataScope)
- };
- }
-}

ADD
src/server/rest/v1/validator/StatisticsValidator.ts
+import HttpStatisticsRequest from '../../../../types/requests/HttpStatisticRequest';
+import Schema from '../../../../types/validator/Schema';
+import SchemaValidator from '../../../../validator/SchemaValidator';
+import fs from 'fs';
+import global from '../../../../types/GlobalType';
+
+export default class StatisticsValidator extends SchemaValidator {
+ private static instance: StatisticsValidator|null = null;
+ private statisticsGet: Schema;
+ private statisticsExport: Schema;
+
+ private constructor() {
+ super('StatisticsValidator');
+ this.statisticsGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/statistic/statistics-get.json`, 'utf8'));
+ this.statisticsExport = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/statistic/statistics-export.json`, 'utf8'));
+ }
+
+ public static getInstance(): StatisticsValidator {
+ if (!StatisticsValidator.instance) {
+ StatisticsValidator.instance = new StatisticsValidator();
+ }
+ return StatisticsValidator.instance;
+ }
+
+ public validateStatisticsGet(data: Record<string, unknown>): HttpStatisticsRequest {
+ return this.validate(this.statisticsGet, data);
+ }
+
+ public validateStatisticsExport(data: Record<string, unknown>): HttpStatisticsRequest {
+ return this.validate(this.statisticsExport, data);
+ }
+}

Added statistics validation schemas