Files
ipal-kit/src/modules/consent/scanner/merge.ts
T
Radosław SmolińskiandClaude Opus 5.5 a0545b6716 Cookie scanner, phase 1: declarations, database lookup, review list
ipalKit({ cookieScanner }) (opt-in) adds the cookie-review collection, a
"Scan cookies" box above its list and GET/POST /api/ipal/cookie-scan. A scan
lists the cookies of every integration the project switches on (consent,
admin login, locale, stats, GA4, GTM, Meta Pixel, Clarity, LinkedIn, TikTok,
Turnstile), the project's own declarations, and extra names looked up in the
bundled Open Cookie Database, as drafts. Editors accept (→ Cookie Settings →
Cookies, every locale, only empty text filled), edit or reject. A rescan never
deletes, never changes status and never overwrites edited text.

Everything is overridable (access, collectionOverrides, integrations,
declarations, databaseCategories, labels, syncAccepted) and the building
blocks are exported. Admin labels in en + pl. Docs: cookie-scanner.md,
consent.md, AGENT.md, ipal-reference, ipal-consent-check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 14:55:20 +01:00

83 lines
3.2 KiB
TypeScript

import type { ConsentCategory } from '../categories.js'
import type { CookieFinding, LocalizedText } from './types.js'
/** The editable values of a review entry; `purpose` / `retention` per locale. */
export type CookieReviewValues = {
category?: ConsentCategory | null
domain?: null | string
purpose?: Record<string, null | string>
retention?: Record<string, null | string>
setBeforeConsent?: boolean | null
vendor?: null | string
}
const SCALARS = ['category', 'domain', 'setBeforeConsent', 'vendor'] as const
const TEXTS = ['purpose', 'retention'] as const
/** Text for one locale: that language, else English, else the first one given. */
export function textFor(text: LocalizedText | undefined, locale: string): string | undefined {
if (text === undefined) {return undefined}
if (typeof text === 'string') {return text}
return text[locale] ?? text.en ?? Object.values(text)[0]
}
/** What a scan suggests for an entry, resolved for the project's locales. */
export function suggestionFor(finding: CookieFinding, locales: string[]): CookieReviewValues {
const perLocale = (text: LocalizedText | undefined) => {
if (text === undefined) {return undefined}
return Object.fromEntries(locales.map((l) => [l, textFor(text, l) ?? null]))
}
const out: CookieReviewValues = {}
if (finding.category) {out.category = finding.category}
if (finding.domain) {out.domain = finding.domain}
if (finding.vendor) {out.vendor = finding.vendor}
if (typeof finding.setBeforeConsent === 'boolean') {out.setBeforeConsent = finding.setBeforeConsent}
const purpose = perLocale(finding.purpose)
const retention = perLocale(finding.retention)
if (purpose) {out.purpose = purpose}
if (retention) {out.retention = retention}
return out
}
const isEmpty = (v: unknown) => v === undefined || v === null || v === ''
/**
* New values for an entry after a scan. A value takes the new suggestion only
* while it is empty or still equal to what the previous scan suggested — once an
* editor changed it, it is theirs and stays. Text is compared per locale.
*/
export function mergeCookieReview(
current: CookieReviewValues,
previous: CookieReviewValues | null | undefined,
next: CookieReviewValues,
): { changed: boolean; values: CookieReviewValues } {
const prev = previous ?? {}
const values: CookieReviewValues = { ...current }
let changed = false
const pick = <V>(cur: V, before: undefined | V, suggested: undefined | V): V => {
if (suggested === undefined || isEmpty(suggested)) {return cur}
if (isEmpty(cur) || cur === before) {return suggested}
return cur
}
for (const key of SCALARS) {
const v = pick(current[key], prev[key], next[key])
if (v !== current[key]) {
;(values as Record<string, unknown>)[key] = v
changed = true
}
}
for (const key of TEXTS) {
const cur = current[key] ?? {}
const merged = { ...cur }
for (const [locale, suggested] of Object.entries(next[key] ?? {})) {
const v = pick(cur[locale], prev[key]?.[locale], suggested)
if (v !== cur[locale]) {
merged[locale] = v ?? null
changed = true
}
}
if (Object.keys(merged).length) {values[key] = merged}
}
return { changed, values }
}