This commit is contained in:
Vins 2023-11-30 10:14:49 +01:00
parent f00eda1714
commit bb8b49edbe
4 changed files with 64 additions and 5 deletions

View File

@ -63,6 +63,7 @@
"next": "^13.1.5", "next": "^13.1.5",
"node-cache": "^5.1.2", "node-cache": "^5.1.2",
"node-schedule": "^2.1.1", "node-schedule": "^2.1.1",
"ovh": "^2.0.3",
"prisma-query": "^2.0.0", "prisma-query": "^2.0.0",
"puppeteer": "^21.3.4", "puppeteer": "^21.3.4",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",

View File

@ -112,6 +112,15 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly SMS_PROVIDER!: string; public readonly SMS_PROVIDER!: string;
@IsNotEmpty()
public readonly OVH_APP_KEY!: string;
@IsNotEmpty()
public readonly OVH_APP_SECRET!: string;
@IsNotEmpty()
public readonly OVH_CONSUMER_KEY!: string;
public constructor() { public constructor() {
dotenv.config(); dotenv.config();
this.DATABASE_PORT = process.env["DATABASE_PORT"]!; this.DATABASE_PORT = process.env["DATABASE_PORT"]!;
@ -150,6 +159,10 @@ export class BackendVariables {
this.DOCAPOST_APP_ID = process.env["DOCAPOST_APP_ID"]!; this.DOCAPOST_APP_ID = process.env["DOCAPOST_APP_ID"]!;
this.DOCAPOST_APP_PASSWORD = process.env["DOCAPOST_APP_PASSWORD"]!; this.DOCAPOST_APP_PASSWORD = process.env["DOCAPOST_APP_PASSWORD"]!;
this.SMS_PROVIDER = process.env["SMS_PROVIDER"]!; this.SMS_PROVIDER = process.env["SMS_PROVIDER"]!;
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"]!;
} }
public async validate(groups?: string[]) { public async validate(groups?: string[]) {

View File

@ -0,0 +1,43 @@
import { BackendVariables } from "@Common/config/variables/Variables";
import BaseService from "@Services/BaseService";
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')({
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) {
if(err) {
console.log(err, serviceName);
}
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', {
message: message,
senderForResponse: true,
receivers: [phoneNumber]
}, function (errsend: any, result: any) {
console.log(errsend, result);
});
}
});
}
}

View File

@ -7,6 +7,7 @@ import AuthService from "@Services/common/AuthService/AuthService";
import TotpCodes, { TotpCodesReasons } from "le-coffre-resources/dist/Customer/TotpCodes"; import TotpCodes, { TotpCodesReasons } from "le-coffre-resources/dist/Customer/TotpCodes";
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";
export class SmsNotExpiredError extends Error { export class SmsNotExpiredError extends Error {
constructor() { constructor() {
@ -50,6 +51,7 @@ export default class CustomersService extends BaseService {
private authService: AuthService, private authService: AuthService,
private totpCodesRepository: TotpCodesRepository, private totpCodesRepository: TotpCodesRepository,
private variables: BackendVariables, private variables: BackendVariables,
private ovhService: OvhService,
) { ) {
super(); super();
} }
@ -247,16 +249,16 @@ export default class CustomersService extends BaseService {
private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) { private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) {
try { 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.smsService1 : this.smsService2; const selectedProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.ovhService : null;
// Envoi du SMS // Envoi du SMS
let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin); let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin);
// 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.smsService1; // const alternateProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.smsService2 : this.ovhService;
success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin); // success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin);
} // }
return success; return success;