From 95ced1f33e171d51b17c8a5517c20d159d1cf102 Mon Sep 17 00:00:00 2001 From: Vins Date: Tue, 28 Nov 2023 00:56:33 +0100 Subject: [PATCH] Building the switch --- src/common/config/variables/Variables.ts | 4 ++++ .../CustomersService/CustomersService.ts | 24 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index 5a0d22dd..f23db83b 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -109,6 +109,9 @@ export class BackendVariables { @IsNotEmpty() public readonly DOCAPOST_APP_PASSWORD!: string; + @IsNotEmpty() + public readonly SMS_PROVIDER!: string; + public constructor() { dotenv.config(); this.DATABASE_PORT = process.env["DATABASE_PORT"]!; @@ -146,6 +149,7 @@ export class BackendVariables { this.BACK_API_HOST = process.env["BACK_API_HOST"]!; this.DOCAPOST_APP_ID = process.env["DOCAPOST_APP_ID"]!; this.DOCAPOST_APP_PASSWORD = process.env["DOCAPOST_APP_PASSWORD"]!; + this.SMS_PROVIDER = process.env["SMS_PROVIDER"]!; } public async validate(groups?: string[]) { diff --git a/src/services/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts index 496967e1..9c9a5e34 100644 --- a/src/services/customer/CustomersService/CustomersService.ts +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -1,3 +1,4 @@ +import { BackendVariables } from "@Common/config/variables/Variables"; import { Customers, Prisma } from "@prisma/client"; import CustomersRepository from "@Repositories/CustomersRepository"; import TotpCodesRepository from "@Repositories/TotpCodesRepository"; @@ -48,6 +49,7 @@ export default class CustomersService extends BaseService { private customerRepository: CustomersRepository, private authService: AuthService, private totpCodesRepository: TotpCodesRepository, + private variables: BackendVariables, ) { super(); } @@ -243,7 +245,27 @@ export default class CustomersService extends BaseService { } private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) { - console.log(totpPin); + try { + const selectedProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.smsService1 : this.smsService2; + + let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin); + + // Si l'envoi échoue, basculez automatiquement sur le second fournisseur + if (!success) { + const alternateProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.smsService2 : this.smsService1; + success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin); + } + + return success; + } catch (error) { + console.error(`Erreur lors de l'envoi du SMS : ${error}`); + return false; + } + + + + + } /**