Building the switch

This commit is contained in:
Vins 2023-11-28 00:56:33 +01:00
parent b6e1b2ff62
commit 95ced1f33e
2 changed files with 27 additions and 1 deletions

View File

@ -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[]) {

View File

@ -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;
}
}
/**