2021-08-02 13:43:27 +00:00
|
|
|
import { resolver, NotFoundError } from "blitz";
|
2021-08-05 17:07:15 +00:00
|
|
|
import { z } from "zod";
|
2021-08-26 20:12:11 +00:00
|
|
|
import PhoneNumber from "awesome-phonenumber";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db, { Direction, Message, Prisma } from "../../../db";
|
|
|
|
import { decrypt } from "../../../db/_encryption";
|
2021-08-05 17:07:15 +00:00
|
|
|
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../../core/utils";
|
|
|
|
|
2021-08-28 21:07:51 +00:00
|
|
|
type Conversation = {
|
|
|
|
recipient: string;
|
|
|
|
formattedPhoneNumber: string;
|
|
|
|
messages: Message[];
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
export default resolver.pipe(
|
|
|
|
resolver.zod(z.object({ organizationId: z.string().optional() })),
|
|
|
|
resolver.authorize(),
|
|
|
|
setDefaultOrganizationId,
|
|
|
|
enforceSuperAdminIfNotCurrentOrganization,
|
|
|
|
async ({ organizationId }) => {
|
|
|
|
const organization = await db.organization.findFirst({
|
|
|
|
where: { id: organizationId },
|
|
|
|
include: { phoneNumbers: true },
|
|
|
|
});
|
|
|
|
if (!organization) {
|
|
|
|
throw new NotFoundError();
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const phoneNumberId = organization.phoneNumbers[0]!.id;
|
|
|
|
const messages = await db.message.findMany({
|
|
|
|
where: { organizationId, phoneNumberId },
|
2021-08-13 15:54:13 +00:00
|
|
|
orderBy: { sentAt: Prisma.SortOrder.desc },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-28 21:07:51 +00:00
|
|
|
let conversations: Record<string, Conversation> = {};
|
2021-08-05 17:07:15 +00:00
|
|
|
for (const message of messages) {
|
|
|
|
let recipient: string;
|
|
|
|
if (message.direction === Direction.Outbound) {
|
|
|
|
recipient = message.to;
|
|
|
|
} else {
|
|
|
|
recipient = message.from;
|
|
|
|
}
|
2021-08-28 21:07:51 +00:00
|
|
|
const formattedPhoneNumber = new PhoneNumber(recipient).getNumber("international");
|
2021-08-05 17:07:15 +00:00
|
|
|
|
|
|
|
if (!conversations[recipient]) {
|
2021-08-28 21:07:51 +00:00
|
|
|
conversations[recipient] = {
|
|
|
|
recipient,
|
|
|
|
formattedPhoneNumber,
|
|
|
|
messages: [],
|
|
|
|
};
|
2021-08-05 17:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:07:51 +00:00
|
|
|
conversations[recipient]!.messages.push({
|
2021-08-05 17:07:15 +00:00
|
|
|
...message,
|
|
|
|
content: decrypt(message.content, organization.encryptionKey),
|
|
|
|
});
|
|
|
|
|
2021-08-28 21:07:51 +00:00
|
|
|
conversations[recipient]!.messages.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
2021-08-05 17:07:15 +00:00
|
|
|
}
|
|
|
|
conversations = Object.fromEntries(
|
|
|
|
Object.entries(conversations).sort(
|
2021-08-28 21:07:51 +00:00
|
|
|
([, a], [, b]) =>
|
|
|
|
b.messages[b.messages.length - 1]!.sentAt.getTime() -
|
|
|
|
a.messages[a.messages.length - 1]!.sentAt.getTime(),
|
2021-08-05 17:07:15 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return conversations;
|
|
|
|
},
|
|
|
|
);
|