2022-05-14 22:35:51 +00:00
|
|
|
import { type LoaderFunction, redirect } from "@remix-run/node";
|
2022-05-17 23:54:06 +00:00
|
|
|
import twilio from "twilio";
|
|
|
|
|
2022-05-14 22:35:51 +00:00
|
|
|
import { refreshSessionData, requireLoggedIn } from "~/utils/auth.server";
|
|
|
|
import { commitSession } from "~/utils/session.server";
|
2022-05-17 23:54:06 +00:00
|
|
|
import db from "~/utils/db.server";
|
2022-05-14 23:29:51 +00:00
|
|
|
import serverConfig from "~/config/config.server";
|
2022-05-17 23:54:06 +00:00
|
|
|
import getTwilioClient from "~/utils/twilio.server";
|
|
|
|
import fetchPhoneCallsQueue from "~/queues/fetch-phone-calls.server";
|
|
|
|
import fetchMessagesQueue from "~/queues/fetch-messages.server";
|
2022-05-21 19:33:23 +00:00
|
|
|
import { encrypt } from "~/utils/encryption";
|
2022-05-14 22:35:51 +00:00
|
|
|
|
|
|
|
export const loader: LoaderFunction = async ({ request }) => {
|
2022-05-21 19:33:23 +00:00
|
|
|
const { organization } = await requireLoggedIn(request);
|
2022-05-14 22:35:51 +00:00
|
|
|
const url = new URL(request.url);
|
2022-05-14 23:29:51 +00:00
|
|
|
const twilioSubAccountSid = url.searchParams.get("AccountSid");
|
|
|
|
if (!twilioSubAccountSid) {
|
2022-05-14 22:35:51 +00:00
|
|
|
throw new Error("unreachable");
|
|
|
|
}
|
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
let twilioClient = twilio(twilioSubAccountSid, serverConfig.twilio.authToken);
|
2022-05-14 23:29:51 +00:00
|
|
|
const twilioSubAccount = await twilioClient.api.accounts(twilioSubAccountSid).fetch();
|
2022-05-21 19:33:23 +00:00
|
|
|
const twilioMainAccountSid = twilioSubAccount.ownerAccountSid;
|
|
|
|
const twilioMainAccount = await twilioClient.api.accounts(twilioMainAccountSid).fetch();
|
|
|
|
console.log("twilioSubAccount", twilioSubAccount);
|
|
|
|
console.log("twilioAccount", twilioMainAccount);
|
|
|
|
const twilioAccount = await db.twilioAccount.update({
|
|
|
|
where: { organizationId: organization.id },
|
|
|
|
data: {
|
|
|
|
subAccountSid: twilioSubAccount.sid,
|
|
|
|
subAccountAuthToken: encrypt(twilioSubAccount.authToken),
|
|
|
|
accountSid: twilioMainAccount.sid,
|
|
|
|
accountAuthToken: encrypt(twilioMainAccount.authToken),
|
|
|
|
},
|
2022-05-14 22:35:51 +00:00
|
|
|
});
|
|
|
|
|
2022-05-21 19:33:23 +00:00
|
|
|
twilioClient = getTwilioClient(twilioAccount);
|
2022-05-17 23:54:06 +00:00
|
|
|
const phoneNumbers = await twilioClient.incomingPhoneNumbers.list();
|
|
|
|
await Promise.all(
|
|
|
|
phoneNumbers.map(async (phoneNumber) => {
|
|
|
|
const phoneNumberId = phoneNumber.sid;
|
|
|
|
try {
|
|
|
|
await db.phoneNumber.create({
|
|
|
|
data: {
|
|
|
|
id: phoneNumberId,
|
|
|
|
organizationId: organization.id,
|
|
|
|
number: phoneNumber.phoneNumber,
|
|
|
|
isCurrent: false,
|
|
|
|
isFetchingCalls: true,
|
|
|
|
isFetchingMessages: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
fetchPhoneCallsQueue.add(`fetch calls of id=${phoneNumberId}`, {
|
|
|
|
phoneNumberId,
|
|
|
|
}),
|
|
|
|
fetchMessagesQueue.add(`fetch messages of id=${phoneNumberId}`, {
|
|
|
|
phoneNumberId,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
} catch (error: any) {
|
|
|
|
if (error.code !== "P2002") {
|
|
|
|
// if it's not a duplicate, it's a real error we need to handle
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-05-14 22:35:51 +00:00
|
|
|
const { session } = await refreshSessionData(request);
|
|
|
|
return redirect("/settings/phone", {
|
|
|
|
headers: {
|
|
|
|
"Set-Cookie": await commitSession(session),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|