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