From 084d39096c2fe99399332946e834757142f9c246 Mon Sep 17 00:00:00 2001 From: Abdalhamid Alhamad Date: Sun, 5 Jan 2025 13:15:57 +0300 Subject: [PATCH] feat:integration brancjio dynamic links to junior qr code registration --- .env.example | 11 +++++- src/junior/junior.module.ts | 14 ++++++-- src/junior/services/branch-io.service.ts | 43 ++++++++++++++++++++++++ src/junior/services/index.ts | 1 + src/junior/services/qrcode.service.ts | 9 +++-- 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/junior/services/branch-io.service.ts diff --git a/.env.example b/.env.example index 6586f51..6af5ce0 100644 --- a/.env.example +++ b/.env.example @@ -28,4 +28,13 @@ MAIL_HOST=smtp.gmail.com MAIL_USER=aahalhmad@gmail.com MAIL_PASSWORD= MAIL_PORT=587 -MAIL_FROM=UBA \ No newline at end of file +MAIL_FROM=UBA + + +BRANCH_IO_URL=https://api2.branch.io/v1/url +BRANCH_IO_KEY= +ZOD_BASE_URL=http://localhost:5001 +ANDROID_PACKAGE_NAME=com.zod +IOS_PACKAGE_NAME=com.zod +ANDRIOD_JUNIOR_DEEPLINK_PATH=zodbank://juniors/qr-code/validate +IOS_JUNIOR_DEEPLINK_PATH=zodbank://juniors/qr-code/validate \ No newline at end of file diff --git a/src/junior/junior.module.ts b/src/junior/junior.module.ts index d1d8ec4..fab48a4 100644 --- a/src/junior/junior.module.ts +++ b/src/junior/junior.module.ts @@ -1,3 +1,4 @@ +import { HttpModule } from '@nestjs/axios'; import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { CustomerModule } from '~/customer/customer.module'; @@ -5,12 +6,19 @@ import { UserModule } from '~/user/user.module'; import { JuniorController } from './controllers'; import { Junior, JuniorRegistrationToken, Theme } from './entities'; import { JuniorRepository, JuniorTokenRepository } from './repositories'; -import { JuniorService, JuniorTokenService, QrcodeService } from './services'; +import { BranchIoService, JuniorService, JuniorTokenService, QrcodeService } from './services'; @Module({ controllers: [JuniorController], - providers: [JuniorService, JuniorRepository, JuniorTokenService, JuniorTokenRepository, QrcodeService], - imports: [TypeOrmModule.forFeature([Junior, Theme, JuniorRegistrationToken]), UserModule, CustomerModule], + providers: [ + JuniorService, + JuniorRepository, + JuniorTokenService, + JuniorTokenRepository, + QrcodeService, + BranchIoService, + ], + imports: [TypeOrmModule.forFeature([Junior, Theme, JuniorRegistrationToken]), UserModule, CustomerModule, HttpModule], exports: [JuniorService, JuniorTokenService], }) export class JuniorModule {} diff --git a/src/junior/services/branch-io.service.ts b/src/junior/services/branch-io.service.ts new file mode 100644 index 0000000..8f7e4c7 --- /dev/null +++ b/src/junior/services/branch-io.service.ts @@ -0,0 +1,43 @@ +import { HttpService } from '@nestjs/axios'; +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; + +@Injectable() +export class BranchIoService { + private readonly logger = new Logger(BranchIoService.name); + private readonly branchIoKey = this.configService.getOrThrow('BRANCH_IO_KEY'); + private readonly branchIoUrl = this.configService.getOrThrow('BRANCH_IO_URL'); + private readonly zodBaseUrl = this.configService.getOrThrow('ZOD_BASE_URL'); + private readonly andrioPackageName = this.configService.getOrThrow('ANDROID_PACKAGE_NAME'); + private readonly iosPackageName = this.configService.getOrThrow('IOS_PACKAGE_NAME'); + private readonly androidDeeplinkPath = this.configService.getOrThrow('ANDRIOD_JUNIOR_DEEPLINK_PATH'); + private readonly iosDeeplinkPath = this.configService.getOrThrow('IOS_JUNIOR_DEEPLINK_PATH'); + constructor(private readonly configService: ConfigService, private readonly httpService: HttpService) {} + + async createBranchLink(token: string) { + this.logger.log(`Creating branch link`); + const payload = { + branch_key: this.branchIoKey, + channel: 'Website', + feature: 'Share', + alias: token, + data: { + $desktop_url: `${this.zodBaseUrl}/juniors/qr-code/${token}/validate`, + $android_package: this.andrioPackageName, + $ios_package: this.iosPackageName, + $android_deeplink_path: this.androidDeeplinkPath, + $ios_url: this.iosDeeplinkPath, + token: token, + }, + }; + const response = await this.httpService.axiosRef({ + url: this.branchIoUrl, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + data: payload, + }); + return response.data.url; + } +} diff --git a/src/junior/services/index.ts b/src/junior/services/index.ts index bf6f733..38a9d27 100644 --- a/src/junior/services/index.ts +++ b/src/junior/services/index.ts @@ -1,3 +1,4 @@ +export * from './branch-io.service'; export * from './junior-token.service'; export * from './junior.service'; export * from './qrcode.service'; diff --git a/src/junior/services/qrcode.service.ts b/src/junior/services/qrcode.service.ts index 5b1f931..7001f61 100644 --- a/src/junior/services/qrcode.service.ts +++ b/src/junior/services/qrcode.service.ts @@ -1,11 +1,16 @@ import { Injectable, Logger } from '@nestjs/common'; import * as qrcode from 'qrcode'; +import { BranchIoService } from './branch-io.service'; @Injectable() export class QrcodeService { + constructor(private readonly branchIoService: BranchIoService) {} private readonly logger = new Logger(QrcodeService.name); - generateQrCode(token: string): Promise { + async generateQrCode(token: string): Promise { this.logger.log(`Generating QR code for token ${token}`); - return qrcode.toDataURL(token); + + const link = await this.branchIoService.createBranchLink(token); + + return qrcode.toDataURL(link); } }