|
| 1 | +import { db } from '@db'; |
| 2 | +import type { Prisma } from '@db'; |
| 3 | +import type { IsmsTeamSizeBand, OperationalOwnershipRow } from './types'; |
| 4 | +import { teamSizeBand } from './roles'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Extra data the Roles document (5.3) needs at export time but that isn't on the |
| 8 | + * document's own rows: display names for assigned members (assignments store a |
| 9 | + * plain memberId, no FK), the live per-artifact operational owners, and the |
| 10 | + * team-size band. Resolved once and frozen into the version snapshot so a |
| 11 | + * historical export re-renders byte-faithfully. |
| 12 | + */ |
| 13 | +export interface RolesExtras { |
| 14 | + /** memberId → display name (name, else email, else a placeholder). */ |
| 15 | + memberNames: Record<string, string>; |
| 16 | + operationalOwnership: OperationalOwnershipRow[]; |
| 17 | + band: IsmsTeamSizeBand; |
| 18 | +} |
| 19 | + |
| 20 | +type Client = Prisma.TransactionClient | typeof db; |
| 21 | + |
| 22 | +const OWNER_DISPLAY_CAP = 12; |
| 23 | + |
| 24 | +type NamedAssignee = { |
| 25 | + assignee: { user: { name: string | null; email: string | null } | null } | null; |
| 26 | +}; |
| 27 | + |
| 28 | +function memberDisplayName(user: { name: string | null; email: string | null } | null): string { |
| 29 | + return user?.name?.trim() || user?.email?.trim() || 'Unknown member'; |
| 30 | +} |
| 31 | + |
| 32 | +function dedupeOwners(rows: NamedAssignee[]): string[] { |
| 33 | + const seen = new Set<string>(); |
| 34 | + const names: string[] = []; |
| 35 | + for (const row of rows) { |
| 36 | + if (!row.assignee?.user) continue; |
| 37 | + const name = memberDisplayName(row.assignee.user); |
| 38 | + if (seen.has(name)) continue; |
| 39 | + seen.add(name); |
| 40 | + names.push(name); |
| 41 | + } |
| 42 | + names.sort((a, b) => a.localeCompare(b)); |
| 43 | + if (names.length <= OWNER_DISPLAY_CAP) return names; |
| 44 | + return [ |
| 45 | + ...names.slice(0, OWNER_DISPLAY_CAP), |
| 46 | + `and ${names.length - OWNER_DISPLAY_CAP} more`, |
| 47 | + ]; |
| 48 | +} |
| 49 | + |
| 50 | +/** Load the Roles document's export extras for an organization. */ |
| 51 | +export async function loadRolesExtras({ |
| 52 | + organizationId, |
| 53 | + client, |
| 54 | +}: { |
| 55 | + organizationId: string; |
| 56 | + client?: Client; |
| 57 | +}): Promise<RolesExtras> { |
| 58 | + const prisma = client ?? db; |
| 59 | + const assigneeSelect = { |
| 60 | + assignee: { select: { user: { select: { name: true, email: true } } } }, |
| 61 | + } as const; |
| 62 | + |
| 63 | + const [members, memberCount, policies, risks, tasks, vendors] = |
| 64 | + await Promise.all([ |
| 65 | + prisma.member.findMany({ |
| 66 | + where: { organizationId }, |
| 67 | + select: { id: true, user: { select: { name: true, email: true } } }, |
| 68 | + }), |
| 69 | + prisma.member.count({ where: { organizationId, deactivated: false } }), |
| 70 | + prisma.policy.findMany({ |
| 71 | + where: { organizationId, assigneeId: { not: null } }, |
| 72 | + select: assigneeSelect, |
| 73 | + }), |
| 74 | + prisma.risk.findMany({ |
| 75 | + where: { organizationId, assigneeId: { not: null } }, |
| 76 | + select: assigneeSelect, |
| 77 | + }), |
| 78 | + prisma.task.findMany({ |
| 79 | + where: { organizationId, assigneeId: { not: null } }, |
| 80 | + select: assigneeSelect, |
| 81 | + }), |
| 82 | + prisma.vendor.findMany({ |
| 83 | + where: { organizationId, assigneeId: { not: null } }, |
| 84 | + select: assigneeSelect, |
| 85 | + }), |
| 86 | + ]); |
| 87 | + |
| 88 | + const memberNames: Record<string, string> = {}; |
| 89 | + for (const member of members) { |
| 90 | + memberNames[member.id] = memberDisplayName(member.user); |
| 91 | + } |
| 92 | + |
| 93 | + // Controls have no owner field in the platform; their ownership is descriptive |
| 94 | + // (assigned per control via their linked tasks). Kept in the matrix per the |
| 95 | + // reference document, without enumerating names. |
| 96 | + const operationalOwnership: OperationalOwnershipRow[] = [ |
| 97 | + { |
| 98 | + artifact: 'Policies', |
| 99 | + assignedWhere: 'Policy assignee / approver in Comp AI', |
| 100 | + ownerResponsibility: |
| 101 | + 'Keep the policy current and accurate; ensure required acknowledgement.', |
| 102 | + owners: dedupeOwners(policies), |
| 103 | + }, |
| 104 | + { |
| 105 | + artifact: 'Controls', |
| 106 | + assignedWhere: 'Control owner in Comp AI', |
| 107 | + ownerResponsibility: 'Implement, operate, and evidence the control.', |
| 108 | + owners: [], |
| 109 | + }, |
| 110 | + { |
| 111 | + artifact: 'Risks', |
| 112 | + assignedWhere: 'Risk owner in Comp AI', |
| 113 | + ownerResponsibility: |
| 114 | + 'Assess, treat, and monitor the risk within the risk appetite.', |
| 115 | + owners: dedupeOwners(risks), |
| 116 | + }, |
| 117 | + { |
| 118 | + artifact: 'Evidence tasks', |
| 119 | + assignedWhere: 'Task assignee in Comp AI', |
| 120 | + ownerResponsibility: 'Complete and evidence the task by its due date.', |
| 121 | + owners: dedupeOwners(tasks), |
| 122 | + }, |
| 123 | + { |
| 124 | + artifact: 'Vendors / sub-processors', |
| 125 | + assignedWhere: 'Vendor owner in Comp AI', |
| 126 | + ownerResponsibility: |
| 127 | + 'Perform due diligence and ongoing security review; maintain the DPA.', |
| 128 | + owners: dedupeOwners(vendors), |
| 129 | + }, |
| 130 | + ]; |
| 131 | + |
| 132 | + return { |
| 133 | + memberNames, |
| 134 | + operationalOwnership, |
| 135 | + band: teamSizeBand(memberCount), |
| 136 | + }; |
| 137 | +} |
0 commit comments