feat: add waiting number and handle resent otp

This commit is contained in:
Abdalhamid Alhamad
2025-03-16 11:34:08 +03:00
parent 9b5f863577
commit ec38b82a7b
7 changed files with 68 additions and 5 deletions

View File

@ -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<Otp>) {
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,
},
);
}
}

View File

@ -15,8 +15,11 @@ export class OtpService {
private readonly otpRepository: OtpRepository,
private readonly notificationService: NotificationsService,
) {}
private useMock = this.configService.get<boolean>('USE_MOCK', false);
private useMock = [true, 'true'].includes(this.configService.get<boolean>('USE_MOCK', false));
async generateAndSendOtp(sendOtpRequest: ISendOtp): Promise<string> {
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);

View File

@ -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;
}

View File

@ -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';

View File

@ -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;
}

View File

@ -0,0 +1,36 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateCustomerTable1742112997024 implements MigrationInterface {
name = 'UpdateCustomerTable1742112997024';
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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"
`);
}
}

View File

@ -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';