From 2dcc7e161c31ddf4951950b229a15a7f6f3ca6a7 Mon Sep 17 00:00:00 2001 From: Rafael Date: Tue, 24 Oct 2023 15:51:48 -0300 Subject: [PATCH] Improve validation of activities import (#2496) * Improve validation of activities import: expects positive values for fee, quantity and unitPrice * Update changelog --------- Co-authored-by: Rafael Claudio Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com> --- CHANGELOG.md | 1 + apps/api/src/app/order/create-order.dto.ts | 7 ++++++- apps/api/src/app/order/update-order.dto.ts | 7 ++++++- apps/client/src/app/services/import-activities.service.ts | 4 ++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d8584c5..05081fc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Improved the style and wording of the position detail dialog +- Improved the validation in the activities import (expects positive values for `fee`, `quantity` and `unitPrice`) - Changed the currency selector in the create or update account dialog to `@angular/material/autocomplete` - Upgraded `uuid` from version `9.0.0` to `9.0.1` - Upgraded `yahoo-finance2` from version `2.8.0` to `2.8.1` diff --git a/apps/api/src/app/order/create-order.dto.ts b/apps/api/src/app/order/create-order.dto.ts index 49b193ca..3eafa704 100644 --- a/apps/api/src/app/order/create-order.dto.ts +++ b/apps/api/src/app/order/create-order.dto.ts @@ -13,7 +13,9 @@ import { IsISO8601, IsNumber, IsOptional, - IsString + IsPositive, + IsString, + Min } from 'class-validator'; import { isString } from 'lodash'; @@ -48,9 +50,11 @@ export class CreateOrderDto { date: string; @IsNumber() + @Min(0) fee: number; @IsNumber() + @IsPositive() quantity: number; @IsString() @@ -64,6 +68,7 @@ export class CreateOrderDto { type: Type; @IsNumber() + @IsPositive() unitPrice: number; @IsBoolean() diff --git a/apps/api/src/app/order/update-order.dto.ts b/apps/api/src/app/order/update-order.dto.ts index a8c33c40..9d968aa8 100644 --- a/apps/api/src/app/order/update-order.dto.ts +++ b/apps/api/src/app/order/update-order.dto.ts @@ -13,7 +13,9 @@ import { IsISO8601, IsNumber, IsOptional, - IsString + IsPositive, + IsString, + Min } from 'class-validator'; import { isString } from 'lodash'; @@ -47,12 +49,14 @@ export class UpdateOrderDto { date: string; @IsNumber() + @Min(0) fee: number; @IsString() id: string; @IsNumber() + @IsPositive() quantity: number; @IsString() @@ -66,5 +70,6 @@ export class UpdateOrderDto { type: Type; @IsNumber() + @IsPositive() unitPrice: number; } diff --git a/apps/client/src/app/services/import-activities.service.ts b/apps/client/src/app/services/import-activities.service.ts index 76087192..af7e8e6c 100644 --- a/apps/client/src/app/services/import-activities.service.ts +++ b/apps/client/src/app/services/import-activities.service.ts @@ -286,7 +286,7 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.QUANTITY_KEYS) { if (isFinite(item[key])) { - return item[key]; + return Math.abs(item[key]); } } @@ -372,7 +372,7 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.UNIT_PRICE_KEYS) { if (isFinite(item[key])) { - return item[key]; + return Math.abs(item[key]); } }