refactor(nestjs): validation schema dtos

This commit is contained in:
Ahmed Bouhuolia
2025-05-25 23:39:54 +02:00
parent 2b3f98d8fe
commit 24bf3dd06d
24 changed files with 247 additions and 135 deletions

View File

@@ -7,6 +7,8 @@ import { GetCreditNotePdf } from './queries/GetCreditNotePdf.serivce';
import { ICreditNotesQueryDTO } from './types/CreditNotes.types';
import { GetCreditNotesService } from './queries/GetCreditNotes.service';
import { CreateCreditNoteDto, EditCreditNoteDto } from './dtos/CreditNote.dto';
import { GetCreditNoteState } from './queries/GetCreditNoteState.service';
import { GetCreditNoteService } from './queries/GetCreditNote.service';
@Injectable()
export class CreditNoteApplication {
@@ -17,6 +19,8 @@ export class CreditNoteApplication {
private readonly deleteCreditNoteService: DeleteCreditNoteService,
private readonly getCreditNotePdfService: GetCreditNotePdf,
private readonly getCreditNotesService: GetCreditNotesService,
private readonly getCreditNoteStateService: GetCreditNoteState,
private readonly getCreditNoteService: GetCreditNoteService
) {}
/**
@@ -76,4 +80,21 @@ export class CreditNoteApplication {
getCreditNotes(creditNotesQuery: ICreditNotesQueryDTO) {
return this.getCreditNotesService.getCreditNotesList(creditNotesQuery);
}
/**
* Retrieves the create/edit initial state of the credit note.
* @returns {Promise<ICreditNoteState>}
*/
getCreditNoteState() {
return this.getCreditNoteStateService.getCreditNoteState();
}
/**
* Retrieves the credit note.
* @param {number} creditNoteId
* @returns {Promise<CreditNote>}
*/
getCreditNote(creditNoteId: number) {
return this.getCreditNoteService.getCreditNote(creditNoteId);
}
}

View File

@@ -1,4 +1,4 @@
import { ApiTags } from '@nestjs/swagger';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import {
Body,
Controller,
@@ -22,16 +22,42 @@ export class CreditNotesController {
constructor(private creditNoteApplication: CreditNoteApplication) {}
@Post()
@ApiOperation({ summary: 'Create a new credit note' })
@ApiResponse({ status: 201, description: 'Credit note successfully created' })
@ApiResponse({ status: 400, description: 'Invalid input data' })
createCreditNote(@Body() creditNoteDTO: CreateCreditNoteDto) {
return this.creditNoteApplication.createCreditNote(creditNoteDTO);
}
@Get('state')
@ApiOperation({ summary: 'Get credit note state' })
@ApiResponse({ status: 200, description: 'Returns the credit note state' })
getCreditNoteState() {
return this.creditNoteApplication.getCreditNoteState();
}
@Get(':id')
@ApiOperation({ summary: 'Get a specific credit note by ID' })
@ApiParam({ name: 'id', description: 'Credit note ID', type: 'number' })
@ApiResponse({ status: 200, description: 'Returns the credit note' })
@ApiResponse({ status: 404, description: 'Credit note not found' })
getCreditNote(@Param('id') creditNoteId: number) {
return this.creditNoteApplication.getCreditNote(creditNoteId);
}
@Get()
@ApiOperation({ summary: 'Get all credit notes' })
@ApiResponse({ status: 200, description: 'Returns a list of credit notes' })
getCreditNotes(@Query() creditNotesQuery: ICreditNotesQueryDTO) {
return this.creditNoteApplication.getCreditNotes(creditNotesQuery);
}
@Put(':id')
@ApiOperation({ summary: 'Update a credit note' })
@ApiParam({ name: 'id', description: 'Credit note ID', type: 'number' })
@ApiResponse({ status: 200, description: 'Credit note successfully updated' })
@ApiResponse({ status: 404, description: 'Credit note not found' })
@ApiResponse({ status: 400, description: 'Invalid input data' })
editCreditNote(
@Param('id') creditNoteId: number,
@Body() creditNoteDTO: EditCreditNoteDto,
@@ -43,11 +69,19 @@ export class CreditNotesController {
}
@Put(':id/open')
@ApiOperation({ summary: 'Open a credit note' })
@ApiParam({ name: 'id', description: 'Credit note ID', type: 'number' })
@ApiResponse({ status: 200, description: 'Credit note successfully opened' })
@ApiResponse({ status: 404, description: 'Credit note not found' })
openCreditNote(@Param('id') creditNoteId: number) {
return this.creditNoteApplication.openCreditNote(creditNoteId);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete a credit note' })
@ApiParam({ name: 'id', description: 'Credit note ID', type: 'number' })
@ApiResponse({ status: 200, description: 'Credit note successfully deleted' })
@ApiResponse({ status: 404, description: 'Credit note not found' })
deleteCreditNote(@Param('id') creditNoteId: number) {
return this.creditNoteApplication.deleteCreditNote(creditNoteId);
}

View File

@@ -15,7 +15,7 @@ import { WarehousesModule } from '../Warehouses/Warehouses.module';
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
import { ChromiumlyTenancyModule } from '../ChromiumlyTenancy/ChromiumlyTenancy.module';
import { TemplateInjectableModule } from '../TemplateInjectable/TemplateInjectable.module';
import { GetCreditNote } from './queries/GetCreditNote.service';
import { GetCreditNoteService } from './queries/GetCreditNote.service';
import { CreditNoteBrandingTemplate } from './queries/CreditNoteBrandingTemplate.service';
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
import { CreditNoteGLEntries } from './commands/CreditNoteGLEntries';
@@ -52,7 +52,7 @@ import { CreditNotesApplyInvoiceModule } from '../CreditNotesApplyInvoice/Credit
],
providers: [
CreateCreditNoteService,
GetCreditNote,
GetCreditNoteService,
CommandCreditNoteDTOTransform,
EditCreditNoteService,
OpenCreditNoteService,
@@ -74,7 +74,7 @@ import { CreditNotesApplyInvoiceModule } from '../CreditNotesApplyInvoice/Credit
],
exports: [
CreateCreditNoteService,
GetCreditNote,
GetCreditNoteService,
CommandCreditNoteDTOTransform,
EditCreditNoteService,
OpenCreditNoteService,

View File

@@ -1,3 +1,4 @@
import { ToNumber } from '@/common/decorators/Validators';
import { ItemEntryDto } from '@/modules/TransactionItemEntry/dto/ItemEntry.dto';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
@@ -5,9 +6,10 @@ import {
ArrayMinSize,
IsArray,
IsBoolean,
IsDate,
IsDateString,
IsEnum,
IsInt,
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,
@@ -25,21 +27,25 @@ export class CreditNoteEntryDto extends ItemEntryDto {}
class AttachmentDto {
@IsString()
@IsNotEmpty()
key: string;
}
export class CommandCreditNoteDto {
@ToNumber()
@IsInt()
@IsNotEmpty()
@ApiProperty({ example: 1, description: 'The customer ID' })
customerId: number;
@IsOptional()
@ToNumber()
@IsPositive()
@ApiProperty({ example: 3.43, description: 'The exchange rate' })
exchangeRate?: number;
@IsDate()
@Type(() => Date)
@IsNotEmpty()
@IsDateString()
@ApiProperty({ example: '2021-09-01', description: 'The credit note date' })
creditNoteDate: Date;
@@ -64,26 +70,19 @@ export class CommandCreditNoteDto {
termsConditions?: string;
@IsBoolean()
@ApiProperty({
example: false,
description: 'The credit note is open',
})
@ApiProperty({ example: false, description: 'The credit note is open' })
open: boolean = false;
@IsOptional()
@ToNumber()
@IsInt()
@ApiProperty({
example: 1,
description: 'The warehouse ID',
})
@ApiProperty({ example: 1, description: 'The warehouse ID' })
warehouseId?: number;
@IsOptional()
@ToNumber()
@IsInt()
@ApiProperty({
example: 1,
description: 'The branch ID',
})
@ApiProperty({ example: 1, description: 'The branch ID' })
branchId?: number;
@IsArray()
@@ -91,14 +90,7 @@ export class CommandCreditNoteDto {
@Type(() => CreditNoteEntryDto)
@ArrayMinSize(1)
@ApiProperty({
example: [
{
itemId: 1,
quantity: 1,
rate: 10,
taxRateId: 1,
},
],
example: [{ itemId: 1, quantity: 1, rate: 10, taxRateId: 1 }],
description: 'The credit note entries',
})
entries: CreditNoteEntryDto[];
@@ -110,19 +102,15 @@ export class CommandCreditNoteDto {
attachments?: AttachmentDto[];
@IsOptional()
@ToNumber()
@IsInt()
@ApiProperty({
example: 1,
description: 'The pdf template ID',
})
@ApiProperty({ example: 1, description: 'The pdf template ID' })
pdfTemplateId?: number;
@IsOptional()
@ToNumber()
@IsNumber()
@ApiProperty({
example: 10,
description: 'The discount amount',
})
@ApiProperty({ example: 10, description: 'The discount amount' })
discount?: number;
@IsOptional()
@@ -135,6 +123,7 @@ export class CommandCreditNoteDto {
discountType?: DiscountType;
@IsOptional()
@ToNumber()
@IsNumber()
adjustment?: number;
}

View File

@@ -7,7 +7,7 @@ import { ServiceError } from '@/modules/Items/ServiceError';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetCreditNote {
export class GetCreditNoteService {
constructor(
private readonly transformer: TransformerInjectable,

View File

@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import { GetCreditNote } from './GetCreditNote.service';
import { GetCreditNoteService } from './GetCreditNote.service';
import { CreditNoteBrandingTemplate } from './CreditNoteBrandingTemplate.service';
import { transformCreditNoteToPdfTemplate } from '../utils';
import { CreditNote } from '../models/CreditNote';
@@ -25,7 +25,7 @@ export class GetCreditNotePdf {
constructor(
private readonly chromiumlyTenancy: ChromiumlyTenancy,
private readonly templateInjectable: TemplateInjectable,
private readonly getCreditNoteService: GetCreditNote,
private readonly getCreditNoteService: GetCreditNoteService,
private readonly creditNoteBrandingTemplate: CreditNoteBrandingTemplate,
private readonly eventPublisher: EventEmitter2,