shellphone.app/app/features/settings/loaders/phone.ts
2023-04-29 07:36:22 +02:00

35 lines
1.0 KiB
TypeScript

import { type LoaderArgs, json } from "@remix-run/node";
import { type PhoneNumber, Prisma } from "@prisma/client";
import db from "~/utils/db.server";
import { requireLoggedIn } from "~/utils/auth.server";
import logger from "~/utils/logger.server";
import { decrypt } from "~/utils/encryption";
export type PhoneSettingsLoaderData = {
accountSid?: string;
authToken?: string;
phoneNumbers: Pick<PhoneNumber, "id" | "number" | "isCurrent">[];
};
const loader = async ({ request }: LoaderArgs) => {
const { organization, twilio } = await requireLoggedIn(request);
if (!twilio) {
logger.warn("Twilio account is not connected");
return json({ phoneNumbers: [] });
}
const phoneNumbers = await db.phoneNumber.findMany({
where: { twilioAccount: { organizationId: organization.id } },
select: { id: true, number: true, isCurrent: true },
orderBy: { id: Prisma.SortOrder.desc },
});
return json({
accountSid: twilio.accountSid,
authToken: decrypt(twilio.authToken),
phoneNumbers,
});
};
export default loader;