|
| 1 | +/** |
| 2 | + * Well-known framework name patterns used in task descriptions. |
| 3 | + * Each entry maps a canonical label (used in "For <label>:" paragraphs) |
| 4 | + * to the possible names stored in FrameworkEditorFramework.name. |
| 5 | + * |
| 6 | + * Matching is case-insensitive. |
| 7 | + */ |
| 8 | +const FRAMEWORK_ALIASES: Record<string, string[]> = { |
| 9 | + 'soc 2': ['soc 2', 'soc2', 'soc 2 v.1'], |
| 10 | + 'iso 27001': ['iso 27001', 'iso27001'], |
| 11 | + 'iso 42001': ['iso 42001', 'iso42001'], |
| 12 | + 'iso 9001': ['iso 9001', 'iso9001'], |
| 13 | + hipaa: ['hipaa'], |
| 14 | + gdpr: ['gdpr'], |
| 15 | + 'pci dss': ['pci dss', 'pci', 'pci v0', 'example pci'], |
| 16 | + 'nen 7510': ['nen 7510', 'nen7510'], |
| 17 | + 'nist csf': ['nist csf'], |
| 18 | + 'nist 800-53': ['nist 800-53'], |
| 19 | + 'nis 2': ['nis 2', 'nis2'], |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Regex that matches a paragraph that starts with a framework-specific |
| 24 | + * prefix such as "For ISO 27001:" or "For HIPAA:". |
| 25 | + * |
| 26 | + * Capture group 1 = framework label (e.g. "ISO 27001", "PCI"). |
| 27 | + * |
| 28 | + * The pattern is intentionally broad: it catches "For <words>:" at the |
| 29 | + * beginning of a paragraph (after optional whitespace / newlines). |
| 30 | + */ |
| 31 | +const FOR_FRAMEWORK_LINE_RE = |
| 32 | + /^[ \t]*For\s+([A-Za-z0-9][A-Za-z0-9 .\-/]*?)\s*:/im; |
| 33 | +const COMPOSITE_LABEL_SEPARATOR_RE = /\s*(?:,|\/|&|\band\b)\s*/i; |
| 34 | + |
| 35 | +/** |
| 36 | + * Normalise a framework name for comparison. |
| 37 | + */ |
| 38 | +function normalise(name: string): string { |
| 39 | + return name.trim().toLowerCase(); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Build a Set of normalised active framework labels from the org's |
| 44 | + * framework instance names. We expand each name through the alias map |
| 45 | + * so that both the canonical label and the DB name are included. |
| 46 | + */ |
| 47 | +function buildActiveLabels(activeFrameworkNames: string[]): Set<string> { |
| 48 | + const labels = new Set<string>(); |
| 49 | + |
| 50 | + for (const name of activeFrameworkNames) { |
| 51 | + const normName = normalise(name); |
| 52 | + labels.add(normName); |
| 53 | + |
| 54 | + // Also add canonical labels that match this name |
| 55 | + for (const [canonical, aliases] of Object.entries(FRAMEWORK_ALIASES)) { |
| 56 | + if (aliases.some((a) => normalise(a) === normName)) { |
| 57 | + labels.add(normalise(canonical)); |
| 58 | + for (const alias of aliases) { |
| 59 | + labels.add(normalise(alias)); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return labels; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Check whether a framework label extracted from a "For <label>:" line |
| 70 | + * matches one of the active frameworks. |
| 71 | + */ |
| 72 | +function isLabelActive(label: string, activeLabels: Set<string>): boolean { |
| 73 | + const normLabel = normalise(label); |
| 74 | + const aliasEntries = Object.entries(FRAMEWORK_ALIASES); |
| 75 | + |
| 76 | + // Direct match |
| 77 | + if (activeLabels.has(normLabel)) return true; |
| 78 | + |
| 79 | + // Check alias map: if the label is a known canonical key or alias, |
| 80 | + // see if any of its counterparts are in the active set. |
| 81 | + for (const [canonical, aliases] of aliasEntries) { |
| 82 | + const allNames = [canonical, ...aliases].map(normalise); |
| 83 | + if (allNames.includes(normLabel)) { |
| 84 | + return allNames.some((n) => activeLabels.has(n)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Handle headers like "For ISO 27001 and HIPAA:" |
| 89 | + const parts = normLabel |
| 90 | + .split(COMPOSITE_LABEL_SEPARATOR_RE) |
| 91 | + .map((part) => part.trim()) |
| 92 | + .filter(Boolean); |
| 93 | + |
| 94 | + if (parts.length > 1) { |
| 95 | + let hasKnownPart = false; |
| 96 | + let hasUnknownPart = false; |
| 97 | + let anyKnownPartIsActive = false; |
| 98 | + |
| 99 | + for (const part of parts) { |
| 100 | + if (activeLabels.has(part)) { |
| 101 | + hasKnownPart = true; |
| 102 | + anyKnownPartIsActive = true; |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + const matchingAliasEntry = aliasEntries.find(([canonical, aliases]) => { |
| 107 | + const allNames = [canonical, ...aliases].map(normalise); |
| 108 | + return allNames.includes(part); |
| 109 | + }); |
| 110 | + |
| 111 | + if (!matchingAliasEntry) { |
| 112 | + hasUnknownPart = true; |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + hasKnownPart = true; |
| 117 | + const [canonical, aliases] = matchingAliasEntry; |
| 118 | + const allNames = [canonical, ...aliases].map(normalise); |
| 119 | + if (allNames.some((name) => activeLabels.has(name))) { |
| 120 | + anyKnownPartIsActive = true; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (hasKnownPart && !hasUnknownPart) { |
| 125 | + return anyKnownPartIsActive; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Unknown label - keep it visible (safe default) |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Filter framework-specific paragraphs from a task description. |
| 135 | + * |
| 136 | + * Paragraphs starting with "For <FrameworkName>:" are removed if the |
| 137 | + * framework is not among the organisation's active frameworks. |
| 138 | + * |
| 139 | + * @returns The filtered description string. |
| 140 | + */ |
| 141 | +export function filterDescriptionByFrameworks( |
| 142 | + description: string, |
| 143 | + activeFrameworkNames: string[], |
| 144 | +): string { |
| 145 | + if (!description) return description; |
| 146 | + if (activeFrameworkNames.length === 0) return description; |
| 147 | + |
| 148 | + const activeLabels = buildActiveLabels(activeFrameworkNames); |
| 149 | + |
| 150 | + // Split description into paragraphs (double-newline separated) |
| 151 | + const paragraphs = description.split(/\n\n+/); |
| 152 | + let pendingHeaderIsActive: boolean | null = null; |
| 153 | + |
| 154 | + const filtered = paragraphs.filter((paragraph) => { |
| 155 | + if (pendingHeaderIsActive !== null) { |
| 156 | + const shouldKeep = pendingHeaderIsActive; |
| 157 | + pendingHeaderIsActive = null; |
| 158 | + return shouldKeep; |
| 159 | + } |
| 160 | + |
| 161 | + const match = paragraph.match(FOR_FRAMEWORK_LINE_RE); |
| 162 | + if (!match) return true; // Not a framework-specific paragraph |
| 163 | + |
| 164 | + const label = match[1]; |
| 165 | + const isActive = isLabelActive(label, activeLabels); |
| 166 | + |
| 167 | + // Handle seed data format where header and section content are split: |
| 168 | + // "For GDPR:\n\n<framework-specific paragraph>" |
| 169 | + const paragraphWithoutPrefix = paragraph |
| 170 | + .replace(FOR_FRAMEWORK_LINE_RE, '') |
| 171 | + .trim(); |
| 172 | + if (!paragraphWithoutPrefix) { |
| 173 | + pendingHeaderIsActive = isActive; |
| 174 | + } |
| 175 | + |
| 176 | + return isActive; |
| 177 | + }); |
| 178 | + |
| 179 | + return filtered.join('\n\n').trim(); |
| 180 | +} |
0 commit comments