shellphone.app/app/settings/mutations/set-twilio-api-fields.ts

43 lines
945 B
TypeScript
Raw Normal View History

import { resolver } from "blitz";
import { z } from "zod";
2021-07-31 14:33:18 +00:00
2021-10-15 23:25:13 +00:00
import db from "db";
import getCurrentUser from "app/users/queries/get-current-user";
2021-07-31 14:33:18 +00:00
const Body = z.object({
twilioAccountSid: z.string(),
twilioAuthToken: z.string(),
});
2021-07-31 14:33:18 +00:00
export default resolver.pipe(
resolver.zod(Body),
resolver.authorize(),
async ({ twilioAccountSid, twilioAuthToken }, context) => {
2021-08-05 17:07:15 +00:00
const user = await getCurrentUser(null, context);
if (!user) {
return;
}
2021-08-08 03:00:00 +00:00
const organizationId = user.memberships[0]!.organizationId;
2021-08-05 17:07:15 +00:00
await db.organization.update({
where: { id: organizationId },
2021-07-31 14:33:18 +00:00
data: {
2021-08-05 17:07:15 +00:00
twilioAccountSid: twilioAccountSid,
twilioAuthToken: twilioAuthToken,
2021-07-31 14:33:18 +00:00
},
});
2021-10-15 23:25:13 +00:00
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId } });
if (phoneNumber) {
await db.phoneNumber.delete({
where: {
organizationId_id: {
organizationId,
id: phoneNumber.id,
},
},
});
}
2021-08-01 12:04:04 +00:00
},
);