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";
|
2022-06-08 22:33:19 +00:00
|
|
|
import { decrypt } from "~/utils/encryption";
|
2022-05-22 11:09:42 +00:00
|
|
|
|
|
|
|
export type PhoneSettingsLoaderData = {
|
2022-06-08 22:33:19 +00:00
|
|
|
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) => {
|
2022-06-08 22:33:19 +00:00
|
|
|
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({
|
2022-06-08 22:33:19 +00:00
|
|
|
accountSid: twilio.accountSid,
|
|
|
|
authToken: decrypt(twilio.authToken),
|
|
|
|
phoneNumbers,
|
|
|
|
});
|
2022-05-22 11:09:42 +00:00
|
|
|
};
|
|
|
|
export default loader;
|