diff --git a/src/common/modules/otp/repositories/otp.repository.ts b/src/common/modules/otp/repositories/otp.repository.ts index 879a6f8..f9d13f9 100644 --- a/src/common/modules/otp/repositories/otp.repository.ts +++ b/src/common/modules/otp/repositories/otp.repository.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { MoreThan, Repository } from 'typeorm'; import { Otp } from '../entities'; -import { IVerifyOtp } from '../interfaces'; +import { ISendOtp, IVerifyOtp } from '../interfaces'; const FIVE = 5; const SIXTY = 60; const ONE_THOUSAND = 1000; @@ -42,4 +42,18 @@ export class OtpRepository { updateOtp(id: string, data: Partial) { return this.otpRepository.update(id, data); } + + invalidateOtp(otp: ISendOtp) { + return this.otpRepository.update( + { + userId: otp.userId, + scope: otp.scope, + otpType: otp.otpType, + isUsed: false, + }, + { + isUsed: true, + }, + ); + } } diff --git a/src/common/modules/otp/services/otp.service.ts b/src/common/modules/otp/services/otp.service.ts index 51f6dad..01ebb79 100644 --- a/src/common/modules/otp/services/otp.service.ts +++ b/src/common/modules/otp/services/otp.service.ts @@ -15,8 +15,11 @@ export class OtpService { private readonly otpRepository: OtpRepository, private readonly notificationService: NotificationsService, ) {} - private useMock = this.configService.get('USE_MOCK', false); + private useMock = [true, 'true'].includes(this.configService.get('USE_MOCK', false)); async generateAndSendOtp(sendOtpRequest: ISendOtp): Promise { + this.logger.log(`invalidate OTP for ${sendOtpRequest.recipient} and ${sendOtpRequest.otpType}`); + await this.otpRepository.invalidateOtp(sendOtpRequest); + this.logger.log(`Generating OTP for ${sendOtpRequest.recipient}`); const otp = this.useMock ? DEFAULT_OTP_DIGIT.repeat(DEFAULT_OTP_LENGTH) : generateRandomOtp(DEFAULT_OTP_LENGTH); diff --git a/src/customer/dtos/response/customer-response.dto.ts b/src/customer/dtos/response/customer.response.dto.ts similarity index 95% rename from src/customer/dtos/response/customer-response.dto.ts rename to src/customer/dtos/response/customer.response.dto.ts index 04c849e..3d40cf7 100644 --- a/src/customer/dtos/response/customer-response.dto.ts +++ b/src/customer/dtos/response/customer.response.dto.ts @@ -55,6 +55,9 @@ export class CustomerResponseDto { @ApiProperty() isGuardian!: boolean; + @ApiProperty() + waitingNumber!: number; + @ApiPropertyOptional({ type: DocumentMetaResponseDto }) profilePicture!: DocumentMetaResponseDto | null; @@ -76,6 +79,7 @@ export class CustomerResponseDto { this.gender = customer.gender; this.isJunior = customer.isJunior; this.isGuardian = customer.isGuardian; + this.waitingNumber = customer.waitingNumber; this.profilePicture = customer.profilePicture ? new DocumentMetaResponseDto(customer.profilePicture) : null; } diff --git a/src/customer/dtos/response/index.ts b/src/customer/dtos/response/index.ts index be77608..16eaeba 100644 --- a/src/customer/dtos/response/index.ts +++ b/src/customer/dtos/response/index.ts @@ -1,3 +1,3 @@ -export * from './customer-response.dto'; +export * from './customer.response.dto'; export * from './internal.customer-details.response.dto'; export * from './internal.customer-list.response.dto'; diff --git a/src/customer/entities/customer.entity.ts b/src/customer/entities/customer.entity.ts index 4c7b6ea..48be217 100644 --- a/src/customer/entities/customer.entity.ts +++ b/src/customer/entities/customer.entity.ts @@ -3,6 +3,7 @@ import { Column, CreateDateColumn, Entity, + Generated, JoinColumn, OneToOne, PrimaryColumn, @@ -67,6 +68,10 @@ export class Customer extends BaseEntity { @Column('boolean', { default: false, name: 'is_guardian' }) isGuardian!: boolean; + @Column('int', { name: 'waiting_number' }) + @Generated('increment') + waitingNumber!: number; + @Column('varchar', { name: 'user_id' }) userId!: string; @@ -101,9 +106,9 @@ export class Customer extends BaseEntity { @JoinColumn({ name: 'civil_id_back_id' }) civilIdBack!: Document; - @CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) + @CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' }) createdAt!: Date; - @UpdateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) + @UpdateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'updated_at' }) updatedAt!: Date; } diff --git a/src/db/migrations/1742112997024-update-customer-table.ts b/src/db/migrations/1742112997024-update-customer-table.ts new file mode 100644 index 0000000..481bee7 --- /dev/null +++ b/src/db/migrations/1742112997024-update-customer-table.ts @@ -0,0 +1,36 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class UpdateCustomerTable1742112997024 implements MigrationInterface { + name = 'UpdateCustomerTable1742112997024'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + ALTER TABLE "customers" + RENAME COLUMN "createdAt" + TO "created_at" + `); + + await queryRunner.query(` + ALTER TABLE "customers" + RENAME COLUMN "updatedAt" + TO "updated_at" + `); + await queryRunner.query(`ALTER TABLE "customers" ADD "waiting_number" SERIAL NOT NULL`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "customers" DROP COLUMN "waiting_number"`); + + await queryRunner.query(` + ALTER TABLE "customers" + RENAME COLUMN "created_at" + TO "createdAt" + `); + + await queryRunner.query(` + ALTER TABLE "customers" + RENAME COLUMN "updated_at" + TO "updatedAt" + `); + } +} diff --git a/src/db/migrations/index.ts b/src/db/migrations/index.ts index 50a4852..9ca8914 100644 --- a/src/db/migrations/index.ts +++ b/src/db/migrations/index.ts @@ -23,3 +23,4 @@ export * from './1739868002943-add-kyc-status-to-customer'; export * from './1739954239949-add-civilid-to-customers-and-update-notifications-settings'; export * from './1740045960580-create-user-registration-table'; export * from './1741087742821-add-used-flag-to-otp-and-remove-constraints-from-customers'; +export * from './1742112997024-update-customer-table';