attach phone numbers to twilio account

This commit is contained in:
m5r
2022-06-11 02:09:37 +02:00
parent c47b57e4bf
commit 3ddd0d73ea
17 changed files with 119 additions and 128 deletions

View File

@ -12,24 +12,14 @@ export default Queue<Payload>("fetch messages", async ({ data }) => {
const { phoneNumberId } = data;
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: {
organization: {
select: { twilioAccount: true },
},
},
include: { twilioAccount: true },
});
if (!phoneNumber) {
logger.warn(`No phone number found with id=${phoneNumberId}`);
return;
}
const twilioAccount = phoneNumber.organization.twilioAccount;
if (!twilioAccount) {
logger.warn(`Phone number with id=${phoneNumberId} doesn't have a connected twilio account`);
return;
}
const twilioClient = getTwilioClient(twilioAccount);
const twilioClient = getTwilioClient(phoneNumber.twilioAccount);
const [sent, received] = await Promise.all([
twilioClient.messages.list({ from: phoneNumber.number }),
twilioClient.messages.list({ to: phoneNumber.number }),

View File

@ -12,24 +12,14 @@ export default Queue<Payload>("fetch phone calls", async ({ data }) => {
const { phoneNumberId } = data;
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: {
organization: {
select: { twilioAccount: true },
},
},
include: { twilioAccount: true },
});
if (!phoneNumber) {
logger.warn(`No phone number found with id=${phoneNumberId}`);
return;
}
const twilioAccount = phoneNumber.organization.twilioAccount;
if (!twilioAccount) {
logger.warn(`Phone number with id=${phoneNumberId} doesn't have a connected twilio account`);
return;
}
const twilioClient = getTwilioClient(twilioAccount);
const twilioClient = getTwilioClient(phoneNumber.twilioAccount);
const [callsSent, callsReceived] = await Promise.all([
twilioClient.calls.list({ from: phoneNumber.number }),
twilioClient.calls.list({ to: phoneNumber.number }),

View File

@ -16,24 +16,14 @@ export default Queue<Payload>("insert incoming message", async ({ data }) => {
logger.info(`received message ${messageSid} for ${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: {
organization: {
select: { twilioAccount: true },
},
},
include: { twilioAccount: true },
});
if (!phoneNumber) {
logger.warn(`No phone number found with id=${phoneNumberId}`);
return;
}
const twilioAccount = phoneNumber.organization.twilioAccount;
if (!twilioAccount) {
logger.warn(`Phone number with id=${phoneNumberId} doesn't have a connected twilio account`);
return;
}
const twilioClient = getTwilioClient(twilioAccount);
const twilioClient = getTwilioClient(phoneNumber.twilioAccount);
const message = await twilioClient.messages.get(messageSid).fetch();
const status = translateMessageStatus(message.status);
const direction = translateMessageDirection(message.direction);

View File

@ -13,10 +13,7 @@ type Payload = {
export default Queue<Payload>("insert messages", async ({ data }) => {
const { messages, phoneNumberId } = data;
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: { organization: true },
});
const phoneNumber = await db.phoneNumber.findUnique({ where: { id: phoneNumberId } });
if (!phoneNumber) {
return;
}

View File

@ -13,10 +13,7 @@ type Payload = {
export default Queue<Payload>("insert phone calls", async ({ data }) => {
const { calls, phoneNumberId } = data;
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: { organization: true },
});
const phoneNumber = await db.phoneNumber.findUnique({ where: { id: phoneNumberId } });
if (!phoneNumber) {
return;
}
@ -39,8 +36,8 @@ export default Queue<Payload>("insert phone calls", async ({ data }) => {
})
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
const ddd = await db.phoneCall.createMany({ data: phoneCalls, skipDuplicates: true });
logger.info(`inserted ${ddd.count || "no"} new phone calls for phoneNumberId=${phoneNumberId}`);
const { count } = await db.phoneCall.createMany({ data: phoneCalls, skipDuplicates: true });
logger.info(`inserted ${count} new phone calls for phoneNumberId=${phoneNumberId}`);
if (!phoneNumber.isFetchingCalls) {
return;

View File

@ -14,22 +14,18 @@ type Payload = {
export default Queue<Payload>("set twilio webhooks", async ({ data }) => {
const { phoneNumberId, organizationId } = data;
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
where: { id: phoneNumberId, twilioAccount: { organizationId } },
include: {
organization: {
select: {
twilioAccount: {
select: { accountSid: true, twimlAppSid: true, authToken: true },
},
},
twilioAccount: {
select: { accountSid: true, twimlAppSid: true, authToken: true },
},
},
});
if (!phoneNumber || !phoneNumber.organization.twilioAccount) {
if (!phoneNumber) {
return;
}
const twilioAccount = phoneNumber.organization.twilioAccount;
const twilioAccount = phoneNumber.twilioAccount;
const authToken = decrypt(twilioAccount.authToken);
const twilioClient = twilio(twilioAccount.accountSid, authToken);
const twimlApp = await getTwimlApplication(twilioClient, twilioAccount.twimlAppSid);