From e161bfff961650bd4bc1c4a7c687c941fe9da45b Mon Sep 17 00:00:00 2001 From: Victor Pino Date: Mon, 26 Aug 2024 15:40:20 -0400 Subject: [PATCH] feat(src): Add onboarding proccess to load names --- src/common/services/crud/crud.service.ts | 1 - src/modules/auth/auth.module.ts | 2 + src/modules/auth/auth.service.ts | 25 +++- src/modules/auth/interfaces/LoginResponse.ts | 7 + src/modules/customer/customer.controller.ts | 17 +++ src/modules/customer/customer.service.ts | 49 +++++++ .../individual-customer/dtos/address.dto.ts | 36 +++++ .../dtos/company-info.dto.ts | 59 ++++++++ .../dtos/contact-info.dto.ts | 11 ++ .../dtos/education-data.dto.ts | 28 ++++ .../dtos/housing-data.dto.ts | 28 ++++ .../dtos/identity-document.dto.ts | 28 ++++ .../dtos/load-names.dto.ts | 20 +++ .../dtos/occupation.dto.ts | 20 +++ .../entities/individual-customer.entity.ts | 64 +++++++- .../enums/individual-customer.enum.ts | 10 ++ .../individual-customer.controller.ts | 137 ++++++++++++++++++ .../individual-customer.service.ts | 99 ++++++++++++- 18 files changed, 632 insertions(+), 9 deletions(-) create mode 100644 src/modules/auth/interfaces/LoginResponse.ts create mode 100644 src/modules/individual-customer/dtos/address.dto.ts create mode 100644 src/modules/individual-customer/dtos/company-info.dto.ts create mode 100644 src/modules/individual-customer/dtos/contact-info.dto.ts create mode 100644 src/modules/individual-customer/dtos/education-data.dto.ts create mode 100644 src/modules/individual-customer/dtos/housing-data.dto.ts create mode 100644 src/modules/individual-customer/dtos/identity-document.dto.ts create mode 100644 src/modules/individual-customer/dtos/load-names.dto.ts create mode 100644 src/modules/individual-customer/dtos/occupation.dto.ts create mode 100644 src/modules/individual-customer/enums/individual-customer.enum.ts diff --git a/src/common/services/crud/crud.service.ts b/src/common/services/crud/crud.service.ts index 4722c90..bda1f5f 100644 --- a/src/common/services/crud/crud.service.ts +++ b/src/common/services/crud/crud.service.ts @@ -2,7 +2,6 @@ import { Injectable, BadRequestException, ConflictException, - Inject, } from '@nestjs/common'; import { Repository, diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index 15693ed..6c7c0a2 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -7,11 +7,13 @@ import { JwtStrategy } from './strategies/jwt.strategy'; import { LocalStrategy } from './strategies/local.strategy'; import { AuthController } from './auth.controller'; import { CustomerModule } from '../customer/customer.module'; +import { IndividualCustomerModule } from '../individual-customer/individual-customer.module'; @Module({ imports: [ PassportModule, CustomerModule, + IndividualCustomerModule, JwtModule.registerAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 964dd39..6775c9c 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -7,15 +7,20 @@ import { MESSAGES } from '../../common/constans/messages'; import { CustomerService } from '../customer/customer.service'; import { Customer } from '../customer/entities/customer.entity'; import { CreateCustomerDto } from '../customer/dtos/create.customer.dto'; +import { IndividualCustomerService } from '../individual-customer/individual-customer.service'; +import { LoginResponse } from './interfaces/LoginResponse'; +import { IndividualCustomer } from '../individual-customer/entities/individual-customer.entity'; @Injectable() export class AuthService { constructor( private customerService: CustomerService, + private individualCustomerService: IndividualCustomerService, + private jwtService: JwtService, ) {} - async validateCustomer(email: string, password: string): Promise { + async validateCustomer(email: string, password: string): Promise { const response: ResponseDTO = await this.customerService.findOne({ email, }); @@ -26,29 +31,37 @@ export class AuthService { const customer = response.data; + const individualCustomer: ResponseDTO = await this.individualCustomerService.findOne( + { customerId: { id: customer.id } } + ); + + const individual = individualCustomer.data + const comparePass = await bcrypt.compare(password, customer.password); if (!comparePass) { throw new UnauthorizedException(MESSAGES.INVALID_CREDENTIALS_ERROR); } - return customer; + return {customer, individual}; } async login(data: AuthValidation): Promise { - const customer = await this.validateCustomer(data.email, data.password); + const response = await this.validateCustomer(data.email, data.password); const payload = { - email: customer.email, - _id: customer.id, + email: response.customer.email, + id: response.customer.id, }; const accessToken = this.jwtService.sign(payload); return { data: { - status: customer.isActive, + status: response.customer.isActive, access_token: accessToken, + individual: response.individual, + id: response.customer.id }, }; } diff --git a/src/modules/auth/interfaces/LoginResponse.ts b/src/modules/auth/interfaces/LoginResponse.ts new file mode 100644 index 0000000..d8c67c8 --- /dev/null +++ b/src/modules/auth/interfaces/LoginResponse.ts @@ -0,0 +1,7 @@ +import { Customer } from "src/modules/customer/entities/customer.entity"; +import { IndividualCustomer } from "src/modules/individual-customer/entities/individual-customer.entity"; + +export interface LoginResponse { + customer: Customer, + individual: IndividualCustomer +} \ No newline at end of file diff --git a/src/modules/customer/customer.controller.ts b/src/modules/customer/customer.controller.ts index 18edf41..0540c00 100644 --- a/src/modules/customer/customer.controller.ts +++ b/src/modules/customer/customer.controller.ts @@ -22,6 +22,7 @@ import { kycRequirements } from './dtos/kyc.requirements.dto'; import { SkipJwtAuth } from 'src/common/decorators/skip-guard.decorator'; import { SubmitKycDto } from './dtos/submit.kyc.dto'; import { UpdateKycDto } from './dtos/update.kyc.dto'; +import { LoadNamesDTO } from '../individual-customer/dtos/load-names.dto'; @ApiBearerAuth('JWT-auth') @ApiTags('Customer') @@ -125,4 +126,20 @@ export class CustomerController { return error; } } + + + @Put(':id/load-names') + @ApiOperation({ summary: 'Load Names for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The names have been successfully loaded.', + }) + async updateLoadNames( + @Param('id') id: string, + @Body() loadNamesDTO: LoadNamesDTO, + ): Promise { + const data = await this.customerService.updateLoadNames(id, loadNamesDTO); + return { data }; + } + } diff --git a/src/modules/customer/customer.service.ts b/src/modules/customer/customer.service.ts index 2e1c4e9..448e932 100644 --- a/src/modules/customer/customer.service.ts +++ b/src/modules/customer/customer.service.ts @@ -18,6 +18,9 @@ import { CreateCustomerDto } from './dtos/create.customer.dto'; import { IResponseCustomer } from './interfaces/response.customer.interface'; import { IResponseIndividualCustomer } from './interfaces/response.individual-customer.interface'; import { StatusKyc } from 'src/common/enums/customer.enums'; +import { IndividualCustomer } from '../individual-customer/entities/individual-customer.entity'; +import { LoadNamesDTO } from '../individual-customer/dtos/load-names.dto'; +import { StatusOnboarding } from '../individual-customer/enums/individual-customer.enum'; @Injectable() export class CustomerService extends CrudService { @@ -300,4 +303,50 @@ export class CustomerService extends CrudService { throw error; } } + + async findOrCreateIndividualCustomer( + customerId: string, + ): Promise { + const customer = await this.customerRepository.findOne({ + where: { id: customerId }, + }); + + if (!customer) { + throw new NotFoundException(`Customer with ID ${customerId} not found`); + } + + let individualCustomer = await this.individualCustomerService.findOne( + { customerId: { id: customerId } }, + ); + + if (!individualCustomer.data) { + const newIndividualCustomer = await this.individualCustomerService.create({ + customerId: customer, + }); + + individualCustomer.data = newIndividualCustomer; + } + + return individualCustomer.data; + } + + async updateLoadNames( + id: string, + loadNamesDTO: LoadNamesDTO, + ): Promise { + try { + const individualCustomer = await this.findOrCreateIndividualCustomer(id); + + individualCustomer.firstName = loadNamesDTO.firstName; + individualCustomer.lastName = loadNamesDTO.lastName; + individualCustomer.status = StatusOnboarding.IDENTITY_DOCUMENT; + + return await this.individualCustomerService.update( + individualCustomer.id, + individualCustomer, + ); + } catch (error) { + throw error; + } + } } diff --git a/src/modules/individual-customer/dtos/address.dto.ts b/src/modules/individual-customer/dtos/address.dto.ts new file mode 100644 index 0000000..5c26001 --- /dev/null +++ b/src/modules/individual-customer/dtos/address.dto.ts @@ -0,0 +1,36 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length } from 'class-validator'; + +export class AddressDTO { + @ApiProperty({ + description: 'Address of the individual customer', + example: '123 Main St', + }) + @IsString() + @Length(1, 200) + address: string; + + @ApiProperty({ + description: 'State of the individual customer', + example: 'California', + }) + @IsString() + @Length(1, 100) + state: string; + + @ApiProperty({ + description: 'City of the individual customer', + example: 'Los Angeles', + }) + @IsString() + @Length(1, 100) + city: string; + + @ApiProperty({ + description: 'Town or locality', + example: 'Downtown', + }) + @IsString() + @Length(1, 100) + town: string; +} diff --git a/src/modules/individual-customer/dtos/company-info.dto.ts b/src/modules/individual-customer/dtos/company-info.dto.ts new file mode 100644 index 0000000..55b23b5 --- /dev/null +++ b/src/modules/individual-customer/dtos/company-info.dto.ts @@ -0,0 +1,59 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length, IsPhoneNumber, IsNumberString } from 'class-validator'; + +export class CompanyInfoDTO { + @ApiProperty({ + description: 'Name of the company', + example: 'Tech Solutions Inc.', + }) + @IsString() + @Length(1, 100) + name: string; + + @ApiProperty({ + description: 'Phone number of the company', + example: '+1-800-123-4567', + }) + @IsPhoneNumber(null) + phone: string; + + @ApiProperty({ + description: 'Address of the company', + example: '456 Business Rd', + }) + @IsString() + @Length(1, 200) + address: string; + + @ApiProperty({ + description: 'State where the company is located', + example: 'New York', + }) + @IsString() + @Length(1, 100) + state: string; + + @ApiProperty({ + description: 'City where the company is located', + example: 'New York City', + }) + @IsString() + @Length(1, 100) + city: string; + + @ApiProperty({ + description: 'Year the company was founded or the individual joined', + example: '2015', + }) + @IsNumberString() + @Length(4, 4) + year: string; + + @ApiProperty({ + description: 'Month the company was founded or the individual joined', + example: 'March', + }) + @IsString() + @Length(1, 50) + month: string; +} diff --git a/src/modules/individual-customer/dtos/contact-info.dto.ts b/src/modules/individual-customer/dtos/contact-info.dto.ts new file mode 100644 index 0000000..4a2164c --- /dev/null +++ b/src/modules/individual-customer/dtos/contact-info.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsPhoneNumber } from 'class-validator'; + +export class ContactInfoDTO { + @ApiProperty({ + description: 'Phone number of the individual customer', + example: '+1-555-555-5555', + }) + @IsPhoneNumber(null) + phone: string; +} diff --git a/src/modules/individual-customer/dtos/education-data.dto.ts b/src/modules/individual-customer/dtos/education-data.dto.ts new file mode 100644 index 0000000..e3d6724 --- /dev/null +++ b/src/modules/individual-customer/dtos/education-data.dto.ts @@ -0,0 +1,28 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length, IsNumberString } from 'class-validator'; + +export class EducationDataDTO { + @ApiProperty({ + description: 'Level of education', + example: 'Bachelor', + }) + @IsString() + @Length(1, 100) + level: string; + + @ApiProperty({ + description: 'Area of specialization', + example: 'Computer Science', + }) + @IsString() + @Length(1, 100) + area: string; + + @ApiProperty({ + description: 'Year of graduation or last year attended', + example: '2015', + }) + @IsNumberString() + @Length(4, 4) + year: string; +} diff --git a/src/modules/individual-customer/dtos/housing-data.dto.ts b/src/modules/individual-customer/dtos/housing-data.dto.ts new file mode 100644 index 0000000..3fdd78a --- /dev/null +++ b/src/modules/individual-customer/dtos/housing-data.dto.ts @@ -0,0 +1,28 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length, IsNumberString } from 'class-validator'; + +export class HousingDataDTO { + @ApiProperty({ + description: 'Type of housing', + example: 'Apartment', + }) + @IsString() + @Length(1, 100) + type: string; + + @ApiProperty({ + description: 'Year of purchase or construction', + example: '2010', + }) + @IsNumberString() + @Length(4, 4) + year: string; + + @ApiProperty({ + description: 'Month of purchase or construction', + example: 'June', + }) + @IsString() + @Length(1, 50) + month: string; +} diff --git a/src/modules/individual-customer/dtos/identity-document.dto.ts b/src/modules/individual-customer/dtos/identity-document.dto.ts new file mode 100644 index 0000000..59d9f67 --- /dev/null +++ b/src/modules/individual-customer/dtos/identity-document.dto.ts @@ -0,0 +1,28 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length } from 'class-validator'; + +export class IdentityDocumentDTO { + @ApiProperty({ + description: 'Country of the identity document', + example: 'USA', + }) + @IsString() + @Length(2, 100) + country: string; + + @ApiProperty({ + description: 'Type of the identity document', + example: 'Passport', + }) + @IsString() + @Length(2, 100) + typeDocument: string; + + @ApiProperty({ + description: 'Document number (DNI)', + example: '123456789', + }) + @IsString() + @Length(1, 100) + dni: string; +} diff --git a/src/modules/individual-customer/dtos/load-names.dto.ts b/src/modules/individual-customer/dtos/load-names.dto.ts new file mode 100644 index 0000000..0d58775 --- /dev/null +++ b/src/modules/individual-customer/dtos/load-names.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length } from 'class-validator'; + +export class LoadNamesDTO { + @ApiProperty({ + description: 'First name of the individual customer', + example: 'John', + }) + @IsString() + @Length(1, 100) + firstName: string; + + @ApiProperty({ + description: 'Last name of the individual customer', + example: 'Doe', + }) + @IsString() + @Length(1, 100) + lastName: string; +} diff --git a/src/modules/individual-customer/dtos/occupation.dto.ts b/src/modules/individual-customer/dtos/occupation.dto.ts new file mode 100644 index 0000000..ee1999d --- /dev/null +++ b/src/modules/individual-customer/dtos/occupation.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, Length } from 'class-validator'; + +export class OccupationDTO { + @ApiProperty({ + description: 'Occupation or job title', + example: 'Software Engineer', + }) + @IsString() + @Length(1, 100) + occupation: string; + + @ApiProperty({ + description: 'Type of business or industry', + example: 'Technology', + }) + @IsString() + @Length(1, 100) + typeBusiness: string; +} diff --git a/src/modules/individual-customer/entities/individual-customer.entity.ts b/src/modules/individual-customer/entities/individual-customer.entity.ts index 332a3fe..8b8743b 100644 --- a/src/modules/individual-customer/entities/individual-customer.entity.ts +++ b/src/modules/individual-customer/entities/individual-customer.entity.ts @@ -6,10 +6,10 @@ import { Column, CreateDateColumn, UpdateDateColumn, - OneToMany, JoinColumn, OneToOne, } from 'typeorm'; +import { StatusOnboarding } from '../enums/individual-customer.enum'; @Entity() export class IndividualCustomer { @@ -53,6 +53,60 @@ export class IndividualCustomer { @Column({ type: 'varchar', length: 200, nullable: true }) addressExtension: string; + @Column({ type: 'varchar', length: 100, nullable: true }) + typeDocument: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + town: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + housingType: string; + + @Column({ type: 'varchar', length: 4, nullable: true }) + housingYear: string; + + @Column({ type: 'varchar', length: 2, nullable: true }) + housingMonth: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + educationLevel: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + educationArea: string; + + @Column({ type: 'varchar', length: 4, nullable: true }) + educationYear: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + occupation: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + typeBusiness: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + companyName: string; + + @Column({ type: 'varchar', length: 15, nullable: true }) + companyPhone: string; + + @Column({ type: 'varchar', length: 200, nullable: true }) + companyAddress: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + companyCity: string; + + @Column({ type: 'varchar', length: 100, nullable: true }) + companyState: string; + + @Column({ type: 'varchar', length: 4, nullable: true }) + companyYear: string; + + @Column({ type: 'varchar', length: 2, nullable: true }) + companyMonth: string; + + @Column({ type: 'varchar', length: 15, nullable: true }) + phone: string; + @Column({ type: 'enum', enum: StatusKyc, @@ -61,6 +115,14 @@ export class IndividualCustomer { }) statusKyc: StatusKyc; + @Column({ + type: 'enum', + enum: StatusOnboarding, + default: StatusOnboarding.LOAD_NAMES, + nullable: true, + }) + status: StatusOnboarding; + @Column({ nullable: true, default: null }) verificationSessionId: string; diff --git a/src/modules/individual-customer/enums/individual-customer.enum.ts b/src/modules/individual-customer/enums/individual-customer.enum.ts new file mode 100644 index 0000000..ca5097b --- /dev/null +++ b/src/modules/individual-customer/enums/individual-customer.enum.ts @@ -0,0 +1,10 @@ +export enum StatusOnboarding { + LOAD_NAMES = 'LOAD_NAMES', + IDENTITY_DOCUMENT = 'IDENTITY_DOCUMENT', + ADDRESS = 'ADDRESS', + HOUSING = 'HOUSING', + EDUCATION = 'EDUCATION', + OCCUPATION = 'OCCUPATION', + COMPANY = 'COMPANY', + CONTACT = 'CONTACT' +} diff --git a/src/modules/individual-customer/individual-customer.controller.ts b/src/modules/individual-customer/individual-customer.controller.ts index cd7b29c..1a1f872 100644 --- a/src/modules/individual-customer/individual-customer.controller.ts +++ b/src/modules/individual-customer/individual-customer.controller.ts @@ -17,6 +17,13 @@ import { ResponseDTO } from '../../common/dtos/response.dto'; import { IndividualCustomerService } from './individual-customer.service'; import { CreateIndividualCustomerDto } from './dtos/create.individual-customer.dto'; import { SkipJwtAuth } from 'src/common/decorators/skip-guard.decorator'; +import { IdentityDocumentDTO } from './dtos/identity-document.dto'; +import { AddressDTO } from './dtos/address.dto'; +import { HousingDataDTO } from './dtos/housing-data.dto'; +import { EducationDataDTO } from './dtos/education-data.dto'; +import { OccupationDTO } from './dtos/occupation.dto'; +import { CompanyInfoDTO } from './dtos/company-info.dto'; +import { ContactInfoDTO } from './dtos/contact-info.dto'; @SkipJwtAuth() @ApiBearerAuth('JWT-auth') @@ -42,6 +49,15 @@ export class IndividualCustomerController { return this.individualCustomerService.findOne({ id }); } + @Get('customer/:id') + @ApiOperation({ summary: 'Get a individual by customerId' }) + @ApiResponse({ status: 200, description: 'Return a customer' }) + async getIndividualByCustomerId( + @Param('id') customerId: string, + ): Promise { + return this.individualCustomerService.findOne({ customerId }); + } + @Post() @ApiOperation({ summary: 'Create a new individual customer' }) @ApiResponse({ @@ -76,4 +92,125 @@ export class IndividualCustomerController { async delete(@Param('id') id: string): Promise { return this.individualCustomerService.deleteOne({ _id: id }); } + + @Put(':id/identity-document') + @ApiOperation({ + summary: 'Load Identity Document for an individual customer', + }) + @ApiResponse({ + status: 200, + description: 'The identity document has been successfully loaded.', + }) + async updateIdentityDocument( + @Param('id') id: string, + @Body() identityDocumentDTO: IdentityDocumentDTO, + ): Promise { + const data = await this.individualCustomerService.updateIdentityDocument( + id, + identityDocumentDTO, + ); + return { data }; + } + + @Put(':id/address') + @ApiOperation({ summary: 'Load Address for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The address has been successfully loaded.', + }) + async updateAddress( + @Param('id') id: string, + @Body() addressDTO: AddressDTO, + ): Promise { + const data = await this.individualCustomerService.updateAddress( + id, + addressDTO, + ); + return { data }; + } + + @Put(':id/housing-data') + @ApiOperation({ summary: 'Load Housing Data for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The housing data has been successfully loaded.', + }) + async updateHousingData( + @Param('id') id: string, + @Body() housingDataDTO: HousingDataDTO, + ): Promise { + const data = await this.individualCustomerService.updateHousingData( + id, + housingDataDTO, + ); + return { data }; + } + + @Put(':id/education-data') + @ApiOperation({ summary: 'Load Education Data for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The education data has been successfully loaded.', + }) + async updateEducationData( + @Param('id') id: string, + @Body() educationDataDTO: EducationDataDTO, + ): Promise { + const data = await this.individualCustomerService.updateEducationData( + id, + educationDataDTO, + ); + return { data }; + } + + @Put(':id/occupation') + @ApiOperation({ summary: 'Load Occupation Data for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The occupation data has been successfully loaded.', + }) + async updateOccupation( + @Param('id') id: string, + @Body() occupationDTO: OccupationDTO, + ): Promise { + const data = await this.individualCustomerService.updateOccupation( + id, + occupationDTO, + ); + return { data }; + } + + @Put(':id/company-info') + @ApiOperation({ summary: 'Load Company Info for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The company info has been successfully loaded.', + }) + async updateCompanyInfo( + @Param('id') id: string, + @Body() companyInfoDTO: CompanyInfoDTO, + ): Promise { + const data = await this.individualCustomerService.updateCompanyInfo( + id, + companyInfoDTO, + ); + return { data }; + } + + @Put(':id/contact-info') + @ApiOperation({ summary: 'Load Contact Info for an individual customer' }) + @ApiResponse({ + status: 200, + description: 'The contact info has been successfully loaded.', + }) + async updateContactInfo( + @Param('id') id: string, + @Body() contactInfoDTO: ContactInfoDTO, + ): Promise { + const data = await this.individualCustomerService.updateContactInfo( + id, + contactInfoDTO, + ); + return { data }; + } } diff --git a/src/modules/individual-customer/individual-customer.service.ts b/src/modules/individual-customer/individual-customer.service.ts index 61c0202..5c62cc2 100644 --- a/src/modules/individual-customer/individual-customer.service.ts +++ b/src/modules/individual-customer/individual-customer.service.ts @@ -1,8 +1,15 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { CrudService } from '../../common/services/crud/crud.service'; import { InjectRepository } from '@nestjs/typeorm'; import { DataSource, Repository } from 'typeorm'; import { IndividualCustomer } from './entities/individual-customer.entity'; +import { AddressDTO } from './dtos/address.dto'; +import { CompanyInfoDTO } from './dtos/company-info.dto'; +import { ContactInfoDTO } from './dtos/contact-info.dto'; +import { EducationDataDTO } from './dtos/education-data.dto'; +import { HousingDataDTO } from './dtos/housing-data.dto'; +import { IdentityDocumentDTO } from './dtos/identity-document.dto'; +import { OccupationDTO } from './dtos/occupation.dto'; @Injectable() export class IndividualCustomerService extends CrudService { @@ -13,4 +20,94 @@ export class IndividualCustomerService extends CrudService { ) { super(individualCustomerRepository, 'id', dataSourceInject); } + + async findCustomerById(customerId: string): Promise { + const individualCustomer = await this.individualCustomerRepository.findOne({ + where: { customerId: { id: customerId } }, + }); + + if (!individualCustomer) { + throw new NotFoundException(`Customer with ID ${customerId} not found`); + } + return individualCustomer; + } + + async updateIdentityDocument( + id: string, + identityDocumentDTO: IdentityDocumentDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.country = identityDocumentDTO.country; + individualCustomer.typeDocument = identityDocumentDTO.typeDocument; + individualCustomer.dni = identityDocumentDTO.dni; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateAddress( + id: string, + addressDTO: AddressDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.address = addressDTO.address; + individualCustomer.state = addressDTO.state; + individualCustomer.city = addressDTO.city; + individualCustomer.town = addressDTO.town; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateHousingData( + id: string, + housingDataDTO: HousingDataDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.housingType = housingDataDTO.type; + individualCustomer.housingYear = housingDataDTO.year; + individualCustomer.housingMonth = housingDataDTO.month; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateEducationData( + id: string, + educationDataDTO: EducationDataDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.educationLevel = educationDataDTO.level; + individualCustomer.educationArea = educationDataDTO.area; + individualCustomer.educationYear = educationDataDTO.year; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateOccupation( + id: string, + occupationDTO: OccupationDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.occupation = occupationDTO.occupation; + individualCustomer.typeBusiness = occupationDTO.typeBusiness; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateCompanyInfo( + id: string, + companyInfoDTO: CompanyInfoDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.companyName = companyInfoDTO.name; + individualCustomer.companyPhone = companyInfoDTO.phone; + individualCustomer.companyAddress = companyInfoDTO.address; + individualCustomer.companyState = companyInfoDTO.state; + individualCustomer.companyCity = companyInfoDTO.city; + individualCustomer.companyYear = companyInfoDTO.year; + individualCustomer.companyMonth = companyInfoDTO.month; + return this.individualCustomerRepository.save(individualCustomer); + } + + async updateContactInfo( + id: string, + contactInfoDTO: ContactInfoDTO, + ): Promise { + const individualCustomer = await this.findCustomerById(id); + individualCustomer.phone = contactInfoDTO.phone; + return this.individualCustomerRepository.save(individualCustomer); + } }