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:17:43 +00:00
|
|
|
import { type Message, Prisma } from "@prisma/client";
|
2022-05-14 10:22:06 +00:00
|
|
|
|
|
|
|
import db from "~/utils/db.server";
|
2022-05-21 19:33:23 +00:00
|
|
|
import { requireLoggedIn, type SessionData } from "~/utils/auth.server";
|
2022-05-14 10:22:06 +00:00
|
|
|
|
|
|
|
export type MessagesLoaderData = {
|
2022-05-19 23:16:38 +00:00
|
|
|
user: { hasPhoneNumber: boolean };
|
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:17:43 +00:00
|
|
|
const { phoneNumber } = await requireLoggedIn(request);
|
2022-05-21 19:33:23 +00:00
|
|
|
return json<MessagesLoaderData>({
|
2022-05-21 23:17:43 +00:00
|
|
|
user: { hasPhoneNumber: Boolean(phoneNumber) },
|
|
|
|
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 19:33:23 +00:00
|
|
|
async function getConversations(sessionPhoneNumber: SessionData["phoneNumber"]) {
|
|
|
|
if (!sessionPhoneNumber) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-21 23:17:43 +00:00
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({ where: { id: sessionPhoneNumber.id } });
|
2022-05-21 19:33:23 +00:00
|
|
|
if (!phoneNumber || phoneNumber.isFetchingMessages) {
|
|
|
|
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
|
|
|
}
|