From 8f00d3e4bdd2a7127dba9bee007e899030636fbb Mon Sep 17 00:00:00 2001 From: Vins Date: Tue, 5 Dec 2023 10:56:22 +0100 Subject: [PATCH] Fixed cellphonenumber --- src/services/common/OvhService/OvhService.ts | 8 +++--- .../SmsFactorService/SmsFactorService.ts | 7 +++--- .../CustomersService/CustomersService.ts | 25 +++++++++++-------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/services/common/OvhService/OvhService.ts b/src/services/common/OvhService/OvhService.ts index eaa9196b..b4868daf 100644 --- a/src/services/common/OvhService/OvhService.ts +++ b/src/services/common/OvhService/OvhService.ts @@ -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; } } diff --git a/src/services/common/SmsFactorService/SmsFactorService.ts b/src/services/common/SmsFactorService/SmsFactorService.ts index 13332750..1bc3f714 100644 --- a/src/services/common/SmsFactorService/SmsFactorService.ts +++ b/src/services/common/SmsFactorService/SmsFactorService.ts @@ -9,7 +9,7 @@ export default class SmsFactorService extends BaseService { super(); } - public async sendSms(phoneNumber: string, message: string): Promise { + 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; } } diff --git a/src/services/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts index 8af9653e..8870873d 100644 --- a/src/services/customer/CustomersService/CustomersService.ts +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -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({ @@ -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); } }