2022-05-17 23:54:06 +00:00
|
|
|
import { type LoaderFunction } from "@remix-run/node";
|
|
|
|
import { json, useLoaderData } from "superjson-remix";
|
|
|
|
import { type Message, Prisma, Direction, SubscriptionStatus } from "@prisma/client";
|
2022-05-14 10:22:06 +00:00
|
|
|
import { parsePhoneNumber } from "awesome-phonenumber";
|
|
|
|
|
|
|
|
import PageTitle from "~/features/core/components/page-title";
|
|
|
|
import MissingTwilioCredentials from "~/features/core/components/missing-twilio-credentials";
|
|
|
|
import ConversationsList from "~/features/messages/components/conversations-list";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
import { requireLoggedIn } from "~/utils/auth.server";
|
|
|
|
|
|
|
|
export type MessagesLoaderData = {
|
|
|
|
user: {
|
|
|
|
hasPhoneNumber: boolean;
|
|
|
|
};
|
|
|
|
conversations: Record<string, Conversation> | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Conversation = {
|
|
|
|
recipient: string;
|
|
|
|
formattedPhoneNumber: string;
|
|
|
|
lastMessage: Message;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loader: LoaderFunction = async ({ request }) => {
|
|
|
|
const { id, organizations } = await requireLoggedIn(request);
|
2022-05-17 23:54:06 +00:00
|
|
|
const user = await db.user.findFirst({
|
2022-05-14 10:22:06 +00:00
|
|
|
where: { id },
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
fullName: true,
|
|
|
|
email: true,
|
|
|
|
role: true,
|
|
|
|
memberships: {
|
|
|
|
include: {
|
|
|
|
organization: {
|
|
|
|
include: {
|
|
|
|
subscriptions: {
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{ status: { not: SubscriptionStatus.deleted } },
|
|
|
|
{
|
|
|
|
status: SubscriptionStatus.deleted,
|
|
|
|
cancellationEffectiveDate: { gt: new Date() },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
orderBy: { lastEventTime: Prisma.SortOrder.desc },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-05-17 23:54:06 +00:00
|
|
|
const organization = user!.memberships[0].organization;
|
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { organizationId_isCurrent: { organizationId: organization.id, isCurrent: true } },
|
2022-05-14 10:22:06 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
organizationId: true,
|
|
|
|
number: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const conversations = await getConversations();
|
|
|
|
|
|
|
|
return json<MessagesLoaderData>({
|
2022-05-17 23:54:06 +00:00
|
|
|
user: { hasPhoneNumber: Boolean(phoneNumber) },
|
2022-05-14 10:22:06 +00:00
|
|
|
conversations,
|
|
|
|
});
|
|
|
|
|
|
|
|
async function getConversations() {
|
|
|
|
const organizationId = organizations[0].id;
|
2022-05-17 23:54:06 +00:00
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { organizationId_isCurrent: { organizationId, isCurrent: true } },
|
2022-05-14 10:22:06 +00:00
|
|
|
});
|
2022-05-17 23:54:06 +00:00
|
|
|
if (!phoneNumber || phoneNumber.isFetchingMessages) {
|
2022-05-14 10:22:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = await db.message.findMany({
|
2022-05-17 23:54:06 +00:00
|
|
|
where: { phoneNumberId: phoneNumber.id },
|
2022-05-14 10:22:06 +00:00
|
|
|
orderBy: { sentAt: Prisma.SortOrder.desc },
|
|
|
|
});
|
|
|
|
|
|
|
|
let conversations: Record<string, Conversation> = {};
|
|
|
|
for (const message of messages) {
|
|
|
|
let recipient: string;
|
|
|
|
if (message.direction === Direction.Outbound) {
|
|
|
|
recipient = message.to;
|
|
|
|
} else {
|
|
|
|
recipient = message.from;
|
|
|
|
}
|
|
|
|
const formattedPhoneNumber = parsePhoneNumber(recipient).getNumber("international");
|
|
|
|
|
|
|
|
if (!conversations[recipient]) {
|
|
|
|
conversations[recipient] = {
|
|
|
|
recipient,
|
|
|
|
formattedPhoneNumber,
|
|
|
|
lastMessage: message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
if (message.sentAt > conversations[recipient].lastMessage.sentAt) {
|
2022-05-14 10:22:06 +00:00
|
|
|
conversations[recipient].lastMessage = message;
|
|
|
|
}
|
|
|
|
/*conversations[recipient]!.messages.push({
|
|
|
|
...message,
|
|
|
|
content: decrypt(message.content, organization.encryptionKey),
|
|
|
|
});*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return conversations;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function MessagesPage() {
|
|
|
|
const { user } = useLoaderData<MessagesLoaderData>();
|
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
if (!user.hasPhoneNumber) {
|
2022-05-14 10:22:06 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<MissingTwilioCredentials />
|
|
|
|
<PageTitle className="filter blur-sm select-none absolute top-0" title="Messages" />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageTitle title="Messages" />
|
|
|
|
<section className="flex flex-grow flex-col">
|
|
|
|
{/* TODO: skeleton conversations list */}
|
|
|
|
<ConversationsList />
|
|
|
|
</section>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|