61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import type { Prisma } from "../generated/prisma/client/client.js";
|
|
|
|
export type AdminSalesAccessScope = {
|
|
readonly adminUserId: string;
|
|
readonly canReadAll: boolean;
|
|
readonly canWriteAll: boolean;
|
|
readonly canReadAllCommunication: boolean;
|
|
};
|
|
|
|
export const UNRESTRICTED_ADMIN_SALES_ACCESS = Object.freeze({
|
|
adminUserId: "",
|
|
canReadAll: true,
|
|
canWriteAll: true,
|
|
canReadAllCommunication: true,
|
|
}) satisfies AdminSalesAccessScope;
|
|
|
|
export function adminSalesLeadReadWhere(
|
|
scope: AdminSalesAccessScope
|
|
): Prisma.AdminLeadWhereInput {
|
|
return scope.canReadAll
|
|
? {}
|
|
: { assignedToAdminUserId: scope.adminUserId };
|
|
}
|
|
|
|
export function adminSalesDealReadWhere(
|
|
scope: AdminSalesAccessScope
|
|
): Prisma.AdminDealWhereInput {
|
|
return scope.canReadAll ? {} : { ownerAdminUserId: scope.adminUserId };
|
|
}
|
|
|
|
export function adminSalesTaskReadWhere(
|
|
scope: AdminSalesAccessScope
|
|
): Prisma.AdminTaskWhereInput {
|
|
return scope.canReadAll
|
|
? {}
|
|
: { assignedToAdminUserId: scope.adminUserId };
|
|
}
|
|
|
|
export function adminCommunicationReadWhere(
|
|
scope: AdminSalesAccessScope
|
|
): { readonly adminUserId?: string } {
|
|
return scope.canReadAllCommunication ? {} : { adminUserId: scope.adminUserId };
|
|
}
|
|
|
|
export function assertAdminSalesWriteOwner(
|
|
ownerAdminUserId: string | null,
|
|
scope: AdminSalesAccessScope
|
|
): void {
|
|
if (!scope.canWriteAll && ownerAdminUserId !== scope.adminUserId) {
|
|
throw new AdminSalesScopeNotFoundError();
|
|
}
|
|
}
|
|
|
|
export class AdminSalesScopeNotFoundError extends Error {
|
|
public override readonly name = "AdminSalesScopeNotFoundError";
|
|
|
|
public constructor() {
|
|
super("Admin sales record was not found.");
|
|
}
|
|
}
|