purge phone calls and messages from cache when switching phone numbers or twilio account

This commit is contained in:
m5r
2022-06-11 16:13:00 +02:00
parent 1e9b7a8aa2
commit dbe209c7fc
7 changed files with 73 additions and 28 deletions

View File

@ -24,11 +24,16 @@ const loader: LoaderFunction = async ({ request }) => {
where: { phoneNumberId: phoneNumber.id },
orderBy: { createdAt: Prisma.SortOrder.desc },
}));
return json<KeypadLoaderData>({
hasOngoingSubscription,
hasPhoneNumber,
lastRecipientCalled: lastCall?.recipient,
});
return json<KeypadLoaderData>(
{
hasOngoingSubscription,
hasPhoneNumber,
lastRecipientCalled: lastCall?.recipient,
},
{
headers: { Vary: "Cookie" },
},
);
};
export default loader;

View File

@ -23,11 +23,16 @@ const loader: LoaderFunction = async ({ request }) => {
const phoneNumber = await db.phoneNumber.findUnique({
where: { twilioAccountSid_isCurrent: { twilioAccountSid: twilio?.accountSid ?? "", isCurrent: true } },
});
return json<MessagesLoaderData>({
hasPhoneNumber: Boolean(phoneNumber),
isFetchingMessages: phoneNumber?.isFetchingMessages ?? null,
conversations: await getConversations(phoneNumber),
});
return json<MessagesLoaderData>(
{
hasPhoneNumber: Boolean(phoneNumber),
isFetchingMessages: phoneNumber?.isFetchingMessages ?? null,
conversations: await getConversations(phoneNumber),
},
{
headers: { Vary: "Cookie" },
},
);
};
export default loader;

View File

@ -44,15 +44,20 @@ const loader: LoaderFunction = async ({ request }) => {
where: { phoneNumberId: phoneNumber.id },
orderBy: { createdAt: Prisma.SortOrder.desc },
});
return json<PhoneCallsLoaderData>({
hasOngoingSubscription,
hasPhoneNumber,
phoneCalls: phoneCalls.map((phoneCall) => ({
...phoneCall,
fromMeta: getPhoneNumberMeta(phoneCall.from),
toMeta: getPhoneNumberMeta(phoneCall.to),
})),
});
return json<PhoneCallsLoaderData>(
{
hasOngoingSubscription,
hasPhoneNumber,
phoneCalls: phoneCalls.map((phoneCall) => ({
...phoneCall,
fromMeta: getPhoneNumberMeta(phoneCall.from),
toMeta: getPhoneNumberMeta(phoneCall.to),
})),
},
{
headers: { Vary: "Cookie" },
},
);
};
export default loader;

View File

@ -23,7 +23,6 @@ const action: ActionFunction = async ({ request }) => {
return badRequest({ errorMessage });
}
console.log("formData._action", formData._action);
switch (formData._action as Action) {
case "setPhoneNumber":
return setPhoneNumber(request, formData);
@ -128,9 +127,6 @@ async function setTwilioCredentials(request: Request, formData: unknown) {
};
const [phoneNumbers] = await Promise.all([
twilioClient.incomingPhoneNumbers.list(),
setTwilioApiKeyQueue.add(`set twilio api key for accountSid=${twilioAccountSid}`, {
accountSid: twilioAccountSid,
}),
db.twilioAccount.upsert({
where: { organizationId: organization.id },
create: {
@ -143,6 +139,9 @@ async function setTwilioCredentials(request: Request, formData: unknown) {
}),
]);
setTwilioApiKeyQueue.add(`set twilio api key for accountSid=${twilioAccountSid}`, {
accountSid: twilioAccountSid,
});
await Promise.all(
phoneNumbers.map(async (phoneNumber) => {
const phoneNumberId = phoneNumber.sid;