shellphone.app/app/settings/mutations/set-twilio-api-fields.ts
2021-10-16 01:26:59 +02:00

43 lines
945 B
TypeScript

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,
},
},
});
}
},
);