Merge branch 'dev' into staging

This commit is contained in:
Vins 2023-12-05 10:56:35 +01:00
commit 0fb6f1ddd7
3 changed files with 22 additions and 18 deletions

View File

@ -17,20 +17,20 @@ export default class OvhService extends BaseService {
const serviceName = this.variables.OVH_SMS_SERVICE_NAME;
ovh.request('POST', '/sms/' + serviceName + '/jobs/', {
await ovh.request('POST', '/sms/' + serviceName + '/jobs/', {
message: message,
sender: "LeCoffre",
senderForResponse: true,
receivers: [phoneNumber],
}, (error: any, response: any) => {
if (error) {
console.error('Error sending Ovh Sms:', error);
console.error('Error sending Ovh Sms');
return false;
} else {
console.log('SMS sent successfully via Ovh:', response);
console.log('SMS sent successfully via Ovh');
return true;
}
});
return false;
return true;
}
}

View File

@ -9,7 +9,7 @@ export default class SmsFactorService extends BaseService {
super();
}
public async sendSms(phoneNumber: string, message: string): Promise<boolean> {
public async sendSms(phoneNumber: string, message: string){
axios
.get(
"https://api.smsfactor.com/send/simulate?to=" +
@ -21,13 +21,12 @@ export default class SmsFactorService extends BaseService {
{},
)
.then((response) => {
console.log("SMS sent successfully via Sms Factor :" + response);
console.log("SMS sent successfully via Sms Factor");
return true;
})
.catch((error) => {
console.error("Error sending Sms Factor SMS:", error);
console.error("Error sending Sms Factor SMS");
return false;
});
return false;
}
}

View File

@ -1,4 +1,4 @@
import { BackendVariables } from "@Common/config/variables/Variables";
// import { BackendVariables } from "@Common/config/variables/Variables";
import { Customers, Prisma, TotpCodes } from "@prisma/client";
import CustomersRepository from "@Repositories/CustomersRepository";
import TotpCodesRepository from "@Repositories/TotpCodesRepository";
@ -57,7 +57,7 @@ export default class CustomersService extends BaseService {
private customerRepository: CustomersRepository,
private authService: AuthService,
private totpCodesRepository: TotpCodesRepository,
private variables: BackendVariables,
// private variables: BackendVariables,
private ovhService: OvhService,
private smsFactorService: SmsFactorService,
) {
@ -110,7 +110,8 @@ export default class CustomersService extends BaseService {
const totpCode = await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60 * 1000), reason);
if (!totpCode) return null;
// 5: Send the SMS code to the customer
if(this.variables.ENV !== 'dev') await this.sendSmsCodeToCustomer(totpPin, customer);
// if(this.variables.ENV !== 'dev')
await this.sendSmsCodeToCustomer(totpPin, customer);
return {
customer,
totpCode: TotpCodesResource.hydrate<TotpCodesResource>({
@ -162,7 +163,8 @@ export default class CustomersService extends BaseService {
await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60000), TotpCodesReasons.RESET_PASSWORD);
// 5: Send the SMS code to the customer
if(this.variables.ENV !== 'dev') await this.sendSmsCodeToCustomer(totpPin, customer);
// if(this.variables.ENV !== 'dev')
await this.sendSmsCodeToCustomer(totpPin, customer);
return customer;
}
@ -292,7 +294,8 @@ export default class CustomersService extends BaseService {
const totpCode = await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60 * 1000), totpCodeToResend.reason!, true);
// 7: Send the SMS code to the customer
if(this.variables.ENV !== 'dev') await this.sendSmsCodeToCustomer(totpPin, customer);
// if(this.variables.ENV !== 'dev')
await this.sendSmsCodeToCustomer(totpPin, customer);
return { customer, totpCode };
}
@ -356,17 +359,19 @@ export default class CustomersService extends BaseService {
private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) {
const message = "Votre code de vérification LEcoffre.io est : " + totpPin.toString();
// Sélectionnez le fournisseur de SMS en fonction de la variable d'environnement
const selectedProvider = this.variables.SMS_PROVIDER === "OVH" ? this.ovhService : this.smsFactorService;
//const selectedProvider = this.variables.SMS_PROVIDER === "OVH" ? this.ovhService : this.smsFactorService;
// Envoi du SMS
if (!customer.contact?.phone_number) return;
let success = await selectedProvider.sendSms(customer.contact?.phone_number, message);
if (!customer.contact?.cell_phone_number) return;
let success = await this.ovhService.sendSms(customer.contact?.cell_phone_number, message);
// Si l'envoi échoue, basculez automatiquement sur le second fournisseur
if (!success) {
const alternateProvider = this.variables.SMS_PROVIDER === "OVH" ? this.smsFactorService : this.ovhService;
success = await alternateProvider.sendSms(customer.contact?.phone_number, message);
//const alternateProvider = this.variables.SMS_PROVIDER === "OVH" ? this.smsFactorService : this.ovhService;
await this.smsFactorService.sendSms(customer.contact?.cell_phone_number, message);
}
}