sms logs
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
export interface ISmsResponse {
|
||||
status: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface ISmsParams {
|
||||
phone: string;
|
||||
parameters?: Record<string, string | number | Date>;
|
||||
templateId: string;
|
||||
}
|
||||
|
||||
export interface ISmsBodyParameters {
|
||||
Mobile: string;
|
||||
TemplateId: string;
|
||||
Parameters: { name: string; value: string }[];
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import axios from 'axios';
|
||||
import { ISmsParams, ISmsResponse, ISmsBodyParameters } from './interface/sms';
|
||||
|
||||
@Injectable()
|
||||
export class SmsService {
|
||||
private readonly baseURL: string;
|
||||
private readonly OTP: string;
|
||||
private readonly apiKey: string;
|
||||
|
||||
constructor(
|
||||
// private readonly httpService: HttpService,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
this.baseURL = this.configService.getOrThrow<string>('SMS_BASE_URL');
|
||||
this.apiKey = this.configService.getOrThrow<string>('SMS_API_KEY');
|
||||
this.OTP = this.configService.getOrThrow<string>('SMS_PATTERN_OTP');
|
||||
}
|
||||
|
||||
sendotp(phone: string, otp: string) {
|
||||
return this.sendSms({
|
||||
phone,
|
||||
parameters: { otp },
|
||||
templateId: this.OTP,
|
||||
});
|
||||
}
|
||||
|
||||
async sendSms(params: ISmsParams) {
|
||||
const { parameters, phone, templateId } = params;
|
||||
const parametersArray = Object.entries(parameters ?? {}).map(([name, value]) => ({
|
||||
name,
|
||||
value: value.toString(),
|
||||
}));
|
||||
const cleanedPhone = this.formatPhone(phone);
|
||||
|
||||
const smsData: ISmsBodyParameters = {
|
||||
Parameters: parametersArray,
|
||||
Mobile: cleanedPhone,
|
||||
TemplateId: templateId,
|
||||
};
|
||||
|
||||
const url = `${this.baseURL}/send/verify`;
|
||||
|
||||
try {
|
||||
const { data } = await axios.post<ISmsResponse>(url, smsData, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-KEY': this.apiKey,
|
||||
},
|
||||
});
|
||||
if (data.status !== 1) {
|
||||
throw new HttpException('The SMS was not sent. Please try again later.', HttpStatus.BAD_GATEWAY);
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('SMS sending failed:', JSON.stringify(error));
|
||||
throw new HttpException('The SMS was not sent. Please try again later.', HttpStatus.BAD_GATEWAY);
|
||||
}
|
||||
}
|
||||
formatPhone(phone: string): string {
|
||||
// remove spaces, dashes, parentheses
|
||||
phone = phone.replace(/[^\d+]/g, '');
|
||||
|
||||
if (phone.startsWith('+98')) {
|
||||
return phone;
|
||||
}
|
||||
|
||||
if (phone.startsWith('98')) {
|
||||
return `+${phone}`;
|
||||
}
|
||||
|
||||
if (phone.startsWith('0')) {
|
||||
return `+98${phone.slice(1)}`;
|
||||
}
|
||||
|
||||
return `+98${phone}`;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CacheService } from './cache.service';
|
||||
import { SmsService } from './sms.service';
|
||||
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [],
|
||||
providers: [CacheService, SmsService],
|
||||
exports: [CacheService, SmsService],
|
||||
providers: [CacheService, ],
|
||||
exports: [CacheService],
|
||||
})
|
||||
export class UtilsModule {}
|
||||
|
||||
Reference in New Issue
Block a user