2022-05-14 10:22:06 +00:00
|
|
|
import type { LoaderFunction } from "@remix-run/node";
|
2022-05-19 23:16:38 +00:00
|
|
|
import { json } from "superjson-remix";
|
|
|
|
import { parsePhoneNumber } from "awesome-phonenumber";
|
2022-05-21 23:45:13 +00:00
|
|
|
import { type Message, type PhoneNumber, Prisma } from "@prisma/client";
|
2022-05-14 10:22:06 +00:00
|
|
|
|
|
|
|
import db from "~/utils/db.server";
|
2023-04-29 16:30:07 +00:00
|
|
|
import { getSession } from "~/utils/session.server";
|
2022-05-14 10:22:06 +00:00
|
|
|
|
|
|
|
export type MessagesLoaderData = {
|
2022-05-21 23:45:13 +00:00
|
|
|
hasPhoneNumber: boolean;
|
|
|
|
isFetchingMessages: boolean | null;
|
2022-05-21 23:17:43 +00:00
|
|
|
conversations: Conversations | undefined;
|
2022-05-14 10:22:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Conversation = {
|
|
|
|
recipient: string;
|
|
|
|
formattedPhoneNumber: string;
|
2022-05-19 23:16:38 +00:00
|
|
|
lastMessage: Message;
|
2022-05-14 10:22:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const loader: LoaderFunction = async ({ request }) => {
|
2023-04-29 16:30:07 +00:00
|
|
|
const session = await getSession(request);
|
|
|
|
const twilio = session.get("twilio");
|
2022-06-11 00:45:07 +00:00
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { twilioAccountSid_isCurrent: { twilioAccountSid: twilio?.accountSid ?? "", isCurrent: true } },
|
|
|
|
});
|
2022-06-11 14:13:00 +00:00
|
|
|
return json<MessagesLoaderData>(
|
|
|
|
{
|
|
|
|
hasPhoneNumber: Boolean(phoneNumber),
|
|
|
|
isFetchingMessages: phoneNumber?.isFetchingMessages ?? null,
|
|
|
|
conversations: await getConversations(phoneNumber),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
headers: { Vary: "Cookie" },
|
|
|
|
},
|
|
|
|
);
|
2022-05-21 19:33:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default loader;
|
|
|
|
|
2022-05-21 23:17:43 +00:00
|
|
|
type Conversations = Record<string, Conversation>;
|
|
|
|
|
2022-05-21 23:45:13 +00:00
|
|
|
async function getConversations(phoneNumber: PhoneNumber | null) {
|
|
|
|
if (!phoneNumber) {
|
2022-05-21 19:33:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-19 23:16:38 +00:00
|
|
|
|
2022-05-21 19:33:23 +00:00
|
|
|
const messages = await db.message.findMany({
|
|
|
|
where: { phoneNumberId: phoneNumber.id },
|
|
|
|
orderBy: { sentAt: Prisma.SortOrder.desc },
|
2022-05-21 23:17:43 +00:00
|
|
|
distinct: "recipient",
|
2022-05-19 23:16:38 +00:00
|
|
|
});
|
|
|
|
|
2022-05-21 23:17:43 +00:00
|
|
|
return messages.reduce<Conversations>((conversations, message) => {
|
|
|
|
const recipient = message.recipient;
|
2022-05-21 19:33:23 +00:00
|
|
|
const formattedPhoneNumber = parsePhoneNumber(recipient).getNumber("international");
|
2022-05-19 23:16:38 +00:00
|
|
|
|
2022-05-21 23:17:43 +00:00
|
|
|
conversations[recipient] = {
|
|
|
|
recipient,
|
|
|
|
formattedPhoneNumber,
|
|
|
|
lastMessage: message,
|
|
|
|
};
|
2022-05-21 19:33:23 +00:00
|
|
|
/*conversations[recipient]!.messages.push({
|
|
|
|
...message,
|
|
|
|
content: decrypt(message.content, organization.encryptionKey),
|
|
|
|
});*/
|
2022-05-21 23:17:43 +00:00
|
|
|
return conversations;
|
|
|
|
}, {});
|
2022-05-21 19:33:23 +00:00
|
|
|
}
|