2022-05-14 23:29:51 +00:00
|
|
|
import { type LoaderFunction, json } from "@remix-run/node";
|
2022-05-17 23:54:06 +00:00
|
|
|
import { type PhoneNumber, Prisma } from "@prisma/client";
|
2022-05-14 23:29:51 +00:00
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
import { requireLoggedIn } from "~/utils/auth.server";
|
|
|
|
import settingsPhoneAction from "~/features/settings/actions/phone";
|
2022-05-14 22:35:51 +00:00
|
|
|
import TwilioConnect from "~/features/settings/components/phone/twilio-connect";
|
2022-05-14 10:22:06 +00:00
|
|
|
import PhoneNumberForm from "~/features/settings/components/phone/phone-number-form";
|
2022-05-17 23:54:06 +00:00
|
|
|
import logger from "~/utils/logger.server";
|
|
|
|
import db from "~/utils/db.server";
|
2022-05-14 23:29:51 +00:00
|
|
|
|
|
|
|
export type PhoneSettingsLoaderData = {
|
2022-05-17 23:54:06 +00:00
|
|
|
phoneNumbers: Pick<PhoneNumber, "id" | "number" | "isCurrent">[];
|
|
|
|
};
|
2022-05-14 23:29:51 +00:00
|
|
|
|
|
|
|
export const loader: LoaderFunction = async ({ request }) => {
|
2022-05-21 19:33:23 +00:00
|
|
|
const { organization, twilioAccount } = await requireLoggedIn(request);
|
|
|
|
if (!twilioAccount) {
|
2022-05-17 23:54:06 +00:00
|
|
|
logger.warn("Twilio account is not connected");
|
|
|
|
return json<PhoneSettingsLoaderData>({ phoneNumbers: [] });
|
2022-05-14 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
const phoneNumbers = await db.phoneNumber.findMany({
|
|
|
|
where: { organizationId: organization.id },
|
|
|
|
select: { id: true, number: true, isCurrent: true },
|
|
|
|
orderBy: { id: Prisma.SortOrder.desc },
|
|
|
|
});
|
2022-05-14 23:29:51 +00:00
|
|
|
|
|
|
|
return json<PhoneSettingsLoaderData>({ phoneNumbers });
|
|
|
|
};
|
2022-05-14 10:22:06 +00:00
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
export const action = settingsPhoneAction;
|
|
|
|
|
2022-05-14 10:22:06 +00:00
|
|
|
function PhoneSettings() {
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col space-y-6">
|
2022-05-14 22:35:51 +00:00
|
|
|
<TwilioConnect />
|
2022-05-14 10:22:06 +00:00
|
|
|
<PhoneNumberForm />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PhoneSettings;
|