import { Controller, Post, Body, Headers, HttpException, HttpStatus, Logger } from '@nestjs/common'; import { CallerResolutionService } from './caller-resolution.service'; import { CallerContextService } from './caller-context.service'; @Controller('api/caller') export class CallerResolutionController { private readonly logger = new Logger(CallerResolutionController.name); constructor( private readonly resolution: CallerResolutionService, private readonly callerContext: CallerContextService, ) {} @Post('resolve') async resolve( @Body('phone') phone: string, @Headers('authorization') auth: string, ) { if (!phone) { throw new HttpException('phone is required', HttpStatus.BAD_REQUEST); } if (!auth) { throw new HttpException('Authorization header required', HttpStatus.UNAUTHORIZED); } this.logger.log(`[RESOLVE] Resolving caller: ${phone}`); const result = await this.resolution.resolve(phone, auth); // Pre-warm caller context cache so the AI chat has it ready if (result.leadId) { this.callerContext.prewarm(result.leadId, result.patientId, auth); } return result; } @Post('invalidate-context') async invalidateContext(@Body('leadId') leadId: string) { if (!leadId) { throw new HttpException('leadId is required', HttpStatus.BAD_REQUEST); } await this.callerContext.invalidateCache(leadId); return { status: 'ok' }; } }