import type { UserRole } from '@mento/shared'; import { canViewAllRecords } from '@mento/shared'; import type { Prisma } from '@prisma/client'; export type Actor = { userId: string; role: string; organizationId: string; }; export function elevated(role: string | null | undefined): boolean { return canViewAllRecords(role); } /** فیلتر مالکیت برای Deal / Quote / Invoice / Person / Company */ export function ownerWhere( actor: Pick, ): { ownerId: string } | Record { if (elevated(actor.role)) return {}; return { ownerId: actor.userId }; } /** فعالیت‌ها بر اساس مسئول */ export function assigneeWhere( actor: Pick, ): { assigneeId: string } | Record { if (elevated(actor.role)) return {}; return { assigneeId: actor.userId }; } /** یادداشت: نویسنده یا پرونده‌های تحت مالکیت */ export function noteAccessWhere( actor: Pick, ): Prisma.NoteWhereInput { if (elevated(actor.role)) return {}; return { OR: [ { authorId: actor.userId }, { deal: { ownerId: actor.userId } }, { person: { ownerId: actor.userId } }, { company: { ownerId: actor.userId } }, ], }; } export function resolveOwnerId( actor: Pick, requested?: string | null, ): string { if (elevated(actor.role) && requested?.trim()) { return requested.trim(); } return actor.userId; } /** تیکت: مسئول یا ثبت‌کننده */ export function ticketAccessWhere( actor: Pick, ): Prisma.TicketWhereInput { if (elevated(actor.role)) return {}; return { OR: [{ assigneeId: actor.userId }, { reporterId: actor.userId }], }; } export function canReassignOwner(role: string | null | undefined): boolean { return elevated(role); } export type OrgRole = UserRole;