chore: sms service with configs
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export const SMS_CONFIG = "SMS_CONFIG";
|
||||
@@ -0,0 +1,18 @@
|
||||
export interface ISmsBody {
|
||||
ParameterArray: IParameterArray[];
|
||||
Mobile: string;
|
||||
TemplateId: string;
|
||||
UserApiKey: string;
|
||||
SecretKey: string;
|
||||
}
|
||||
|
||||
export interface IParameterArray {
|
||||
Parameter: string;
|
||||
ParameterValue: string;
|
||||
}
|
||||
|
||||
export interface ISmsResponse {
|
||||
VerificationCodeId: number;
|
||||
isSuccessful: boolean;
|
||||
Message: string;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import axios from "axios";
|
||||
import { ISmsConfigs } from "../../../configs/sms.config";
|
||||
import { SMS_CONFIG } from "../constants";
|
||||
import { ISmsBody } from "../interfaces/ISms";
|
||||
|
||||
@Injectable()
|
||||
export class SmsService {
|
||||
private logger = new Logger(SmsService.name);
|
||||
constructor(@Inject(SMS_CONFIG) private smsConfigs: ISmsConfigs) {}
|
||||
|
||||
async sendSmsVerifyCode(mobile: string, otpCode: string) {
|
||||
const smsData: ISmsBody = {
|
||||
ParameterArray: [{ Parameter: "otp", ParameterValue: otpCode }],
|
||||
Mobile: mobile,
|
||||
TemplateId: this.smsConfigs.SMS_PATTERN_OTP,
|
||||
...this.smsConfigs.SMS_SECRET_BODY,
|
||||
};
|
||||
|
||||
return this.sendSms(smsData);
|
||||
}
|
||||
|
||||
private async sendSms(body: ISmsBody) {
|
||||
try {
|
||||
const { data } = await axios.post(this.smsConfigs.URL, body, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return data;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,15 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { SMS_CONFIG } from "./constants";
|
||||
import { SmsConfigs } from "../../configs/sms.config";
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
providers: [
|
||||
{
|
||||
provide: SMS_CONFIG,
|
||||
useFactory: SmsConfigs().useFactory,
|
||||
inject: SmsConfigs().inject
|
||||
},
|
||||
],
|
||||
exports: [SMS_CONFIG]
|
||||
})
|
||||
export class UtilsModule {}
|
||||
export class UtilsModule { }
|
||||
|
||||
Reference in New Issue
Block a user