shellphone.app/app/features/messages/loaders/messages.ts
2022-05-22 01:45:13 +02:00

66 lines
1.8 KiB
TypeScript

import type { LoaderFunction } from "@remix-run/node";
import { json } from "superjson-remix";
import { parsePhoneNumber } from "awesome-phonenumber";
import { type Message, type PhoneNumber, Prisma } from "@prisma/client";
import db from "~/utils/db.server";
import { requireLoggedIn } from "~/utils/auth.server";
export type MessagesLoaderData = {
hasPhoneNumber: boolean;
isFetchingMessages: boolean | null;
conversations: Conversations | undefined;
};
type Conversation = {
recipient: string;
formattedPhoneNumber: string;
lastMessage: Message;
};
const loader: LoaderFunction = async ({ request }) => {
const sessionData = await requireLoggedIn(request);
const phoneNumber =
sessionData.phoneNumber &&
(await db.phoneNumber.findUnique({
where: { id: sessionData.phoneNumber.id },
}));
return json<MessagesLoaderData>({
hasPhoneNumber: Boolean(phoneNumber),
isFetchingMessages: phoneNumber?.isFetchingMessages ?? null,
conversations: await getConversations(phoneNumber),
});
};
export default loader;
type Conversations = Record<string, Conversation>;
async function getConversations(phoneNumber: PhoneNumber | null) {
if (!phoneNumber) {
return;
}
const messages = await db.message.findMany({
where: { phoneNumberId: phoneNumber.id },
orderBy: { sentAt: Prisma.SortOrder.desc },
distinct: "recipient",
});
return messages.reduce<Conversations>((conversations, message) => {
const recipient = message.recipient;
const formattedPhoneNumber = parsePhoneNumber(recipient).getNumber("international");
conversations[recipient] = {
recipient,
formattedPhoneNumber,
lastMessage: message,
};
/*conversations[recipient]!.messages.push({
...message,
content: decrypt(message.content, organization.encryptionKey),
});*/
return conversations;
}, {});
}