@murumets-ee/imports 0.49.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/analyze/index.d.mts +265 -0
  2. package/dist/analyze/index.d.mts.map +1 -0
  3. package/dist/analyze/index.mjs +2 -0
  4. package/dist/analyze/index.mjs.map +1 -0
  5. package/dist/carmaker-feed-B-QYr3PI.mjs +2 -0
  6. package/dist/{carmaker-feed-CxhZBrkO.mjs.map → carmaker-feed-B-QYr3PI.mjs.map} +1 -1
  7. package/dist/carmaker-feed-_QVQFLUl.d.mts +59 -0
  8. package/dist/carmaker-feed-_QVQFLUl.d.mts.map +1 -0
  9. package/dist/{cleanup-CHjDgvKT.mjs → cleanup-DDljBe0Z.mjs} +2 -2
  10. package/dist/{cleanup-CHjDgvKT.mjs.map → cleanup-DDljBe0Z.mjs.map} +1 -1
  11. package/dist/engine-B0ExE4jL.mjs +2 -0
  12. package/dist/engine-B0ExE4jL.mjs.map +1 -0
  13. package/dist/{parts-schema-x8_GRYWc.d.mts → engine-DiM4r2Vj.d.mts} +349 -49
  14. package/dist/engine-DiM4r2Vj.d.mts.map +1 -0
  15. package/dist/index-Bm69N5DW.d.mts +97 -0
  16. package/dist/index-Bm69N5DW.d.mts.map +1 -0
  17. package/dist/index.d.mts +30 -6
  18. package/dist/index.d.mts.map +1 -1
  19. package/dist/index.mjs +1 -1
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/mapping/index.d.mts +3 -359
  22. package/dist/mapping/index.d.mts.map +1 -1
  23. package/dist/mapping/index.mjs +1 -1
  24. package/dist/mapping-transform-CO4MNhMe.mjs +2 -0
  25. package/dist/{mapping-transform-D76b-P-p.mjs.map → mapping-transform-CO4MNhMe.mjs.map} +1 -1
  26. package/dist/matching/index.d.mts +2 -0
  27. package/dist/matching/index.mjs +1 -0
  28. package/dist/matching-Sr0z4HvN.mjs +2 -0
  29. package/dist/matching-Sr0z4HvN.mjs.map +1 -0
  30. package/dist/parts-schema-DOWhBJYh.d.mts +61 -0
  31. package/dist/parts-schema-DOWhBJYh.d.mts.map +1 -0
  32. package/dist/parts-schema-QqLci_Pl.mjs +2 -0
  33. package/dist/parts-schema-QqLci_Pl.mjs.map +1 -0
  34. package/dist/plugin.d.mts +1 -1
  35. package/dist/plugin.mjs +1 -1
  36. package/dist/plugin.mjs.map +1 -1
  37. package/dist/read-feed-DGjqejw3.mjs +2 -0
  38. package/dist/read-feed-DGjqejw3.mjs.map +1 -0
  39. package/dist/runner-C9GIe5Dr.mjs +2 -0
  40. package/dist/runner-C9GIe5Dr.mjs.map +1 -0
  41. package/dist/{schema-registry-C_lQje-K.mjs → schema-registry-DB5aIZMN.mjs} +2 -2
  42. package/dist/{schema-registry-C_lQje-K.mjs.map → schema-registry-DB5aIZMN.mjs.map} +1 -1
  43. package/dist/storage-resolver.d.mts +1 -1
  44. package/dist/transform-Cu5YEoOl.mjs +2 -0
  45. package/dist/transform-Cu5YEoOl.mjs.map +1 -0
  46. package/dist/{transform-CABBaYmF.d.mts → transform-WeiiLdYP.d.mts} +12 -1
  47. package/dist/transform-WeiiLdYP.d.mts.map +1 -0
  48. package/dist/transforms.d.mts +2 -46
  49. package/dist/transforms.mjs +1 -1
  50. package/dist/{worker-DRTgbtT7.d.mts → worker-_MlRH5hd.d.mts} +209 -8
  51. package/dist/worker-_MlRH5hd.d.mts.map +1 -0
  52. package/dist/worker.d.mts +1 -1
  53. package/dist/worker.mjs +1 -1
  54. package/dist/worker.mjs.map +1 -1
  55. package/dist/xlsx-reader-_ioPAqm5.mjs +2 -0
  56. package/dist/{xlsx-reader-Ccesszbc.mjs.map → xlsx-reader-_ioPAqm5.mjs.map} +1 -1
  57. package/package.json +16 -8
  58. package/dist/carmaker-feed-CxhZBrkO.mjs +0 -2
  59. package/dist/mapping-transform-D76b-P-p.mjs +0 -2
  60. package/dist/parts-schema-D0gMhNWj.mjs +0 -2
  61. package/dist/parts-schema-D0gMhNWj.mjs.map +0 -1
  62. package/dist/parts-schema-x8_GRYWc.d.mts.map +0 -1
  63. package/dist/runner-C-s6SD7t.mjs +0 -2
  64. package/dist/runner-C-s6SD7t.mjs.map +0 -1
  65. package/dist/transform-CABBaYmF.d.mts.map +0 -1
  66. package/dist/transform-DIzWppgJ.mjs +0 -2
  67. package/dist/transform-DIzWppgJ.mjs.map +0 -1
  68. package/dist/transforms.d.mts.map +0 -1
  69. package/dist/worker-DRTgbtT7.d.mts.map +0 -1
  70. package/dist/xlsx-reader-Ccesszbc.mjs +0 -2
@@ -1 +1 @@
1
- {"version":3,"file":"cleanup-CHjDgvKT.mjs","names":[],"sources":["../src/admin/cleanup.ts"],"sourcesContent":["/**\n * Cleanup orphaned imports uploads.\n *\n * Finds rows in `toolkit_files` within the configured imports bucket\n * that:\n * - were created more than `ttlHours` ago, AND\n * - have no matching `import_run.filePath` reference\n *\n * For each match, calls `StorageClient.delete(key)` — which removes\n * BOTH the adapter bytes (local file / R2 object) AND the\n * `toolkit_files` metadata row in one transactional unit (see\n * `@murumets-ee/storage` `StorageClient.delete`).\n *\n * Why this exists: the Tus upload flow stores bytes + metadata BEFORE\n * the client's downstream `register` step inserts an `import_run` row.\n * If the register call fails (validation, MIME mismatch, network\n * blip, agent abandons the workflow), the storage row is left\n * dangling. The commerce-side register handler does opportunistic\n * cleanup on its OWN failure paths, but rejections from the Zod\n * schema (before the handler runs), rejections where the agent\n * never calls register at all, AND the handler's operator-fixable\n * mapping rejections (mapping not-found / no-mapping 422 / structure\n * drift 409 — which deliberately KEEP the valid upload for a re-run)\n * all rely on this sweep.\n *\n * Scheduling: driven by the `defineJob({ schedule: '@every 1h' })`\n * declaration in `plugin.ts`. The queue worker bootstraps a\n * `toolkit_scheduled_jobs` row for this job at startup and its cron\n * loop (`processDueSchedules`) fires the registered handler on each\n * tick. The handler does NOT re-enqueue itself — the schedule is\n * driven by the worker, not the handler.\n */\n\nimport type { ToolkitApp } from '@murumets-ee/core'\nimport type { Logger } from '@murumets-ee/logging'\nimport { getImportsStorage } from '../storage.js'\n\nexport interface CleanupOrphanedImportsArgs {\n /** Toolkit app (provides db). */\n app: ToolkitApp\n /**\n * Bucket name the imports flow uses. Rows in `toolkit_files` are\n * filtered to this bucket so the cleanup doesn't touch media,\n * ticketing attachments, or any other consumer's uploads even if\n * they share the same Postgres instance.\n */\n bucket: string\n /**\n * Hours after which an unreferenced upload is considered orphaned.\n * Operator-grace window: should be long enough that a normal\n * upload-then-register round-trip never wipes a fresh upload, but\n * short enough that storage waste from failed uploads doesn't\n * accumulate. 2 hours is the default in the imports plugin\n * config — calibrated against the typical \"operator clicks\n * upload, walks away, comes back to check\" workflow.\n */\n ttlHours: number\n /**\n * Maximum rows to process per run. Bounded so a sudden flood of\n * orphans (e.g. after a brand-new supplier with unrecognised\n * filenames floods the queue with rejected uploads) doesn't\n * block the queue worker on a single multi-hour cleanup.\n */\n limit?: number\n /** Logger for diagnostics. */\n logger: Pick<Logger, 'debug' | 'info' | 'warn'>\n}\n\n/** Default per-run cap. Tuned against ticketing's `cleanupOrphanedUploads` (also 100). */\nconst DEFAULT_LIMIT = 100\n\n/** Hard cap on per-run limit. Protects against pathological caller input. */\nconst MAX_LIMIT = 1000\n\n/**\n * Validate the numeric inputs every cleanup helper accepts. Returns\n * the sanitised values OR throws — the queue worker logs thrown\n * errors against the scheduled-job row so a misconfigured TTL\n * doesn't silently delete every bucket file (which would happen if\n * a negative `ttlHours` reached the date arithmetic and pushed the\n * cutoff into the future).\n */\nfunction validateCleanupArgs(\n raw: { ttlOrRetention: number; ttlLabel: string; limit?: number },\n): { ttlOrRetention: number; limit: number } {\n const { ttlOrRetention, ttlLabel, limit = DEFAULT_LIMIT } = raw\n if (!Number.isFinite(ttlOrRetention) || ttlOrRetention < 0) {\n throw new Error(\n `imports cleanup: ${ttlLabel} must be a finite, non-negative number (got ${ttlOrRetention})`,\n )\n }\n if (!Number.isFinite(limit) || !Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) {\n throw new Error(\n `imports cleanup: limit must be an integer in [1, ${MAX_LIMIT}] (got ${limit})`,\n )\n }\n return { ttlOrRetention, limit }\n}\n\n/**\n * Run one cleanup pass. Returns the count of rows actually deleted\n * (which may be less than the count of orphans found if some\n * `storage.delete` calls failed — see the `warn` log inside the loop).\n */\nexport async function cleanupOrphanedImports(args: CleanupOrphanedImportsArgs): Promise<number> {\n const { app, bucket, logger } = args\n const db = app.db.readWrite\n const { ttlOrRetention: ttlHours, limit } = validateCleanupArgs({\n ttlOrRetention: args.ttlHours,\n ttlLabel: 'ttlHours',\n // `exactOptionalPropertyTypes` rejects an explicit `undefined`\n // for the optional `limit?: number` slot. Spread the field only\n // when defined so the validator's default-via-destructure kicks in.\n ...(args.limit !== undefined && { limit: args.limit }),\n })\n\n // Cross-table query: `toolkit_files` LEFT JOIN `import_run` — same\n // documented exception for cross-entity JOINs that ticketing's\n // cleanup uses. Two-table joins can't go through a single-entity\n // AdminClient (per CLAUDE.md \"Database Queries\").\n const { and, asc, eq, isNull, lt, getTableColumns } = await import('drizzle-orm')\n const { toolkitFiles } = await import('@murumets-ee/storage')\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const { ImportRun } = await import('../entities/import-run.js')\n type AnyPgColumn = import('drizzle-orm/pg-core').AnyPgColumn\n\n const importRunsTable = createAdminClient(ImportRun).getTable()\n\n // Both `toolkitFiles` and the `importRunsTable` are exported as\n // generic PgTables so direct dot-access widens to `T | undefined`.\n // Pull the column refs through `getTableColumns` (same shape as\n // ticketing's cleanup + the entity-version helper).\n const fileCols = getTableColumns(toolkitFiles) as Record<string, AnyPgColumn>\n const fileKey = fileCols.key\n const fileBucket = fileCols.bucket\n const fileCreatedAt = fileCols.createdAt\n if (!fileKey || !fileBucket || !fileCreatedAt) {\n throw new Error(\n 'cleanupOrphanedImports: toolkit_files schema missing expected columns (key/bucket/createdAt)',\n )\n }\n const importRunCols = getTableColumns(importRunsTable) as Record<string, AnyPgColumn>\n const irFilePath = importRunCols.filePath\n const irId = importRunCols.id\n if (!irFilePath || !irId) {\n throw new Error(\n 'cleanupOrphanedImports: import_run schema missing expected columns (filePath/id)',\n )\n }\n\n const cutoff = new Date(Date.now() - ttlHours * 60 * 60 * 1000)\n\n // Find toolkit_files rows in the imports bucket, older than cutoff,\n // where the storage key is NOT referenced by any import_run row.\n // ORDER BY createdAt ASC delivers strict FIFO so a sustained\n // orphan stream makes deterministic progress on the oldest rows\n // first — without the orderBy, Postgres returns rows in heap\n // order and a LIMIT 100 sweep could keep recycling the same\n // young rows while ancient orphans never get touched.\n const orphans = await db\n .select({ key: fileKey })\n .from(toolkitFiles)\n .leftJoin(importRunsTable, eq(irFilePath, fileKey))\n .where(and(eq(fileBucket, bucket), lt(fileCreatedAt, cutoff), isNull(irId)))\n .orderBy(asc(fileCreatedAt))\n .limit(limit)\n\n if (orphans.length === 0) {\n logger.debug({ bucket, ttlHours }, 'No orphaned imports uploads found')\n return 0\n }\n\n const storage = await getImportsStorage()\n let cleaned = 0\n for (const row of orphans) {\n const key = row.key as unknown as string\n try {\n await storage.delete(key)\n cleaned++\n } catch (err) {\n logger.warn({ key, err }, 'Failed to delete orphaned imports upload')\n }\n }\n\n logger.info(\n { cleaned, total: orphans.length, bucket, ttlHours },\n 'Cleaned up orphaned imports uploads',\n )\n return cleaned\n}\n\n/** Statuses considered \"done\" for the post-success/failure cleanup. */\nconst TERMINAL_IMPORT_RUN_STATUSES = ['succeeded', 'failed', 'cancelled'] as const\n\nexport interface CleanupProcessedImportsArgs {\n /** Toolkit app (provides db). */\n app: ToolkitApp\n /** Bucket name to scope the cleanup. Same semantics as `cleanupOrphanedImports`. */\n bucket: string\n /**\n * Days after which a terminal-status `import_run`'s source file is\n * deleted. Operator-grace window: long enough that an operator can\n * inspect the source file if a transform bug surfaces shortly after\n * a successful import; short enough that storage doesn't accumulate\n * months of supplier feeds.\n *\n * The plugin default is 7. Set to 0 to delete the source file\n * immediately at the next cleanup tick after the import row\n * reaches terminal status — useful when storage cost matters more\n * than forensic reprocess capability.\n */\n retentionDays: number\n /** Max rows per run. Defaults to 100 (same cap as the orphan sweep). */\n limit?: number\n logger: Pick<Logger, 'debug' | 'info' | 'warn'>\n}\n\n/**\n * Cleanup source files for `import_run`s that finished (succeeded /\n * failed / cancelled) more than `retentionDays` days ago.\n *\n * Counterpart to `cleanupOrphanedImports`: that one targets uploads\n * with NO matching `import_run` (handshake never completed); this one\n * targets uploads whose `import_run` row IS present and terminal.\n * Together they cover every \"useless storage row\" case the imports\n * flow can produce.\n *\n * Deletes ONLY the storage row (via `StorageClient.delete`); the\n * `import_run` row is kept so the History panel + audit trail still\n * surface the past run. The `import_run.filePath` value points at a\n * now-deleted key after this runs — consumers that try to re-download\n * the source file (e.g. a future \"reprocess\" button) will get a\n * `getMetadata` → null and should show \"source file unavailable\" in\n * the UI rather than crashing.\n */\nexport async function cleanupProcessedImports(args: CleanupProcessedImportsArgs): Promise<number> {\n const { app, bucket, logger } = args\n const db = app.db.readWrite\n const { ttlOrRetention: retentionDays, limit } = validateCleanupArgs({\n ttlOrRetention: args.retentionDays,\n ttlLabel: 'retentionDays',\n // `exactOptionalPropertyTypes` rejects an explicit `undefined`\n // for the optional `limit?: number` slot. Spread the field only\n // when defined so the validator's default-via-destructure kicks in.\n ...(args.limit !== undefined && { limit: args.limit }),\n })\n\n const { and, asc, eq, getTableColumns, inArray, lt } = await import('drizzle-orm')\n const { toolkitFiles } = await import('@murumets-ee/storage')\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const { ImportRun } = await import('../entities/import-run.js')\n type AnyPgColumn = import('drizzle-orm/pg-core').AnyPgColumn\n\n const importRunsTable = createAdminClient(ImportRun).getTable()\n\n const fileCols = getTableColumns(toolkitFiles) as Record<string, AnyPgColumn>\n const fileKey = fileCols.key\n const fileBucket = fileCols.bucket\n if (!fileKey || !fileBucket) {\n throw new Error(\n 'cleanupProcessedImports: toolkit_files schema missing expected columns (key/bucket)',\n )\n }\n const importRunCols = getTableColumns(importRunsTable) as Record<string, AnyPgColumn>\n const irFilePath = importRunCols.filePath\n const irStatus = importRunCols.status\n const irFinishedAt = importRunCols.finishedAt\n if (!irFilePath || !irStatus || !irFinishedAt) {\n throw new Error(\n 'cleanupProcessedImports: import_run schema missing expected columns (filePath/status/finishedAt)',\n )\n }\n\n const cutoff = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000)\n\n // INNER JOIN so we only consider keys that DO have a matching\n // import_run row — the inverse of the orphan-cleanup query. Filter\n // on terminal status + finishedAt cutoff so a long-running batch\n // doesn't get its still-being-processed file ripped out from under\n // the worker.\n //\n // `selectDistinctOn([fileKey])` is the right primitive here:\n //\n // (a) **Dedupe before LIMIT.** `toolkit_files.key` is UNIQUE\n // within its own table, but `import_run.filePath` has no\n // unique constraint — a future re-run / retry flow that\n // creates two terminal `import_run` rows pointing at the\n // same `storageKey` would make the INNER JOIN project the\n // key twice. Without dedupe in SQL, a pathological cluster\n // of 100 duplicates sharing 1 storageKey would burn the\n // LIMIT cap to clean 1 file.\n //\n // (b) **Deterministic ordering** (NOT strict global FIFO).\n // `DISTINCT ON` requires the ORDER BY to start with the\n // distinct columns (Postgres rule). So we sort by `fileKey`\n // first, then `irFinishedAt` within each key group. The\n // LIMIT then picks the first N keys in `fileKey` order —\n // i.e. alphabetical by storage path, not globally oldest by\n // finishedAt. In practice this is roughly chronological\n // anyway: storage keys are `uploads/YYYY/MM/<uuid>/...` so\n // earlier months sort first. Within a month, ordering is\n // UUID-random — but every key eventually gets cleaned\n // (no starvation), so over ~10 ticks the entire backlog\n // drains either way. A subquery-wrapped \"true global FIFO\"\n // (DISTINCT ON inner, ORDER BY finishedAt outer) is\n // possible but adds complexity for marginal benefit; the\n // practical-FIFO-via-key-prefix property is acceptable.\n const candidates = await db\n .selectDistinctOn([fileKey], { key: fileKey })\n .from(toolkitFiles)\n .innerJoin(importRunsTable, eq(irFilePath, fileKey))\n .where(\n and(\n eq(fileBucket, bucket),\n inArray(irStatus, [...TERMINAL_IMPORT_RUN_STATUSES]),\n lt(irFinishedAt, cutoff),\n ),\n )\n .orderBy(asc(fileKey), asc(irFinishedAt))\n .limit(limit)\n\n if (candidates.length === 0) {\n logger.debug({ bucket, retentionDays }, 'No processed-imports uploads ready for cleanup')\n return 0\n }\n\n const storage = await getImportsStorage()\n let cleaned = 0\n for (const row of candidates) {\n const key = row.key as unknown as string\n try {\n await storage.delete(key)\n cleaned++\n } catch (err) {\n logger.warn({ key, err }, 'Failed to delete processed-imports upload')\n }\n }\n\n logger.info(\n { cleaned, total: candidates.length, bucket, retentionDays },\n 'Cleaned up processed imports uploads',\n )\n return cleaned\n}\n"],"mappings":"4KAqEA,MAGM,EAAY,IAUlB,SAAS,EACP,EAC2C,CAC3C,GAAM,CAAE,iBAAgB,WAAU,QAAQ,KAAkB,EAC5D,GAAI,CAAC,OAAO,SAAS,CAAc,GAAK,EAAiB,EACvD,MAAU,MACR,oBAAoB,EAAS,8CAA8C,EAAe,EAC5F,EAEF,GAAI,CAAC,OAAO,SAAS,CAAK,GAAK,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,GAAK,EAAQ,EAC9E,MAAU,MACR,oDAAoD,EAAU,SAAS,EAAM,EAC/E,EAEF,MAAO,CAAE,iBAAgB,OAAM,CACjC,CAOA,eAAsB,EAAuB,EAAmD,CAC9F,GAAM,CAAE,MAAK,SAAQ,UAAW,EAC1B,EAAK,EAAI,GAAG,UACZ,CAAE,eAAgB,EAAU,SAAU,EAAoB,CAC9D,eAAgB,EAAK,SACrB,SAAU,WAIV,GAAI,EAAK,QAAU,IAAA,IAAa,CAAE,MAAO,EAAK,KAAM,CACtD,CAAC,EAMK,CAAE,MAAK,MAAK,KAAI,SAAQ,KAAI,mBAAoB,MAAM,OAAO,eAC7D,CAAE,gBAAiB,MAAM,OAAO,wBAChC,CAAE,qBAAsB,MAAM,OAAO,6BACrC,CAAE,aAAc,MAAM,OAAO,2BAA4B,CAAA,KAAA,GAAA,EAAA,CAAA,EAGzD,EAAkB,EAAk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EAAI,IAChB,GAAI,CACF,MAAM,EAAQ,OAAO,CAAG,EACxB,GACF,OAAS,EAAK,CACZ,EAAO,KAAK,CAAE,MAAK,KAAI,EAAG,2CAA2C,CACvE,CACF,CAMA,OAJA,EAAO,KACL,CAAE,UAAS,MAAO,EAAW,OAAQ,SAAQ,eAAc,EAC3D,sCACF,EACO,CACT"}
1
+ {"version":3,"file":"cleanup-DDljBe0Z.mjs","names":[],"sources":["../src/admin/cleanup.ts"],"sourcesContent":["/**\n * Cleanup orphaned imports uploads.\n *\n * Finds rows in `toolkit_files` within the configured imports bucket\n * that:\n * - were created more than `ttlHours` ago, AND\n * - have no matching `import_run.filePath` reference\n *\n * For each match, calls `StorageClient.delete(key)` — which removes\n * BOTH the adapter bytes (local file / R2 object) AND the\n * `toolkit_files` metadata row in one transactional unit (see\n * `@murumets-ee/storage` `StorageClient.delete`).\n *\n * Why this exists: the Tus upload flow stores bytes + metadata BEFORE\n * the client's downstream `register` step inserts an `import_run` row.\n * If the register call fails (validation, MIME mismatch, network\n * blip, agent abandons the workflow), the storage row is left\n * dangling. The commerce-side register handler does opportunistic\n * cleanup on its OWN failure paths, but rejections from the Zod\n * schema (before the handler runs), rejections where the agent\n * never calls register at all, AND the handler's operator-fixable\n * mapping rejections (mapping not-found / no-mapping 422 / structure\n * drift 409 — which deliberately KEEP the valid upload for a re-run)\n * all rely on this sweep.\n *\n * Scheduling: driven by the `defineJob({ schedule: '@every 1h' })`\n * declaration in `plugin.ts`. The queue worker bootstraps a\n * `toolkit_scheduled_jobs` row for this job at startup and its cron\n * loop (`processDueSchedules`) fires the registered handler on each\n * tick. The handler does NOT re-enqueue itself — the schedule is\n * driven by the worker, not the handler.\n */\n\nimport type { ToolkitApp } from '@murumets-ee/core'\nimport type { Logger } from '@murumets-ee/logging'\nimport { getImportsStorage } from '../storage.js'\n\nexport interface CleanupOrphanedImportsArgs {\n /** Toolkit app (provides db). */\n app: ToolkitApp\n /**\n * Bucket name the imports flow uses. Rows in `toolkit_files` are\n * filtered to this bucket so the cleanup doesn't touch media,\n * ticketing attachments, or any other consumer's uploads even if\n * they share the same Postgres instance.\n */\n bucket: string\n /**\n * Hours after which an unreferenced upload is considered orphaned.\n * Operator-grace window: should be long enough that a normal\n * upload-then-register round-trip never wipes a fresh upload, but\n * short enough that storage waste from failed uploads doesn't\n * accumulate. 2 hours is the default in the imports plugin\n * config — calibrated against the typical \"operator clicks\n * upload, walks away, comes back to check\" workflow.\n */\n ttlHours: number\n /**\n * Maximum rows to process per run. Bounded so a sudden flood of\n * orphans (e.g. after a brand-new supplier with unrecognised\n * filenames floods the queue with rejected uploads) doesn't\n * block the queue worker on a single multi-hour cleanup.\n */\n limit?: number\n /** Logger for diagnostics. */\n logger: Pick<Logger, 'debug' | 'info' | 'warn'>\n}\n\n/** Default per-run cap. Tuned against ticketing's `cleanupOrphanedUploads` (also 100). */\nconst DEFAULT_LIMIT = 100\n\n/** Hard cap on per-run limit. Protects against pathological caller input. */\nconst MAX_LIMIT = 1000\n\n/**\n * Validate the numeric inputs every cleanup helper accepts. Returns\n * the sanitised values OR throws — the queue worker logs thrown\n * errors against the scheduled-job row so a misconfigured TTL\n * doesn't silently delete every bucket file (which would happen if\n * a negative `ttlHours` reached the date arithmetic and pushed the\n * cutoff into the future).\n */\nfunction validateCleanupArgs(\n raw: { ttlOrRetention: number; ttlLabel: string; limit?: number },\n): { ttlOrRetention: number; limit: number } {\n const { ttlOrRetention, ttlLabel, limit = DEFAULT_LIMIT } = raw\n if (!Number.isFinite(ttlOrRetention) || ttlOrRetention < 0) {\n throw new Error(\n `imports cleanup: ${ttlLabel} must be a finite, non-negative number (got ${ttlOrRetention})`,\n )\n }\n if (!Number.isFinite(limit) || !Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) {\n throw new Error(\n `imports cleanup: limit must be an integer in [1, ${MAX_LIMIT}] (got ${limit})`,\n )\n }\n return { ttlOrRetention, limit }\n}\n\n/**\n * Run one cleanup pass. Returns the count of rows actually deleted\n * (which may be less than the count of orphans found if some\n * `storage.delete` calls failed — see the `warn` log inside the loop).\n */\nexport async function cleanupOrphanedImports(args: CleanupOrphanedImportsArgs): Promise<number> {\n const { app, bucket, logger } = args\n const db = app.db.readWrite\n const { ttlOrRetention: ttlHours, limit } = validateCleanupArgs({\n ttlOrRetention: args.ttlHours,\n ttlLabel: 'ttlHours',\n // `exactOptionalPropertyTypes` rejects an explicit `undefined`\n // for the optional `limit?: number` slot. Spread the field only\n // when defined so the validator's default-via-destructure kicks in.\n ...(args.limit !== undefined && { limit: args.limit }),\n })\n\n // Cross-table query: `toolkit_files` LEFT JOIN `import_run` — same\n // documented exception for cross-entity JOINs that ticketing's\n // cleanup uses. Two-table joins can't go through a single-entity\n // AdminClient (per CLAUDE.md \"Database Queries\").\n const { and, asc, eq, isNull, lt, getTableColumns } = await import('drizzle-orm')\n const { toolkitFiles } = await import('@murumets-ee/storage')\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const { ImportRun } = await import('../entities/import-run.js')\n type AnyPgColumn = import('drizzle-orm/pg-core').AnyPgColumn\n\n const importRunsTable = createAdminClient(ImportRun).getTable()\n\n // Both `toolkitFiles` and the `importRunsTable` are exported as\n // generic PgTables so direct dot-access widens to `T | undefined`.\n // Pull the column refs through `getTableColumns` (same shape as\n // ticketing's cleanup + the entity-version helper).\n const fileCols = getTableColumns(toolkitFiles) as Record<string, AnyPgColumn>\n const fileKey = fileCols.key\n const fileBucket = fileCols.bucket\n const fileCreatedAt = fileCols.createdAt\n if (!fileKey || !fileBucket || !fileCreatedAt) {\n throw new Error(\n 'cleanupOrphanedImports: toolkit_files schema missing expected columns (key/bucket/createdAt)',\n )\n }\n const importRunCols = getTableColumns(importRunsTable) as Record<string, AnyPgColumn>\n const irFilePath = importRunCols.filePath\n const irId = importRunCols.id\n if (!irFilePath || !irId) {\n throw new Error(\n 'cleanupOrphanedImports: import_run schema missing expected columns (filePath/id)',\n )\n }\n\n const cutoff = new Date(Date.now() - ttlHours * 60 * 60 * 1000)\n\n // Find toolkit_files rows in the imports bucket, older than cutoff,\n // where the storage key is NOT referenced by any import_run row.\n // ORDER BY createdAt ASC delivers strict FIFO so a sustained\n // orphan stream makes deterministic progress on the oldest rows\n // first — without the orderBy, Postgres returns rows in heap\n // order and a LIMIT 100 sweep could keep recycling the same\n // young rows while ancient orphans never get touched.\n const orphans = await db\n .select({ key: fileKey })\n .from(toolkitFiles)\n .leftJoin(importRunsTable, eq(irFilePath, fileKey))\n .where(and(eq(fileBucket, bucket), lt(fileCreatedAt, cutoff), isNull(irId)))\n .orderBy(asc(fileCreatedAt))\n .limit(limit)\n\n if (orphans.length === 0) {\n logger.debug({ bucket, ttlHours }, 'No orphaned imports uploads found')\n return 0\n }\n\n const storage = await getImportsStorage()\n let cleaned = 0\n for (const row of orphans) {\n const key = row.key as unknown as string\n try {\n await storage.delete(key)\n cleaned++\n } catch (err) {\n logger.warn({ key, err }, 'Failed to delete orphaned imports upload')\n }\n }\n\n logger.info(\n { cleaned, total: orphans.length, bucket, ttlHours },\n 'Cleaned up orphaned imports uploads',\n )\n return cleaned\n}\n\n/** Statuses considered \"done\" for the post-success/failure cleanup. */\nconst TERMINAL_IMPORT_RUN_STATUSES = ['succeeded', 'failed', 'cancelled'] as const\n\nexport interface CleanupProcessedImportsArgs {\n /** Toolkit app (provides db). */\n app: ToolkitApp\n /** Bucket name to scope the cleanup. Same semantics as `cleanupOrphanedImports`. */\n bucket: string\n /**\n * Days after which a terminal-status `import_run`'s source file is\n * deleted. Operator-grace window: long enough that an operator can\n * inspect the source file if a transform bug surfaces shortly after\n * a successful import; short enough that storage doesn't accumulate\n * months of supplier feeds.\n *\n * The plugin default is 7. Set to 0 to delete the source file\n * immediately at the next cleanup tick after the import row\n * reaches terminal status — useful when storage cost matters more\n * than forensic reprocess capability.\n */\n retentionDays: number\n /** Max rows per run. Defaults to 100 (same cap as the orphan sweep). */\n limit?: number\n logger: Pick<Logger, 'debug' | 'info' | 'warn'>\n}\n\n/**\n * Cleanup source files for `import_run`s that finished (succeeded /\n * failed / cancelled) more than `retentionDays` days ago.\n *\n * Counterpart to `cleanupOrphanedImports`: that one targets uploads\n * with NO matching `import_run` (handshake never completed); this one\n * targets uploads whose `import_run` row IS present and terminal.\n * Together they cover every \"useless storage row\" case the imports\n * flow can produce.\n *\n * Deletes ONLY the storage row (via `StorageClient.delete`); the\n * `import_run` row is kept so the History panel + audit trail still\n * surface the past run. The `import_run.filePath` value points at a\n * now-deleted key after this runs — consumers that try to re-download\n * the source file (e.g. a future \"reprocess\" button) will get a\n * `getMetadata` → null and should show \"source file unavailable\" in\n * the UI rather than crashing.\n */\nexport async function cleanupProcessedImports(args: CleanupProcessedImportsArgs): Promise<number> {\n const { app, bucket, logger } = args\n const db = app.db.readWrite\n const { ttlOrRetention: retentionDays, limit } = validateCleanupArgs({\n ttlOrRetention: args.retentionDays,\n ttlLabel: 'retentionDays',\n // `exactOptionalPropertyTypes` rejects an explicit `undefined`\n // for the optional `limit?: number` slot. Spread the field only\n // when defined so the validator's default-via-destructure kicks in.\n ...(args.limit !== undefined && { limit: args.limit }),\n })\n\n const { and, asc, eq, getTableColumns, inArray, lt } = await import('drizzle-orm')\n const { toolkitFiles } = await import('@murumets-ee/storage')\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const { ImportRun } = await import('../entities/import-run.js')\n type AnyPgColumn = import('drizzle-orm/pg-core').AnyPgColumn\n\n const importRunsTable = createAdminClient(ImportRun).getTable()\n\n const fileCols = getTableColumns(toolkitFiles) as Record<string, AnyPgColumn>\n const fileKey = fileCols.key\n const fileBucket = fileCols.bucket\n if (!fileKey || !fileBucket) {\n throw new Error(\n 'cleanupProcessedImports: toolkit_files schema missing expected columns (key/bucket)',\n )\n }\n const importRunCols = getTableColumns(importRunsTable) as Record<string, AnyPgColumn>\n const irFilePath = importRunCols.filePath\n const irStatus = importRunCols.status\n const irFinishedAt = importRunCols.finishedAt\n if (!irFilePath || !irStatus || !irFinishedAt) {\n throw new Error(\n 'cleanupProcessedImports: import_run schema missing expected columns (filePath/status/finishedAt)',\n )\n }\n\n const cutoff = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000)\n\n // INNER JOIN so we only consider keys that DO have a matching\n // import_run row — the inverse of the orphan-cleanup query. Filter\n // on terminal status + finishedAt cutoff so a long-running batch\n // doesn't get its still-being-processed file ripped out from under\n // the worker.\n //\n // `selectDistinctOn([fileKey])` is the right primitive here:\n //\n // (a) **Dedupe before LIMIT.** `toolkit_files.key` is UNIQUE\n // within its own table, but `import_run.filePath` has no\n // unique constraint — a future re-run / retry flow that\n // creates two terminal `import_run` rows pointing at the\n // same `storageKey` would make the INNER JOIN project the\n // key twice. Without dedupe in SQL, a pathological cluster\n // of 100 duplicates sharing 1 storageKey would burn the\n // LIMIT cap to clean 1 file.\n //\n // (b) **Deterministic ordering** (NOT strict global FIFO).\n // `DISTINCT ON` requires the ORDER BY to start with the\n // distinct columns (Postgres rule). So we sort by `fileKey`\n // first, then `irFinishedAt` within each key group. The\n // LIMIT then picks the first N keys in `fileKey` order —\n // i.e. alphabetical by storage path, not globally oldest by\n // finishedAt. In practice this is roughly chronological\n // anyway: storage keys are `uploads/YYYY/MM/<uuid>/...` so\n // earlier months sort first. Within a month, ordering is\n // UUID-random — but every key eventually gets cleaned\n // (no starvation), so over ~10 ticks the entire backlog\n // drains either way. A subquery-wrapped \"true global FIFO\"\n // (DISTINCT ON inner, ORDER BY finishedAt outer) is\n // possible but adds complexity for marginal benefit; the\n // practical-FIFO-via-key-prefix property is acceptable.\n const candidates = await db\n .selectDistinctOn([fileKey], { key: fileKey })\n .from(toolkitFiles)\n .innerJoin(importRunsTable, eq(irFilePath, fileKey))\n .where(\n and(\n eq(fileBucket, bucket),\n inArray(irStatus, [...TERMINAL_IMPORT_RUN_STATUSES]),\n lt(irFinishedAt, cutoff),\n ),\n )\n .orderBy(asc(fileKey), asc(irFinishedAt))\n .limit(limit)\n\n if (candidates.length === 0) {\n logger.debug({ bucket, retentionDays }, 'No processed-imports uploads ready for cleanup')\n return 0\n }\n\n const storage = await getImportsStorage()\n let cleaned = 0\n for (const row of candidates) {\n const key = row.key as unknown as string\n try {\n await storage.delete(key)\n cleaned++\n } catch (err) {\n logger.warn({ key, err }, 'Failed to delete processed-imports upload')\n }\n }\n\n logger.info(\n { cleaned, total: candidates.length, bucket, retentionDays },\n 'Cleaned up processed imports uploads',\n )\n return cleaned\n}\n"],"mappings":"4KAqEA,MAGM,EAAY,IAUlB,SAAS,EACP,EAC2C,CAC3C,GAAM,CAAE,iBAAgB,WAAU,QAAQ,KAAkB,EAC5D,GAAI,CAAC,OAAO,SAAS,CAAc,GAAK,EAAiB,EACvD,MAAU,MACR,oBAAoB,EAAS,8CAA8C,EAAe,EAC5F,EAEF,GAAI,CAAC,OAAO,SAAS,CAAK,GAAK,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,GAAK,EAAQ,EAC9E,MAAU,MACR,oDAAoD,EAAU,SAAS,EAAM,EAC/E,EAEF,MAAO,CAAE,iBAAgB,OAAM,CACjC,CAOA,eAAsB,EAAuB,EAAmD,CAC9F,GAAM,CAAE,MAAK,SAAQ,UAAW,EAC1B,EAAK,EAAI,GAAG,UACZ,CAAE,eAAgB,EAAU,SAAU,EAAoB,CAC9D,eAAgB,EAAK,SACrB,SAAU,WAIV,GAAI,EAAK,QAAU,IAAA,IAAa,CAAE,MAAO,EAAK,KAAM,CACtD,CAAC,EAMK,CAAE,MAAK,MAAK,KAAI,SAAQ,KAAI,mBAAoB,MAAM,OAAO,eAC7D,CAAE,gBAAiB,MAAM,OAAO,wBAChC,CAAE,qBAAsB,MAAM,OAAO,6BACrC,CAAE,aAAc,MAAM,OAAO,2BAA4B,CAAA,KAAA,GAAA,EAAA,CAAA,EAGzD,EAAkB,EAAkB,CAAS,CAAC,CAAC,SAAS,EAMxD,EAAW,EAAgB,CAAY,EACvC,EAAU,EAAS,IACnB,EAAa,EAAS,OACtB,EAAgB,EAAS,UAC/B,GAAI,CAAC,GAAW,C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MA,OAJA,EAAO,KACL,CAAE,UAAS,MAAO,EAAW,OAAQ,SAAQ,eAAc,EAC3D,sCACF,EACO,CACT"}
@@ -0,0 +1,2 @@
1
+ import{c as e,s as t}from"./parts-schema-QqLci_Pl.mjs";import{z as n}from"zod";const r=n.discriminatedUnion(`op`,[n.object({op:n.literal(`trim`)}),n.object({op:n.literal(`uppercase`)}),n.object({op:n.literal(`lowercase`)}),n.object({op:n.literal(`stripPrefix`),value:n.string().min(1).max(32)}),n.object({op:n.literal(`stripSuffix`),value:n.string().min(1).max(32)}),n.object({op:n.literal(`defaultIfEmpty`),value:n.string().max(255)}),n.object({op:n.literal(`number`)}),n.object({op:n.literal(`europeanNumber`)})]),i=[{op:`trim`,label:`Trim whitespace`,hasValue:!1},{op:`uppercase`,label:`Uppercase`,hasValue:!1},{op:`lowercase`,label:`Lowercase`,hasValue:!1},{op:`stripPrefix`,label:`Strip prefix`,hasValue:!0},{op:`stripSuffix`,label:`Strip suffix`,hasValue:!0},{op:`defaultIfEmpty`,label:`Default if empty`,hasValue:!0},{op:`number`,label:`Parse number (uses number format)`,hasValue:!1},{op:`europeanNumber`,label:`Parse European number (1.234,56)`,hasValue:!1}];function a(n,r,i){let a=n;for(let n of r)if(typeof a==`string`)switch(n.op){case`trim`:a=a.trim();break;case`uppercase`:a=a.toUpperCase();break;case`lowercase`:a=a.toLowerCase();break;case`stripPrefix`:a=a.startsWith(n.value)?a.slice(n.value.length):a;break;case`stripSuffix`:a=a.endsWith(n.value)?a.slice(0,a.length-n.value.length):a;break;case`defaultIfEmpty`:a=a.trim()===``?n.value:a;break;case`number`:a=e(a,i);break;case`europeanNumber`:a=t(a);break}return a}const o=n.object({source:n.string().min(1).max(256),target:n.string().min(1).max(64),transforms:n.array(r).default([])}),s=n.object({decimal:n.enum([`.`,`,`]),thousands:n.enum([``,`.`,`,`,` `])}).refine(e=>e.decimal!==e.thousands,{message:`decimal and thousands separators must differ`,path:[`thousands`]}),c=n.object({targetSchema:n.string().min(1).max(64),numberFormat:s,columnMap:n.array(o)});function l(e,t){let n=new Set(t.fields.map(e=>e.key));for(let r of e.columnMap)if(!n.has(r.target))throw Error(`Mapping target "${r.target}" is not a field of schema "${t.name}" (allowed: ${[...n].join(`, `)})`);let r=e.numberFormat,i=e.columnMap;return async(e,n)=>{let o={};for(let t of i){let n=e[t.source]??``;o[t.target]=a(n,t.transforms,r)}return t.buildRow(o,{importRunId:n.importRunId,rowNumber:n.rowNumber,params:n.params})}}function u(e,t){let n=e.filter(e=>e.active);if(t!==null){let e=n.find(e=>e.brandId===t);if(e)return e}return n.find(e=>e.brandId===null)}export{u as a,r as c,s as i,o as n,i as o,c as r,a as s,l as t};
2
+ //# sourceMappingURL=engine-B0ExE4jL.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine-B0ExE4jL.mjs","names":[],"sources":["../src/mapping/column-transforms.ts","../src/mapping/engine.ts"],"sourcesContent":["/**\n * Per-column transforms for the mapping engine (commerce-demo phase 02, D005).\n *\n * A **bounded, enum-keyed** library of cleaning operations applied to a source\n * cell before it lands on a target field. This is the security model from R02:\n * transforms are declarative operations with Zod-validated params, NEVER\n * user-supplied executable code. The carmaker feed's logic decomposes exactly\n * into `stripPrefix` + `europeanNumber` (+ the schema's code-normalize).\n *\n * Number-producing ops (`number`, `europeanNumber`) are TERMINAL by convention —\n * they convert the string cell into `number | null`; string ops after them are\n * no-ops. The engine reads the result as `string | number | null` and hands it to\n * the target schema's `buildRow`.\n */\n\nimport { z } from 'zod'\nimport { type NumberFormat, parseEuropeanNumber, parseNumber } from './number-format.js'\n\n/** Max length for a strip-affix value — bounds the param, no ReDoS surface. */\nconst MAX_AFFIX_LEN = 32\n\n/**\n * Discriminated union of bounded transform ops. The TS type is INFERRED from\n * this schema so there's a single source of truth + the snapshot is validated at\n * the engine boundary.\n */\nexport const columnTransformSchema = z.discriminatedUnion('op', [\n z.object({ op: z.literal('trim') }),\n z.object({ op: z.literal('uppercase') }),\n z.object({ op: z.literal('lowercase') }),\n z.object({ op: z.literal('stripPrefix'), value: z.string().min(1).max(MAX_AFFIX_LEN) }),\n z.object({ op: z.literal('stripSuffix'), value: z.string().min(1).max(MAX_AFFIX_LEN) }),\n z.object({ op: z.literal('defaultIfEmpty'), value: z.string().max(255) }),\n z.object({ op: z.literal('number') }),\n z.object({ op: z.literal('europeanNumber') }),\n])\n\n/** One declarative cleaning operation. */\nexport type ColumnTransform = z.infer<typeof columnTransformSchema>\n\n/** The discriminator literal of a {@link ColumnTransform}. */\nexport type ColumnTransformOp = ColumnTransform['op']\n\n/** UI descriptor for one transform op: its key, a label, and whether it takes a `value` param. */\nexport interface ColumnTransformOpDescriptor {\n op: ColumnTransformOp\n label: string\n /** `true` → the op carries a `value` string param (`stripPrefix`/`stripSuffix`/`defaultIfEmpty`). */\n hasValue: boolean\n}\n\n/**\n * Static catalogue of the available per-column transform ops, for the PR3 wizard's\n * transform picker. The ORDER is the suggested UI order; `number`/`europeanNumber`\n * are terminal (see {@link applyColumnTransforms}) and labelled accordingly.\n */\nexport const COLUMN_TRANSFORM_OPS: readonly ColumnTransformOpDescriptor[] = [\n { op: 'trim', label: 'Trim whitespace', hasValue: false },\n { op: 'uppercase', label: 'Uppercase', hasValue: false },\n { op: 'lowercase', label: 'Lowercase', hasValue: false },\n { op: 'stripPrefix', label: 'Strip prefix', hasValue: true },\n { op: 'stripSuffix', label: 'Strip suffix', hasValue: true },\n { op: 'defaultIfEmpty', label: 'Default if empty', hasValue: true },\n { op: 'number', label: 'Parse number (uses number format)', hasValue: false },\n { op: 'europeanNumber', label: 'Parse European number (1.234,56)', hasValue: false },\n] as const\n\n/** The value a column carries as it moves through the transform pipeline. */\nexport type CellValue = string | number | null\n\n/**\n * Apply a transform pipeline to a raw cell, left-to-right. String ops act on the\n * string value; the `number` / `europeanNumber` ops are terminal (convert to\n * `number | null`, with `NaN` for a malformed numeric — the schema's `buildRow`\n * surfaces that as a row error). Once the value is non-string, later string ops\n * pass through unchanged.\n */\nexport function applyColumnTransforms(\n raw: string,\n transforms: readonly ColumnTransform[],\n format: NumberFormat,\n): CellValue {\n let value: CellValue = raw\n for (const t of transforms) {\n if (typeof value !== 'string') {\n // A prior number op already produced number|null — string ops are no-ops\n // past this point (number ops are terminal by convention).\n continue\n }\n switch (t.op) {\n case 'trim':\n value = value.trim()\n break\n case 'uppercase':\n value = value.toUpperCase()\n break\n case 'lowercase':\n value = value.toLowerCase()\n break\n case 'stripPrefix':\n value = value.startsWith(t.value) ? value.slice(t.value.length) : value\n break\n case 'stripSuffix':\n value = value.endsWith(t.value) ? value.slice(0, value.length - t.value.length) : value\n break\n case 'defaultIfEmpty':\n value = value.trim() === '' ? t.value : value\n break\n case 'number':\n value = parseNumber(value, format)\n break\n case 'europeanNumber':\n value = parseEuropeanNumber(value)\n break\n }\n }\n return value\n}\n","/**\n * Mapping engine (commerce-demo phase 02, D005).\n *\n * Turns a declarative `MappingConfig` (which source column → which whitelisted\n * target field, plus per-column transforms + a number format) into a\n * `RowTransform` the existing runner drives. The pipeline per row is:\n *\n * raw cells → applyColumnTransforms(per column) → MappedRow → schema.buildRow\n *\n * Two whitelists guard the boundary (R02 injection model): the `target_schema`\n * name resolves to a registered `TargetSchema`, and every `column_map[].target`\n * must be one of that schema's fields — enforced here at build time (throws),\n * before any row is processed.\n *\n * `resolveMapping` implements the D002 most-specific ladder used at enqueue time\n * to pick the `(supplier, brand)` mapping over the `(supplier, NULL)` default.\n */\n\nimport { z } from 'zod'\nimport type { RowTransform } from '../transform.js'\nimport { applyColumnTransforms, columnTransformSchema } from './column-transforms.js'\nimport type { MappedRow, TargetSchema } from './target-schema.js'\n\n/** One column-map entry. `target` is checked against the schema whitelist separately. */\nexport const mappingColumnSchema = z.object({\n /** Source header name (or positional index string for headerless feeds). */\n source: z.string().min(1).max(256),\n /** Target field key — must be in the chosen schema's whitelist (enforced in `buildMappingTransform`). */\n target: z.string().min(1).max(64),\n /** Ordered per-column cleaning ops. Defaults to none. */\n transforms: z.array(columnTransformSchema).default([]),\n})\nexport type MappingColumn = z.infer<typeof mappingColumnSchema>\n\n/**\n * Decimal + thousands separators for the `number` transform.\n *\n * The two MUST differ: `parseNumber` strips every thousands separator FIRST, so a\n * config where `decimal === thousands` (e.g. both `,`) would strip the decimal\n * separator too — `326,88` → `32688`, a silent 100× error rather than a row error.\n * Rejected at save (`assertValidMapping`) AND at run (`mappingConfigSchema`).\n */\nexport const numberFormatSchema = z\n .object({\n decimal: z.enum(['.', ',']),\n thousands: z.enum(['', '.', ',', ' ']),\n })\n .refine((f) => f.decimal !== f.thousands, {\n message: 'decimal and thousands separators must differ',\n path: ['thousands'],\n })\n\n/**\n * The portable mapping config — the subset of an `import_mapping` row the engine\n * needs. The enqueuer SNAPSHOTS this into `import_run.params.mapping` so a run\n * records the exact mapping it used (reproducible) and the transform stays a pure\n * function of `(row, ctx)` with no per-row DB I/O.\n */\nexport const mappingConfigSchema = z.object({\n targetSchema: z.string().min(1).max(64),\n numberFormat: numberFormatSchema,\n columnMap: z.array(mappingColumnSchema),\n})\nexport type MappingConfig = z.infer<typeof mappingConfigSchema>\n\n/**\n * Build a `RowTransform` from a validated mapping config + its target schema.\n *\n * Throws at BUILD time if any `column_map` target is outside the schema's\n * whitelist (deny-by-default, R02) — so a misconfigured mapping fails loudly\n * before a single row is written, rather than silently dropping a column.\n *\n * Generic over the schema's output type, so passing the concrete `partsSchema`\n * yields a `RowTransform<PartsDocument>` (the carmaker path keeps its typing); the\n * registry-resolved `TargetSchema<unknown>` yields `RowTransform<unknown>` (the\n * `commerce:mapping` path, where the output type isn't known until runtime).\n */\nexport function buildMappingTransform<TOut>(\n config: MappingConfig,\n schema: TargetSchema<TOut>,\n): RowTransform<TOut> {\n const allowed = new Set(schema.fields.map((f) => f.key))\n for (const col of config.columnMap) {\n if (!allowed.has(col.target)) {\n throw new Error(\n `Mapping target \"${col.target}\" is not a field of schema \"${schema.name}\" ` +\n `(allowed: ${[...allowed].join(', ')})`,\n )\n }\n }\n\n const format = config.numberFormat\n const columnMap = config.columnMap\n\n return async (row, ctx) => {\n const mapped: MappedRow = {}\n for (const col of columnMap) {\n // `source` is read as data from the parsed row map — never used as a key\n // into framework objects (R02). A missing source column yields `''`.\n const raw = row[col.source] ?? ''\n mapped[col.target] = applyColumnTransforms(raw, col.transforms, format)\n }\n return schema.buildRow(mapped, {\n importRunId: ctx.importRunId,\n rowNumber: ctx.rowNumber,\n params: ctx.params,\n })\n }\n}\n\n/** The minimal shape `resolveMapping` needs — satisfied by an `import_mapping` row or a test fixture. */\nexport interface ResolvableMapping {\n /** `null` = supplier default (applies to all brands). */\n brandId: string | null\n active: boolean\n}\n\n/**\n * Most-specific resolution (D002 ladder): given the candidate mappings for one\n * `(supplier, target_schema)` and the requested brand, return the brand-specific\n * mapping if one exists, else the supplier-default (`brandId === null`). Inactive\n * mappings are skipped. Returns `undefined` when nothing matches.\n */\nexport function resolveMapping<M extends ResolvableMapping>(\n mappings: readonly M[],\n brandId: string | null,\n): M | undefined {\n const active = mappings.filter((m) => m.active)\n if (brandId !== null) {\n const specific = active.find((m) => m.brandId === brandId)\n if (specific) return specific\n }\n return active.find((m) => m.brandId === null)\n}\n"],"mappings":"+EAmBA,MAOa,EAAwB,EAAE,mBAAmB,KAAM,CAC9D,EAAE,OAAO,CAAE,GAAI,EAAE,QAAQ,MAAM,CAAE,CAAC,EAClC,EAAE,OAAO,CAAE,GAAI,EAAE,QAAQ,WAAW,CAAE,CAAC,EACvC,EAAE,OAAO,CAAE,GAAI,EAAE,QAAQ,WAAW,CAAE,CAAC,EACvC,EAAE,OAAO,CAAE,GAAI,EAAE,QAAQ,aAAa,EAAG,MAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAa,CAAE,CAAC,EACtF,EAAE,OAAO,CAAE,GAAI,EAAE,QAAQ,aAAa,EAAG,MAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAa,CAAE,CAAC,EACtF,EAAE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Y,CAAM,CACxE,CACA,OAAO,EAAO,SAAS,EAAQ,CAC7B,YAAa,EAAI,YACjB,UAAW,EAAI,UACf,OAAQ,EAAI,MACd,CAAC,CACH,CACF,CAeA,SAAgB,EACd,EACA,EACe,CACf,IAAM,EAAS,EAAS,OAAQ,GAAM,EAAE,MAAM,EAC9C,GAAI,IAAY,KAAM,CACpB,IAAM,EAAW,EAAO,KAAM,GAAM,EAAE,UAAY,CAAO,EACzD,GAAI,EAAU,OAAO,CACvB,CACA,OAAO,EAAO,KAAM,GAAM,EAAE,UAAY,IAAI,CAC9C"}
@@ -1,6 +1,5 @@
1
- import { r as RowResult } from "./transform-CABBaYmF.mjs";
1
+ import { i as RowTransform, r as RowResult } from "./transform-WeiiLdYP.mjs";
2
2
  import { z } from "zod";
3
- import { PartsDocument } from "@murumets-ee/search-elasticsearch";
4
3
 
5
4
  //#region src/mapping/number-format.d.ts
6
5
  /**
@@ -250,60 +249,361 @@ declare function rowError(errorType: string, message: string, field: string): {
250
249
  };
251
250
  };
252
251
  //#endregion
253
- //#region src/mapping/parts-schema.d.ts
252
+ //#region src/mapping/engine.d.ts
253
+ /** One column-map entry. `target` is checked against the schema whitelist separately. */
254
+ declare const mappingColumnSchema: z.ZodObject<{
255
+ /** Source header name (or positional index string for headerless feeds). */source: z.ZodString; /** Target field key — must be in the chosen schema's whitelist (enforced in `buildMappingTransform`). */
256
+ target: z.ZodString; /** Ordered per-column cleaning ops. Defaults to none. */
257
+ transforms: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<"op", [z.ZodObject<{
258
+ op: z.ZodLiteral<"trim">;
259
+ }, "strip", z.ZodTypeAny, {
260
+ op: "trim";
261
+ }, {
262
+ op: "trim";
263
+ }>, z.ZodObject<{
264
+ op: z.ZodLiteral<"uppercase">;
265
+ }, "strip", z.ZodTypeAny, {
266
+ op: "uppercase";
267
+ }, {
268
+ op: "uppercase";
269
+ }>, z.ZodObject<{
270
+ op: z.ZodLiteral<"lowercase">;
271
+ }, "strip", z.ZodTypeAny, {
272
+ op: "lowercase";
273
+ }, {
274
+ op: "lowercase";
275
+ }>, z.ZodObject<{
276
+ op: z.ZodLiteral<"stripPrefix">;
277
+ value: z.ZodString;
278
+ }, "strip", z.ZodTypeAny, {
279
+ value: string;
280
+ op: "stripPrefix";
281
+ }, {
282
+ value: string;
283
+ op: "stripPrefix";
284
+ }>, z.ZodObject<{
285
+ op: z.ZodLiteral<"stripSuffix">;
286
+ value: z.ZodString;
287
+ }, "strip", z.ZodTypeAny, {
288
+ value: string;
289
+ op: "stripSuffix";
290
+ }, {
291
+ value: string;
292
+ op: "stripSuffix";
293
+ }>, z.ZodObject<{
294
+ op: z.ZodLiteral<"defaultIfEmpty">;
295
+ value: z.ZodString;
296
+ }, "strip", z.ZodTypeAny, {
297
+ value: string;
298
+ op: "defaultIfEmpty";
299
+ }, {
300
+ value: string;
301
+ op: "defaultIfEmpty";
302
+ }>, z.ZodObject<{
303
+ op: z.ZodLiteral<"number">;
304
+ }, "strip", z.ZodTypeAny, {
305
+ op: "number";
306
+ }, {
307
+ op: "number";
308
+ }>, z.ZodObject<{
309
+ op: z.ZodLiteral<"europeanNumber">;
310
+ }, "strip", z.ZodTypeAny, {
311
+ op: "europeanNumber";
312
+ }, {
313
+ op: "europeanNumber";
314
+ }>]>, "many">>;
315
+ }, "strip", z.ZodTypeAny, {
316
+ source: string;
317
+ target: string;
318
+ transforms: ({
319
+ op: "trim";
320
+ } | {
321
+ op: "uppercase";
322
+ } | {
323
+ op: "lowercase";
324
+ } | {
325
+ value: string;
326
+ op: "stripPrefix";
327
+ } | {
328
+ value: string;
329
+ op: "stripSuffix";
330
+ } | {
331
+ value: string;
332
+ op: "defaultIfEmpty";
333
+ } | {
334
+ op: "number";
335
+ } | {
336
+ op: "europeanNumber";
337
+ })[];
338
+ }, {
339
+ source: string;
340
+ target: string;
341
+ transforms?: ({
342
+ op: "trim";
343
+ } | {
344
+ op: "uppercase";
345
+ } | {
346
+ op: "lowercase";
347
+ } | {
348
+ value: string;
349
+ op: "stripPrefix";
350
+ } | {
351
+ value: string;
352
+ op: "stripSuffix";
353
+ } | {
354
+ value: string;
355
+ op: "defaultIfEmpty";
356
+ } | {
357
+ op: "number";
358
+ } | {
359
+ op: "europeanNumber";
360
+ })[] | undefined;
361
+ }>;
362
+ type MappingColumn = z.infer<typeof mappingColumnSchema>;
254
363
  /**
255
- * Params the `parts` schema reads from the run — the per-(supplier, brand)
256
- * identity resolved at enqueue time. Identical to the carmaker transform's params
257
- * (the carmaker path re-exports this), so brand/supplier injection + the optional
258
- * supplier code-prefix strip are validated the same way everywhere.
364
+ * Decimal + thousands separators for the `number` transform.
365
+ *
366
+ * The two MUST differ: `parseNumber` strips every thousands separator FIRST, so a
367
+ * config where `decimal === thousands` (e.g. both `,`) would strip the decimal
368
+ * separator too — `326,88` → `32688`, a silent 100× error rather than a row error.
369
+ * Rejected at save (`assertValidMapping`) AND at run (`mappingConfigSchema`).
259
370
  */
260
- declare const partsParamsSchema: z.ZodObject<{
261
- brandId: z.ZodString;
262
- brandSlug: z.ZodString;
263
- supplierId: z.ZodString;
264
- supplierDisplayName: z.ZodString;
265
- importBatchId: z.ZodString; /** Supplier code prefix to strip before normalization (e.g. `'ME-'`). Optional. */
266
- supplierCodePrefix: z.ZodOptional<z.ZodString>;
267
- /**
268
- * Purchase-discount map for this supplier (commerce-demo PR4, phase 03 §2):
269
- * `{ groupCode → discountPct }`, resolved from `supplier_discount_group` at
270
- * enqueue time and snapshotted onto the run so `buildRow` stays a pure function
271
- * (no per-row DB I/O; `@murumets-ee/imports` stays a leaf). Absent for direct-net
272
- * suppliers + the carmaker path. Percentages are 0–100 (the entity constrains them).
273
- */
274
- discountGroups: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
371
+ declare const numberFormatSchema: z.ZodEffects<z.ZodObject<{
372
+ decimal: z.ZodEnum<[".", ","]>;
373
+ thousands: z.ZodEnum<["", ".", ",", " "]>;
275
374
  }, "strip", z.ZodTypeAny, {
276
- supplierId: string;
277
- brandId: string;
278
- brandSlug: string;
279
- supplierDisplayName: string;
280
- importBatchId: string;
281
- supplierCodePrefix?: string | undefined;
282
- discountGroups?: Record<string, number> | undefined;
375
+ decimal: "." | ",";
376
+ thousands: "" | "." | "," | " ";
377
+ }, {
378
+ decimal: "." | ",";
379
+ thousands: "" | "." | "," | " ";
380
+ }>, {
381
+ decimal: "." | ",";
382
+ thousands: "" | "." | "," | " ";
283
383
  }, {
284
- supplierId: string;
285
- brandId: string;
286
- brandSlug: string;
287
- supplierDisplayName: string;
288
- importBatchId: string;
289
- supplierCodePrefix?: string | undefined;
290
- discountGroups?: Record<string, number> | undefined;
384
+ decimal: "." | ",";
385
+ thousands: "" | "." | "," | " ";
291
386
  }>;
292
- type PartsParams = z.infer<typeof partsParamsSchema>;
293
387
  /**
294
- * Uppercase + dash/whitespace-stripped (D2). `*`/other chars are preserved (e.g. `*S`
295
- * markers). EXPORTED so downstream consumers that key off `code_normalized` — the
296
- * replacement editor (commerce-demo PR6), which normalizes admin-typed codes before
297
- * matching members — use the IDENTICAL rule (R05 Q7: normalization must match across
298
- * every code source or merge-on-collision silently fails to collide).
388
+ * The portable mapping config — the subset of an `import_mapping` row the engine
389
+ * needs. The enqueuer SNAPSHOTS this into `import_run.params.mapping` so a run
390
+ * records the exact mapping it used (reproducible) and the transform stays a pure
391
+ * function of `(row, ctx)` with no per-row DB I/O.
299
392
  */
300
- declare function normalizeCode(s: string): string;
393
+ declare const mappingConfigSchema: z.ZodObject<{
394
+ targetSchema: z.ZodString;
395
+ numberFormat: z.ZodEffects<z.ZodObject<{
396
+ decimal: z.ZodEnum<[".", ","]>;
397
+ thousands: z.ZodEnum<["", ".", ",", " "]>;
398
+ }, "strip", z.ZodTypeAny, {
399
+ decimal: "." | ",";
400
+ thousands: "" | "." | "," | " ";
401
+ }, {
402
+ decimal: "." | ",";
403
+ thousands: "" | "." | "," | " ";
404
+ }>, {
405
+ decimal: "." | ",";
406
+ thousands: "" | "." | "," | " ";
407
+ }, {
408
+ decimal: "." | ",";
409
+ thousands: "" | "." | "," | " ";
410
+ }>;
411
+ columnMap: z.ZodArray<z.ZodObject<{
412
+ /** Source header name (or positional index string for headerless feeds). */source: z.ZodString; /** Target field key — must be in the chosen schema's whitelist (enforced in `buildMappingTransform`). */
413
+ target: z.ZodString; /** Ordered per-column cleaning ops. Defaults to none. */
414
+ transforms: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<"op", [z.ZodObject<{
415
+ op: z.ZodLiteral<"trim">;
416
+ }, "strip", z.ZodTypeAny, {
417
+ op: "trim";
418
+ }, {
419
+ op: "trim";
420
+ }>, z.ZodObject<{
421
+ op: z.ZodLiteral<"uppercase">;
422
+ }, "strip", z.ZodTypeAny, {
423
+ op: "uppercase";
424
+ }, {
425
+ op: "uppercase";
426
+ }>, z.ZodObject<{
427
+ op: z.ZodLiteral<"lowercase">;
428
+ }, "strip", z.ZodTypeAny, {
429
+ op: "lowercase";
430
+ }, {
431
+ op: "lowercase";
432
+ }>, z.ZodObject<{
433
+ op: z.ZodLiteral<"stripPrefix">;
434
+ value: z.ZodString;
435
+ }, "strip", z.ZodTypeAny, {
436
+ value: string;
437
+ op: "stripPrefix";
438
+ }, {
439
+ value: string;
440
+ op: "stripPrefix";
441
+ }>, z.ZodObject<{
442
+ op: z.ZodLiteral<"stripSuffix">;
443
+ value: z.ZodString;
444
+ }, "strip", z.ZodTypeAny, {
445
+ value: string;
446
+ op: "stripSuffix";
447
+ }, {
448
+ value: string;
449
+ op: "stripSuffix";
450
+ }>, z.ZodObject<{
451
+ op: z.ZodLiteral<"defaultIfEmpty">;
452
+ value: z.ZodString;
453
+ }, "strip", z.ZodTypeAny, {
454
+ value: string;
455
+ op: "defaultIfEmpty";
456
+ }, {
457
+ value: string;
458
+ op: "defaultIfEmpty";
459
+ }>, z.ZodObject<{
460
+ op: z.ZodLiteral<"number">;
461
+ }, "strip", z.ZodTypeAny, {
462
+ op: "number";
463
+ }, {
464
+ op: "number";
465
+ }>, z.ZodObject<{
466
+ op: z.ZodLiteral<"europeanNumber">;
467
+ }, "strip", z.ZodTypeAny, {
468
+ op: "europeanNumber";
469
+ }, {
470
+ op: "europeanNumber";
471
+ }>]>, "many">>;
472
+ }, "strip", z.ZodTypeAny, {
473
+ source: string;
474
+ target: string;
475
+ transforms: ({
476
+ op: "trim";
477
+ } | {
478
+ op: "uppercase";
479
+ } | {
480
+ op: "lowercase";
481
+ } | {
482
+ value: string;
483
+ op: "stripPrefix";
484
+ } | {
485
+ value: string;
486
+ op: "stripSuffix";
487
+ } | {
488
+ value: string;
489
+ op: "defaultIfEmpty";
490
+ } | {
491
+ op: "number";
492
+ } | {
493
+ op: "europeanNumber";
494
+ })[];
495
+ }, {
496
+ source: string;
497
+ target: string;
498
+ transforms?: ({
499
+ op: "trim";
500
+ } | {
501
+ op: "uppercase";
502
+ } | {
503
+ op: "lowercase";
504
+ } | {
505
+ value: string;
506
+ op: "stripPrefix";
507
+ } | {
508
+ value: string;
509
+ op: "stripSuffix";
510
+ } | {
511
+ value: string;
512
+ op: "defaultIfEmpty";
513
+ } | {
514
+ op: "number";
515
+ } | {
516
+ op: "europeanNumber";
517
+ })[] | undefined;
518
+ }>, "many">;
519
+ }, "strip", z.ZodTypeAny, {
520
+ targetSchema: string;
521
+ numberFormat: {
522
+ decimal: "." | ",";
523
+ thousands: "" | "." | "," | " ";
524
+ };
525
+ columnMap: {
526
+ source: string;
527
+ target: string;
528
+ transforms: ({
529
+ op: "trim";
530
+ } | {
531
+ op: "uppercase";
532
+ } | {
533
+ op: "lowercase";
534
+ } | {
535
+ value: string;
536
+ op: "stripPrefix";
537
+ } | {
538
+ value: string;
539
+ op: "stripSuffix";
540
+ } | {
541
+ value: string;
542
+ op: "defaultIfEmpty";
543
+ } | {
544
+ op: "number";
545
+ } | {
546
+ op: "europeanNumber";
547
+ })[];
548
+ }[];
549
+ }, {
550
+ targetSchema: string;
551
+ numberFormat: {
552
+ decimal: "." | ",";
553
+ thousands: "" | "." | "," | " ";
554
+ };
555
+ columnMap: {
556
+ source: string;
557
+ target: string;
558
+ transforms?: ({
559
+ op: "trim";
560
+ } | {
561
+ op: "uppercase";
562
+ } | {
563
+ op: "lowercase";
564
+ } | {
565
+ value: string;
566
+ op: "stripPrefix";
567
+ } | {
568
+ value: string;
569
+ op: "stripSuffix";
570
+ } | {
571
+ value: string;
572
+ op: "defaultIfEmpty";
573
+ } | {
574
+ op: "number";
575
+ } | {
576
+ op: "europeanNumber";
577
+ })[] | undefined;
578
+ }[];
579
+ }>;
580
+ type MappingConfig = z.infer<typeof mappingConfigSchema>;
581
+ /**
582
+ * Build a `RowTransform` from a validated mapping config + its target schema.
583
+ *
584
+ * Throws at BUILD time if any `column_map` target is outside the schema's
585
+ * whitelist (deny-by-default, R02) — so a misconfigured mapping fails loudly
586
+ * before a single row is written, rather than silently dropping a column.
587
+ *
588
+ * Generic over the schema's output type, so passing the concrete `partsSchema`
589
+ * yields a `RowTransform<PartsDocument>` (the carmaker path keeps its typing); the
590
+ * registry-resolved `TargetSchema<unknown>` yields `RowTransform<unknown>` (the
591
+ * `commerce:mapping` path, where the output type isn't known until runtime).
592
+ */
593
+ declare function buildMappingTransform<TOut>(config: MappingConfig, schema: TargetSchema<TOut>): RowTransform<TOut>;
594
+ /** The minimal shape `resolveMapping` needs — satisfied by an `import_mapping` row or a test fixture. */
595
+ interface ResolvableMapping {
596
+ /** `null` = supplier default (applies to all brands). */
597
+ brandId: string | null;
598
+ active: boolean;
599
+ }
301
600
  /**
302
- * The `parts` schema. `buildRow` THROWS on invalid run params (so a misconfigured
303
- * run surfaces as a transform error, matching the carmaker contract) and returns a
304
- * typed `RowResult` per row otherwise.
601
+ * Most-specific resolution (D002 ladder): given the candidate mappings for one
602
+ * `(supplier, target_schema)` and the requested brand, return the brand-specific
603
+ * mapping if one exists, else the supplier-default (`brandId === null`). Inactive
604
+ * mappings are skipped. Returns `undefined` when nothing matches.
305
605
  */
306
- declare const partsSchema: TargetSchema<PartsDocument>;
606
+ declare function resolveMapping<M extends ResolvableMapping>(mappings: readonly M[], brandId: string | null): M | undefined;
307
607
  //#endregion
308
- export { toMinorUnits as C, parseNumber as S, ColumnTransformOpDescriptor as _, BuildRowContext as a, NumberFormat as b, TargetField as c, rowError as d, textOrNull as f, ColumnTransformOp as g, ColumnTransform as h, partsSchema as i, TargetSchema as l, CellValue as m, normalizeCode as n, MappedRow as o, COLUMN_TRANSFORM_OPS as p, partsParamsSchema as r, NumberOutcome as s, PartsParams as t, numberCell as u, applyColumnTransforms as v, parseEuropeanNumber as x, columnTransformSchema as y };
309
- //# sourceMappingURL=parts-schema-x8_GRYWc.d.mts.map
608
+ export { columnTransformSchema as C, toMinorUnits as D, parseNumber as E, applyColumnTransforms as S, parseEuropeanNumber as T, COLUMN_TRANSFORM_OPS as _, mappingColumnSchema as a, ColumnTransformOp as b, resolveMapping as c, NumberOutcome as d, TargetField as f, textOrNull as g, rowError as h, buildMappingTransform as i, BuildRowContext as l, numberCell as m, MappingConfig as n, mappingConfigSchema as o, TargetSchema as p, ResolvableMapping as r, numberFormatSchema as s, MappingColumn as t, MappedRow as u, CellValue as v, NumberFormat as w, ColumnTransformOpDescriptor as x, ColumnTransform as y };
609
+ //# sourceMappingURL=engine-DiM4r2Vj.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine-DiM4r2Vj.d.mts","names":[],"sources":["../src/mapping/number-format.ts","../src/mapping/column-transforms.ts","../src/mapping/target-schema.ts","../src/mapping/engine.ts"],"mappings":";;;;;;;;AAqBA;;;;AAIW;AAsBX;;;;;;;;AAA6D;AA+B7D;;UAzDiB,YAAA;EAyDyB;EAvDxC,OAAA;EAiFc;EA/Ed,SAAS;AAAA;;AA+EkC;;;;AC9E7C;;;;;;;;;;;iBDqBgB,WAAA,CAAY,GAAA,UAAa,MAAA,EAAQ,YAAY;;;;;;;;iBA+B7C,YAAA,CAAa,KAAa;;;;;;;;;;;;;;;;;;;;;;;iBA0B1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