import { forwardRef, Inject, Injectable, NestMiddleware } from '@nestjs/common' import { debug, Debugger } from 'debug' import { isNil } from 'lodash' import { AuthenticationService } from '../services' @Injectable() export class ValidateLocaleMiddleware implements NestMiddleware { logger: Debugger constructor( @Inject(forwardRef(() => AuthenticationService)) private readonly authenticationService: AuthenticationService, ) { this.logger = debug('validate-locale') } async use(req: any, res: any, next: () => void) { const sessionVariable = req.body?.session_variables || {} const patientId = sessionVariable['x-hasura-user-id'] this.logger.extend(':')(`Validating locale`) if (patientId && isNil(sessionVariable['x-hasura-locale'])) try { this.authenticationService.revokePatientToken(patientId) this.logger.extend(':')(`Patient token revocked: ${patientId}`) } catch (error) {} next() } }