shoc-frontend-new/src/app/(protected)/completion-docs/_components/use-completion-template-permissions.ts
Alexandre Brandizzi 50b97a96d0 feat(completion-docs): manage completion document templates
Add the Completion Docs page under Data Management: a searchable template
table, a read-only detail panel, add and edit with a live document preview,
and delete behind a confirmation that counts the open work orders still
using the template and links to them. Add, edit and delete are hidden for
users whose role lacks the matching template permission.

The preview shares the work-order completion document's page frame,
header, safety bullets and procedure heading so the two stay identical.
2026-09-25 11:08:21 -03:00

36 lines
1.4 KiB
TypeScript

import { defaultsForRole } from "@/app/(protected)/team-members/_components/team-member-permissions";
import type { TeamMemberRole } from "@/domain/team-members/types/team-member";
import { useAuthContext } from "@/providers/auth-context";
const TEAM_ROLES: readonly TeamMemberRole[] = ["Admin", "Scheduler", "Dispatcher"];
export interface CompletionTemplatePermissions {
canCreate: boolean;
canEdit: boolean;
canDelete: boolean;
}
function teamRoles(userRoles: string | null | undefined): TeamMemberRole[] {
const names = (userRoles ?? "").split(",").map((role) => role.trim().toLowerCase());
return TEAM_ROLES.filter((role) => names.includes(role.toLowerCase()));
}
/**
* The signed-in user's session carries roles, not per-user permission overrides, so each key
* resolves to the role default the Team Members screen and the server policy start from.
*/
export function completionTemplatePermissions(
userRoles: string | null | undefined,
): CompletionTemplatePermissions {
const granted = new Set(teamRoles(userRoles).flatMap((role) => [...defaultsForRole(role)]));
return {
canCreate: granted.has("createCompletionDocTemplates"),
canEdit: granted.has("editCompletionDocTemplates"),
canDelete: granted.has("deleteCompletionDocTemplates"),
};
}
export function useCompletionTemplatePermissions(): CompletionTemplatePermissions {
const { user } = useAuthContext();
return completionTemplatePermissions(user?.userRoles);
}