Files
klz-cables.com/.pnpm-store/v10/files/1f/8cfc1edfe7ee63efa46e24229a512bb3ef04367af36f40f4a296e25b913b9178d7a50772fb2b40f43b4cdcc027133b336f22d761e8b95b7af6ed2461dffc6a
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

1 line
9.1 KiB
Plaintext

{"version":3,"sources":["../../../../src/versions/migrations/localizeStatus/shared.ts"],"sourcesContent":["import type { Payload } from '../../../types/index.js'\n\n/**\n * Convert to snake_case (matches to-snake-case library behavior)\n * Handles camelCase, PascalCase, and hyphens\n */\nexport const toSnakeCase = (str: string): string => {\n return str\n .replace(/([A-Z])/g, '_$1')\n .toLowerCase()\n .replace(/^_/, '')\n .replace(/-/g, '_') // Convert hyphens to underscores\n}\n\nexport type VersionRecord = {\n _status: 'draft' | 'published'\n created_at?: Date | string\n createdAt?: Date | string\n id: number | string\n parent: number | string\n published_locale?: string\n publishedLocale?: string\n snapshot?: boolean\n}\n\nexport type VersionLocaleStatusMap = Map<number | string, Map<string, 'draft' | 'published'>>\n\n/**\n * Core logic for calculating the status of each locale for each version\n * by processing version history chronologically.\n *\n * This works by:\n * 1. Processing versions in chronological order (oldest first)\n * 2. Tracking the cumulative published state for each document as we process versions\n * 3. For each version, determining what status each locale should have based on:\n * - Publish events with publishedLocale: mark that locale as published, version shows NEW state\n * - Publish events without publishedLocale: mark all locales as published, version shows NEW state\n * - Draft saves (_status='draft'): mark all locales as draft (unpublish everything)\n * - Snapshots: preserve state AFTER publish (snapshots created after publishing specific locale)\n *\n * Snapshot creation flow when publishing one locale:\n * 1. Merge incoming content with last published → update main table\n * 2. Create snapshot object (preserves other locales' draft content + updates published locale)\n * 3. Create publish version (_status='published', publishedLocale set)\n * 4. Create snapshot version (_status='draft', snapshot=true)\n * - Snapshot CONTENT is mixed (draft + published content)\n * - Snapshot STATUS reflects which locales are actually published\n *\n * Example scenario:\n * - V1: publish all locales (no snapshot) → state: {en: published, es: published, de: published}\n * - V2: draft save → state: {en: draft, es: draft, de: draft}\n * - V3: publish en only → state: {en: published, es: draft, de: draft}\n * - V4: snapshot after publishing en → state: {en: published, es: draft, de: draft}\n * - V5: publish all locales (no snapshot) → state: {en: published, es: published, de: published}\n *\n * @param versions - Array of version records (must be sorted by parent, then createdAt ASC)\n * @param locales - Array of locale codes (e.g., ['en', 'es', 'pt'])\n * @param payload - Payload instance for logging\n * @returns Map of versionId -> Map of locale -> status\n */\nexport function calculateVersionLocaleStatuses(\n versions: VersionRecord[],\n locales: string[],\n payload: Payload,\n): VersionLocaleStatusMap {\n payload.logger.info({ msg: `Processing ${versions.length} version records` })\n\n // Track the cumulative published state for each document across all locales\n // This represents what IS published at any given point in the version history\n const documentPublishState = new Map<number | string, Map<string, 'draft' | 'published'>>()\n\n // Map to store the final status for each version\n const versionLocaleStatus: VersionLocaleStatusMap = new Map()\n\n // Process versions chronologically to build up status history\n for (const version of versions) {\n const versionId = version.id\n const documentId = version.parent\n const status = version._status\n const publishedLocale = version.published_locale || version.publishedLocale\n const isSnapshot = version.snapshot === true\n\n // Initialize document state if first time seeing this document\n if (!documentPublishState.has(documentId)) {\n const localeMap = new Map<string, 'draft' | 'published'>()\n for (const locale of locales) {\n localeMap.set(locale, 'draft')\n }\n documentPublishState.set(documentId, localeMap)\n }\n\n const currentPublishState = documentPublishState.get(documentId)!\n const versionStatusMap = new Map<string, 'draft' | 'published'>()\n\n if (isSnapshot) {\n // Snapshots are created AFTER publishing a specific locale\n // Snapshot CONTENT is mixed: preserves other locales' draft content + new published locale content\n // But snapshot STATUS should reflect publish state: which locales are published vs draft\n // We use currentPublishState to track this, which has been updated by the previous publish\n for (const [locale, publishedStatus] of currentPublishState.entries()) {\n versionStatusMap.set(locale, publishedStatus)\n }\n } else if (status === 'published') {\n // This is a publish event\n if (publishedLocale) {\n // Publishing ONE locale - update the document's published state for that locale\n currentPublishState.set(publishedLocale, 'published')\n\n // This version should show the NEW state (after this publish)\n for (const [locale, publishedStatus] of currentPublishState.entries()) {\n versionStatusMap.set(locale, publishedStatus)\n }\n } else {\n // Publishing ALL locales - update all locales to published\n for (const locale of locales) {\n currentPublishState.set(locale, 'published')\n versionStatusMap.set(locale, 'published')\n }\n }\n } else {\n // This is a draft save - in the OLD system, _status='draft' meant unpublish ALL locales\n for (const locale of locales) {\n currentPublishState.set(locale, 'draft')\n versionStatusMap.set(locale, 'draft')\n }\n }\n\n // Store the status for this version\n versionLocaleStatus.set(versionId, versionStatusMap)\n }\n\n return versionLocaleStatus\n}\n\n/**\n * Sorts version records by parent document, then by creation date (oldest first)\n *\n * @param versions - Array of version records\n * @returns Sorted array of version records\n */\nexport function sortVersionsChronologically(versions: VersionRecord[]): VersionRecord[] {\n return versions.sort((a, b) => {\n // First sort by parent\n const parentA = String(a.parent)\n const parentB = String(b.parent)\n if (parentA !== parentB) {\n return parentA.localeCompare(parentB)\n }\n\n // Then sort by creation date\n const dateA = new Date(a.created_at || a.createdAt || 0)\n const dateB = new Date(b.created_at || b.createdAt || 0)\n return dateA.getTime() - dateB.getTime()\n })\n}\n"],"names":["toSnakeCase","str","replace","toLowerCase","calculateVersionLocaleStatuses","versions","locales","payload","logger","info","msg","length","documentPublishState","Map","versionLocaleStatus","version","versionId","id","documentId","parent","status","_status","publishedLocale","published_locale","isSnapshot","snapshot","has","localeMap","locale","set","currentPublishState","get","versionStatusMap","publishedStatus","entries","sortVersionsChronologically","sort","a","b","parentA","String","parentB","localeCompare","dateA","Date","created_at","createdAt","dateB","getTime"],"mappings":"AAEA;;;CAGC,GACD,OAAO,MAAMA,cAAc,CAACC;IAC1B,OAAO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