Added getConsumption / assignUser validation schemas
ADD
src/assets/server/rest/v1/schemas/transaction/transaction-get-consumption.json
+{
+ "title": "Get Transaction Consumption Request",
+ "type": "object",
+ "properties": {
+ "TransactionId": {
+ "$ref": "transaction.json#/definitions/id"
+ },
+ "WithTag": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "WithUser": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "WithCar": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "LoadAllConsumptions": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ }
+ },
+ "required": [
+ "TransactionId"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/transaction/transactions-assign-user.json
+{
+ "title": "Get Transaction Consumption Request",
+ "type": "object",
+ "properties": {
+ "TagID": {
+ "$ref": "tag.json#/definitions/id"
+ },
+ "UserID": {
+ "$ref": "user.json#/definitions/id"
+ }
+ },
+ "required": [
+ "TagID",
+ "UserID"
+ ]
+}

MODIFY
src/server/rest/v1/router/api/TransactionRouter.ts
src/server/rest/v1/router/api/TransactionRouter.ts
@@ -113,6 +113,7 @@ export default class TransactionRouter {
protected buildRouteTransactionsRefund(): void {
this.router.post(`/${ServerRoute.REST_TRANSACTIONS_REFUND}`, async (req: Request, res: Response, next: NextFunction) => {
+ req.body.transactionsIDs = req.body.transactionIds;
await RouterUtils.handleServerAction(TransactionService.handleRefundTransactions.bind(this), ServerAction.TRANSACTIONS_REFUND, req, res, next);
});
}

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -86,8 +86,8 @@ export default class TransactionService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.REFUND,
Action.REFUND_TRANSACTION, Entity.TRANSACTION, MODULE_NAME, 'handleRefundTransactions');
// Filter
- const filteredRequest = TransactionSecurity.filterTransactionsRefund(req.body);
- if (!filteredRequest.transactionIds) {
+ const filteredRequest = TransactionValidator.getInstance().validateTransactionsGetByIDsReq(req.body);
+ if (!filteredRequest.transactionsIDs) {
// Not Found!
throw new AppError({
source: Constants.CENTRAL_SERVER,
@@ -99,7 +99,7 @@ export default class TransactionService {
});
}
const transactionsToRefund: Transaction[] = [];
- for (const transactionId of filteredRequest.transactionIds) {
+ for (const transactionId of filteredRequest.transactionsIDs) {
const transaction = await TransactionStorage.getTransaction(req.user.tenantID, transactionId, { withUser: true });
if (!transaction) {
await Logging.logError({
@@ -362,7 +362,7 @@ export default class TransactionService {
});
}
// Filter
- const filteredRequest = TransactionSecurity.filterAssignTransactionsToUser(req.query);
+ const filteredRequest = TransactionValidator.getInstance().validateTransactionsAssignUserReq(req.query);
// Check
if (!filteredRequest.TagID) {
throw new AppError({
@@ -463,7 +463,7 @@ export default class TransactionService {
public static async handleTransactionSoftStop(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const transactionId = TransactionSecurity.filterTransactionSoftStop(req.body);
+ const transactionId = TransactionValidator.getInstance().validateTransactionGetReq(req.body).ID;
// Transaction Id is mandatory
UtilsService.assertIdIsProvided(action, transactionId, MODULE_NAME, 'handleTransactionSoftStop', req.user);
// Check auth
@@ -539,7 +539,7 @@ export default class TransactionService {
public static async handleGetTransactionConsumption(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const filteredRequest = TransactionSecurity.filterConsumptionFromTransactionRequest(req.query);
+ const filteredRequest = TransactionValidator.getInstance().validateTransactionGetConsumptionReq(req.query);
// Transaction Id is mandatory
UtilsService.assertIdIsProvided(action, filteredRequest.TransactionId, MODULE_NAME,
'handleGetConsumptionFromTransaction', req.user);
@@ -780,7 +780,7 @@ export default class TransactionService {
}
const filter: any = { stop: { $exists: true } };
// Filter
- const filteredRequest = TransactionSecurity.filterTransactionsRequest(req.query);
+ const filteredRequest = TransactionValidator.getInstance().validateTransactionsGetReq(req.query);
if (Authorizations.isBasic(req.user)) {
filter.ownerID = req.user.id;
}

MODIFY
src/server/rest/v1/validator/TransactionValidator.ts
src/server/rest/v1/validator/TransactionValidator.ts
-import { HttpPushTransactionCdrRequest, HttpTransactionRequest, HttpTransactionsRequest } from '../../../../types/requests/HttpTransactionRequest';
+import { HttpAssignTransactionsToUserRequest, HttpConsumptionFromTransactionRequest, HttpPushTransactionCdrRequest, HttpTransactionRequest, HttpTransactionsRequest } from '../../../../types/requests/HttpTransactionRequest';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from './SchemaValidator';
@@ -11,6 +11,8 @@ export default class TransactionValidator extends SchemaValidator {
private transactionGet: Schema;
private transactionsGetByIDs: Schema;
private transactionPushCDR: Schema;
+ private transactionGetConsumption: Schema;
+ private transactionsAssignUser: Schema;
private constructor() {
@@ -19,6 +21,8 @@ export default class TransactionValidator extends SchemaValidator {
this.transactionGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transaction-get.json`, 'utf8'));
this.transactionsGetByIDs = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/transaction/transactions-get-by-ids.json`, 'utf8'));
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'));
}
public static getInstance(): TransactionValidator {
@@ -51,4 +55,16 @@ export default class TransactionValidator extends SchemaValidator {
this.validate(this.transactionPushCDR, data);
return data;
}
+
+ public validateTransactionGetConsumptionReq(data: any): HttpConsumptionFromTransactionRequest {
+ // Validate schema
+ this.validate(this.transactionGetConsumption, data);
+ return data;
+ }
+
+ public validateTransactionsAssignUserReq(data: any): HttpAssignTransactionsToUserRequest {
+ // Validate schema
+ this.validate(this.transactionsAssignUser, data);
+ return data;
+ }
}

Added getConsumption / assignUser validation schemas