2021-07-31 15:57:43 +00:00
|
|
|
import { Queue } from "quirrel/blitz";
|
|
|
|
import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db, { Direction, CallStatus } from "../../../db";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
type Payload = {
|
2021-07-31 15:57:43 +00:00
|
|
|
customerId: string;
|
|
|
|
calls: CallInstance[];
|
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
const insertCallsQueue = Queue<Payload>("api/queue/insert-calls", async ({ calls, customerId }) => {
|
|
|
|
const phoneCalls = calls
|
|
|
|
.map((call) => ({
|
|
|
|
customerId,
|
|
|
|
twilioSid: call.sid,
|
|
|
|
from: call.from,
|
|
|
|
to: call.to,
|
|
|
|
direction: translateDirection(call.direction),
|
|
|
|
status: translateStatus(call.status),
|
|
|
|
duration: call.duration,
|
|
|
|
createdAt: new Date(call.dateCreated),
|
|
|
|
}))
|
2021-07-31 15:57:43 +00:00
|
|
|
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
await db.phoneCall.createMany({ data: phoneCalls });
|
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
export default insertCallsQueue;
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
function translateDirection(direction: CallInstance["direction"]): Direction {
|
|
|
|
switch (direction) {
|
|
|
|
case "inbound":
|
2021-07-31 15:57:43 +00:00
|
|
|
return Direction.Inbound;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "outbound":
|
|
|
|
default:
|
2021-07-31 15:57:43 +00:00
|
|
|
return Direction.Outbound;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateStatus(status: CallInstance["status"]): CallStatus {
|
|
|
|
switch (status) {
|
|
|
|
case "busy":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Busy;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "canceled":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Canceled;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "completed":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Completed;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "failed":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Failed;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "in-progress":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.InProgress;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "no-answer":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.NoAnswer;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "queued":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Queued;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "ringing":
|
2021-07-31 15:57:43 +00:00
|
|
|
return CallStatus.Ringing;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|