diff --git a/src/ozonetel/ozonetel-agent.controller.ts b/src/ozonetel/ozonetel-agent.controller.ts index 8aabd3b..a9f1432 100644 --- a/src/ozonetel/ozonetel-agent.controller.ts +++ b/src/ozonetel/ozonetel-agent.controller.ts @@ -146,6 +146,37 @@ export class OzonetelAgentController { this.logger.error(`[DISPOSE] FAILED: ${message} ${responseData}`); } + // Create call record for outbound calls. Inbound calls are + // created by the webhook — but we skip outbound in the webhook + // (they're not "missed calls"). So the dispose endpoint is the + // only place that creates the call record for outbound dials. + if (body.direction === 'OUTBOUND' && body.callerPhone) { + try { + const callData: Record = { + name: `Outbound — ${body.callerPhone}`, + direction: 'OUTBOUND', + callStatus: 'COMPLETED', + callerNumber: { primaryPhoneNumber: body.callerPhone }, + agentName: agentId, + durationSec: body.durationSec ?? 0, + disposition: body.disposition, + }; + if (body.leadId) callData.leadId = body.leadId; + + const apiKey = process.env.PLATFORM_API_KEY; + if (apiKey) { + const result = await this.platform.queryWithAuth( + `mutation($data: CallCreateInput!) { createCall(data: $data) { id } }`, + { data: callData }, + `Bearer ${apiKey}`, + ); + this.logger.log(`[DISPOSE] Created outbound call record: ${result.createCall.id}`); + } + } catch (err: any) { + this.logger.warn(`[DISPOSE] Failed to create outbound call record: ${err.message}`); + } + } + // Handle missed call callback status update if (body.missedCallId) { const statusMap: Record = {