set your twilio things from settings
This commit is contained in:
51
app/settings/mutations/set-phone-number.ts
Normal file
51
app/settings/mutations/set-phone-number.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { resolver } from "blitz";
|
||||
import { z } from "zod";
|
||||
import twilio from "twilio";
|
||||
|
||||
import db from "db";
|
||||
import getCurrentUser from "app/users/queries/get-current-user";
|
||||
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks";
|
||||
|
||||
const Body = z.object({
|
||||
phoneNumberSid: z.string(),
|
||||
});
|
||||
|
||||
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => {
|
||||
const user = await getCurrentUser(null, context);
|
||||
const organization = user?.memberships[0]!.organization;
|
||||
if (!user || !organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
const phoneNumbers = await twilio(
|
||||
organization.twilioAccountSid,
|
||||
organization.twilioAuthToken,
|
||||
).incomingPhoneNumbers.list();
|
||||
const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!;
|
||||
const organizationId = organization.id;
|
||||
await db.phoneNumber.create({
|
||||
data: {
|
||||
organizationId,
|
||||
id: phoneNumberSid,
|
||||
number: phoneNumber.phoneNumber,
|
||||
},
|
||||
});
|
||||
|
||||
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;
|
||||
await Promise.all([
|
||||
setTwilioWebhooks.enqueue(
|
||||
{ organizationId, phoneNumberId },
|
||||
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
|
||||
),
|
||||
]);
|
||||
});
|
42
app/settings/mutations/set-twilio-api-fields.ts
Normal file
42
app/settings/mutations/set-twilio-api-fields.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { resolver } from "blitz";
|
||||
import { z } from "zod";
|
||||
|
||||
import db from "db";
|
||||
import getCurrentUser from "app/users/queries/get-current-user";
|
||||
|
||||
const Body = z.object({
|
||||
twilioAccountSid: z.string(),
|
||||
twilioAuthToken: z.string(),
|
||||
});
|
||||
|
||||
export default resolver.pipe(
|
||||
resolver.zod(Body),
|
||||
resolver.authorize(),
|
||||
async ({ twilioAccountSid, twilioAuthToken }, context) => {
|
||||
const user = await getCurrentUser(null, context);
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
const organizationId = user.memberships[0]!.organizationId;
|
||||
await db.organization.update({
|
||||
where: { id: organizationId },
|
||||
data: {
|
||||
twilioAccountSid: twilioAccountSid,
|
||||
twilioAuthToken: twilioAuthToken,
|
||||
},
|
||||
});
|
||||
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId } });
|
||||
if (phoneNumber) {
|
||||
await db.phoneNumber.delete({
|
||||
where: {
|
||||
organizationId_id: {
|
||||
organizationId,
|
||||
id: phoneNumber.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
Reference in New Issue
Block a user