forked from Yara724/api
sms logging in mongo part was added
This commit is contained in:
2
src/sms-orchestration/provider/kavenegar-sender.ts
Normal file
2
src/sms-orchestration/provider/kavenegar-sender.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
/** Kavenegar shortcode used on plain `send` (and recorded on SMS logs). */
|
||||
export const KAVENEGAR_SMS_SENDER = "10008663";
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
SmsProviderException,
|
||||
SmsTransportException,
|
||||
} from "./sms-provider.exception";
|
||||
import { KAVENEGAR_SMS_SENDER } from "./kavenegar-sender";
|
||||
import { SendMessage, VerifyLookUpMessage } from "./sms-gateway.types";
|
||||
|
||||
@Injectable()
|
||||
@@ -22,7 +23,7 @@ export class KavenegarSmsGateway {
|
||||
async sendMessage(data: SendMessage) {
|
||||
try {
|
||||
const body = await this.sender.send({
|
||||
sender: "10008663",
|
||||
sender: KAVENEGAR_SMS_SENDER,
|
||||
...data,
|
||||
});
|
||||
|
||||
@@ -48,7 +49,8 @@ export class KavenegarSmsGateway {
|
||||
|
||||
async verifyLookUp(data: VerifyLookUpMessage) {
|
||||
try {
|
||||
const body = await this.sender.verifyLookup(data);
|
||||
const { isOtp: _isOtp, ...providerPayload } = data;
|
||||
const body = await this.sender.verifyLookup(providerPayload);
|
||||
|
||||
if (!isKavenegarSuccess(body)) {
|
||||
const normalized = normalizeKavenegarBody(body);
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { HttpModule } from "@nestjs/axios";
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule, ConfigService } from "@nestjs/config";
|
||||
import { MongooseModule } from "@nestjs/mongoose";
|
||||
import { createHttpModuleOptions } from "src/core/config/http-proxy.factory";
|
||||
import { SmsSendLogService } from "../entities/db-service/sms-send-log.service";
|
||||
import {
|
||||
SmsSendLog,
|
||||
SmsSendLogSchema,
|
||||
} from "../entities/schema/sms-send-log.schema";
|
||||
|
||||
import { KavenegarService } from "./kavenegar.service";
|
||||
import { KavenegarSmsGateway } from "./kavenegar-sms.gateway";
|
||||
@@ -16,13 +22,17 @@ import { SmsGatewayService } from "./sms-gateway.service";
|
||||
useFactory: createHttpModuleOptions,
|
||||
}),
|
||||
ConfigModule,
|
||||
MongooseModule.forFeature([
|
||||
{ name: SmsSendLog.name, schema: SmsSendLogSchema },
|
||||
]),
|
||||
],
|
||||
providers: [
|
||||
KavenegarService,
|
||||
KavenegarSmsGateway,
|
||||
ParsianSmsGateway,
|
||||
SmsSendLogService,
|
||||
SmsGatewayService,
|
||||
],
|
||||
exports: [SmsGatewayService],
|
||||
exports: [SmsGatewayService, SmsSendLogService],
|
||||
})
|
||||
export class SmsGatewayModule {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable, Logger, OnModuleInit } from "@nestjs/common";
|
||||
import { SmsSendLogService } from "../entities/db-service/sms-send-log.service";
|
||||
import { KavenegarSmsGateway } from "./kavenegar-sms.gateway";
|
||||
import { ParsianSmsGateway } from "./parsian-sms.gateway";
|
||||
import { getSmsProvider } from "./sms-provider.config";
|
||||
@@ -13,6 +14,7 @@ export class SmsGatewayService implements OnModuleInit {
|
||||
constructor(
|
||||
private readonly kavenegar: KavenegarSmsGateway,
|
||||
private readonly parsian: ParsianSmsGateway,
|
||||
private readonly smsSendLogService: SmsSendLogService,
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
@@ -20,11 +22,43 @@ export class SmsGatewayService implements OnModuleInit {
|
||||
}
|
||||
|
||||
async sendMessage(data: SendMessage) {
|
||||
return this.activeGateway().sendMessage(data);
|
||||
const startedAt = Date.now();
|
||||
try {
|
||||
const result = await this.activeGateway().sendMessage(data);
|
||||
await this.smsSendLogService.recordGatewaySend({
|
||||
data,
|
||||
startedAt,
|
||||
result,
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
await this.smsSendLogService.recordGatewaySend({
|
||||
data,
|
||||
startedAt,
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async verifyLookUp(data: VerifyLookUpMessage) {
|
||||
return this.activeGateway().verifyLookUp(data);
|
||||
const startedAt = Date.now();
|
||||
try {
|
||||
const result = await this.activeGateway().verifyLookUp(data);
|
||||
await this.smsSendLogService.recordGatewayVerifyLookup({
|
||||
data,
|
||||
startedAt,
|
||||
result,
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
await this.smsSendLogService.recordGatewayVerifyLookup({
|
||||
data,
|
||||
startedAt,
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private activeGateway(): KavenegarSmsGateway | ParsianSmsGateway {
|
||||
|
||||
@@ -10,4 +10,6 @@ export interface VerifyLookUpMessage {
|
||||
token3?: string;
|
||||
token10?: string;
|
||||
receptor: string;
|
||||
/** When true, SMS log records this as an OTP (plain-text token preserved). */
|
||||
isOtp?: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user