Fixed cellphonenumber

This commit is contained in:
Vins 2023-12-05 10:56:22 +01:00
parent 00b46a2407
commit 8f00d3e4bd
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; const serviceName = this.variables.OVH_SMS_SERVICE_NAME;
ovh.request('POST', '/sms/' + serviceName + '/jobs/', { await ovh.request('POST', '/sms/' + serviceName + '/jobs/', {
message: message, message: message,
sender: "LeCoffre", sender: "LeCoffre",
senderForResponse: true, senderForResponse: true,
receivers: [phoneNumber], receivers: [phoneNumber],
}, (error: any, response: any) => { }, (error: any, response: any) => {
if (error) { if (error) {
console.error('Error sending Ovh Sms:', error); console.error('Error sending Ovh Sms');
return false; return false;
} else { } else {
console.log('SMS sent successfully via Ovh:', response); console.log('SMS sent successfully via Ovh');
return true; return true;
} }
}); });
return false; return true;
} }
} }

View File

@ -9,7 +9,7 @@ export default class SmsFactorService extends BaseService {
super(); super();
} }
public async sendSms(phoneNumber: string, message: string): Promise<boolean> { public async sendSms(phoneNumber: string, message: string){
axios axios
.get( .get(
"https://api.smsfactor.com/send/simulate?to=" + "https://api.smsfactor.com/send/simulate?to=" +
@ -21,13 +21,12 @@ export default class SmsFactorService extends BaseService {
{}, {},
) )
.then((response) => { .then((response) => {
console.log("SMS sent successfully via Sms Factor :" + response); console.log("SMS sent successfully via Sms Factor");
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.error("Error sending Sms Factor SMS:", error); console.error("Error sending Sms Factor SMS");
return false; 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 { Customers, Prisma, TotpCodes } from "@prisma/client";
import CustomersRepository from "@Repositories/CustomersRepository"; import CustomersRepository from "@Repositories/CustomersRepository";
import TotpCodesRepository from "@Repositories/TotpCodesRepository"; import TotpCodesRepository from "@Repositories/TotpCodesRepository";
@ -57,7 +57,7 @@ export default class CustomersService extends BaseService {
private customerRepository: CustomersRepository, private customerRepository: CustomersRepository,
private authService: AuthService, private authService: AuthService,
private totpCodesRepository: TotpCodesRepository, private totpCodesRepository: TotpCodesRepository,
private variables: BackendVariables, // private variables: BackendVariables,
private ovhService: OvhService, private ovhService: OvhService,
private smsFactorService: SmsFactorService, 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); const totpCode = await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60 * 1000), reason);
if (!totpCode) return null; if (!totpCode) return null;
// 5: Send the SMS code to the customer // 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 { return {
customer, customer,
totpCode: TotpCodesResource.hydrate<TotpCodesResource>({ 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); await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60000), TotpCodesReasons.RESET_PASSWORD);
// 5: Send the SMS code to the customer // 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; 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); const totpCode = await this.saveTotpPin(customer, totpPin, new Date(now + 5 * 60 * 1000), totpCodeToResend.reason!, true);
// 7: Send the SMS code to the customer // 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 }; return { customer, totpCode };
} }
@ -356,17 +359,19 @@ export default class CustomersService extends BaseService {
private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) { private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) {
const message = "Votre code de vérification LEcoffre.io est : " + totpPin.toString(); 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 // 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 // Envoi du SMS
if (!customer.contact?.phone_number) return; if (!customer.contact?.cell_phone_number) return;
let success = await selectedProvider.sendSms(customer.contact?.phone_number, message); let success = await this.ovhService.sendSms(customer.contact?.cell_phone_number, message);
// 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.smsFactorService : this.ovhService; //const alternateProvider = this.variables.SMS_PROVIDER === "OVH" ? this.smsFactorService : this.ovhService;
success = await alternateProvider.sendSms(customer.contact?.phone_number, message); await this.smsFactorService.sendSms(customer.contact?.cell_phone_number, message);
} }
} }