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,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"];
}
const Body = z.object({
phoneNumberId: z.string(),
});
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 }),
});
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberId }, context) => {
const organizationId = context.session.orgId;
return {
phoneCalls,
nextPage,
hasMore,
count,
};
},
);
return db.phoneCall.findMany({
where: { organizationId, phoneNumberId },
orderBy: { createdAt: Prisma.SortOrder.asc },
});
});