feat:integration brancjio dynamic links to junior qr code registration

This commit is contained in:
Abdalhamid Alhamad
2025-01-05 13:15:57 +03:00
parent eca84b4e75
commit 084d39096c
5 changed files with 72 additions and 6 deletions

View File

@ -28,4 +28,13 @@ MAIL_HOST=smtp.gmail.com
MAIL_USER=aahalhmad@gmail.com
MAIL_PASSWORD=
MAIL_PORT=587
MAIL_FROM=UBA
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

View File

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

View File

@ -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<string>('BRANCH_IO_KEY');
private readonly branchIoUrl = this.configService.getOrThrow<string>('BRANCH_IO_URL');
private readonly zodBaseUrl = this.configService.getOrThrow<string>('ZOD_BASE_URL');
private readonly andrioPackageName = this.configService.getOrThrow<string>('ANDROID_PACKAGE_NAME');
private readonly iosPackageName = this.configService.getOrThrow<string>('IOS_PACKAGE_NAME');
private readonly androidDeeplinkPath = this.configService.getOrThrow<string>('ANDRIOD_JUNIOR_DEEPLINK_PATH');
private readonly iosDeeplinkPath = this.configService.getOrThrow<string>('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;
}
}

View File

@ -1,3 +1,4 @@
export * from './branch-io.service';
export * from './junior-token.service';
export * from './junior.service';
export * from './qrcode.service';

View File

@ -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<string> {
async generateQrCode(token: string): Promise<string> {
this.logger.log(`Generating QR code for token ${token}`);
return qrcode.toDataURL(token);
const link = await this.branchIoService.createBranchLink(token);
return qrcode.toDataURL(link);
}
}