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";
|
2022-05-21 23:45:13 +00:00
|
|
|
import { requireLoggedIn } from "~/utils/auth.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 }) => {
|
2022-05-21 23:45:13 +00:00
|
|
|
const sessionData = await requireLoggedIn(request);
|
|
|
|
const phoneNumber =
|
|
|
|
sessionData.phoneNumber &&
|
|
|
|
(await db.phoneNumber.findUnique({
|
|
|
|
where: { id: sessionData.phoneNumber.id },
|
|
|
|
}));
|
2022-05-21 19:33:23 +00:00
|
|
|
return json<MessagesLoaderData>({
|
2022-05-21 23:45:13 +00:00
|
|
|
hasPhoneNumber: Boolean(phoneNumber),
|
|
|
|
isFetchingMessages: phoneNumber?.isFetchingMessages ?? null,
|
2022-05-21 23:17:43 +00:00
|
|
|
conversations: await getConversations(phoneNumber),
|
2022-05-14 10:22:06 +00:00
|
|
|
});
|
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
|
|
|
}
|