From 674a30b5fcddc4eb118dc8013c7a23f98ea2c2a0 Mon Sep 17 00:00:00 2001 From: Rehman Ahmed Khan Date: Tue, 14 Jan 2025 13:22:53 +0500 Subject: [PATCH] feat: add option to add customerNumber Id on customer create. --- src/modules/auth/auth.service.ts | 13 ++++++++++- src/modules/auth/dto/sign-up-request.dto.ts | 5 ++++- src/modules/person/dto/person.dto.ts | 4 +++- src/modules/person/person.controller.ts | 24 +++++++++++---------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 6b811c8..8128bb4 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -1,4 +1,5 @@ import { + ConflictException, forwardRef, HttpStatus, Inject, @@ -146,7 +147,17 @@ export class AuthService { data.role = role._id.toString(); - data.odooCustomerId = await this.personService.getLastOdooCustomerId(); + if (data.odooCustomerId) { + const customer = await this.personService.findOneByQuery({ + odooCustomerId: data.odooCustomerId, + }); + if (customer) + throw new ConflictException( + 'Customer with the same customer number already exist in system!' + ); + } else { + data.odooCustomerId = await this.personService.getLastOdooCustomerId(); + } data.session = new Date(); diff --git a/src/modules/auth/dto/sign-up-request.dto.ts b/src/modules/auth/dto/sign-up-request.dto.ts index ea96516..e35862e 100644 --- a/src/modules/auth/dto/sign-up-request.dto.ts +++ b/src/modules/auth/dto/sign-up-request.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsISO8601, IsMongoId, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { IsISO8601, IsMongoId, IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator'; export class AdminCreateUserRequest { @ApiProperty() @@ -67,6 +67,9 @@ export class MobileSignUpRequest { role?: string; + @ApiProperty() + @IsOptional() + @IsNumber() odooCustomerId?: number; @ApiProperty() diff --git a/src/modules/person/dto/person.dto.ts b/src/modules/person/dto/person.dto.ts index a5b09a5..6c389ee 100644 --- a/src/modules/person/dto/person.dto.ts +++ b/src/modules/person/dto/person.dto.ts @@ -78,6 +78,9 @@ export class PersonCreateDto { @IsNumber() customerNumber?: number; + @ApiProperty() + @IsOptional() + @IsNumber() odooCustomerId?: number; @ApiProperty() @@ -346,7 +349,6 @@ export class PersonFiltersDto { @ApiProperty({ description: 'Filter object' }) @IsOptional() @IsObject() - // filters?: Record; filters?: filterPayload; @ApiProperty({ description: 'PopulatedFilter object' }) diff --git a/src/modules/person/person.controller.ts b/src/modules/person/person.controller.ts index f167bae..dd5a80a 100644 --- a/src/modules/person/person.controller.ts +++ b/src/modules/person/person.controller.ts @@ -1,5 +1,6 @@ import { Body, + ConflictException, Controller, Delete, Get, @@ -7,16 +8,11 @@ import { Patch, Post, Query, - Res, - UploadedFile, - UseGuards, - UseInterceptors, } from '@nestjs/common'; import { ApiBadRequestResponse, ApiBearerAuth, ApiBody, - ApiConsumes, ApiInternalServerErrorResponse, ApiNotAcceptableResponse, ApiNotFoundResponse, @@ -27,8 +23,6 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; import { - BulkUploadDto, - BulkUploadResponseDto, PaginatedPersonResponseDto, PasswordUpdateRequestDto, PersonCreateDto, @@ -39,9 +33,6 @@ import { UpdateFcmTokenRequestDto, } from './dto/person.dto'; import { PersonService } from './person.service'; -import { FileInterceptor } from '@nestjs/platform-express'; -import { AuthGuard } from '@nestjs/passport'; -import { Response } from 'express'; @ApiBearerAuth('access-token') @ApiTags('Person') @@ -63,7 +54,18 @@ export class PersonController { }) @Post() async create(@Body() data: PersonCreateDto): Promise { - data.odooCustomerId = await this.service.getLastOdooCustomerId(); + if (data.odooCustomerId) { + const customer = await this.service.findOneByQuery({ + odooCustomerId: data.odooCustomerId, + }); + if (customer) + throw new ConflictException( + 'Customer with the same customer number already exist in system!' + ); + } else { + data.odooCustomerId = await this.service.getLastOdooCustomerId(); + } + return await this.service.create(data); }