Allow dueAt/description patches for collaborators when editPolicy is creator-only, and show the no-edit warning only when they try to change locked fields—not as a permanent banner. Co-authored-by: Cursor <cursoragent@cursor.com>
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import type { ActivityDto, ActivityEditPolicy } from '@mento/shared';
|
|
|
|
type ActivityAccessUser = {
|
|
id?: string | null;
|
|
role?: string | null;
|
|
} | null | undefined;
|
|
|
|
type ActivityAccessRow = Pick<
|
|
ActivityDto,
|
|
'editPolicy' | 'createdBy' | 'assignee' | 'assignees'
|
|
> | null | undefined;
|
|
|
|
function isAssignee(
|
|
activity: NonNullable<ActivityAccessRow>,
|
|
userId: string,
|
|
): boolean {
|
|
if (activity.assignee?.id === userId) return true;
|
|
return (activity.assignees ?? []).some((a) => a.id === userId);
|
|
}
|
|
|
|
/** ویرایش/حذف فیلدهای فعالیت — انجام/جابهجایی ستون جداست */
|
|
export function canMutateActivity(
|
|
activity: ActivityAccessRow,
|
|
user: ActivityAccessUser,
|
|
): boolean {
|
|
if (!activity || !user?.id) return false;
|
|
if (user.role === 'owner' || user.role === 'admin') return true;
|
|
if (activity.createdBy?.id === user.id) return true;
|
|
const policy: ActivityEditPolicy = activity.editPolicy ?? 'creator';
|
|
if (policy !== 'assignees') return false;
|
|
return isAssignee(activity, user.id);
|
|
}
|
|
|
|
/** ثبت نظر و تعویق برای همهٔ مسئولان (حتی وقتی ویرایش فقط با ایجادکننده است) */
|
|
export function canCollaborateOnActivity(
|
|
activity: ActivityAccessRow,
|
|
user: ActivityAccessUser,
|
|
): boolean {
|
|
if (!activity || !user?.id) return false;
|
|
if (user.role === 'owner' || user.role === 'admin') return true;
|
|
if (activity.createdBy?.id === user.id) return true;
|
|
return isAssignee(activity, user.id);
|
|
}
|