diff --git a/src/app.module.ts b/src/app.module.ts index e8f891d..75ab21a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -104,6 +104,8 @@ import { TimeGroupModel } from './modules/item-related/time-group/data/models/ti import { OtpVerificationModule } from './modules/configuration/otp-verification/otp-verification.module'; import { OtpVerificationModel } from './modules/configuration/otp-verification/data/models/otp-verification.model'; +import { OtpVerifierModel } from './modules/configuration/otp-verification/data/models/otp-verifier.model'; + @Module({ imports: [ ApmModule.register(), @@ -168,7 +170,9 @@ import { OtpVerificationModel } from './modules/configuration/otp-verification/d // Booking Online VerificationModel, + OtpVerificationModel, + OtpVerifierModel, ], synchronize: false, }), @@ -234,7 +238,6 @@ import { OtpVerificationModel } from './modules/configuration/otp-verification/d BookingOnlineAuthModule, BookingOrderModule, - OtpVerificationModule, ], controllers: [], diff --git a/src/core/strings/constants/table.constants.ts b/src/core/strings/constants/table.constants.ts index edc0b7b..2139ba6 100644 --- a/src/core/strings/constants/table.constants.ts +++ b/src/core/strings/constants/table.constants.ts @@ -46,4 +46,5 @@ export enum TABLE_NAME { TIME_GROUPS = 'time_groups', OTP_VERIFICATIONS = 'otp_verifications', + OTP_VERIFIER = 'otp_verifier', } diff --git a/src/database/migrations/1749043616622-add_table_otp_verifier.ts b/src/database/migrations/1749043616622-add_table_otp_verifier.ts new file mode 100644 index 0000000..b2085c4 --- /dev/null +++ b/src/database/migrations/1749043616622-add_table_otp_verifier.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddTableOtpVerifier1749043616622 implements MigrationInterface { + name = 'AddTableOtpVerifier1749043616622'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "otp_verifier" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "creator_id" character varying(36), "creator_name" character varying(125), "editor_id" character varying(36), "editor_name" character varying(125), "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "name" character varying, "phone_number" character varying NOT NULL, CONSTRAINT "PK_884e2d0873fc589a1bdc477b2ea" PRIMARY KEY ("id"))`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "otp_verifier"`); + } +} diff --git a/src/database/migrations/1749046285398-update_enum_otp_action_type.ts b/src/database/migrations/1749046285398-update_enum_otp_action_type.ts new file mode 100644 index 0000000..e107607 --- /dev/null +++ b/src/database/migrations/1749046285398-update_enum_otp_action_type.ts @@ -0,0 +1,37 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class UpdateEnumOtpActionType1749046285398 + implements MigrationInterface +{ + name = 'UpdateEnumOtpActionType1749046285398'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TYPE "public"."otp_verifications_action_type_enum" RENAME TO "otp_verifications_action_type_enum_old"`, + ); + await queryRunner.query( + `CREATE TYPE "public"."otp_verifications_action_type_enum" AS ENUM('CREATE_DISCOUNT', 'CANCEL_TRANSACTION', 'REJECT_RECONCILIATION')`, + ); + await queryRunner.query( + `ALTER TABLE "otp_verifications" ALTER COLUMN "action_type" TYPE "public"."otp_verifications_action_type_enum" USING "action_type"::"text"::"public"."otp_verifications_action_type_enum"`, + ); + await queryRunner.query( + `DROP TYPE "public"."otp_verifications_action_type_enum_old"`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TYPE "public"."otp_verifications_action_type_enum_old" AS ENUM('CREATE_DISCOUNT', 'CANCEL_TRANSACTION')`, + ); + await queryRunner.query( + `ALTER TABLE "otp_verifications" ALTER COLUMN "action_type" TYPE "public"."otp_verifications_action_type_enum_old" USING "action_type"::"text"::"public"."otp_verifications_action_type_enum_old"`, + ); + await queryRunner.query( + `DROP TYPE "public"."otp_verifications_action_type_enum"`, + ); + await queryRunner.query( + `ALTER TYPE "public"."otp_verifications_action_type_enum_old" RENAME TO "otp_verifications_action_type_enum"`, + ); + } +} diff --git a/src/modules/configuration/otp-verification/data/models/otp-verifier.model.ts b/src/modules/configuration/otp-verification/data/models/otp-verifier.model.ts new file mode 100644 index 0000000..cbb7f3d --- /dev/null +++ b/src/modules/configuration/otp-verification/data/models/otp-verifier.model.ts @@ -0,0 +1,16 @@ +import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; +import { OtpVerifierEntity } from '../../domain/entities/otp-verification.entity'; +import { Column, Entity } from 'typeorm'; +import { BaseModel } from 'src/core/modules/data/model/base.model'; + +@Entity(TABLE_NAME.OTP_VERIFIER) +export class OtpVerifierModel + extends BaseModel + implements OtpVerifierEntity +{ + @Column({ type: 'varchar', nullable: true }) + name: string; + + @Column({ type: 'varchar', nullable: false }) + phone_number: string; +} diff --git a/src/modules/configuration/otp-verification/data/services/otp-verification.service.ts b/src/modules/configuration/otp-verification/data/services/otp-verification.service.ts index 87ae630..fc9a7e5 100644 --- a/src/modules/configuration/otp-verification/data/services/otp-verification.service.ts +++ b/src/modules/configuration/otp-verification/data/services/otp-verification.service.ts @@ -3,19 +3,26 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { OtpVerificationModel } from '../models/otp-verification.model'; import { - OTP_SOURCE, OtpRequestEntity, OtpVerificationEntity, + OtpVerifierEntity, + // OtpVerifierEntity, OtpVerifyEntity, } from '../../domain/entities/otp-verification.entity'; import * as moment from 'moment'; import { OtpService } from 'src/core/helpers/otp/otp-service'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; +import { WhatsappService } from 'src/services/whatsapp/whatsapp.service'; +import { OtpVerifierModel } from '../models/otp-verifier.model'; + @Injectable() export class OtpVerificationService { constructor( @InjectRepository(OtpVerificationModel) private readonly otpVerificationRepo: Repository, + + @InjectRepository(OtpVerifierModel) + private readonly otpVerifierRepo: Repository, ) {} private generateOtpExpiration(minutes = 5): number { @@ -82,6 +89,15 @@ export class OtpVerificationService { // save otp to database await this.otpVerificationRepo.save(newOtp); + const verifiers: OtpVerifierEntity[] = await this.otpVerifierRepo.find(); + const notificationService = new WhatsappService(); + + // verifiers.map((v) => { + // notificationService.sendOtpNotification({ + // phone: v.phone_number, + // code: otpCode, + // }); + // }); return { message: `OTP has been sent to the admin's WhatsApp.`, diff --git a/src/modules/configuration/otp-verification/domain/entities/otp-verification.entity.ts b/src/modules/configuration/otp-verification/domain/entities/otp-verification.entity.ts index 3d0dc5a..951b4a9 100644 --- a/src/modules/configuration/otp-verification/domain/entities/otp-verification.entity.ts +++ b/src/modules/configuration/otp-verification/domain/entities/otp-verification.entity.ts @@ -3,6 +3,7 @@ import { BaseEntity } from 'src/core/modules/domain/entities//base.entity'; export enum OTP_ACTION_TYPE { CREATE_DISCOUNT = 'CREATE_DISCOUNT', CANCEL_TRANSACTION = 'CANCEL_TRANSACTION', + REJECT_RECONCILIATION = 'REJECT_RECONCILIATION', } export enum OTP_SOURCE { @@ -32,3 +33,8 @@ export interface OtpRequestEntity { export interface OtpVerifyEntity extends OtpRequestEntity { otp_code: string; } + +export interface OtpVerifierEntity { + name: string; + phone_number: string; +} diff --git a/src/modules/configuration/otp-verification/otp-verification.module.ts b/src/modules/configuration/otp-verification/otp-verification.module.ts index 1fd4aeb..6e1f02d 100644 --- a/src/modules/configuration/otp-verification/otp-verification.module.ts +++ b/src/modules/configuration/otp-verification/otp-verification.module.ts @@ -6,10 +6,14 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { OtpVerificationModel } from './data/models/otp-verification.model'; import { OtpVerificationController } from './infrastructure/otp-verification-data.controller'; import { OtpVerificationService } from './data/services/otp-verification.service'; +import { OtpVerifierModel } from './data/models/otp-verifier.model'; @Module({ imports: [ ConfigModule.forRoot(), - TypeOrmModule.forFeature([OtpVerificationModel], CONNECTION_NAME.DEFAULT), + TypeOrmModule.forFeature( + [OtpVerificationModel, OtpVerifierModel], + CONNECTION_NAME.DEFAULT, + ), ], controllers: [OtpVerificationController], providers: [OtpVerificationService],