diff --git a/src/services/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts index 65bd52f8..1f7b144c 100644 --- a/src/services/customer/CustomersService/CustomersService.ts +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -120,10 +120,23 @@ export default class CustomersService extends BaseService { // 2: Check in the array of totpCodes if one is still valid const validTotpCode = customerHydrated.totpCodes?.find((totpCode) => { - return totpCode.expire_at && totpCode.expire_at.getTime() > now; + return totpCode.expire_at && totpCode.expire_at.getTime() > now && totpCode.reason === TotpCodesReasons.RESET_PASSWORD; }); if (validTotpCode) throw new SmsNotExpiredError(); + // 3: Archive all active totp codes for this customer + const activeTotpCodes = customerHydrated.totpCodes?.filter((totpCode) => { + return totpCode.expire_at && totpCode.expire_at.getTime() > now; + }); + + if (activeTotpCodes) { + await Promise.all( + activeTotpCodes.map(async (totpCode) => { + await this.totpCodesRepository.disable(totpCode); + }), + ); + } + // 3: Generate a new SMS code const totpPin = this.generateTotp();