shellphone.app/app/features/settings/loaders/phone.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-04-29 05:36:22 +00:00
import { type LoaderArgs, json } from "@remix-run/node";
2022-05-22 11:09:42 +00:00
import { type PhoneNumber, Prisma } from "@prisma/client";
import db from "~/utils/db.server";
import logger from "~/utils/logger.server";
import { decrypt } from "~/utils/encryption";
import { getSession } from "~/utils/session.server";
2022-05-22 11:09:42 +00:00
export type PhoneSettingsLoaderData = {
accountSid?: string;
authToken?: string;
2022-05-22 11:09:42 +00:00
phoneNumbers: Pick<PhoneNumber, "id" | "number" | "isCurrent">[];
};
2023-04-29 05:36:22 +00:00
const loader = async ({ request }: LoaderArgs) => {
const session = await getSession(request);
const twilio = session.get("twilio");
if (!twilio) {
2022-05-22 11:09:42 +00:00
logger.warn("Twilio account is not connected");
2023-04-29 05:36:22 +00:00
return json({ phoneNumbers: [] });
2022-05-22 11:09:42 +00:00
}
const phoneNumbers = await db.phoneNumber.findMany({
where: { twilioAccount: { accountSid: twilio.accountSid } },
2022-05-22 11:09:42 +00:00
select: { id: true, number: true, isCurrent: true },
orderBy: { id: Prisma.SortOrder.desc },
});
2023-04-29 05:36:22 +00:00
return json({
accountSid: twilio.accountSid,
authToken: decrypt(twilio.authToken),
phoneNumbers,
});
2022-05-22 11:09:42 +00:00
};
export default loader;