2022-05-22 11:06:43 +00:00
|
|
|
import type { LoaderFunction } from "@remix-run/node";
|
|
|
|
import { json } from "superjson-remix";
|
|
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
|
|
|
|
import { requireLoggedIn } from "~/utils/auth.server";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
|
|
|
|
export type KeypadLoaderData = {
|
|
|
|
hasOngoingSubscription: boolean;
|
|
|
|
hasPhoneNumber: boolean;
|
|
|
|
lastRecipientCalled?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const loader: LoaderFunction = async ({ request }) => {
|
2022-06-11 00:45:07 +00:00
|
|
|
const { twilio } = await requireLoggedIn(request);
|
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { twilioAccountSid_isCurrent: { twilioAccountSid: twilio?.accountSid ?? "", isCurrent: true } },
|
|
|
|
});
|
2022-05-22 11:06:43 +00:00
|
|
|
const hasOngoingSubscription = true; // TODO
|
|
|
|
const hasPhoneNumber = Boolean(phoneNumber);
|
|
|
|
const lastCall =
|
|
|
|
phoneNumber &&
|
|
|
|
(await db.phoneCall.findFirst({
|
|
|
|
where: { phoneNumberId: phoneNumber.id },
|
|
|
|
orderBy: { createdAt: Prisma.SortOrder.desc },
|
|
|
|
}));
|
2022-06-11 14:13:00 +00:00
|
|
|
return json<KeypadLoaderData>(
|
|
|
|
{
|
|
|
|
hasOngoingSubscription,
|
|
|
|
hasPhoneNumber,
|
|
|
|
lastRecipientCalled: lastCall?.recipient,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
headers: { Vary: "Cookie" },
|
|
|
|
},
|
|
|
|
);
|
2022-05-22 11:06:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default loader;
|