Sms factor
This commit is contained in:
parent
aa85dab287
commit
c0cfb8aec7
@ -46,6 +46,7 @@
|
||||
"@pinata/sdk": "^2.1.0",
|
||||
"@prisma/client": "^4.11.0",
|
||||
"adm-zip": "^0.5.10",
|
||||
"axios": "^1.6.2",
|
||||
"bcrypt": "^5.1.1",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.0",
|
||||
|
@ -121,6 +121,9 @@ export class BackendVariables {
|
||||
@IsNotEmpty()
|
||||
public readonly OVH_CONSUMER_KEY!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
public readonly SMS_FACTOR_TOKEN!: string;
|
||||
|
||||
public constructor() {
|
||||
dotenv.config();
|
||||
this.DATABASE_PORT = process.env["DATABASE_PORT"]!;
|
||||
@ -162,6 +165,7 @@ export class BackendVariables {
|
||||
this.OVH_APP_KEY = process.env["OVH_APP_KEY"]!;
|
||||
this.OVH_APP_SECRET = process.env["OVH_APP_SECRET"]!;
|
||||
this.OVH_CONSUMER_KEY = process.env["OVH_CONSUMER_KEY"]!;
|
||||
this.SMS_FACTOR_TOKEN = process.env["SMS_FACTOR_TOKEN"]!;
|
||||
|
||||
|
||||
}
|
||||
|
@ -4,40 +4,40 @@ import { Service } from "typedi";
|
||||
|
||||
@Service()
|
||||
export default class OvhService extends BaseService {
|
||||
|
||||
constructor(private variables: BackendVariables) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Get all Customers
|
||||
* @throws {Error} If Customers cannot be get
|
||||
*/
|
||||
public async sendSms(phoneNumber: string, message: string): Promise<void> {
|
||||
|
||||
const ovh = require('ovh')({
|
||||
public async sendSms(phoneNumber: string, message: string): Promise<boolean> {
|
||||
const ovh = require("ovh")({
|
||||
appKey: this.variables.OVH_APP_KEY,
|
||||
appSecret: this.variables.OVH_APP_SECRET,
|
||||
consumerKey: this.variables.OVH_CONSUMER_KEY,
|
||||
});
|
||||
|
||||
// Get the serviceName (name of your SMS account)
|
||||
ovh.request('GET', '/sms', function (err: any, serviceName: string) {
|
||||
ovh.request("GET", "/sms", function (err: any, serviceName: string) {
|
||||
if (err) {
|
||||
console.log(err, serviceName);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
} else {
|
||||
console.log("My account SMS is " + serviceName);
|
||||
|
||||
// Send a simple SMS with a short number using your serviceName
|
||||
ovh.request('POST', '/sms/' + serviceName + '/jobs', {
|
||||
ovh.request(
|
||||
"POST",
|
||||
"/sms/" + serviceName + "/jobs",
|
||||
{
|
||||
message: message,
|
||||
senderForResponse: true,
|
||||
receivers: [phoneNumber]
|
||||
}, function (errsend: any, result: any) {
|
||||
receivers: [phoneNumber],
|
||||
},
|
||||
function (errsend: any, result: any) {
|
||||
console.log(errsend, result);
|
||||
});
|
||||
},
|
||||
);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
29
src/services/common/SmsFactorService/SmsFactorService.ts
Normal file
29
src/services/common/SmsFactorService/SmsFactorService.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { BackendVariables } from "@Common/config/variables/Variables";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
import axios from "axios";
|
||||
|
||||
@Service()
|
||||
export default class SmsFactorService extends BaseService {
|
||||
|
||||
constructor(private variables: BackendVariables) {
|
||||
super();
|
||||
}
|
||||
|
||||
public async sendSms(phoneNumber: string, message: string): Promise<boolean> {
|
||||
axios.post('https://api.smsfactor.com/send/', {
|
||||
token: this.variables.SMS_FACTOR_TOKEN,
|
||||
sender: "LeCoffre",
|
||||
to: phoneNumber,
|
||||
text: message,
|
||||
}).then(response => {
|
||||
console.log('SMS sent successfully:', response.data);
|
||||
return true;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error sending SMS:', error.response.data);
|
||||
return false;
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import TotpCodes, { TotpCodesReasons } from "le-coffre-resources/dist/Customer/T
|
||||
import { Customer } from "le-coffre-resources/dist/Notary";
|
||||
import { Service } from "typedi";
|
||||
import OvhService from "@Services/common/OvhService/OvhService";
|
||||
import SmsFactorService from "@Services/common/SmsFactorService/SmsFactorService";
|
||||
|
||||
export class SmsNotExpiredError extends Error {
|
||||
constructor() {
|
||||
@ -58,6 +59,7 @@ export default class CustomersService extends BaseService {
|
||||
private totpCodesRepository: TotpCodesRepository,
|
||||
private variables: BackendVariables,
|
||||
private ovhService: OvhService,
|
||||
private smsFactorService: SmsFactorService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@ -345,25 +347,17 @@ export default class CustomersService extends BaseService {
|
||||
}
|
||||
|
||||
private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) {
|
||||
try {
|
||||
// Sélectionnez le fournisseur de SMS en fonction de la variable d'environnement
|
||||
const selectedProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.ovhService : this.ovhService;
|
||||
const selectedProvider = this.variables.SMS_PROVIDER === "OVH" ? this.ovhService : this.smsFactorService;
|
||||
|
||||
// Envoi du SMS
|
||||
if(!customer.contact?.phone_number) return false;
|
||||
if (!customer.contact?.phone_number) return;
|
||||
let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin.toString());
|
||||
|
||||
// Si l'envoi échoue, basculez automatiquement sur le second fournisseur
|
||||
// if (!success) {
|
||||
// const alternateProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.smsService2 : this.ovhService;
|
||||
// success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin);
|
||||
// }
|
||||
|
||||
return success;
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Erreur lors de l'envoi du SMS : ${error}`);
|
||||
return false;
|
||||
if (!success) {
|
||||
const alternateProvider = this.variables.SMS_PROVIDER === "OVH" ? this.smsFactorService : this.ovhService;
|
||||
success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user