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

35 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 { requireLoggedIn } from "~/utils/auth.server";
import logger from "~/utils/logger.server";
import { decrypt } from "~/utils/encryption";
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 { organization, twilio } = await requireLoggedIn(request);
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({
2022-06-11 00:09:37 +00:00
where: { twilioAccount: { organizationId: organization.id } },
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;