chore: add new service to add subs to danak services in array
This commit is contained in:
@@ -1,18 +1,28 @@
|
||||
export interface ISmsBody {
|
||||
ParameterArray: IParameterArray[];
|
||||
export interface IParameterArray {
|
||||
name: TemplateParams;
|
||||
value: string;
|
||||
}
|
||||
export interface ISmsResponse {
|
||||
status: number;
|
||||
message: string;
|
||||
}
|
||||
//----------------------------------------------
|
||||
|
||||
export interface ISmsGetLineResponse extends ISmsResponse {
|
||||
data: string[];
|
||||
}
|
||||
|
||||
export interface ISmsVerifyResponse extends ISmsResponse {
|
||||
data: { MessageId: number; Cost: number };
|
||||
}
|
||||
|
||||
//----------------------------------------------
|
||||
export interface ISmsVerifyBody {
|
||||
Parameters: IParameterArray[];
|
||||
Mobile: string;
|
||||
TemplateId: string;
|
||||
UserApiKey: string;
|
||||
SecretKey: string;
|
||||
}
|
||||
|
||||
export interface IParameterArray {
|
||||
Parameter: string;
|
||||
ParameterValue: string;
|
||||
}
|
||||
// export type SmsBodyType = ISmsVerifyBody;
|
||||
|
||||
export interface ISmsResponse {
|
||||
VerificationCodeId: number;
|
||||
isSuccessful: boolean;
|
||||
Message: string;
|
||||
}
|
||||
export type TemplateParams = "Code";
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { MailerService } from "@nestjs-modules/mailer";
|
||||
|
||||
import { EmailMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
@Injectable()
|
||||
export class EmailService {
|
||||
private readonly logger = new Logger(EmailService.name);
|
||||
constructor(private readonly mailerService: MailerService) {}
|
||||
|
||||
async sendEmail(to: string, subject: string) {
|
||||
async sendEmail(to: string, subject: string, link: string, fullName: string) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
const emailInfo = await this.mailerService.sendMail({
|
||||
to: to,
|
||||
from: "noreply@nestjs.com",
|
||||
// from: "noreply@nestjs.com",
|
||||
subject: subject,
|
||||
template: "otp",
|
||||
template: "email-verify",
|
||||
context: {
|
||||
//Data to be sent to template
|
||||
title: EmailMessage.EMAIL_VERIFICATION,
|
||||
link,
|
||||
fullName,
|
||||
},
|
||||
});
|
||||
return emailInfo;
|
||||
} catch (error) {
|
||||
this.logger.error("Email sending failed:", error);
|
||||
return false;
|
||||
|
||||
@@ -1,36 +1,76 @@
|
||||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import axios from "axios";
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
import { AxiosError } from "axios";
|
||||
import { catchError, firstValueFrom } from "rxjs";
|
||||
|
||||
import { ISmsConfigs } from "../../../configs/sms.config";
|
||||
import { SMS_CONFIG } from "../constants";
|
||||
import { ISmsBody } from "../interfaces/ISms";
|
||||
import { ISmsGetLineResponse, ISmsVerifyBody, ISmsVerifyResponse } from "../interfaces/ISms";
|
||||
|
||||
@Injectable()
|
||||
export class SmsService {
|
||||
private logger = new Logger(SmsService.name);
|
||||
constructor(@Inject(SMS_CONFIG) private smsConfigs: ISmsConfigs) {}
|
||||
private readonly logger = new Logger(SmsService.name);
|
||||
|
||||
constructor(
|
||||
@Inject(SMS_CONFIG) private smsConfigs: ISmsConfigs,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
//************************************************* */
|
||||
|
||||
async sendSmsVerifyCode(mobile: string, otpCode: string) {
|
||||
const smsData: ISmsBody = {
|
||||
ParameterArray: [{ Parameter: "otp", ParameterValue: otpCode }],
|
||||
//
|
||||
const smsData: ISmsVerifyBody = {
|
||||
Parameters: [{ name: "Code", value: 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",
|
||||
},
|
||||
});
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.post<ISmsVerifyResponse>(
|
||||
`${this.smsConfigs.API_URL}/send/verify`,
|
||||
{ smsData },
|
||||
{
|
||||
headers: { "X-API-KEY": this.smsConfigs.API_KEY },
|
||||
},
|
||||
)
|
||||
.pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error("error in sending verify sms", err);
|
||||
throw new InternalServerErrorException("error in sending verify sms");
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
this.logger.error("error in sending sms", error);
|
||||
}
|
||||
}
|
||||
|
||||
//************************************************* */
|
||||
|
||||
async getSmsLines() {
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.get<ISmsGetLineResponse>(`${this.smsConfigs.API_URL}/lines`, {
|
||||
headers: { "X-API-KEY": this.smsConfigs.API_KEY },
|
||||
})
|
||||
.pipe(
|
||||
catchError((error: AxiosError) => {
|
||||
this.logger.error("error in getting sms lines", error);
|
||||
throw new InternalServerErrorException("error in getting sms lines");
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
this.logger.error("error in getting sms lines", error);
|
||||
}
|
||||
}
|
||||
|
||||
//************************************************* */
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Module } from "@nestjs/common";
|
||||
|
||||
import { S3_CONFIG, SMS_CONFIG } from "./constants";
|
||||
import { CacheService } from "./providers/cache.service";
|
||||
import { EmailService } from "./providers/email.service";
|
||||
import { OTPService } from "./providers/otp.service";
|
||||
import { PasswordService } from "./providers/password.service";
|
||||
import { S3Service } from "./providers/s3.service";
|
||||
@@ -11,6 +12,7 @@ import { smsConfigs } from "../../configs/sms.config";
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
EmailService,
|
||||
OTPService,
|
||||
PasswordService,
|
||||
CacheService,
|
||||
@@ -27,6 +29,6 @@ import { smsConfigs } from "../../configs/sms.config";
|
||||
inject: S3Configs().inject,
|
||||
},
|
||||
],
|
||||
exports: [SMS_CONFIG, S3_CONFIG, S3Service, OTPService, PasswordService, CacheService, SmsService],
|
||||
exports: [SMS_CONFIG, S3_CONFIG, S3Service, OTPService, PasswordService, CacheService, SmsService, EmailService],
|
||||
})
|
||||
export class UtilsModule {}
|
||||
|
||||
Reference in New Issue
Block a user