set api key when setting the phone number to avoid race conditions later when fetching calls and messages
This commit is contained in:
parent
6ad516a99e
commit
0b34e861e0
@ -22,15 +22,15 @@ const insertMessagesQueue = Queue<Payload>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sms = messages
|
const sms = messages
|
||||||
.map<Omit<Message, "id">>((message) => ({
|
.map<Message>((message) => ({
|
||||||
organizationId,
|
organizationId,
|
||||||
|
id: message.sid,
|
||||||
phoneNumberId: phoneNumber.id,
|
phoneNumberId: phoneNumber.id,
|
||||||
content: encrypt(message.body, phoneNumber.organization.encryptionKey),
|
content: encrypt(message.body, phoneNumber.organization.encryptionKey),
|
||||||
from: message.from,
|
from: message.from,
|
||||||
to: message.to,
|
to: message.to,
|
||||||
status: translateStatus(message.status),
|
status: translateStatus(message.status),
|
||||||
direction: translateDirection(message.direction),
|
direction: translateDirection(message.direction),
|
||||||
twilioSid: message.sid,
|
|
||||||
sentAt: new Date(message.dateCreated),
|
sentAt: new Date(message.dateCreated),
|
||||||
}))
|
}))
|
||||||
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import { getConfig } from "blitz";
|
import { getConfig } from "blitz";
|
||||||
import { Queue } from "quirrel/blitz";
|
import { Queue } from "quirrel/blitz";
|
||||||
import twilio from "twilio";
|
import type twilio from "twilio";
|
||||||
import type { ApplicationInstance } from "twilio/lib/rest/api/v2010/account/application";
|
import type { ApplicationInstance } from "twilio/lib/rest/api/v2010/account/application";
|
||||||
|
|
||||||
import db from "../../../../db";
|
import db from "../../../../db";
|
||||||
|
import getTwilioClient from "../../../../integrations/twilio";
|
||||||
|
|
||||||
type Payload = {
|
type Payload = {
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
@ -22,27 +23,16 @@ const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async
|
|||||||
}
|
}
|
||||||
|
|
||||||
const organization = phoneNumber.organization;
|
const organization = phoneNumber.organization;
|
||||||
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
|
const twilioClient = getTwilioClient(organization);
|
||||||
return;
|
const twimlApp = await getTwimlApplication(twilioClient, organization.twimlAppSid);
|
||||||
}
|
|
||||||
|
|
||||||
const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
|
|
||||||
const [twimlApp, apiKey] = await Promise.all([
|
|
||||||
getTwimlApplication(mainTwilioClient, organization.twimlAppSid),
|
|
||||||
mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }),
|
|
||||||
]);
|
|
||||||
const twimlAppSid = twimlApp.sid;
|
const twimlAppSid = twimlApp.sid;
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
db.organization.update({
|
db.organization.update({
|
||||||
where: { id: organizationId },
|
where: { id: organizationId },
|
||||||
data: {
|
data: { twimlAppSid },
|
||||||
twimlAppSid,
|
|
||||||
twilioApiKey: apiKey.sid,
|
|
||||||
twilioApiSecret: apiKey.secret,
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
mainTwilioClient.incomingPhoneNumbers.get(phoneNumber.id).update({
|
twilioClient.incomingPhoneNumbers.get(phoneNumber.id).update({
|
||||||
smsApplicationSid: twimlAppSid,
|
smsApplicationSid: twimlAppSid,
|
||||||
voiceApplicationSid: twimlAppSid,
|
voiceApplicationSid: twimlAppSid,
|
||||||
}),
|
}),
|
||||||
|
@ -34,6 +34,16 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({
|
|||||||
});
|
});
|
||||||
context.session.$setPrivateData({ hasCompletedOnboarding: true });
|
context.session.$setPrivateData({ hasCompletedOnboarding: true });
|
||||||
|
|
||||||
|
const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
|
||||||
|
const apiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" });
|
||||||
|
await db.organization.update({
|
||||||
|
where: { id: organizationId },
|
||||||
|
data: {
|
||||||
|
twilioApiKey: apiKey.sid,
|
||||||
|
twilioApiSecret: apiKey.secret,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const phoneNumberId = phoneNumberSid;
|
const phoneNumberId = phoneNumberSid;
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fetchMessagesQueue.enqueue(
|
fetchMessagesQueue.enqueue(
|
||||||
|
Loading…
Reference in New Issue
Block a user