import { Injectable } from '@nestjs/common' import { UserNotFoundError } from '../../errors' import { Roles } from '../../models' import { PatientRepository } from '../../repositories' import { LanguageCountryCode } from '../../utils' import { AuthenticationService } from './authentication.service' import { AuthTokenHelper } from './helpers' @Injectable() export class TokenValidatorService { constructor( private readonly authentication: AuthenticationService, private readonly patientRepository: PatientRepository, ) {} async validatePatientToken(login: string, token: string): Promise { const patient = await this.patientRepository.findByLogin(login) if (!patient) throw new UserNotFoundError(login) if (AuthTokenHelper.authTokenIsValid(patient, token)) { return await this.authentication.getAccessToken( patient.id, null, Roles.PATIENT, patient.country_code as LanguageCountryCode, ) } } }