refactor: api validation schema

This commit is contained in:
Ahmed Bouhuolia
2025-03-14 23:24:02 +02:00
parent 08de50e2b1
commit fd65ee9428
34 changed files with 628 additions and 102 deletions

View File

@@ -2,12 +2,11 @@ import { CreateTaxRate } from './commands/CreateTaxRate.service';
import { DeleteTaxRateService } from './commands/DeleteTaxRate.service';
import { EditTaxRateService } from './commands/EditTaxRate.service';
import { GetTaxRateService } from './queries/GetTaxRate.service';
// import { GetTaxRatesService } from './queries/GetTaxRates';
import { ActivateTaxRateService } from './commands/ActivateTaxRate.service';
import { InactivateTaxRateService } from './commands/InactivateTaxRate';
import { Injectable } from '@nestjs/common';
import { ICreateTaxRateDTO, IEditTaxRateDTO } from './TaxRates.types';
import { GetTaxRatesService } from './queries/GetTaxRates.service';
import { CreateTaxRateDto, EditTaxRateDto } from './dtos/TaxRate.dto';
@Injectable()
export class TaxRatesApplication {
@@ -26,7 +25,7 @@ export class TaxRatesApplication {
* @param {ICreateTaxRateDTO} createTaxRateDTO
* @returns {Promise<ITaxRate>}
*/
public createTaxRate(createTaxRateDTO: ICreateTaxRateDTO) {
public createTaxRate(createTaxRateDTO: CreateTaxRateDto) {
return this.createTaxRateService.createTaxRate(createTaxRateDTO);
}
@@ -37,7 +36,7 @@ export class TaxRatesApplication {
* @param {IEditTaxRateDTO} taxRateEditDTO
* @returns {Promise<ITaxRate>}
*/
public editTaxRate(taxRateId: number, editTaxRateDTO: IEditTaxRateDTO) {
public editTaxRate(taxRateId: number, editTaxRateDTO: EditTaxRateDto) {
return this.editTaxRateService.editTaxRate(taxRateId, editTaxRateDTO);
}

View File

@@ -8,9 +8,9 @@ import {
Put,
} from '@nestjs/common';
import { TaxRatesApplication } from './TaxRate.application';
import { ICreateTaxRateDTO, IEditTaxRateDTO } from './TaxRates.types';
import { PublicRoute } from '../Auth/Jwt.guard';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateTaxRateDto, EditTaxRateDto } from './dtos/TaxRate.dto';
@Controller('tax-rates')
@ApiTags('tax-rates')
@@ -20,7 +20,7 @@ export class TaxRatesController {
@Post()
@ApiOperation({ summary: 'Create a new tax rate.' })
public createTaxRate(@Body() createTaxRateDTO: ICreateTaxRateDTO) {
public createTaxRate(@Body() createTaxRateDTO: CreateTaxRateDto) {
return this.taxRatesApplication.createTaxRate(createTaxRateDTO);
}
@@ -28,7 +28,7 @@ export class TaxRatesController {
@ApiOperation({ summary: 'Edit the given tax rate.' })
public editTaxRate(
@Param('id') taxRateId: number,
@Body() editTaxRateDTO: IEditTaxRateDTO,
@Body() editTaxRateDTO: EditTaxRateDto,
) {
return this.taxRatesApplication.editTaxRate(taxRateId, editTaxRateDTO);
}

View File

@@ -11,6 +11,7 @@ import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { CreateTaxRateDto } from '../dtos/TaxRate.dto';
@Injectable()
export class CreateTaxRate {
@@ -34,7 +35,7 @@ export class CreateTaxRate {
* @param {ICreateTaxRateDTO} createTaxRateDTO
*/
public async createTaxRate(
createTaxRateDTO: ICreateTaxRateDTO,
createTaxRateDTO: CreateTaxRateDto,
trx?: Knex.Transaction,
) {
// Validates the tax code uniquiness.

View File

@@ -12,6 +12,7 @@ import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { events } from '@/common/events/events';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { EditTaxRateDto } from '../dtos/TaxRate.dto';
@Injectable()
export class EditTaxRateService {
@@ -38,7 +39,7 @@ export class EditTaxRateService {
*/
private isTaxRateDTOChanged = (
taxRate: TaxRateModel,
editTaxRateDTO: IEditTaxRateDTO,
editTaxRateDTO: EditTaxRateDto,
) => {
return (
taxRate.rate !== editTaxRateDTO.rate ||
@@ -57,7 +58,7 @@ export class EditTaxRateService {
*/
private async editTaxRateOrCreate(
oldTaxRate: TaxRateModel,
editTaxRateDTO: IEditTaxRateDTO,
editTaxRateDTO: EditTaxRateDto,
trx?: Knex.Transaction,
) {
const isTaxDTOChanged = this.isTaxRateDTOChanged(
@@ -90,7 +91,7 @@ export class EditTaxRateService {
* @param {IEditTaxRateDTO} editTaxRateDTO - The tax rate data to edit.
* @returns {Promise<ITaxRate>}
*/
public async editTaxRate(taxRateId: number, editTaxRateDTO: IEditTaxRateDTO) {
public async editTaxRate(taxRateId: number, editTaxRateDTO: EditTaxRateDto) {
const oldTaxRate = await this.taxRateModel().query().findById(taxRateId);
// Validates the tax rate existance.

View File

@@ -0,0 +1,94 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
IsBoolean,
IsNumber,
IsNotEmpty,
IsOptional,
IsString,
} from 'class-validator';
export class CommandTaxRateDto {
/**
* Tax rate name.
*/
@IsString()
@IsNotEmpty()
@ApiProperty({
description: 'The name of the tax rate.',
example: 'VAT',
})
name: string;
/**
* Tax rate code.
*/
@IsString()
@IsNotEmpty()
@ApiProperty({
description: 'The code of the tax rate.',
example: 'VAT',
})
code: string;
/**
* Tax rate percentage.
*/
@IsNumber()
@IsNotEmpty()
@ApiProperty({
description: 'The rate of the tax rate.',
example: 10,
})
rate: number;
/**
* Tax rate description.
*/
@IsString()
@IsOptional()
@ApiProperty({
description: 'The description of the tax rate.',
example: 'VAT',
})
description?: string;
/**
* Whether the tax is non-recoverable.
*/
@IsBoolean()
@IsOptional()
@Transform(({ value }) => value ?? false)
@ApiProperty({
description: 'Whether the tax is non-recoverable.',
example: false,
})
isNonRecoverable?: boolean;
/**
* Whether the tax is compound.
*/
@IsBoolean()
@IsOptional()
@Transform(({ value }) => value ?? false)
@ApiProperty({
description: 'Whether the tax is compound.',
example: false,
})
isCompound?: boolean;
/**
* Whether the tax rate is active.
*/
@IsBoolean()
@IsOptional()
@Transform(({ value }) => value ?? false)
@ApiProperty({
description: 'Whether the tax rate is active.',
example: false,
})
active?: boolean;
}
export class CreateTaxRateDto extends CommandTaxRateDto {}
export class EditTaxRateDto extends CommandTaxRateDto {}