import { Injectable } from '@nestjs/common' import { AuthTokenResult, Patient } from '../../models' import { OS } from '../../params' import { PatientRepository } from '../../repositories' import { CountryCode, LanguageCountryCode } from '../../utils' import { AwsService } from '../aws/aws.service' import { ComteleService } from '../comtele/comtele.service' import { MailService } from '../mail/mail.service' import { AuthToken, AuthTokenHelper } from './helpers' import { TranslateAuthenticationHelper } from './helpers/i18n' import { SMSTokenOptions } from './types' export type SendTo = { sms?: boolean; email?: boolean } export const REVIEWER_TOKEN = '04081' export const ANDROID_REVIEWERS = ['play.br@reviewer.com', 'play.ec@reviewer.com', 'play.mx@reviewer.com'] export const IOS_REVIEWERS = [ 'apple_reviewer.br@apple.com', 'apple_reviewer.mx@apple.com', 'apple_reviewer.ec@apple.com', ] @Injectable() export class TokenService { constructor( private readonly patientRepository: PatientRepository, private readonly awsService: AwsService, private readonly comteleService: ComteleService, private readonly mailService: MailService, private readonly translator: TranslateAuthenticationHelper, ) {} async sendAuthToken(patient: Patient, sendTo?: SendTo, smsOptions?: SMSTokenOptions): Promise { const authToken = this.getAuthToken(patient) patient.setAuthToken(authToken.encryptedCode) await this.patientRepository.update(patient) await this.sendToSmsAndEmail(authToken, patient, sendTo, smsOptions) return { email: patient.email, cellPhone: patient.cell_phone, } } async sendToSmsAndEmail( authToken: AuthToken, patient: Patient, { sms, email }: SendTo = { sms: true, email: true }, smsOptions?: SMSTokenOptions, ): Promise { if (sms) await this.sendSMS(authToken, patient, smsOptions) if (email) await this.sendMail(authToken, patient) } private async sendSMS(authToken: AuthToken, patient: Patient, smsOptions?: SMSTokenOptions): Promise { const smsService = patient.country_code === LanguageCountryCode.PT_BR && this.comteleService.isEnabled ? this.comteleService : this.awsService await smsService.sendSMS({ message: this.getMessage(patient.country_code as CountryCode, authToken.code, smsOptions), phoneNumber: patient.cell_phone, controlId: `${patient.id}/${authToken.code}`, }) } private getMessage(countryCode: CountryCode, code: string, smsOptions?: SMSTokenOptions): string { const message = this.translator.get(countryCode, 'sms.auth_token', { params: { code } }) return `${message}${smsOptions?.os === OS.ANDROID ? ` ${smsOptions.appSignature}` : ''}` } private async sendMail(authToken: AuthToken, patient: Patient): Promise { const templateId = this.translator.get(patient.country_code, `email_templates.auth_token`) await this.mailService.send({ templateId, to: patient.email, dynamicTemplateData: { token: authToken.code, }, }) } private getAuthToken = (patient: Patient): AuthToken => { if ([...ANDROID_REVIEWERS, ...IOS_REVIEWERS].includes(patient.email)) return AuthTokenHelper.buildAuthToken(REVIEWER_TOKEN) return AuthTokenHelper.buildAuthToken() } }