Added transactions-in-error validation schema
ADD
src/assets/server/rest/v1/schemas/transaction/transactions-inerror-get.json
+{
+ "title": "Get Transactions in error Request",
+ "type": "object",
+ "properties": {
+ "ChargingStationID": {
+ "$ref": "chargingstation.json#/definitions/ids"
+ },
+ "StartDateTime": {
+ "type": "string",
+ "format": "date-time",
+ "sanitize": "mongo"
+ },
+ "EndDateTime": {
+ "type": "string",
+ "format": "date-time",
+ "sanitize": "mongo"
+ },
+ "SiteID": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "SiteAreaID": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "ErrorType": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "ConnectorID": {
+ "$ref": "chargingstation.json#/definitions/connectorIDs"
+ },
+ "UserID": {
+ "$ref": "user.json#/definitions/ids"
+ },
+ "Search": {
+ "$ref": "common.json#/definitions/search"
+ },
+ "Limit": {
+ "$ref": "common.json#/definitions/limit"
+ },
+ "SortFields": {
+ "$ref": "common.json#/definitions/sortFields"
+ },
+ "Skip": {
+ "$ref": "common.json#/definitions/skip"
+ },
+ "ProjectFields": {
+ "$ref": "common.json#/definitions/projectFields"
+ }
+ }
+}

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -29,7 +29,6 @@ import TagStorage from '../../../../storage/mongodb/TagStorage';
import Tenant from '../../../../types/Tenant';
import TenantComponents from '../../../../types/TenantComponents';
import TenantStorage from '../../../../storage/mongodb/TenantStorage';
-import TransactionSecurity from './security/TransactionSecurity';
import TransactionStorage from '../../../../storage/mongodb/TransactionStorage';
import TransactionValidator from '../validator/TransactionValidator';
import User from '../../../../types/User';
@@ -904,7 +903,7 @@ export default class TransactionService {
}
const filter: any = {};
// Filter
- const filteredRequest = TransactionSecurity.filterTransactionsInErrorRequest(req.query);
+ const filteredRequest = TransactionValidator.getInstance().validateTransactionsInErrorGetReq(req.query);
// Site Area
const transactions = await TransactionStorage.getTransactionsInError(req.tenant,
{
@@ -922,7 +921,7 @@ export default class TransactionService {
{
limit: filteredRequest.Limit,
skip: filteredRequest.Skip,
- sort: filteredRequest.SortFields
+ sort: UtilsService.httpSortFieldsToMongoDB(filteredRequest.SortFields)
},
projectFields
);

DELETE
src/server/rest/v1/service/security/TransactionSecurity.ts
-import { HttpAssignTransactionsToUserRequest, HttpConsumptionFromTransactionRequest, HttpPushTransactionCdrRequest, HttpTransactionRequest, HttpTransactionsRefundRequest, HttpTransactionsRequest, HttpUnassignTransactionsToUserRequest } from '../../../../../types/requests/HttpTransactionRequest';
-
-import Utils from '../../../../../utils/Utils';
-import UtilsSecurity from './UtilsSecurity';
-import sanitize from 'mongo-sanitize';
-
-export default class TransactionSecurity {
- public static filterTransactionsRefund(request: any): HttpTransactionsRefundRequest {
- if (!Utils.objectHasProperty(request, 'transactionIds') || Utils.isEmptyArray(request.transactionIds)) {
- return { transactionIds: [] };
- }
- return { transactionIds: request.transactionIds.map(sanitize) };
- }
-
- public static filterAssignTransactionsToUser(request: any): HttpAssignTransactionsToUserRequest {
- return {
- TagID: sanitize(request.TagID),
- UserID: sanitize(request.UserID),
- };
- }
-
- static filterUnassignedTransactionsCountRequest(request: any): HttpUnassignTransactionsToUserRequest {
- return {
- TagID: sanitize(request.TagID),
- };
- }
-
- public static filterTransactionRequestByID(request: any): number {
- return Utils.convertToInt(sanitize(request.ID));
- }
-
- public static filterTransactionRequestByIDs(request: any): number[] {
- return request.transactionsIDs.map(sanitize);
- }
-
- public static filterTransactionSoftStop(request: any): number {
- return Utils.convertToInt(sanitize(request.ID));
- }
-
- public static filterTransactionRequest(request: any): HttpTransactionRequest {
- return {
- ID: Utils.convertToInt(sanitize(request.ID)),
- WithCar: UtilsSecurity.filterBoolean(request.WithCar),
- WithTag: UtilsSecurity.filterBoolean(request.WithTag),
- WithUser: UtilsSecurity.filterBoolean(request.WithUser),
- };
- }
-
- public static filterPushTransactionCdrRequest(request: any): HttpPushTransactionCdrRequest {
- return {
- transactionId: Utils.convertToInt(sanitize(request.transactionId))
- };
- }
-
- public static filterTransactionsRequest(request: any): HttpTransactionsRequest {
- const filteredRequest = {} as HttpTransactionsRequest;
- if (Utils.objectHasProperty(request, 'Issuer')) {
- filteredRequest.Issuer = UtilsSecurity.filterBoolean(request.Issuer);
- }
- filteredRequest.ChargingStationID = sanitize(request.ChargingStationID);
- filteredRequest.StartDateTime = sanitize(request.StartDateTime);
- filteredRequest.EndDateTime = sanitize(request.EndDateTime);
- filteredRequest.SiteID = sanitize(request.SiteID);
- filteredRequest.SiteAreaID = sanitize(request.SiteAreaID);
- filteredRequest.VisualTagID = sanitize(request.VisualTagID);
- filteredRequest.Search = sanitize(request.Search);
- filteredRequest.InactivityStatus = sanitize(request.InactivityStatus);
- filteredRequest.RefundStatus = sanitize(request.RefundStatus);
- filteredRequest.MinimalPrice = sanitize(request.MinimalPrice);
- filteredRequest.ConnectorID = sanitize(request.ConnectorID);
- if (Utils.objectHasProperty(request, 'Statistics')) {
- filteredRequest.Statistics = sanitize(request.Statistics);
- }
- if (Utils.objectHasProperty(request, 'UserID')) {
- filteredRequest.UserID = sanitize(request.UserID);
- }
- if (Utils.objectHasProperty(request, 'ReportIDs')) {
- filteredRequest.ReportIDs = sanitize(request.ReportIDs);
- }
- if (Utils.objectHasProperty(request, 'Status')) {
- filteredRequest.Status = sanitize(request.Status);
- }
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- UtilsSecurity.filterProject(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterTransactionsInErrorRequest(request: any): HttpTransactionsRequest {
- const filteredRequest: HttpTransactionsRequest = {} as HttpTransactionsRequest;
- // Handle picture
- filteredRequest.ChargingStationID = sanitize(request.ChargingStationID);
- filteredRequest.StartDateTime = sanitize(request.StartDateTime);
- filteredRequest.EndDateTime = sanitize(request.EndDateTime);
- filteredRequest.SiteID = sanitize(request.SiteID);
- filteredRequest.SiteAreaID = sanitize(request.SiteAreaID);
- filteredRequest.Search = sanitize(request.Search);
- filteredRequest.ErrorType = sanitize(request.ErrorType);
- filteredRequest.ConnectorID = sanitize(request.ConnectorID);
- if (Utils.objectHasProperty(request, 'UserID')) {
- filteredRequest.UserID = sanitize(request.UserID);
- }
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterConsumptionFromTransactionRequest(request: any): HttpConsumptionFromTransactionRequest {
- const filteredRequest: HttpConsumptionFromTransactionRequest = {} as HttpConsumptionFromTransactionRequest;
- // Set
- if (Utils.objectHasProperty(request, 'TransactionId')) {
- filteredRequest.TransactionId = Utils.convertToInt(sanitize(request.TransactionId));
- }
- if (Utils.objectHasProperty(request, 'WithTag')) {
- filteredRequest.WithTag = UtilsSecurity.filterBoolean(request.WithTag);
- }
- if (Utils.objectHasProperty(request, 'WithCar')) {
- filteredRequest.WithCar = UtilsSecurity.filterBoolean(request.WithCar);
- }
- if (Utils.objectHasProperty(request, 'WithUser')) {
- filteredRequest.WithUser = UtilsSecurity.filterBoolean(request.WithUser);
- }
- if (Utils.objectHasProperty(request, 'LoadAllConsumptions')) {
- filteredRequest.LoadAllConsumptions = Utils.convertToBoolean(sanitize(request.LoadAllConsumptions));
- }
- return filteredRequest;
- }
-}

MODIFY
src/server/rest/v1/validator/TransactionValidator.ts
src/server/rest/v1/validator/TransactionValidator.ts
@@ -14,6 +14,7 @@ export default class TransactionValidator extends SchemaValidator {
private transactionGetConsumption: Schema;
private transactionsAssignUser: Schema;
private transactionsUnassignedCountGet: Schema;
+ private transactionsInErrorGet: Schema;
private constructor() {
@@ -24,7 +25,8 @@ export default class TransactionValidator extends SchemaValidator {
this.transactionPushCDR = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transaction-push-cdr.json`, 'utf8'));
this.transactionGetConsumption = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transaction-get-consumption.json`, 'utf8'));
this.transactionsAssignUser = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transactions-assign-user.json`, 'utf8'));
- this.transactionsUnassignedCountGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transactions-unassigned-count-user.json`, 'utf8'));
+ this.transactionsUnassignedCountGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transactions-unassigned-count-get.json`, 'utf8'));
+ this.transactionsInErrorGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transactions-inerror-get.json`, 'utf8'));
}
public static getInstance(): TransactionValidator {
@@ -75,4 +77,10 @@ export default class TransactionValidator extends SchemaValidator {
this.validate(this.transactionsUnassignedCountGet, data);
return data;
}
+
+ public validateTransactionsInErrorGetReq(data: any): HttpTransactionsRequest {
+ // Validate schema
+ this.validate(this.transactionsInErrorGet, data);
+ return data;
+ }
}

Added transactions-in-error validation schema