feat: setup verifier

pull/144/head
Firman Ramdhani 2025-06-04 21:27:34 +07:00
parent 67129a8c69
commit 6a0b1f6e05
8 changed files with 101 additions and 3 deletions

View File

@ -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 { OtpVerificationModule } from './modules/configuration/otp-verification/otp-verification.module';
import { OtpVerificationModel } from './modules/configuration/otp-verification/data/models/otp-verification.model'; 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({ @Module({
imports: [ imports: [
ApmModule.register(), ApmModule.register(),
@ -168,7 +170,9 @@ import { OtpVerificationModel } from './modules/configuration/otp-verification/d
// Booking Online // Booking Online
VerificationModel, VerificationModel,
OtpVerificationModel, OtpVerificationModel,
OtpVerifierModel,
], ],
synchronize: false, synchronize: false,
}), }),
@ -234,7 +238,6 @@ import { OtpVerificationModel } from './modules/configuration/otp-verification/d
BookingOnlineAuthModule, BookingOnlineAuthModule,
BookingOrderModule, BookingOrderModule,
OtpVerificationModule, OtpVerificationModule,
], ],
controllers: [], controllers: [],

View File

@ -46,4 +46,5 @@ export enum TABLE_NAME {
TIME_GROUPS = 'time_groups', TIME_GROUPS = 'time_groups',
OTP_VERIFICATIONS = 'otp_verifications', OTP_VERIFICATIONS = 'otp_verifications',
OTP_VERIFIER = 'otp_verifier',
} }

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddTableOtpVerifier1749043616622 implements MigrationInterface {
name = 'AddTableOtpVerifier1749043616622';
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(`DROP TABLE "otp_verifier"`);
}
}

View File

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

View File

@ -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<OtpVerifierEntity>
implements OtpVerifierEntity
{
@Column({ type: 'varchar', nullable: true })
name: string;
@Column({ type: 'varchar', nullable: false })
phone_number: string;
}

View File

@ -3,19 +3,26 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { OtpVerificationModel } from '../models/otp-verification.model'; import { OtpVerificationModel } from '../models/otp-verification.model';
import { import {
OTP_SOURCE,
OtpRequestEntity, OtpRequestEntity,
OtpVerificationEntity, OtpVerificationEntity,
OtpVerifierEntity,
// OtpVerifierEntity,
OtpVerifyEntity, OtpVerifyEntity,
} from '../../domain/entities/otp-verification.entity'; } from '../../domain/entities/otp-verification.entity';
import * as moment from 'moment'; import * as moment from 'moment';
import { OtpService } from 'src/core/helpers/otp/otp-service'; import { OtpService } from 'src/core/helpers/otp/otp-service';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; 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() @Injectable()
export class OtpVerificationService { export class OtpVerificationService {
constructor( constructor(
@InjectRepository(OtpVerificationModel) @InjectRepository(OtpVerificationModel)
private readonly otpVerificationRepo: Repository<OtpVerificationModel>, private readonly otpVerificationRepo: Repository<OtpVerificationModel>,
@InjectRepository(OtpVerifierModel)
private readonly otpVerifierRepo: Repository<OtpVerifierModel>,
) {} ) {}
private generateOtpExpiration(minutes = 5): number { private generateOtpExpiration(minutes = 5): number {
@ -82,6 +89,15 @@ export class OtpVerificationService {
// save otp to database // save otp to database
await this.otpVerificationRepo.save(newOtp); 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 { return {
message: `OTP has been sent to the admin's WhatsApp.`, message: `OTP has been sent to the admin's WhatsApp.`,

View File

@ -3,6 +3,7 @@ import { BaseEntity } from 'src/core/modules/domain/entities//base.entity';
export enum OTP_ACTION_TYPE { export enum OTP_ACTION_TYPE {
CREATE_DISCOUNT = 'CREATE_DISCOUNT', CREATE_DISCOUNT = 'CREATE_DISCOUNT',
CANCEL_TRANSACTION = 'CANCEL_TRANSACTION', CANCEL_TRANSACTION = 'CANCEL_TRANSACTION',
REJECT_RECONCILIATION = 'REJECT_RECONCILIATION',
} }
export enum OTP_SOURCE { export enum OTP_SOURCE {
@ -32,3 +33,8 @@ export interface OtpRequestEntity {
export interface OtpVerifyEntity extends OtpRequestEntity { export interface OtpVerifyEntity extends OtpRequestEntity {
otp_code: string; otp_code: string;
} }
export interface OtpVerifierEntity {
name: string;
phone_number: string;
}

View File

@ -6,10 +6,14 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { OtpVerificationModel } from './data/models/otp-verification.model'; import { OtpVerificationModel } from './data/models/otp-verification.model';
import { OtpVerificationController } from './infrastructure/otp-verification-data.controller'; import { OtpVerificationController } from './infrastructure/otp-verification-data.controller';
import { OtpVerificationService } from './data/services/otp-verification.service'; import { OtpVerificationService } from './data/services/otp-verification.service';
import { OtpVerifierModel } from './data/models/otp-verifier.model';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot(), ConfigModule.forRoot(),
TypeOrmModule.forFeature([OtpVerificationModel], CONNECTION_NAME.DEFAULT), TypeOrmModule.forFeature(
[OtpVerificationModel, OtpVerifierModel],
CONNECTION_NAME.DEFAULT,
),
], ],
controllers: [OtpVerificationController], controllers: [OtpVerificationController],
providers: [OtpVerificationService], providers: [OtpVerificationService],