multi tenancy stuff

This commit is contained in:
m5r 2021-08-06 01:07:15 +08:00
parent b54f9ef43c
commit d20eeb0617
51 changed files with 907 additions and 2542 deletions

View File

@ -1,6 +1,6 @@
import { resolver, SecurePassword, AuthenticationError } from "blitz";
import db, { Role } from "../../../db";
import db, { GlobalRole } from "../../../db";
import { Login } from "../validations";
export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
@ -25,7 +25,13 @@ export default resolver.pipe(resolver.zod(Login), async ({ email, password }, ct
// This throws an error if credentials are invalid
const user = await authenticateUser(email, password);
await ctx.session.$create({ userId: user.id, role: user.role as Role });
const hasCompletedOnboarding = undefined; // TODO
await ctx.session.$create({
userId: user.id,
roles: [user.role],
hasCompletedOnboarding,
orgId: "user.memberships[0].organizationId",
});
return user;
});

View File

@ -1,18 +1,35 @@
import { resolver, SecurePassword } from "blitz";
import db, { Role } from "../../../db";
import db, { GlobalRole, MembershipRole } from "../../../db";
import { Signup } from "../validations";
import { computeEncryptionKey } from "../../../db/_encryption";
export default resolver.pipe(resolver.zod(Signup), async ({ email, password }, ctx) => {
const hashedPassword = await SecurePassword.hash(password.trim());
const encryptionKey = computeEncryptionKey(email.toLowerCase().trim()).toString("hex");
const user = await db.user.create({
data: { email: email.toLowerCase().trim(), hashedPassword, role: Role.USER },
select: { id: true, name: true, email: true, role: true },
data: {
email: email.toLowerCase().trim(),
hashedPassword,
role: GlobalRole.CUSTOMER,
memberships: {
create: {
role: MembershipRole.OWNER,
organization: {
create: {
encryptionKey,
},
},
},
},
},
include: { memberships: true },
});
const encryptionKey = computeEncryptionKey(user.id).toString("hex");
await db.customer.create({ data: { id: user.id, encryptionKey } });
await ctx.session.$create({ userId: user.id, role: user.role });
await ctx.session.$create({
userId: user.id,
roles: [user.role, user.memberships[0]!.role],
orgId: user.memberships[0]!.organizationId,
});
return user;
});

View File

@ -1,13 +0,0 @@
import { useSession, useQuery } from "blitz";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
export default function useCurrentCustomer() {
const session = useSession();
const [customer] = useQuery(getCurrentCustomer, null, { enabled: Boolean(session.userId) });
return {
customer,
hasFilledTwilioCredentials: Boolean(customer && customer.accountSid && customer.authToken),
hasCompletedOnboarding: session.hasCompletedOnboarding,
};
}

View File

@ -0,0 +1,11 @@
import { useQuery } from "blitz";
import getCurrentPhoneNumber from "../../phone-numbers/queries/get-current-phone-number";
import useCurrentUser from "./use-current-user";
export default function useUserPhoneNumber() {
const { hasFilledTwilioCredentials } = useCurrentUser();
const [phoneNumber] = useQuery(getCurrentPhoneNumber, {}, { enabled: hasFilledTwilioCredentials });
return phoneNumber;
}

View File

@ -0,0 +1,15 @@
import { useSession, useQuery } from "blitz";
import getCurrentUser from "../../users/queries/get-current-user";
export default function useCurrentUser() {
const session = useSession();
const [user] = useQuery(getCurrentUser, null, { enabled: Boolean(session.userId) });
const organization = user?.memberships[0]!.organization;
return {
user,
organization,
hasFilledTwilioCredentials: Boolean(user && organization?.twilioAccountSid && organization?.twilioAuthToken),
hasCompletedOnboarding: session.hasCompletedOnboarding,
};
}

View File

@ -1,12 +0,0 @@
import { useQuery } from "blitz";
import getCurrentCustomerPhoneNumber from "../../phone-numbers/queries/get-current-customer-phone-number";
import useCurrentCustomer from "./use-current-customer";
export default function useCustomerPhoneNumber() {
const { customer } = useCurrentCustomer();
const hasFilledTwilioCredentials = Boolean(customer && customer.accountSid && customer.authToken);
const [customerPhoneNumber] = useQuery(getCurrentCustomerPhoneNumber, {}, { enabled: hasFilledTwilioCredentials });
return customerPhoneNumber;
}

View File

@ -2,6 +2,7 @@ import { getConfig, useMutation } from "blitz";
import { useEffect, useState } from "react";
import setNotificationSubscription from "../mutations/set-notification-subscription";
import useCurrentPhoneNumber from "./use-current-phone-number";
const { publicRuntimeConfig } = getConfig();
@ -9,6 +10,7 @@ export default function useNotifications() {
const isServiceWorkerSupported = "serviceWorker" in navigator;
const [subscription, setSubscription] = useState<PushSubscription | null>(null);
const [setNotificationSubscriptionMutation] = useMutation(setNotificationSubscription);
const phoneNumber = useCurrentPhoneNumber();
useEffect(() => {
(async () => {
@ -23,7 +25,7 @@ export default function useNotifications() {
}, [isServiceWorkerSupported]);
async function subscribe() {
if (!isServiceWorkerSupported) {
if (!isServiceWorkerSupported || !phoneNumber) {
return;
}
@ -33,7 +35,10 @@ export default function useNotifications() {
applicationServerKey: urlBase64ToUint8Array(publicRuntimeConfig.webPush.publicKey),
});
setSubscription(subscription);
await setNotificationSubscriptionMutation({ subscription: subscription.toJSON() as any }); // TODO remove as any
await setNotificationSubscriptionMutation({
phoneNumberId: phoneNumber.id,
subscription: subscription.toJSON() as any,
}); // TODO remove as any
}
async function unsubscribe() {

View File

@ -1,12 +1,12 @@
import { Routes, useRouter } from "blitz";
import useCurrentCustomer from "./use-current-customer";
import useCustomerPhoneNumber from "./use-customer-phone-number";
import useCurrentUser from "./use-current-user";
import useCurrentPhoneNumber from "./use-current-phone-number";
export default function useRequireOnboarding() {
const router = useRouter();
const { hasFilledTwilioCredentials, hasCompletedOnboarding } = useCurrentCustomer();
const customerPhoneNumber = useCustomerPhoneNumber();
const { hasFilledTwilioCredentials, hasCompletedOnboarding } = useCurrentUser();
const phoneNumber = useCurrentPhoneNumber();
if (hasCompletedOnboarding) {
return;
@ -16,12 +16,12 @@ export default function useRequireOnboarding() {
throw router.push(Routes.StepTwo());
}
/*if (!customer.paddleCustomerId || !customer.paddleSubscriptionId) {
/*if (!user.paddleCustomerId || !user.paddleSubscriptionId) {
throw router.push(Routes.StepTwo());
return;
}*/
if (!customerPhoneNumber) {
if (!phoneNumber) {
throw router.push(Routes.StepThree());
}
}

View File

@ -77,6 +77,8 @@ const ErrorBoundary = withRouter(
// let Blitz ErrorBoundary handle this one
throw error;
}
// if network error and connection lost, display the auto-reload page with countdown
}
public render() {

View File

@ -3,10 +3,13 @@ import { z } from "zod";
import db from "../../../db";
import appLogger from "../../../integrations/logger";
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../utils";
const logger = appLogger.child({ mutation: "set-notification-subscription" });
const Body = z.object({
organizationId: z.string().optional(),
phoneNumberId: z.string(),
subscription: z.object({
endpoint: z.string(),
expirationTime: z.number().nullable(),
@ -17,12 +20,25 @@ const Body = z.object({
}),
});
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ subscription }, context) => {
const customerId = context.session.userId;
export default resolver.pipe(
resolver.zod(Body),
resolver.authorize(),
setDefaultOrganizationId,
enforceSuperAdminIfNotCurrentOrganization,
async ({ organizationId, phoneNumberId, subscription }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
try {
await db.notificationSubscription.create({
data: {
customerId,
organizationId,
phoneNumberId,
endpoint: subscription.endpoint,
expirationTime: subscription.expirationTime,
keys_p256dh: subscription.keys.p256dh,
@ -35,4 +51,5 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({
// we might want to `throw error`;
}
}
});
},
);

35
app/core/utils.ts Normal file
View File

@ -0,0 +1,35 @@
import type { Ctx } from "blitz";
import type { Prisma } from "../../db";
import { GlobalRole } from "../../db";
function assert(condition: any, message: string): asserts condition {
if (!condition) throw new Error(message);
}
export function setDefaultOrganizationId<T extends Record<any, any>>(
input: T,
{ session }: Ctx,
): T & { organizationId: Prisma.StringNullableFilter | string } {
assert(session.orgId, "Missing session.orgId in setDefaultOrganizationId");
if (input.organizationId) {
// Pass through the input
return input as T & { organizationId: string };
} else if (session.roles?.includes(GlobalRole.SUPERADMIN)) {
// Allow viewing any organization
return { ...input, organizationId: { not: "" } };
} else {
// Set organizationId to session.orgId
return { ...input, organizationId: session.orgId };
}
}
export function enforceSuperAdminIfNotCurrentOrganization<T extends Record<any, any>>(input: T, ctx: Ctx): T {
assert(ctx.session.orgId, "missing session.orgId");
assert(input.organizationId, "missing input.organizationId");
if (input.organizationId !== ctx.session.orgId) {
ctx.session.$authorize(GlobalRole.SUPERADMIN);
}
return input;
}

View File

@ -1,21 +0,0 @@
import { resolver } from "blitz";
import db from "../../../db";
export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) => {
if (!session.userId) return null;
return db.customer.findFirst({
where: { id: session.userId },
select: {
id: true,
encryptionKey: true,
accountSid: true,
authToken: true,
twimlAppSid: true,
paddleCustomerId: true,
paddleSubscriptionId: true,
user: true,
},
});
});

View File

@ -5,21 +5,27 @@ import db from "../../../../db";
import insertMessagesQueue from "./insert-messages";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
};
const fetchMessagesQueue = Queue<Payload>("api/queue/fetch-messages", async ({ customerId }) => {
const [customer, phoneNumber] = await Promise.all([
db.customer.findFirst({ where: { id: customerId } }),
db.phoneNumber.findFirst({ where: { customerId } }),
]);
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
const fetchMessagesQueue = Queue<Payload>("api/queue/fetch-messages", async ({ organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
const organization = phoneNumber.organization;
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const [sent, received] = await Promise.all([
twilio(customer.accountSid, customer.authToken).messages.list({ from: phoneNumber.phoneNumber }),
twilio(customer.accountSid, customer.authToken).messages.list({ to: phoneNumber.phoneNumber }),
twilio(organization.twilioAccountSid, organization.twilioAuthToken).messages.list({ from: phoneNumber.number }),
twilio(organization.twilioAccountSid, organization.twilioAuthToken).messages.list({ to: phoneNumber.number }),
]);
const messagesSent = sent.filter((message) => message.direction.startsWith("outbound"));
const messagesReceived = received.filter((message) => message.direction === "inbound");
@ -29,11 +35,12 @@ const fetchMessagesQueue = Queue<Payload>("api/queue/fetch-messages", async ({ c
await insertMessagesQueue.enqueue(
{
customerId,
organizationId,
phoneNumberId,
messages,
},
{
id: `insert-messages-${customerId}`,
id: `insert-messages-${organizationId}-${phoneNumberId}`,
},
);
});

View File

@ -4,46 +4,49 @@ import twilio from "twilio";
import db, { Direction, MessageStatus } from "../../../../db";
import { encrypt } from "../../../../db/_encryption";
import notifyIncomingMessageQueue from "./notify-incoming-message";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
messageSid: MessageInstance["sid"];
};
const insertIncomingMessageQueue = Queue<Payload>(
"api/queue/insert-incoming-message",
async ({ messageSid, customerId }) => {
const customer = await db.customer.findFirst({ where: { id: customerId } });
if (!customer || !customer.accountSid || !customer.authToken) {
async ({ messageSid, organizationId, phoneNumberId }) => {
const organization = await db.organization.findFirst({
where: { id: organizationId },
});
if (!organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const encryptionKey = customer.encryptionKey;
const message = await twilio(customer.accountSid, customer.authToken).messages.get(messageSid).fetch();
const message = await twilio(organization.twilioAccountSid, organization.twilioAuthToken)
.messages.get(messageSid)
.fetch();
await db.message.create({
data: {
customerId,
organizationId,
phoneNumberId,
id: messageSid,
to: message.to,
from: message.from,
status: translateStatus(message.status),
direction: translateDirection(message.direction),
sentAt: message.dateCreated,
content: encrypt(message.body, customer.encryptionKey),
content: encrypt(message.body, organization.encryptionKey),
},
});
await db.message.createMany({
data: {
customerId,
content: encrypt(message.body, encryptionKey),
from: message.from,
to: message.to,
status: translateStatus(message.status),
direction: translateDirection(message.direction),
twilioSid: message.sid,
sentAt: new Date(message.dateCreated),
await notifyIncomingMessageQueue.enqueue(
{
messageSid,
organizationId,
phoneNumberId,
},
});
{ id: `notify-${messageSid}-${organizationId}-${phoneNumberId}` },
);
},
);

View File

@ -5,20 +5,27 @@ import db, { Direction, Message, MessageStatus } from "../../../../db";
import { encrypt } from "../../../../db/_encryption";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
messages: MessageInstance[];
};
const insertMessagesQueue = Queue<Payload>("api/queue/insert-messages", async ({ messages, customerId }) => {
const customer = await db.customer.findFirst({ where: { id: customerId } });
if (!customer) {
const insertMessagesQueue = Queue<Payload>(
"api/queue/insert-messages",
async ({ messages, organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
const sms = messages
.map<Omit<Message, "id">>((message) => ({
customerId,
content: encrypt(message.body, customer.encryptionKey),
organizationId,
phoneNumberId: phoneNumber.id,
content: encrypt(message.body, phoneNumber.organization.encryptionKey),
from: message.from,
to: message.to,
status: translateStatus(message.status),
@ -29,7 +36,8 @@ const insertMessagesQueue = Queue<Payload>("api/queue/insert-messages", async ({
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
await db.message.createMany({ data: sms });
});
},
);
export default insertMessagesQueue;

View File

@ -11,27 +11,32 @@ const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();
const logger = appLogger.child({ queue: "notify-incoming-message" });
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
messageSid: MessageInstance["sid"];
};
const notifyIncomingMessageQueue = Queue<Payload>(
"api/queue/notify-incoming-message",
async ({ messageSid, customerId }) => {
async ({ messageSid, organizationId, phoneNumberId }) => {
webpush.setVapidDetails(
"mailto:mokht@rmi.al",
publicRuntimeConfig.webPush.publicKey,
serverRuntimeConfig.webPush.privateKey,
);
const customer = await db.customer.findFirst({ where: { id: customerId } });
if (!customer || !customer.accountSid || !customer.authToken) {
const organization = await db.organization.findFirst({
where: { id: organizationId },
});
if (!organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const message = await twilio(customer.accountSid, customer.authToken).messages.get(messageSid).fetch();
const message = await twilio(organization.twilioAccountSid, organization.twilioAuthToken)
.messages.get(messageSid)
.fetch();
const notification = { message: `${message.from} - ${message.body}` };
const subscriptions = await db.notificationSubscription.findMany({ where: { customerId: customer.id } });
const subscriptions = await db.notificationSubscription.findMany({ where: { organizationId, phoneNumberId } });
await Promise.all(
subscriptions.map(async (subscription) => {
const webPushSubscription: PushSubscription = {

View File

@ -1,40 +1,46 @@
import { Queue } from "quirrel/blitz";
import twilio from "twilio";
import db from "../../../../db";
import db, { MessageStatus } from "../../../../db";
type Payload = {
id: string;
customerId: string;
organizationId: string;
phoneNumberId: string;
to: string;
content: string;
};
const sendMessageQueue = Queue<Payload>(
"api/queue/send-message",
async ({ id, customerId, to, content }) => {
const [customer, phoneNumber] = await Promise.all([
db.customer.findFirst({ where: { id: customerId } }),
db.phoneNumber.findFirst({ where: { customerId } }),
]);
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
async ({ id, organizationId, phoneNumberId, to, content }) => {
const organization = await db.organization.findFirst({
where: { id: organizationId },
include: { phoneNumbers: true },
});
const phoneNumber = organization?.phoneNumbers.find((phoneNumber) => phoneNumber.id === phoneNumberId);
if (!organization || !organization.twilioAccountSid || !organization.twilioAuthToken || !phoneNumber) {
return;
}
try {
const message = await twilio(customer.accountSid, customer.authToken).messages.create({
const message = await twilio(organization.twilioAccountSid, organization.twilioAuthToken).messages.create({
body: content,
to,
from: phoneNumber.phoneNumber,
from: phoneNumber.number,
});
await db.message.update({
where: { id },
data: { twilioSid: message.sid },
where: { organizationId_phoneNumberId_id: { id, organizationId, phoneNumberId } },
data: { id: message.sid },
});
} catch (error) {
// TODO: handle twilio error
console.log(error.code); // 21211
console.log(error.moreInfo); // https://www.twilio.com/docs/errors/21211
await db.message.update({
where: { id },
data: { status: MessageStatus.Error /*errorMessage: "Reason: failed because of"*/ },
});
}
},
{

View File

@ -3,31 +3,26 @@ import twilio from "twilio";
import db from "db";
import handler from "./incoming-message";
import notifyIncomingMessageQueue from "../queue/notify-incoming-message";
import insertIncomingMessageQueue from "../queue/insert-incoming-message";
describe("/api/webhook/incoming-message", () => {
const mockedFindFirstPhoneNumber = db.phoneNumber.findFirst as jest.Mock<
ReturnType<typeof db.phoneNumber.findFirst>
>;
const mockedFindFirstCustomer = db.customer.findFirst as jest.Mock<ReturnType<typeof db.customer.findFirst>>;
const mockedEnqueueNotifyIncomingMessage = notifyIncomingMessageQueue.enqueue as jest.Mock<
ReturnType<typeof notifyIncomingMessageQueue.enqueue>
>;
const mockedFindManyPhoneNumbers = db.phoneNumber.findMany as jest.Mock<ReturnType<typeof db.phoneNumber.findMany>>;
const mockedEnqueueInsertIncomingMessage = insertIncomingMessageQueue.enqueue as jest.Mock<
ReturnType<typeof insertIncomingMessageQueue.enqueue>
>;
const mockedValidateRequest = twilio.validateRequest as jest.Mock<ReturnType<typeof twilio.validateRequest>>;
beforeEach(() => {
mockedFindFirstPhoneNumber.mockResolvedValue({ phoneNumber: "+33757592025" } as any);
mockedFindFirstCustomer.mockResolvedValue({ id: "9292", authToken: "twi" } as any);
mockedFindManyPhoneNumbers.mockResolvedValue([
{
id: "9292",
organization: { id: "2929", twilioAuthToken: "twi" },
} as any,
]);
});
afterEach(() => {
mockedFindFirstPhoneNumber.mockReset();
mockedFindFirstCustomer.mockReset();
mockedEnqueueNotifyIncomingMessage.mockReset();
mockedFindManyPhoneNumbers.mockReset();
mockedEnqueueInsertIncomingMessage.mockReset();
mockedValidateRequest.mockReset();
});
@ -50,16 +45,15 @@ describe("/api/webhook/incoming-message", () => {
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("text/html");
[mockedEnqueueNotifyIncomingMessage, mockedEnqueueNotifyIncomingMessage].forEach((enqueue) => {
expect(enqueue).toHaveBeenCalledTimes(1);
expect(enqueue).toHaveBeenCalledWith(
expect(mockedEnqueueInsertIncomingMessage).toHaveBeenCalledTimes(1);
expect(mockedEnqueueInsertIncomingMessage).toHaveBeenCalledWith(
{
messageSid: "SM157246f02006b80953e8c753fb68fad6",
customerId: "9292",
phoneNumberId: "9292",
organizationId: "2929",
},
{ id: "notify-SM157246f02006b80953e8c753fb68fad6" },
{ id: "insert-SM157246f02006b80953e8c753fb68fad6-2929-9292" },
);
});
},
});
});
@ -107,11 +101,7 @@ describe("/api/webhook/incoming-message", () => {
});
jest.mock("db", () => ({
phoneNumber: { findFirst: jest.fn() },
customer: { findFirst: jest.fn() },
}));
jest.mock("../queue/notify-incoming-message", () => ({
enqueue: jest.fn(),
phoneNumber: { findMany: jest.fn() },
}));
jest.mock("../queue/insert-incoming-message", () => ({
enqueue: jest.fn(),

View File

@ -40,26 +40,25 @@ export default async function incomingMessageHandler(req: BlitzApiRequest, res:
const body: Body = req.body;
try {
const customerPhoneNumber = await db.phoneNumber.findFirst({
where: { phoneNumber: body.To },
const phoneNumbers = await db.phoneNumber.findMany({
where: { number: body.To },
include: { organization: true },
});
if (!customerPhoneNumber) {
// phone number is not registered by any of our customer
res.status(500).end();
return;
}
const customer = await db.customer.findFirst({
where: { id: customerPhoneNumber.customerId },
});
if (!customer || !customer.authToken) {
if (phoneNumbers.length === 0) {
// phone number is not registered by any organization
res.status(500).end();
return;
}
const url = `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-message`;
const isRequestValid = twilio.validateRequest(customer.authToken, twilioSignature, url, req.body);
if (!isRequestValid) {
const phoneNumber = phoneNumbers.find((phoneNumber) => {
// if multiple organizations have the same number
// find the organization currently using that phone number
// maybe we shouldn't let multiple organizations use the same phone number
const authToken = phoneNumber.organization.twilioAuthToken ?? "";
return twilio.validateRequest(authToken, twilioSignature, url, req.body);
});
if (!phoneNumber) {
const statusCode = 400;
const apiError: ApiError = {
statusCode,
@ -72,23 +71,16 @@ export default async function incomingMessageHandler(req: BlitzApiRequest, res:
}
const messageSid = body.MessageSid;
const customerId = customer.id;
await Promise.all([
notifyIncomingMessageQueue.enqueue(
const organizationId = phoneNumber.organization.id;
const phoneNumberId = phoneNumber.id;
await insertIncomingMessageQueue.enqueue(
{
messageSid,
customerId,
organizationId,
phoneNumberId,
},
{ id: `notify-${messageSid}` },
),
insertIncomingMessageQueue.enqueue(
{
messageSid,
customerId,
},
{ id: `insert-${messageSid}` },
),
]);
{ id: `insert-${messageSid}-${organizationId}-${phoneNumberId}` },
);
res.setHeader("content-type", "text/html");
res.status(200).send("<Response></Response>");

View File

@ -1,14 +1,14 @@
import type { FunctionComponent } from "react";
import { useMutation, useQuery } from "blitz";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPaperPlane } from "@fortawesome/pro-regular-svg-icons";
import { useForm } from "react-hook-form";
import { useMutation, useQuery } from "blitz";
import sendMessage from "../mutations/send-message";
import { Direction, Message, MessageStatus } from "../../../db";
import getConversationsQuery from "../queries/get-conversations";
import useCurrentCustomer from "../../core/hooks/use-current-customer";
import useCustomerPhoneNumber from "../../core/hooks/use-customer-phone-number";
import { FunctionComponent } from "react";
import useCurrentUser from "../../core/hooks/use-current-user";
import useCurrentPhoneNumber from "../../core/hooks/use-current-phone-number";
type Form = {
content: string;
@ -20,8 +20,8 @@ type Props = {
};
const NewMessageArea: FunctionComponent<Props> = ({ recipient, onSend }) => {
const { customer } = useCurrentCustomer();
const phoneNumber = useCustomerPhoneNumber();
const { organization } = useCurrentUser();
const phoneNumber = useCurrentPhoneNumber();
const sendMessageMutation = useMutation(sendMessage)[0];
const { setQueryData: setConversationsQueryData, refetch: refetchConversations } = useQuery(
getConversationsQuery,
@ -45,9 +45,9 @@ const NewMessageArea: FunctionComponent<Props> = ({ recipient, onSend }) => {
const id = uuidv4();
const message: Message = {
id,
customerId: customer!.id,
twilioSid: id,
from: phoneNumber!.phoneNumber,
organizationId: organization!.id,
phoneNumberId: phoneNumber!.id,
from: phoneNumber!.number,
to: recipient,
content: content,
direction: Direction.Outbound,
@ -63,7 +63,12 @@ const NewMessageArea: FunctionComponent<Props> = ({ recipient, onSend }) => {
}
nextConversations[recipient] = [...nextConversations[recipient]!, message];
return nextConversations;
return Object.fromEntries(
Object.entries(nextConversations).sort(
([, a], [, b]) => b[b.length - 1]!.sentAt.getTime() - a[a.length - 1]!.sentAt.getTime(),
),
);
},
{ refetch: false },
);

View File

@ -1,10 +1,8 @@
import { resolver } from "blitz";
import { NotFoundError, resolver } from "blitz";
import { z } from "zod";
import twilio from "twilio";
import db, { Direction, MessageStatus } from "../../../db";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
import getCustomerPhoneNumber from "../../phone-numbers/queries/get-customer-phone-number";
import { encrypt } from "../../../db/_encryption";
import sendMessageQueue from "../../messages/api/queue/send-message";
import appLogger from "../../../integrations/logger";
@ -17,32 +15,40 @@ const Body = z.object({
});
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ content, to }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer || !customer.accountSid || !customer.authToken) {
const organizationId = context.session.orgId;
const organization = await db.organization.findFirst({
where: { id: organizationId },
include: { phoneNumbers: true },
});
if (!organization) {
throw new NotFoundError();
}
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
try {
await twilio(customer.accountSid, customer.authToken).lookups.v1.phoneNumbers(to).fetch();
await twilio(organization.twilioAccountSid, organization.twilioAuthToken).lookups.v1.phoneNumbers(to).fetch();
} catch (error) {
logger.error(error);
return;
}
const customerId = customer.id;
const customerPhoneNumber = await getCustomerPhoneNumber({ customerId }, context);
if (!customerPhoneNumber) {
const phoneNumber = organization.phoneNumbers[0];
if (!phoneNumber) {
return;
}
const phoneNumberId = phoneNumber.id;
const message = await db.message.create({
data: {
customerId,
organizationId,
phoneNumberId,
to,
from: customerPhoneNumber.phoneNumber,
from: phoneNumber.number,
direction: Direction.Outbound,
status: MessageStatus.Queued,
content: encrypt(content, customer.encryptionKey),
content: encrypt(content, organization.encryptionKey),
sentAt: new Date(),
},
});
@ -50,12 +56,13 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({
await sendMessageQueue.enqueue(
{
id: message.id,
customerId,
organizationId,
phoneNumberId,
to,
content,
},
{
id: `insert-${message.id}`,
id: `insert-${message.id}-${organizationId}-${phoneNumberId}`,
},
);
});

View File

@ -3,15 +3,14 @@ import { z } from "zod";
import db, { Prisma } from "../../../db";
import { decrypt } from "../../../db/_encryption";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
const GetConversations = z.object({
recipient: z.string(),
});
export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(), async ({ recipient }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
const organization = await db.organization.findFirst({ where: { id: context.session.orgId } });
if (!organization) {
throw new NotFoundError();
}
@ -23,7 +22,7 @@ export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(
return conversation.map((message) => {
return {
...message,
content: decrypt(message.content, customer.encryptionKey),
content: decrypt(message.content, organization.encryptionKey),
};
});
});

View File

@ -1,17 +1,27 @@
import { resolver, NotFoundError } from "blitz";
import { z } from "zod";
import db, { Direction, Message, Prisma } from "../../../db";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
import { decrypt } from "../../../db/_encryption";
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../../core/utils";
export default resolver.pipe(resolver.authorize(), async (_ = null, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
export default resolver.pipe(
resolver.zod(z.object({ organizationId: z.string().optional() })),
resolver.authorize(),
setDefaultOrganizationId,
enforceSuperAdminIfNotCurrentOrganization,
async ({ organizationId }) => {
const organization = await db.organization.findFirst({
where: { id: organizationId },
include: { phoneNumbers: true },
});
if (!organization) {
throw new NotFoundError();
}
const phoneNumberId = organization.phoneNumbers[0]!.id;
const messages = await db.message.findMany({
where: { customerId: customer.id },
where: { organizationId, phoneNumberId },
orderBy: { sentAt: Prisma.SortOrder.asc },
});
@ -30,7 +40,7 @@ export default resolver.pipe(resolver.authorize(), async (_ = null, context) =>
conversations[recipient]!.push({
...message,
content: decrypt(message.content, customer.encryptionKey),
content: decrypt(message.content, organization.encryptionKey),
});
conversations[recipient]!.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
@ -42,4 +52,5 @@ export default resolver.pipe(resolver.authorize(), async (_ = null, context) =>
);
return conversations;
});
},
);

View File

@ -5,23 +5,31 @@ import twilio from "twilio";
import db from "../../../../db";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
};
const { serverRuntimeConfig } = getConfig();
const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async ({ customerId }) => {
const [customer, phoneNumber] = await Promise.all([
db.customer.findFirst({ where: { id: customerId } }),
db.phoneNumber.findFirst({ where: { customerId } }),
]);
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async ({ organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
const twimlApp = customer.twimlAppSid
? await twilio(customer.accountSid, customer.authToken).applications.get(customer.twimlAppSid).fetch()
: await twilio(customer.accountSid, customer.authToken).applications.create({
const organization = phoneNumber.organization;
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const twimlApp = organization.twimlAppSid
? await twilio(organization.twilioAccountSid, organization.twilioAuthToken)
.applications.get(organization.twimlAppSid)
.fetch()
: await twilio(organization.twilioAccountSid, organization.twilioAuthToken).applications.create({
friendlyName: "Shellphone",
smsUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-message`,
smsMethod: "POST",
@ -31,11 +39,13 @@ const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async
const twimlAppSid = twimlApp.sid;
await Promise.all([
db.customer.update({
where: { id: customerId },
db.organization.update({
where: { id: organizationId },
data: { twimlAppSid },
}),
twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.get(phoneNumber.phoneNumberSid).update({
twilio(organization.twilioAccountSid, organization.twilioAuthToken)
.incomingPhoneNumbers.get(phoneNumber.id)
.update({
smsApplicationSid: twimlAppSid,
voiceApplicationSid: twimlAppSid,
}),

View File

@ -3,7 +3,7 @@ import { z } from "zod";
import twilio from "twilio";
import db from "../../../db";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
import getCurrentUser from "../../users/queries/get-current-user";
import fetchMessagesQueue from "../../messages/api/queue/fetch-messages";
import fetchCallsQueue from "../../phone-calls/api/queue/fetch-calls";
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks";
@ -13,26 +13,40 @@ const Body = z.object({
});
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer || !customer.accountSid || !customer.authToken) {
const user = await getCurrentUser(null, context);
const organization = user?.memberships[0]!.organization;
if (!user || !organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const customerId = customer.id;
const phoneNumbers = await twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.list();
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: {
customerId,
phoneNumberSid,
phoneNumber: phoneNumber.phoneNumber,
organizationId,
id: phoneNumberSid,
number: phoneNumber.phoneNumber,
},
});
context.session.$setPrivateData({ hasCompletedOnboarding: true });
const phoneNumberId = phoneNumberSid;
await Promise.all([
fetchMessagesQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }),
fetchCallsQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }),
setTwilioWebhooks.enqueue({ customerId }, { id: `set-twilio-webhooks-${customerId}` }),
fetchMessagesQueue.enqueue(
{ organizationId, phoneNumberId },
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
),
fetchCallsQueue.enqueue(
{ organizationId, phoneNumberId },
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
),
setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
),
]);
});

View File

@ -2,7 +2,7 @@ import { resolver } from "blitz";
import { z } from "zod";
import db from "../../../db";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
import getCurrentUser from "../../users/queries/get-current-user";
const Body = z.object({
twilioAccountSid: z.string(),
@ -13,16 +13,17 @@ export default resolver.pipe(
resolver.zod(Body),
resolver.authorize(),
async ({ twilioAccountSid, twilioAuthToken }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
const user = await getCurrentUser(null, context);
if (!user) {
return;
}
await db.customer.update({
where: { id: customer.id },
const organizationId = user.memberships[0]!.id;
await db.organization.update({
where: { id: organizationId },
data: {
accountSid: twilioAccountSid,
authToken: twilioAuthToken,
twilioAccountSid: twilioAccountSid,
twilioAuthToken: twilioAuthToken,
},
});
},

View File

@ -2,11 +2,11 @@ import type { BlitzPage, GetServerSideProps } from "blitz";
import { getSession, Routes } from "blitz";
import OnboardingLayout from "../../components/onboarding-layout";
import useCurrentCustomer from "../../../core/hooks/use-current-customer";
import useCurrentUser from "../../../core/hooks/use-current-user";
import db from "../../../../db";
const StepOne: BlitzPage = () => {
useCurrentCustomer(); // preload for step two
useCurrentUser(); // preload for step two
return (
<div className="flex flex-col space-y-4 items-center">
@ -35,7 +35,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
};
}
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
if (phoneNumber) {
await session.$setPublicData({ hasCompletedOnboarding: true });
return {

View File

@ -100,7 +100,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
};
}
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
if (phoneNumber) {
await session.$setPublicData({ hasCompletedOnboarding: true });
return {
@ -111,8 +111,8 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
};
}
const customer = await db.customer.findFirst({ where: { id: session.userId } });
if (!customer) {
const organization = await db.organization.findFirst({ where: { id: session.orgId } });
if (!organization) {
return {
redirect: {
destination: Routes.StepOne().pathname,
@ -121,7 +121,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
};
}
if (!customer.accountSid || !customer.authToken) {
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
return {
redirect: {
destination: Routes.StepTwo().pathname,
@ -130,7 +130,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
};
}
const incomingPhoneNumbers = await twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.list();
const incomingPhoneNumbers = await twilio(
organization.twilioAccountSid,
organization.twilioAuthToken,
).incomingPhoneNumbers.list();
const phoneNumbers = incomingPhoneNumbers.map(({ phoneNumber, sid }) => ({ phoneNumber, sid }));
return {

View File

@ -11,7 +11,7 @@ import db from "db";
import setTwilioApiFields from "../../mutations/set-twilio-api-fields";
import OnboardingLayout from "../../components/onboarding-layout";
import HelpModal from "../../components/help-modal";
import useCurrentCustomer from "../../../core/hooks/use-current-customer";
import useCurrentUser from "../../../core/hooks/use-current-user";
type Form = {
twilioAccountSid: string;
@ -26,14 +26,14 @@ const StepTwo: BlitzPage = () => {
formState: { isSubmitting },
} = useForm<Form>();
const router = useRouter();
const { customer } = useCurrentCustomer();
const { organization } = useCurrentUser();
const [setTwilioApiFieldsMutation] = useMutation(setTwilioApiFields);
const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
useEffect(() => {
setValue("twilioAuthToken", customer?.authToken ?? "");
setValue("twilioAccountSid", customer?.accountSid ?? "");
}, [setValue, customer?.authToken, customer?.accountSid]);
setValue("twilioAuthToken", organization?.twilioAuthToken ?? "");
setValue("twilioAccountSid", organization?.twilioAccountSid ?? "");
}, [setValue, organization?.twilioAuthToken, organization?.twilioAccountSid]);
const onSubmit = handleSubmit(async ({ twilioAccountSid, twilioAuthToken }) => {
if (isSubmitting) {
@ -105,9 +105,9 @@ StepTwo.getLayout = (page) => {
};
const StepTwoLayout: FunctionComponent = ({ children }) => {
const { customer } = useCurrentCustomer();
const initialAuthToken = customer?.authToken ?? "";
const initialAccountSid = customer?.accountSid ?? "";
const { organization } = useCurrentUser();
const initialAuthToken = organization?.twilioAuthToken ?? "";
const initialAccountSid = organization?.twilioAccountSid ?? "";
const hasTwilioCredentials = initialAccountSid.length > 0 && initialAuthToken.length > 0;
return (
@ -135,7 +135,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
};
}
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
if (phoneNumber) {
await session.$setPublicData({ hasCompletedOnboarding: true });
return {

View File

@ -1,9 +1,10 @@
import { GlobalRole } from "db";
import { render } from "../../test/utils";
import Home from "./index";
import useCurrentCustomer from "../core/hooks/use-current-customer";
import useCurrentUser from "../core/hooks/use-current-user";
jest.mock("../core/hooks/use-current-customer");
const mockUseCurrentCustomer = useCurrentCustomer as jest.MockedFunction<typeof useCurrentCustomer>;
jest.mock("../core/hooks/use-current-user");
const mockUseCurrentUser = useCurrentUser as jest.MockedFunction<typeof useCurrentUser>;
test.skip("renders blitz documentation link", () => {
// This is an example of how to ensure a specific item is in the document
@ -11,16 +12,14 @@ test.skip("renders blitz documentation link", () => {
// when you remove the the default content from the page
// This is an example on how to mock api hooks when testing
mockUseCurrentCustomer.mockReturnValue({
customer: {
mockUseCurrentUser.mockReturnValue({
organization: undefined,
user: {
id: uuidv4(),
encryptionKey: "",
accountSid: null,
authToken: null,
twimlAppSid: null,
paddleCustomerId: null,
paddleSubscriptionId: null,
user: {} as any,
name: "name",
email: "email@test.com",
role: GlobalRole.CUSTOMER,
memberships: [],
},
hasFilledTwilioCredentials: false,
hasCompletedOnboarding: undefined,

View File

@ -4,7 +4,7 @@ import { Link, useMutation, Routes } from "blitz";
import BaseLayout from "../core/layouts/base-layout";
import logout from "../auth/mutations/logout";
import useCurrentCustomer from "../core/hooks/use-current-customer";
import useCurrentUser from "../core/hooks/use-current-user";
/*
* This file is just for a pleasant getting started page for your new app.
@ -12,10 +12,10 @@ import useCurrentCustomer from "../core/hooks/use-current-customer";
*/
const UserInfo = () => {
const { customer } = useCurrentCustomer();
const { user } = useCurrentUser();
const [logoutMutation] = useMutation(logout);
if (customer) {
if (user) {
return (
<>
<button
@ -27,9 +27,9 @@ const UserInfo = () => {
Logout
</button>
<div>
User id: <code>{customer.id}</code>
User id: <code>{user.id}</code>
<br />
User role: <code>{customer.encryptionKey}</code>
User role: <code>{user.role}</code>
</div>
</>
);

View File

@ -5,35 +5,42 @@ import db from "../../../../db";
import insertCallsQueue from "./insert-calls";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
};
const fetchCallsQueue = Queue<Payload>("api/queue/fetch-calls", async ({ customerId }) => {
const [customer, phoneNumber] = await Promise.all([
db.customer.findFirst({ where: { id: customerId } }),
db.phoneNumber.findFirst({ where: { customerId } }),
]);
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
const fetchCallsQueue = Queue<Payload>("api/queue/fetch-calls", async ({ organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
const organization = phoneNumber.organization;
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const [callsSent, callsReceived] = await Promise.all([
twilio(customer.accountSid, customer.authToken).calls.list({
from: phoneNumber.phoneNumber,
twilio(organization.twilioAccountSid, organization.twilioAuthToken).calls.list({
from: phoneNumber.number,
}),
twilio(customer.accountSid, customer.authToken).calls.list({
to: phoneNumber.phoneNumber,
twilio(organization.twilioAccountSid, organization.twilioAuthToken).calls.list({
to: phoneNumber.number,
}),
]);
const calls = [...callsSent, ...callsReceived].sort((a, b) => a.dateCreated.getTime() - b.dateCreated.getTime());
await insertCallsQueue.enqueue(
{
customerId,
organizationId,
phoneNumberId,
calls,
},
{
id: `insert-calls-${customerId}`,
id: `insert-calls-${organizationId}-${phoneNumberId}`,
},
);
});

View File

@ -4,15 +4,25 @@ import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
import db, { Direction, CallStatus } from "../../../../db";
type Payload = {
customerId: string;
organizationId: string;
phoneNumberId: string;
calls: CallInstance[];
};
const insertCallsQueue = Queue<Payload>("api/queue/insert-calls", async ({ calls, customerId }) => {
const insertCallsQueue = Queue<Payload>("api/queue/insert-calls", async ({ calls, organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
const phoneCalls = calls
.map((call) => ({
customerId,
twilioSid: call.sid,
organizationId,
phoneNumberId,
id: call.sid,
from: call.from,
to: call.to,
direction: translateDirection(call.direction),

View File

@ -2,7 +2,7 @@ import { Direction } from "../../../db";
import usePhoneCalls from "../hooks/use-phone-calls";
export default function PhoneCallsList() {
const phoneCalls = usePhoneCalls();
const phoneCalls = usePhoneCalls()[0];
if (phoneCalls.length === 0) {
return <div>empty state</div>;
@ -13,7 +13,7 @@ export default function PhoneCallsList() {
{phoneCalls.map((phoneCall) => {
const recipient = Direction.Outbound ? phoneCall.to : phoneCall.from;
return (
<li key={phoneCall.twilioSid} className="flex flex-row justify-between py-2">
<li key={phoneCall.id} className="flex flex-row justify-between py-2">
<div>{recipient}</div>
<div>{new Date(phoneCall.createdAt).toLocaleString("fr-FR")}</div>
</li>

View File

@ -1,15 +1,13 @@
import { NotFoundError, useQuery } from "blitz";
import useCurrentCustomer from "../../core/hooks/use-current-customer";
import useCurrentPhoneNumber from "../..//core/hooks/use-current-phone-number";
import getPhoneCalls from "../queries/get-phone-calls";
export default function usePhoneCalls() {
const { customer } = useCurrentCustomer();
if (!customer) {
const phoneNumber = useCurrentPhoneNumber();
if (!phoneNumber) {
throw new NotFoundError();
}
const { phoneCalls } = useQuery(getPhoneCalls, { customerId: customer.id })[0];
return phoneCalls;
return useQuery(getPhoneCalls, { phoneNumberId: phoneNumber.id });
}

View File

@ -1,31 +1,16 @@
import { paginate, resolver } from "blitz";
import db, { Prisma, Customer } from "db";
import { resolver } from "blitz";
import { z } from "zod";
import db, { Prisma } from "db";
interface GetPhoneCallsInput extends Pick<Prisma.PhoneCallFindManyArgs, "where" | "orderBy" | "skip" | "take"> {
customerId: Customer["id"];
}
export default resolver.pipe(
resolver.authorize(),
async ({ where, orderBy, skip = 0, take = 100 }: GetPhoneCallsInput) => {
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const {
items: phoneCalls,
hasMore,
nextPage,
count,
} = await paginate({
skip,
take,
count: () => db.phoneCall.count({ where }),
query: (paginateArgs) => db.phoneCall.findMany({ ...paginateArgs, where, orderBy }),
const Body = z.object({
phoneNumberId: z.string(),
});
return {
phoneCalls,
nextPage,
hasMore,
count,
};
},
);
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberId }, context) => {
const organizationId = context.session.orgId;
return db.phoneCall.findMany({
where: { organizationId, phoneNumberId },
orderBy: { createdAt: Prisma.SortOrder.asc },
});
});

View File

@ -1,20 +0,0 @@
import { NotFoundError, resolver } from "blitz";
import db from "db";
import getCurrentCustomer from "../../customers/queries/get-current-customer";
export default resolver.pipe(resolver.authorize(), async (_ = null, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
throw new NotFoundError();
}
return db.phoneNumber.findFirst({
where: { customerId: customer.id },
select: {
id: true,
phoneNumber: true,
phoneNumberSid: true,
},
});
});

View File

@ -0,0 +1,21 @@
import { resolver } from "blitz";
import { z } from "zod";
import db from "db";
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../../core/utils";
export default resolver.pipe(
resolver.zod(z.object({ organizationId: z.string().optional() })),
resolver.authorize(),
setDefaultOrganizationId,
enforceSuperAdminIfNotCurrentOrganization,
async ({ organizationId }) => {
return db.phoneNumber.findFirst({
where: { organizationId },
select: {
id: true,
number: true,
},
});
},
);

View File

@ -1,19 +0,0 @@
import { resolver } from "blitz";
import db from "db";
import { z } from "zod";
const GetCustomerPhoneNumber = z.object({
// This accepts type of undefined, but is required at runtime
customerId: z.string().optional().refine(Boolean, "Required"),
});
export default resolver.pipe(resolver.zod(GetCustomerPhoneNumber), async ({ customerId }) =>
db.phoneNumber.findFirst({
where: { customerId },
select: {
id: true,
phoneNumber: true,
phoneNumberSid: true,
},
}),
);

View File

@ -4,10 +4,8 @@ import clsx from "clsx";
import Button from "./button";
import SettingsSection from "./settings-section";
import Modal, { ModalTitle } from "./modal";
import useCurrentCustomer from "../../core/hooks/use-current-customer";
export default function DangerZone() {
const customer = useCurrentCustomer();
const [isDeletingUser, setIsDeletingUser] = useState(false);
const [isConfirmationModalOpen, setIsConfirmationModalOpen] = useState(false);
const modalCancelButtonRef = useRef<HTMLButtonElement>(null);

View File

@ -6,7 +6,7 @@ import { useForm } from "react-hook-form";
import Alert from "./alert";
import Button from "./button";
import SettingsSection from "./settings-section";
import useCurrentCustomer from "../../core/hooks/use-current-customer";
import useCurrentUser from "../../core/hooks/use-current-user";
import appLogger from "../../../integrations/logger";
@ -18,7 +18,7 @@ type Form = {
const logger = appLogger.child({ module: "profile-settings" });
const ProfileInformations: FunctionComponent = () => {
const { customer } = useCurrentCustomer();
const { user } = useCurrentUser();
const router = useRouter();
const {
register,
@ -29,9 +29,9 @@ const ProfileInformations: FunctionComponent = () => {
const [errorMessage, setErrorMessage] = useState("");
useEffect(() => {
setValue("name", customer?.user.name ?? "");
setValue("email", customer?.user.email ?? "");
}, [setValue, customer]);
setValue("name", user?.name ?? "");
setValue("email", user?.email ?? "");
}, [setValue, user]);
const onSubmit = handleSubmit(async ({ name, email }) => {
if (isSubmitting) {
@ -40,7 +40,7 @@ const ProfileInformations: FunctionComponent = () => {
try {
// TODO
// await customer.updateUser({ email, data: { name } });
// await updateUser({ email, data: { name } });
} catch (error) {
logger.error(error.response, "error updating user infos");

View File

@ -6,7 +6,6 @@ import { useForm } from "react-hook-form";
import Alert from "./alert";
import Button from "./button";
import SettingsSection from "./settings-section";
import useCurrentCustomer from "../../core/hooks/use-current-customer";
import appLogger from "../../../integrations/logger";
@ -18,7 +17,6 @@ type Form = {
};
const UpdatePassword: FunctionComponent = () => {
const customer = useCurrentCustomer();
const router = useRouter();
const {
register,

View File

@ -0,0 +1,32 @@
import { Ctx } from "blitz";
import db from "db";
export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null;
return db.user.findFirst({
where: { id: session.userId },
select: {
id: true,
name: true,
email: true,
role: true,
memberships: {
include: {
organization: {
select: {
id: true,
encryptionKey: true,
paddleCustomerId: true,
paddleSubscriptionId: true,
twilioAccountSid: true,
twilioAuthToken: true,
twimlAppSid: true,
},
},
},
},
},
});
}

View File

@ -0,0 +1,148 @@
/*
Warnings:
- You are about to drop the column `customerId` on the `Message` table. All the data in the column will be lost.
- You are about to drop the column `twilioSid` on the `Message` table. All the data in the column will be lost.
- You are about to drop the column `customerId` on the `NotificationSubscription` table. All the data in the column will be lost.
- You are about to drop the column `customerId` on the `PhoneCall` table. All the data in the column will be lost.
- You are about to drop the column `twilioSid` on the `PhoneCall` table. All the data in the column will be lost.
- You are about to drop the column `customerId` on the `PhoneNumber` table. All the data in the column will be lost.
- You are about to drop the column `phoneNumber` on the `PhoneNumber` table. All the data in the column will be lost.
- You are about to drop the column `phoneNumberSid` on the `PhoneNumber` table. All the data in the column will be lost.
- The `role` column on the `User` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- You are about to drop the `Customer` table. If the table is not empty, all the data it contains will be lost.
- A unique constraint covering the columns `[phoneNumberId,id]` on the table `Message` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[phoneNumberId,id]` on the table `PhoneCall` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[organizationId,id]` on the table `PhoneNumber` will be added. If there are existing duplicate values, this will fail.
- Added the required column `phoneNumberId` to the `Message` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `NotificationSubscription` table without a default value. This is not possible if the table is not empty.
- Added the required column `phoneNumberId` to the `NotificationSubscription` table without a default value. This is not possible if the table is not empty.
- Added the required column `phoneNumberId` to the `PhoneCall` table without a default value. This is not possible if the table is not empty.
- Added the required column `number` to the `PhoneNumber` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `PhoneNumber` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "MembershipRole" AS ENUM ('OWNER', 'ADMIN', 'USER');
-- CreateEnum
CREATE TYPE "GlobalRole" AS ENUM ('SUPERADMIN', 'CUSTOMER');
-- AlterEnum
ALTER TYPE "MessageStatus" ADD VALUE 'Error';
-- DropForeignKey
ALTER TABLE "Customer" DROP CONSTRAINT "Customer_id_fkey";
-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_customerId_fkey";
-- DropForeignKey
ALTER TABLE "NotificationSubscription" DROP CONSTRAINT "NotificationSubscription_customerId_fkey";
-- DropForeignKey
ALTER TABLE "PhoneCall" DROP CONSTRAINT "PhoneCall_customerId_fkey";
-- DropForeignKey
ALTER TABLE "PhoneNumber" DROP CONSTRAINT "PhoneNumber_customerId_fkey";
-- AlterTable
ALTER TABLE "Message" DROP COLUMN "customerId",
DROP COLUMN "twilioSid",
ADD COLUMN "phoneNumberId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "NotificationSubscription" DROP COLUMN "customerId",
ADD COLUMN "organizationId" TEXT NOT NULL,
ADD COLUMN "phoneNumberId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "PhoneCall" DROP COLUMN "customerId",
DROP COLUMN "twilioSid",
ADD COLUMN "phoneNumberId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "PhoneNumber" DROP COLUMN "customerId",
DROP COLUMN "phoneNumber",
DROP COLUMN "phoneNumberSid",
ADD COLUMN "number" TEXT NOT NULL,
ADD COLUMN "organizationId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "User" DROP COLUMN "role",
ADD COLUMN "role" "GlobalRole" NOT NULL DEFAULT E'CUSTOMER';
-- DropTable
DROP TABLE "Customer";
-- CreateTable
CREATE TABLE "TwilioCredentials" (
"accountSid" TEXT NOT NULL,
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ NOT NULL,
"authToken" TEXT NOT NULL,
"twimlAppSid" TEXT,
"organizationId" TEXT NOT NULL,
PRIMARY KEY ("accountSid")
);
-- CreateTable
CREATE TABLE "Organization" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ NOT NULL,
"encryptionKey" TEXT NOT NULL,
"paddleCustomerId" TEXT,
"paddleSubscriptionId" TEXT,
PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Membership" (
"id" TEXT NOT NULL,
"role" "MembershipRole" NOT NULL,
"organizationId" TEXT NOT NULL,
"userId" TEXT,
"invitedName" TEXT,
"invitedEmail" TEXT,
PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Membership.organizationId_invitedEmail_unique" ON "Membership"("organizationId", "invitedEmail");
-- CreateIndex
CREATE UNIQUE INDEX "Message.phoneNumberId_id_unique" ON "Message"("phoneNumberId", "id");
-- CreateIndex
CREATE UNIQUE INDEX "PhoneCall.phoneNumberId_id_unique" ON "PhoneCall"("phoneNumberId", "id");
-- CreateIndex
CREATE UNIQUE INDEX "PhoneNumber.organizationId_id_unique" ON "PhoneNumber"("organizationId", "id");
-- AddForeignKey
ALTER TABLE "TwilioCredentials" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Membership" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Membership" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Message" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PhoneNumber" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,16 @@
/*
Warnings:
- You are about to drop the `TwilioCredentials` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "TwilioCredentials" DROP CONSTRAINT "TwilioCredentials_organizationId_fkey";
-- AlterTable
ALTER TABLE "Organization" ADD COLUMN "twilioAccountSid" TEXT,
ADD COLUMN "twilioAuthToken" TEXT,
ADD COLUMN "twimlAppSid" TEXT;
-- DropTable
DROP TABLE "TwilioCredentials";

View File

@ -0,0 +1,36 @@
/*
Warnings:
- A unique constraint covering the columns `[organizationId,phoneNumberId,id]` on the table `Message` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[id,twilioAccountSid]` on the table `Organization` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[organizationId,phoneNumberId,id]` on the table `PhoneCall` will be added. If there are existing duplicate values, this will fail.
- Added the required column `organizationId` to the `Message` table without a default value. This is not possible if the table is not empty.
- Added the required column `organizationId` to the `PhoneCall` table without a default value. This is not possible if the table is not empty.
*/
-- DropIndex
DROP INDEX "Message.phoneNumberId_id_unique";
-- DropIndex
DROP INDEX "PhoneCall.phoneNumberId_id_unique";
-- AlterTable
ALTER TABLE "Message" ADD COLUMN "organizationId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "PhoneCall" ADD COLUMN "organizationId" TEXT NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX "Message.organizationId_phoneNumberId_id_unique" ON "Message"("organizationId", "phoneNumberId", "id");
-- CreateIndex
CREATE UNIQUE INDEX "Organization.id_twilioAccountSid_unique" ON "Organization"("id", "twilioAccountSid");
-- CreateIndex
CREATE UNIQUE INDEX "PhoneCall.organizationId_phoneNumberId_id_unique" ON "PhoneCall"("organizationId", "phoneNumberId", "id");
-- AddForeignKey
ALTER TABLE "Message" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -12,6 +12,56 @@ generator client {
// --------------------------------------
model Organization {
id String @id @default(uuid())
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
encryptionKey String
paddleCustomerId String?
paddleSubscriptionId String?
twilioAccountSid String?
twilioAuthToken String? // TODO: encrypt it with encryptionKey
twimlAppSid String?
memberships Membership[]
phoneNumbers PhoneNumber[]
notificationSubscriptions NotificationSubscription[]
messages Message[]
phoneCalls PhoneCall[]
@@unique([id, twilioAccountSid])
}
model Membership {
id String @id @default(uuid())
role MembershipRole
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
user User? @relation(fields: [userId], references: [id])
userId String?
// When the user joins, we will clear out the name and email and set the user.
invitedName String?
invitedEmail String?
@@unique([organizationId, invitedEmail])
}
enum MembershipRole {
OWNER
ADMIN
USER
}
// The owners of the SaaS (you) can have a SUPERADMIN role to access all data
enum GlobalRole {
SUPERADMIN
CUSTOMER
}
model User {
id String @id @default(uuid())
createdAt DateTime @default(now()) @db.Timestamptz
@ -19,11 +69,11 @@ model User {
name String?
email String @unique
hashedPassword String?
role Role @default(USER)
role GlobalRole @default(CUSTOMER)
memberships Membership[]
tokens Token[]
sessions Session[]
customer Customer[]
}
enum Role {
@ -65,25 +115,6 @@ enum TokenType {
RESET_PASSWORD
}
model Customer {
id String @id
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
encryptionKey String
accountSid String?
authToken String?
// TODO: encrypt it with encryptionKey
twimlAppSid String?
paddleCustomerId String?
paddleSubscriptionId String?
user User @relation(fields: [id], references: [id])
messages Message[]
phoneCalls PhoneCall[]
phoneNumbers PhoneNumber[]
notificationSubscriptions NotificationSubscription[]
}
model Message {
id String @id @default(uuid())
sentAt DateTime @db.Timestamptz
@ -92,10 +123,13 @@ model Message {
to String
direction Direction
status MessageStatus
twilioSid String?
customer Customer @relation(fields: [customerId], references: [id])
customerId String
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
phoneNumberId String
@@unique([organizationId, phoneNumberId, id])
}
enum Direction {
@ -117,20 +151,25 @@ enum MessageStatus {
Read
PartiallyDelivered
Canceled
Error
}
model PhoneCall {
id String @id @default(uuid())
id String @id
createdAt DateTime @default(now()) @db.Timestamptz
twilioSid String
from String
to String
status CallStatus
direction Direction
duration String
customer Customer @relation(fields: [customerId], references: [id])
customerId String
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
phoneNumberId String
@@unique([organizationId, phoneNumberId, id])
}
enum CallStatus {
@ -145,13 +184,18 @@ enum CallStatus {
}
model PhoneNumber {
id String @id @default(uuid())
id String @id
createdAt DateTime @default(now()) @db.Timestamptz
phoneNumberSid String
phoneNumber String
number String
customer Customer @relation(fields: [customerId], references: [id])
customerId String
messages Message[]
phoneCalls PhoneCall[]
notificationSubscriptions NotificationSubscription[]
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
@@unique([organizationId, id])
}
model NotificationSubscription {
@ -163,6 +207,8 @@ model NotificationSubscription {
keys_p256dh String
keys_auth String
customer Customer @relation(fields: [customerId], references: [id])
customerId String
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
phoneNumberId String
}

21
docker-compose.yml Normal file
View File

@ -0,0 +1,21 @@
version: "3.7"
services:
db:
image: postgres:13-alpine
restart: unless-stopped
volumes:
- data:/var/lib/postgresql/data
env_file: ./.env.local #Here we are using the already existing .env.local file
ports:
- "5432:5432"
admin:
image: adminer
restart: unless-stopped
depends_on:
- db
ports:
- 8080:8080
volumes:
data:

2067
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -42,11 +42,11 @@
"@fortawesome/pro-light-svg-icons": "file:./fontawesome/fortawesome-pro-light-svg-icons-5.15.3.tgz",
"@fortawesome/pro-regular-svg-icons": "file:./fontawesome/fortawesome-pro-regular-svg-icons-5.15.3.tgz",
"@fortawesome/pro-solid-svg-icons": "file:./fontawesome/fortawesome-pro-solid-svg-icons-5.15.3.tgz",
"@fortawesome/react-fontawesome": "0.1.14",
"@fortawesome/react-fontawesome": "0.1.15",
"@headlessui/react": "1.4.0",
"@heroicons/react": "1.0.3",
"@hookform/resolvers": "2.6.1",
"@prisma/client": "2.27.0",
"@prisma/client": "2.28.0",
"@react-aria/interactions": "3.5.0",
"@tailwindcss/forms": "0.3.3",
"@tailwindcss/typography": "0.4.1",
@ -60,7 +60,7 @@
"pino": "6.13.0",
"pino-pretty": "5.1.2",
"postcss": "8.3.6",
"quirrel": "1.6.3",
"quirrel": "1.7.0",
"react": "18.0.0-alpha-6f3fcbd6f-20210730",
"react-dom": "18.0.0-alpha-6f3fcbd6f-20210730",
"react-hook-form": "7.12.2",
@ -84,7 +84,7 @@
"lint-staged": "10.5.4",
"next-test-api-route-handler": "2.0.2",
"prettier": "2.3.2",
"prettier-plugin-prisma": "0.14.0",
"prettier-plugin-prisma": "2.28.0",
"pretty-quick": "3.1.1",
"preview-email": "3.0.4",
"prisma": "2.28.0",

View File

@ -1,6 +1,8 @@
import { DefaultCtx, SessionContext, SimpleRolesIsAuthorized } from "blitz";
import { User, Role } from "./db";
import { Organization, User, GlobalRole, MembershipRole } from "./db";
type Role = GlobalRole | MembershipRole;
declare module "blitz" {
export interface Ctx extends DefaultCtx {
@ -11,7 +13,8 @@ declare module "blitz" {
isAuthorized: SimpleRolesIsAuthorized<Role>;
PublicData: {
userId: User["id"];
role: Role;
roles: Role[];
orgId: Organization["id"];
hasCompletedOnboarding?: true;
};
}