@open-mercato/core 0.6.7-develop.6756.1.5300c41145 → 0.6.7-develop.6758.1.697eade236

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.
@@ -135,7 +135,8 @@ const historyEntrySchema = z.object({
135
135
  statusFrom: z.string().nullable().optional(),
136
136
  statusTo: z.string().nullable().optional(),
137
137
  documentKind: z.enum(["order", "quote"]).optional(),
138
- commandId: z.string().optional()
138
+ commandId: z.string().optional(),
139
+ changedFields: z.array(z.string()).optional()
139
140
  }).optional()
140
141
  });
141
142
  const documentHistoryResponseSchema = z.object({
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/modules/sales/api/document-history/route.ts"],
4
- "sourcesContent": ["import { z } from 'zod'\nimport { ActionLogService } from '@open-mercato/core/modules/audit_logs/services/actionLogService'\nimport { loadAuditLogDisplayMaps } from '@open-mercato/core/modules/audit_logs/api/audit-logs/display'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { resolveOrganizationScopeFilter } from '@open-mercato/core/modules/directory/utils/organizationScopeFilter'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { NextResponse } from 'next/server'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { buildHistoryEntries } from '../../lib/historyHelpers'\nimport { SalesNote } from '../../data/entities'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('sales')\n\n// Spec: SPEC-006-2026-01-23-order-status-history\n\nexport const metadata = {\n GET: { requireAuth: true },\n}\n\nconst querySchema = z.object({\n kind: z.enum(['order', 'quote']),\n id: z.string().uuid(),\n limit: z.coerce.number().min(1).max(100).default(50),\n before: z.string().optional(),\n after: z.string().optional(),\n types: z.string().optional(), // comma-separated: status,action,comment\n})\n\nexport type DocumentHistoryEntryKind = 'status' | 'action' | 'comment'\n\nconst HISTORY_TYPES: ReadonlySet<DocumentHistoryEntryKind> = new Set(['status', 'action', 'comment'])\n\nexport function parseDocumentHistoryTypes(input: string | null | undefined): Set<DocumentHistoryEntryKind> {\n const raw = typeof input === 'string' ? input.trim() : ''\n if (!raw) return new Set()\n const values = raw\n .split(',')\n .map((part) => part.trim().toLowerCase())\n .filter((value) => value.length > 0)\n const result = new Set<DocumentHistoryEntryKind>()\n values.forEach((value) => {\n if (HISTORY_TYPES.has(value as DocumentHistoryEntryKind)) {\n result.add(value as DocumentHistoryEntryKind)\n }\n })\n return result\n}\n\nexport async function GET(req: Request) {\n try {\n const url = new URL(req.url)\n const query = querySchema.parse(Object.fromEntries(url.searchParams))\n\n const container = await createRequestContainer()\n const auth = await getAuthFromRequest(req)\n const { translate } = await resolveTranslations()\n\n if (!auth || !auth.tenantId) {\n throw new CrudHttpError(401, { error: translate('sales.documents.errors.unauthorized', 'Unauthorized') })\n }\n\n const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req })\n // Resolve the organization scope the same way every other scoped read does.\n // Under \"All organizations\" (super-admin) `rbacOrganizationId` is null and\n // `where` is empty, so the read scopes by tenant + resource only instead of\n // failing \u2014 the document's `id` is a tenant-unique UUID, so no cross-org leak.\n const orgFilter = resolveOrganizationScopeFilter(scope, auth)\n const organizationId = orgFilter.rbacOrganizationId\n\n const requiredFeature = query.kind === 'order' ? 'sales.orders.view' : 'sales.quotes.view'\n const rbac = container.resolve('rbacService') as RbacService\n const hasAccess = await rbac.userHasAllFeatures(auth.sub, [requiredFeature], {\n tenantId: auth.tenantId,\n organizationId,\n })\n if (!hasAccess) {\n throw new CrudHttpError(403, { error: translate('api.errors.forbidden', 'Forbidden') })\n }\n\n const resourceKind = query.kind === 'order' ? 'sales.order' : 'sales.quote'\n\n const actionLogService = container.resolve('actionLogService') as ActionLogService\n const em = (container.resolve('em') as EntityManager).fork()\n\n const [actionLogList, notes] = await Promise.all([\n actionLogService.list({\n tenantId: auth.tenantId,\n organizationId: organizationId ?? undefined,\n resourceKind,\n resourceId: query.id,\n includeRelated: true,\n limit: query.limit,\n before: query.before ? new Date(query.before) : undefined,\n after: query.after ? new Date(query.after) : undefined,\n }),\n findWithDecryption(\n em,\n SalesNote,\n {\n contextType: query.kind,\n contextId: query.id,\n tenantId: auth.tenantId,\n ...orgFilter.where,\n deletedAt: null,\n },\n { orderBy: { createdAt: 'DESC' } },\n { tenantId: auth.tenantId, organizationId: organizationId ?? undefined },\n ),\n ])\n const logs = actionLogList.items as ActionLog[]\n\n const allUserIds = [\n ...logs.map((l) => l.actorUserId).filter((v): v is string => !!v),\n ...notes.map((n) => n.authorUserId).filter((v): v is string => !!v),\n ]\n\n const displayMaps = await loadAuditLogDisplayMaps(em, {\n userIds: allUserIds,\n tenantIds: [],\n organizationIds: [],\n })\n\n let items = buildHistoryEntries({ actionLogs: logs, notes, kind: query.kind, displayUsers: displayMaps.users })\n const typesFilter = parseDocumentHistoryTypes(query.types)\n if (typesFilter.size > 0) {\n items = items.filter((entry) => typesFilter.has(entry.kind as DocumentHistoryEntryKind))\n }\n\n let nextCursor: string | undefined = undefined\n if (logs.length === query.limit && items.length > 0) {\n const last = items[items.length - 1]\n nextCursor = Buffer.from(`${last.occurredAt}|${last.id}`).toString('base64')\n }\n\n return NextResponse.json({ items, nextCursor })\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n logger.error('sales.document-history.get failed', { err })\n const { translate } = await resolveTranslations()\n return NextResponse.json(\n { error: translate('sales.documents.history.error', 'Failed to load history.') },\n { status: 400 }\n )\n }\n}\n\nconst historyActorSchema = z.object({\n id: z.string().uuid().nullable(),\n label: z.string(),\n})\n\nconst historyEntrySchema = z.object({\n id: z.string(),\n occurredAt: z.string().datetime(),\n kind: z.enum(['status', 'action', 'comment']),\n action: z.string(),\n actor: historyActorSchema,\n source: z.enum(['action_log', 'note']),\n metadata: z.object({\n statusFrom: z.string().nullable().optional(),\n statusTo: z.string().nullable().optional(),\n documentKind: z.enum(['order', 'quote']).optional(),\n commandId: z.string().optional(),\n }).optional(),\n})\n\nconst documentHistoryResponseSchema = z.object({\n items: z.array(historyEntrySchema),\n nextCursor: z.string().optional(),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Sales',\n summary: 'Get document change history',\n methods: {\n GET: {\n summary: 'List history entries for an order or quote',\n query: querySchema,\n responses: [\n { status: 200, description: 'History entries', schema: documentHistoryResponseSchema },\n { status: 400, description: 'Invalid query', schema: z.object({ error: z.string() }) },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n { status: 403, description: 'Forbidden', schema: z.object({ error: z.string() }) },\n { status: 404, description: 'Document not found', schema: z.object({ error: z.string() }) },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,SAAS;AAElB,SAAS,+BAA+B;AACxC,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,0CAA0C;AACnD,SAAS,sCAAsC;AAC/C,SAAS,2BAA2B;AACpC,SAAS,eAAe,uBAAuB;AAC/C,SAAS,oBAAoB;AAK7B,SAAS,2BAA2B;AACpC,SAAS,iBAAiB;AAC1B,SAAS,0BAA0B;AACnC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,OAAO;AAI5B,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,KAAK;AAC3B;AAEA,MAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,KAAK,CAAC,SAAS,OAAO,CAAC;AAAA,EAC/B,IAAI,EAAE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AAE,OAAO,EAAE,CAAC,EAAE;AAAA,QACjF,EAAE,QAAQ,KAAK,aAAa,sBAAsB,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;AAAA,MAC5F;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { z } from 'zod'\nimport { ActionLogService } from '@open-mercato/core/modules/audit_logs/services/actionLogService'\nimport { loadAuditLogDisplayMaps } from '@open-mercato/core/modules/audit_logs/api/audit-logs/display'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { resolveOrganizationScopeFilter } from '@open-mercato/core/modules/directory/utils/organizationScopeFilter'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { NextResponse } from 'next/server'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { buildHistoryEntries } from '../../lib/historyHelpers'\nimport { SalesNote } from '../../data/entities'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('sales')\n\n// Spec: SPEC-006-2026-01-23-order-status-history\n\nexport const metadata = {\n GET: { requireAuth: true },\n}\n\nconst querySchema = z.object({\n kind: z.enum(['order', 'quote']),\n id: z.string().uuid(),\n limit: z.coerce.number().min(1).max(100).default(50),\n before: z.string().optional(),\n after: z.string().optional(),\n types: z.string().optional(), // comma-separated: status,action,comment\n})\n\nexport type DocumentHistoryEntryKind = 'status' | 'action' | 'comment'\n\nconst HISTORY_TYPES: ReadonlySet<DocumentHistoryEntryKind> = new Set(['status', 'action', 'comment'])\n\nexport function parseDocumentHistoryTypes(input: string | null | undefined): Set<DocumentHistoryEntryKind> {\n const raw = typeof input === 'string' ? input.trim() : ''\n if (!raw) return new Set()\n const values = raw\n .split(',')\n .map((part) => part.trim().toLowerCase())\n .filter((value) => value.length > 0)\n const result = new Set<DocumentHistoryEntryKind>()\n values.forEach((value) => {\n if (HISTORY_TYPES.has(value as DocumentHistoryEntryKind)) {\n result.add(value as DocumentHistoryEntryKind)\n }\n })\n return result\n}\n\nexport async function GET(req: Request) {\n try {\n const url = new URL(req.url)\n const query = querySchema.parse(Object.fromEntries(url.searchParams))\n\n const container = await createRequestContainer()\n const auth = await getAuthFromRequest(req)\n const { translate } = await resolveTranslations()\n\n if (!auth || !auth.tenantId) {\n throw new CrudHttpError(401, { error: translate('sales.documents.errors.unauthorized', 'Unauthorized') })\n }\n\n const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req })\n // Resolve the organization scope the same way every other scoped read does.\n // Under \"All organizations\" (super-admin) `rbacOrganizationId` is null and\n // `where` is empty, so the read scopes by tenant + resource only instead of\n // failing \u2014 the document's `id` is a tenant-unique UUID, so no cross-org leak.\n const orgFilter = resolveOrganizationScopeFilter(scope, auth)\n const organizationId = orgFilter.rbacOrganizationId\n\n const requiredFeature = query.kind === 'order' ? 'sales.orders.view' : 'sales.quotes.view'\n const rbac = container.resolve('rbacService') as RbacService\n const hasAccess = await rbac.userHasAllFeatures(auth.sub, [requiredFeature], {\n tenantId: auth.tenantId,\n organizationId,\n })\n if (!hasAccess) {\n throw new CrudHttpError(403, { error: translate('api.errors.forbidden', 'Forbidden') })\n }\n\n const resourceKind = query.kind === 'order' ? 'sales.order' : 'sales.quote'\n\n const actionLogService = container.resolve('actionLogService') as ActionLogService\n const em = (container.resolve('em') as EntityManager).fork()\n\n const [actionLogList, notes] = await Promise.all([\n actionLogService.list({\n tenantId: auth.tenantId,\n organizationId: organizationId ?? undefined,\n resourceKind,\n resourceId: query.id,\n includeRelated: true,\n limit: query.limit,\n before: query.before ? new Date(query.before) : undefined,\n after: query.after ? new Date(query.after) : undefined,\n }),\n findWithDecryption(\n em,\n SalesNote,\n {\n contextType: query.kind,\n contextId: query.id,\n tenantId: auth.tenantId,\n ...orgFilter.where,\n deletedAt: null,\n },\n { orderBy: { createdAt: 'DESC' } },\n { tenantId: auth.tenantId, organizationId: organizationId ?? undefined },\n ),\n ])\n const logs = actionLogList.items as ActionLog[]\n\n const allUserIds = [\n ...logs.map((l) => l.actorUserId).filter((v): v is string => !!v),\n ...notes.map((n) => n.authorUserId).filter((v): v is string => !!v),\n ]\n\n const displayMaps = await loadAuditLogDisplayMaps(em, {\n userIds: allUserIds,\n tenantIds: [],\n organizationIds: [],\n })\n\n let items = buildHistoryEntries({ actionLogs: logs, notes, kind: query.kind, displayUsers: displayMaps.users })\n const typesFilter = parseDocumentHistoryTypes(query.types)\n if (typesFilter.size > 0) {\n items = items.filter((entry) => typesFilter.has(entry.kind as DocumentHistoryEntryKind))\n }\n\n let nextCursor: string | undefined = undefined\n if (logs.length === query.limit && items.length > 0) {\n const last = items[items.length - 1]\n nextCursor = Buffer.from(`${last.occurredAt}|${last.id}`).toString('base64')\n }\n\n return NextResponse.json({ items, nextCursor })\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n logger.error('sales.document-history.get failed', { err })\n const { translate } = await resolveTranslations()\n return NextResponse.json(\n { error: translate('sales.documents.history.error', 'Failed to load history.') },\n { status: 400 }\n )\n }\n}\n\nconst historyActorSchema = z.object({\n id: z.string().uuid().nullable(),\n label: z.string(),\n})\n\nconst historyEntrySchema = z.object({\n id: z.string(),\n occurredAt: z.string().datetime(),\n kind: z.enum(['status', 'action', 'comment']),\n action: z.string(),\n actor: historyActorSchema,\n source: z.enum(['action_log', 'note']),\n metadata: z.object({\n statusFrom: z.string().nullable().optional(),\n statusTo: z.string().nullable().optional(),\n documentKind: z.enum(['order', 'quote']).optional(),\n commandId: z.string().optional(),\n changedFields: z.array(z.string()).optional(),\n }).optional(),\n})\n\nconst documentHistoryResponseSchema = z.object({\n items: z.array(historyEntrySchema),\n nextCursor: z.string().optional(),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Sales',\n summary: 'Get document change history',\n methods: {\n GET: {\n summary: 'List history entries for an order or quote',\n query: querySchema,\n responses: [\n { status: 200, description: 'History entries', schema: documentHistoryResponseSchema },\n { status: 400, description: 'Invalid query', schema: z.object({ error: z.string() }) },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n { status: 403, description: 'Forbidden', schema: z.object({ error: z.string() }) },\n { status: 404, description: 'Document not found', schema: z.object({ error: z.string() }) },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAElB,SAAS,+BAA+B;AACxC,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,0CAA0C;AACnD,SAAS,sCAAsC;AAC/C,SAAS,2BAA2B;AACpC,SAAS,eAAe,uBAAuB;AAC/C,SAAS,oBAAoB;AAK7B,SAAS,2BAA2B;AACpC,SAAS,iBAAiB;AAC1B,SAAS,0BAA0B;AACnC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,OAAO;AAI5B,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,KAAK;AAC3B;AAEA,MAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,KAAK,CAAC,SAAS,OAAO,CAAC;AAAA,EAC/B,IAAI,EAAE,OAAO,EAAE,KAAK;AAAA,EACpB,OAAO,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,QAAQ,EAAE;AAAA,EACnD,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,EAC3B,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA;AAC7B,CAAC;AAID,MAAM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 +1,65 @@
1
+ import { isRecord } from "@open-mercato/shared/lib/utils";
2
+ const DOCUMENT_LINE_UPSERT_COMMANDS = /* @__PURE__ */ new Set([
3
+ "sales.orders.lines.upsert",
4
+ "sales.quotes.lines.upsert"
5
+ ]);
6
+ const USER_EDITABLE_LINE_FIELDS = [
7
+ "productId",
8
+ "productVariantId",
9
+ "name",
10
+ "description",
11
+ "comment",
12
+ "quantity",
13
+ "quantityUnit",
14
+ "currencyCode",
15
+ "unitPriceNet",
16
+ "unitPriceGross",
17
+ "discountAmount",
18
+ "discountPercent",
19
+ "taxRate",
20
+ "configuration",
21
+ "promotionCode",
22
+ "customFields",
23
+ "statusEntryId"
24
+ ];
25
+ function readSnapshotLines(snapshot) {
26
+ if (!isRecord(snapshot) || !Array.isArray(snapshot.lines)) return [];
27
+ return snapshot.lines.filter(isRecord);
28
+ }
29
+ function historyValuesEqual(left, right) {
30
+ if (Object.is(left, right)) return true;
31
+ try {
32
+ return JSON.stringify(left) === JSON.stringify(right);
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+ function deriveDocumentLineChangedFields(log) {
38
+ if (!DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) return [];
39
+ const beforeById = new Map(
40
+ readSnapshotLines(log.snapshotBefore).filter((line) => typeof line.id === "string").map((line) => [line.id, line])
41
+ );
42
+ const afterLines = readSnapshotLines(log.snapshotAfter);
43
+ const changedFields = /* @__PURE__ */ new Set();
44
+ for (const afterLine of afterLines) {
45
+ if (typeof afterLine.id !== "string") continue;
46
+ const beforeLine = beforeById.get(afterLine.id);
47
+ if (!beforeLine) continue;
48
+ for (const field of USER_EDITABLE_LINE_FIELDS) {
49
+ if (!historyValuesEqual(beforeLine[field], afterLine[field])) {
50
+ changedFields.add(field);
51
+ }
52
+ }
53
+ }
54
+ return USER_EDITABLE_LINE_FIELDS.filter((field) => changedFields.has(field));
55
+ }
56
+ function deriveHistoryChangedFields(log) {
57
+ if (DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) {
58
+ return deriveDocumentLineChangedFields(log);
59
+ }
60
+ const persisted = Array.isArray(log.changedFields) ? log.changedFields.filter((field) => typeof field === "string" && field.trim().length > 0) : [];
61
+ return Array.from(new Set(persisted));
62
+ }
1
63
  function extractStatusFromSnapshot(snapshot) {
2
64
  if (!snapshot || typeof snapshot !== "object") return null;
3
65
  const s = snapshot;
@@ -28,9 +90,14 @@ function detectStatusChange(log) {
28
90
  }
29
91
  function normalizeActionLogToHistoryEntry(log, kind, displayUsers) {
30
92
  const statusChange = detectStatusChange(log);
93
+ const changedFields = deriveHistoryChangedFields(log);
31
94
  let entryKind = "action";
32
95
  let action = log.actionLabel || log.commandId;
33
- let metadata = { documentKind: kind, commandId: log.commandId };
96
+ let metadata = {
97
+ documentKind: kind,
98
+ commandId: log.commandId,
99
+ ...changedFields.length > 0 ? { changedFields } : {}
100
+ };
34
101
  if (statusChange) {
35
102
  const hasStatusValues = statusChange.statusFrom != null || statusChange.statusTo != null;
36
103
  if (hasStatusValues) {
@@ -75,6 +142,7 @@ function buildHistoryEntries(input) {
75
142
  }
76
143
  export {
77
144
  buildHistoryEntries,
145
+ deriveHistoryChangedFields,
78
146
  detectStatusChange,
79
147
  normalizeActionLogToHistoryEntry,
80
148
  normalizeNoteToHistoryEntry
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/sales/lib/historyHelpers.ts"],
4
- "sourcesContent": ["import type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport type { SalesNote } from '../data/entities'\n\nexport type HistoryEntry = {\n id: string\n occurredAt: string\n kind: 'status' | 'action' | 'comment'\n action: string\n actor: { id: string | null; label: string }\n source: 'action_log' | 'note'\n metadata?: {\n statusFrom?: string | null\n statusTo?: string | null\n documentKind?: 'order' | 'quote'\n commandId?: string\n }\n}\n\nfunction extractStatusFromSnapshot(snapshot: unknown): string | null {\n if (!snapshot || typeof snapshot !== 'object') return null\n const s = snapshot as Record<string, unknown>\n // Dedicated logStatusChange entries: { status: \"value\" }\n if (typeof s.status === 'string') return s.status\n // Regular update snapshots: { order: { status, fulfillment_status, payment_status, ... } }\n if (s.order && typeof s.order === 'object') {\n const order = s.order as Record<string, unknown>\n const orderStatus = typeof order.status === 'string' ? order.status : null\n const fulfillmentStatus = typeof order.fulfillment_status === 'string' ? order.fulfillment_status : null\n const paymentStatus = typeof order.payment_status === 'string' ? order.payment_status : null\n return orderStatus ?? fulfillmentStatus ?? paymentStatus ?? null\n }\n // Regular update snapshots: { quote: { status: \"value\", ... } }\n if (s.quote && typeof s.quote === 'object') {\n const quote = s.quote as Record<string, unknown>\n if (typeof quote.status === 'string') return quote.status\n }\n return null\n}\n\nexport function detectStatusChange(log: ActionLog): {\n statusFrom: string | null\n statusTo: string | null\n} | null {\n const after = extractStatusFromSnapshot(log.snapshotAfter)\n const before = log.snapshotBefore\n ? extractStatusFromSnapshot(log.snapshotBefore)\n : null\n // Creation (no snapshotBefore): always one status entry (initial value or \"created\")\n if (!log.snapshotBefore) {\n return { statusFrom: null, statusTo: after ?? null }\n }\n if (before !== after && (before !== null || after !== null)) {\n return { statusFrom: before, statusTo: after ?? null }\n }\n return null\n}\n\n/**\n * Normalizes ActionLog to HistoryEntry (status/action/comment).\n */\nexport function normalizeActionLogToHistoryEntry(\n log: ActionLog,\n kind: 'order' | 'quote',\n displayUsers?: Record<string, string>,\n): HistoryEntry {\n const statusChange = detectStatusChange(log)\n let entryKind: 'status' | 'action' = 'action'\n let action = log.actionLabel || log.commandId\n let metadata: HistoryEntry['metadata'] = { documentKind: kind, commandId: log.commandId }\n if (statusChange) {\n const hasStatusValues = statusChange.statusFrom != null || statusChange.statusTo != null\n if (hasStatusValues) {\n entryKind = 'status'\n action = statusChange.statusTo ?? 'unknown'\n metadata = { ...metadata, statusFrom: statusChange.statusFrom, statusTo: statusChange.statusTo }\n }\n // When both are null (e.g. Create return/shipment/payment with non-document snapshot), keep as action and use actionLabel\n }\n const actorLabel = log.actorUserId\n ? (displayUsers?.[log.actorUserId] ?? log.actorUserId)\n : 'system'\n return {\n id: log.id,\n occurredAt: log.createdAt.toISOString(),\n kind: entryKind,\n action,\n actor: { id: log.actorUserId, label: actorLabel },\n source: 'action_log',\n metadata,\n }\n}\n\nexport function normalizeNoteToHistoryEntry(\n note: SalesNote,\n kind: 'order' | 'quote',\n displayUsers?: Record<string, string>,\n): HistoryEntry {\n const actorLabel = note.authorUserId\n ? (displayUsers?.[note.authorUserId] ?? note.authorUserId)\n : 'system'\n return {\n id: note.id,\n occurredAt: note.createdAt.toISOString(),\n kind: 'comment',\n action: note.body,\n actor: { id: note.authorUserId ?? null, label: actorLabel },\n source: 'note',\n metadata: { documentKind: kind },\n }\n}\n\nexport type HistoryBuilderInput = {\n actionLogs: ActionLog[]\n notes?: SalesNote[]\n kind: 'order' | 'quote'\n displayUsers?: Record<string, string>\n}\n\nexport function buildHistoryEntries(input: HistoryBuilderInput): HistoryEntry[] {\n const logEntries = input.actionLogs.map((log) =>\n normalizeActionLogToHistoryEntry(log, input.kind, input.displayUsers)\n )\n const noteEntries = (input.notes ?? []).map((note) =>\n normalizeNoteToHistoryEntry(note, input.kind, input.displayUsers)\n )\n return [...logEntries, ...noteEntries].sort(\n (a, b) => new Date(b.occurredAt).getTime() - new Date(a.occurredAt).getTime()\n )\n}\n"],
5
- "mappings": "AAkBA,SAAS,0BAA0B,UAAkC;AACnE,MAAI,CAAC,YAAY,OAAO,aAAa,SAAU,QAAO;AACtD,QAAM,IAAI;AAEV,MAAI,OAAO,EAAE,WAAW,SAAU,QAAO,EAAE;AAE3C,MAAI,EAAE,SAAS,OAAO,EAAE,UAAU,UAAU;AAC1C,UAAM,QAAQ,EAAE;AAChB,UAAM,cAAc,OAAO,MAAM,WAAW,WAAW,MAAM,SAAS;AACtE,UAAM,oBAAoB,OAAO,MAAM,uBAAuB,WAAW,MAAM,qBAAqB;AACpG,UAAM,gBAAgB,OAAO,MAAM,mBAAmB,WAAW,MAAM,iB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,cACc;AACd,QAAM,aAAa,KAAK,eACnB,eAAe,KAAK,YAAY,KAAK,KAAK,eAC3C;AACJ,SAAO;AAAA,IACL,IAAI,KAAK;AAAA,IACT,YAAY,KAAK,UAAU,YAAY;AAAA,IACvC,MAAM;AAAA,IACN,QAAQ,KAAK;AAAA,IACb,OAAO,EAAE,IAAI,KAAK,gBAAgB,MAAM,OAAO,WAAW;AAAA,IAC1D,QAAQ;AAAA,IACR,UAAU,EAAE,cAAc,KAAK;AAAA,EACjC;AACF;AASO,SAAS,oBAAoB,OAA4C;AAC9E,QAAM,aAAa,MAAM,WAAW;AAAA,IAAI,CAAC,QACvC,iCAAiC,KAAK,MAAM,MAAM,MAAM,YAAY;AAAA,EACtE;AACA,QAAM,eAAe,MAAM,SAAS,CAAC,GAAG;AAAA,IAAI,CAAC,SAC3C,4BAA4B,MAAM,MAAM,MAAM,MAAM,YAAY;AAAA,EAClE;AACA,SAAO,CAAC,GAAG,YAAY,GAAG,WAAW,EAAE;AAAA,IACrC,CAAC,GAAG,MAAM,IAAI,KAAK,EAAE,UAAU,EAAE,QAAQ,IAAI,IAAI,KAAK,EAAE,UAAU,EAAE,QAAQ;AAAA,EAC9E;AACF;",
4
+ "sourcesContent": ["import type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport { isRecord } from '@open-mercato/shared/lib/utils'\nimport type { SalesNote } from '../data/entities'\n\nexport type HistoryEntry = {\n id: string\n occurredAt: string\n kind: 'status' | 'action' | 'comment'\n action: string\n actor: { id: string | null; label: string }\n source: 'action_log' | 'note'\n metadata?: {\n statusFrom?: string | null\n statusTo?: string | null\n documentKind?: 'order' | 'quote'\n commandId?: string\n changedFields?: string[]\n }\n}\n\nconst DOCUMENT_LINE_UPSERT_COMMANDS = new Set([\n 'sales.orders.lines.upsert',\n 'sales.quotes.lines.upsert',\n])\n\nconst USER_EDITABLE_LINE_FIELDS = [\n 'productId',\n 'productVariantId',\n 'name',\n 'description',\n 'comment',\n 'quantity',\n 'quantityUnit',\n 'currencyCode',\n 'unitPriceNet',\n 'unitPriceGross',\n 'discountAmount',\n 'discountPercent',\n 'taxRate',\n 'configuration',\n 'promotionCode',\n 'customFields',\n 'statusEntryId',\n] as const\n\nfunction readSnapshotLines(snapshot: unknown): Array<Record<string, unknown>> {\n if (!isRecord(snapshot) || !Array.isArray(snapshot.lines)) return []\n return snapshot.lines.filter(isRecord)\n}\n\nfunction historyValuesEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true\n try {\n return JSON.stringify(left) === JSON.stringify(right)\n } catch {\n return false\n }\n}\n\n/**\n * Derives a concise, user-editable field list for document line updates.\n * Calculated totals and normalized UoM fields are intentionally excluded so\n * the timeline describes the user's edit rather than downstream recalculation.\n */\nfunction deriveDocumentLineChangedFields(log: ActionLog): string[] {\n if (!DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) return []\n\n const beforeById = new Map(\n readSnapshotLines(log.snapshotBefore)\n .filter((line) => typeof line.id === 'string')\n .map((line) => [line.id as string, line]),\n )\n const afterLines = readSnapshotLines(log.snapshotAfter)\n\n const changedFields = new Set<string>()\n for (const afterLine of afterLines) {\n if (typeof afterLine.id !== 'string') continue\n const beforeLine = beforeById.get(afterLine.id)\n if (!beforeLine) continue\n\n for (const field of USER_EDITABLE_LINE_FIELDS) {\n if (!historyValuesEqual(beforeLine[field], afterLine[field])) {\n changedFields.add(field)\n }\n }\n }\n\n return USER_EDITABLE_LINE_FIELDS.filter((field) => changedFields.has(field))\n}\n\nexport function deriveHistoryChangedFields(log: ActionLog): string[] {\n if (DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) {\n return deriveDocumentLineChangedFields(log)\n }\n const persisted = Array.isArray(log.changedFields)\n ? log.changedFields.filter((field): field is string => typeof field === 'string' && field.trim().length > 0)\n : []\n return Array.from(new Set(persisted))\n}\n\nfunction extractStatusFromSnapshot(snapshot: unknown): string | null {\n if (!snapshot || typeof snapshot !== 'object') return null\n const s = snapshot as Record<string, unknown>\n // Dedicated logStatusChange entries: { status: \"value\" }\n if (typeof s.status === 'string') return s.status\n // Regular update snapshots: { order: { status, fulfillment_status, payment_status, ... } }\n if (s.order && typeof s.order === 'object') {\n const order = s.order as Record<string, unknown>\n const orderStatus = typeof order.status === 'string' ? order.status : null\n const fulfillmentStatus = typeof order.fulfillment_status === 'string' ? order.fulfillment_status : null\n const paymentStatus = typeof order.payment_status === 'string' ? order.payment_status : null\n return orderStatus ?? fulfillmentStatus ?? paymentStatus ?? null\n }\n // Regular update snapshots: { quote: { status: \"value\", ... } }\n if (s.quote && typeof s.quote === 'object') {\n const quote = s.quote as Record<string, unknown>\n if (typeof quote.status === 'string') return quote.status\n }\n return null\n}\n\nexport function detectStatusChange(log: ActionLog): {\n statusFrom: string | null\n statusTo: string | null\n} | null {\n const after = extractStatusFromSnapshot(log.snapshotAfter)\n const before = log.snapshotBefore\n ? extractStatusFromSnapshot(log.snapshotBefore)\n : null\n // Creation (no snapshotBefore): always one status entry (initial value or \"created\")\n if (!log.snapshotBefore) {\n return { statusFrom: null, statusTo: after ?? null }\n }\n if (before !== after && (before !== null || after !== null)) {\n return { statusFrom: before, statusTo: after ?? null }\n }\n return null\n}\n\n/**\n * Normalizes ActionLog to HistoryEntry (status/action/comment).\n */\nexport function normalizeActionLogToHistoryEntry(\n log: ActionLog,\n kind: 'order' | 'quote',\n displayUsers?: Record<string, string>,\n): HistoryEntry {\n const statusChange = detectStatusChange(log)\n const changedFields = deriveHistoryChangedFields(log)\n let entryKind: 'status' | 'action' = 'action'\n let action = log.actionLabel || log.commandId\n let metadata: HistoryEntry['metadata'] = {\n documentKind: kind,\n commandId: log.commandId,\n ...(changedFields.length > 0 ? { changedFields } : {}),\n }\n if (statusChange) {\n const hasStatusValues = statusChange.statusFrom != null || statusChange.statusTo != null\n if (hasStatusValues) {\n entryKind = 'status'\n action = statusChange.statusTo ?? 'unknown'\n metadata = { ...metadata, statusFrom: statusChange.statusFrom, statusTo: statusChange.statusTo }\n }\n // When both are null (e.g. Create return/shipment/payment with non-document snapshot), keep as action and use actionLabel\n }\n const actorLabel = log.actorUserId\n ? (displayUsers?.[log.actorUserId] ?? log.actorUserId)\n : 'system'\n return {\n id: log.id,\n occurredAt: log.createdAt.toISOString(),\n kind: entryKind,\n action,\n actor: { id: log.actorUserId, label: actorLabel },\n source: 'action_log',\n metadata,\n }\n}\n\nexport function normalizeNoteToHistoryEntry(\n note: SalesNote,\n kind: 'order' | 'quote',\n displayUsers?: Record<string, string>,\n): HistoryEntry {\n const actorLabel = note.authorUserId\n ? (displayUsers?.[note.authorUserId] ?? note.authorUserId)\n : 'system'\n return {\n id: note.id,\n occurredAt: note.createdAt.toISOString(),\n kind: 'comment',\n action: note.body,\n actor: { id: note.authorUserId ?? null, label: actorLabel },\n source: 'note',\n metadata: { documentKind: kind },\n }\n}\n\nexport type HistoryBuilderInput = {\n actionLogs: ActionLog[]\n notes?: SalesNote[]\n kind: 'order' | 'quote'\n displayUsers?: Record<string, string>\n}\n\nexport function buildHistoryEntries(input: HistoryBuilderInput): HistoryEntry[] {\n const logEntries = input.actionLogs.map((log) =>\n normalizeActionLogToHistoryEntry(log, input.kind, input.displayUsers)\n )\n const noteEntries = (input.notes ?? []).map((note) =>\n normalizeNoteToHistoryEntry(note, input.kind, input.displayUsers)\n )\n return [...logEntries, ...noteEntries].sort(\n (a, b) => new Date(b.occurredAt).getTime() - new Date(a.occurredAt).getTime()\n )\n}\n"],
5
+ "mappings": "AACA,SAAS,gBAAgB;AAmBzB,MAAM,gCAAgC,oBAAI,IAAI;AAAA,EAC5C;AAAA,EACA;AACF,CAAC;AAED,MAAM,4BAA4B;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,kBAAkB,UAAmD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,IAAI,KAAK,EAAE,UAAU,EAAE,QAAQ,IAAI,IAAI,KAAK,EAAE,UAAU,EAAE,QAAQ;AAAA,EAC9E;AACF;",
6
6
  "names": []
7
7
  }
@@ -23,6 +23,44 @@ const KIND_BG_COLORS = {
23
23
  action: "bg-muted",
24
24
  comment: "bg-muted"
25
25
  };
26
+ const CHANGED_FIELD_LABELS = {
27
+ productId: { key: "sales.documents.history.fields.product", fallback: "Product" },
28
+ productVariantId: { key: "sales.documents.history.fields.variant", fallback: "Variant" },
29
+ name: { key: "sales.documents.history.fields.name", fallback: "Name" },
30
+ description: { key: "sales.documents.history.fields.description", fallback: "Description" },
31
+ comment: { key: "sales.documents.history.fields.comment", fallback: "Comment" },
32
+ quantity: { key: "sales.documents.history.fields.quantity", fallback: "Quantity" },
33
+ quantityUnit: { key: "sales.documents.history.fields.quantityUnit", fallback: "Quantity unit" },
34
+ currencyCode: { key: "sales.documents.history.fields.currency", fallback: "Currency" },
35
+ unitPriceNet: { key: "sales.documents.history.fields.unitPriceNet", fallback: "Net unit price" },
36
+ unitPriceGross: { key: "sales.documents.history.fields.unitPriceGross", fallback: "Gross unit price" },
37
+ discountAmount: { key: "sales.documents.history.fields.discountAmount", fallback: "Discount amount" },
38
+ discountPercent: { key: "sales.documents.history.fields.discountPercent", fallback: "Discount percent" },
39
+ taxRate: { key: "sales.documents.history.fields.taxRate", fallback: "Tax class" },
40
+ configuration: { key: "sales.documents.history.fields.configuration", fallback: "Configuration" },
41
+ promotionCode: { key: "sales.documents.history.fields.promotionCode", fallback: "Promotion code" },
42
+ customFields: { key: "sales.documents.history.fields.customFields", fallback: "Custom fields" },
43
+ statusEntryId: { key: "sales.documents.history.fields.status", fallback: "Status" }
44
+ };
45
+ function normalizeChangedField(field) {
46
+ const segment = field.split(".").pop() ?? field;
47
+ return segment.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
48
+ }
49
+ function humanizeChangedField(field) {
50
+ const normalized = normalizeChangedField(field);
51
+ const spaced = normalized.replace(/([a-z0-9])([A-Z])/g, "$1 $2").trim();
52
+ return spaced.length > 0 ? `${spaced[0].toUpperCase()}${spaced.slice(1)}` : field;
53
+ }
54
+ function translateChangedField(t, field) {
55
+ const normalized = normalizeChangedField(field);
56
+ const translation = CHANGED_FIELD_LABELS[normalized];
57
+ return translation ? t(translation.key, translation.fallback) : humanizeChangedField(field);
58
+ }
59
+ function translateAction(t, entry) {
60
+ const commandId = entry.metadata?.commandId;
61
+ if (!commandId?.startsWith("sales.")) return entry.action;
62
+ return t(`sales.audit.${commandId.slice("sales.".length)}`, entry.action);
63
+ }
26
64
  function StatusDot({ color, className }) {
27
65
  if (!color) return /* @__PURE__ */ jsx("span", { className: cn("h-2.5 w-2.5 rounded-full bg-muted-foreground/40 border border-border inline-flex", className) });
28
66
  return /* @__PURE__ */ jsx(
@@ -58,10 +96,16 @@ function TimelineItem({
58
96
  statusMap,
59
97
  isLast
60
98
  }) {
99
+ const t = useT();
61
100
  const KindIcon = KIND_ICONS[entry.kind];
62
101
  const relativeTime = formatRelativeTime(entry.occurredAt);
63
102
  const absoluteTime = formatDateTime(entry.occurredAt);
64
103
  const isStatusChange = entry.kind === "status" && entry.metadata?.statusTo;
104
+ const action = translateAction(t, entry);
105
+ const actorLabel = entry.actor.id ? entry.actor.label : t("sales.documents.history.actor.system", "System");
106
+ const changedFieldLabels = Array.from(
107
+ new Set((entry.metadata?.changedFields ?? []).map((field) => translateChangedField(t, field)))
108
+ );
65
109
  return /* @__PURE__ */ jsxs("div", { "data-testid": "timeline-entry", className: "relative flex gap-3", children: [
66
110
  !isLast && /* @__PURE__ */ jsx("div", { className: "absolute left-[11px] top-6 bottom-0 w-px bg-border", "aria-hidden": true }),
67
111
  /* @__PURE__ */ jsx(
@@ -78,7 +122,7 @@ function TimelineItem({
78
122
  /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2 flex-wrap", children: [
79
123
  /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5 text-xs font-medium text-foreground", children: [
80
124
  /* @__PURE__ */ jsx(User, { className: "h-3 w-3 text-muted-foreground", "aria-hidden": true }),
81
- entry.actor.label
125
+ actorLabel
82
126
  ] }),
83
127
  /* @__PURE__ */ jsx(
84
128
  "span",
@@ -96,7 +140,12 @@ function TimelineItem({
96
140
  statusTo: entry.metadata?.statusTo,
97
141
  statusMap
98
142
  }
99
- ) : /* @__PURE__ */ jsx("div", { className: "text-sm text-foreground", children: entry.action })
143
+ ) : /* @__PURE__ */ jsxs("div", { className: "space-y-0.5", children: [
144
+ /* @__PURE__ */ jsx("div", { className: "text-sm text-foreground", children: action }),
145
+ entry.kind === "action" && changedFieldLabels.length > 0 ? /* @__PURE__ */ jsx("div", { className: "text-xs text-muted-foreground", children: t("sales.documents.history.changedFields", "Changed fields: {fields}", {
146
+ fields: changedFieldLabels.join(", ")
147
+ }) }) : null
148
+ ] })
100
149
  ] }) })
101
150
  ] });
102
151
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../src/modules/sales/widgets/injection/document-history/widget.client.tsx"],
4
- "sourcesContent": ["\"use client\"\n\nimport * as React from \"react\"\nimport { Spinner } from \"@open-mercato/ui/primitives/spinner\"\nimport { useT } from \"@open-mercato/shared/lib/i18n/context\"\nimport { apiCall } from \"@open-mercato/ui/backend/utils/apiCall\"\nimport { formatRelativeTime, formatDateTime } from \"@open-mercato/shared/lib/time\"\nimport { cn } from \"@open-mercato/shared/lib/utils\"\nimport type { InjectionWidgetComponentProps } from '@open-mercato/shared/modules/widgets/injection'\nimport { ArrowRightLeft, Zap, MessageSquare, User, Filter, ChevronDown, Check } from 'lucide-react'\n\nexport type TimelineEntry = {\n id: string\n occurredAt: string\n kind: \"status\" | \"action\" | \"comment\"\n action: string\n actor: { id: string | null; label: string }\n source: \"action_log\" | \"note\"\n metadata?: {\n statusFrom?: string | null\n statusTo?: string | null\n documentKind?: \"order\" | \"quote\"\n commandId?: string\n }\n}\n\ntype StatusOption = {\n value: string\n label: string\n color: string | null\n icon: string | null\n}\n\ntype TimelineContext = {\n kind: \"order\" | \"quote\"\n record: { id: string }\n}\n\nconst isValidContext = (ctx: unknown): ctx is TimelineContext =>\n ctx !== null &&\n typeof ctx === 'object' &&\n 'kind' in ctx &&\n 'record' in ctx &&\n ((ctx as TimelineContext).kind === 'order' || (ctx as TimelineContext).kind === 'quote') &&\n typeof (ctx as TimelineContext).record === 'object' &&\n (ctx as TimelineContext).record !== null &&\n 'id' in (ctx as TimelineContext).record &&\n typeof (ctx as TimelineContext).record.id === 'string'\n\nconst KIND_ICONS = {\n status: ArrowRightLeft,\n action: Zap,\n comment: MessageSquare,\n}\n\nconst KIND_ICON_COLORS = {\n status: 'text-foreground',\n action: 'text-foreground',\n comment: 'text-foreground',\n}\n\nconst KIND_BG_COLORS = {\n status: 'bg-muted',\n action: 'bg-muted',\n comment: 'bg-muted',\n}\n\nfunction StatusDot({ color, className }: { color: string | null | undefined; className?: string }) {\n if (!color) return <span className={cn('h-2.5 w-2.5 rounded-full bg-muted-foreground/40 border border-border inline-flex', className)} />\n return (\n <span\n className={cn('h-2.5 w-2.5 rounded-full border border-border/70 inline-flex', className)}\n style={{ backgroundColor: color }}\n aria-hidden\n />\n )\n}\n\nfunction StatusTransition({\n statusFrom,\n statusTo,\n statusMap,\n}: {\n statusFrom: string | null | undefined\n statusTo: string | null | undefined\n statusMap: Record<string, StatusOption>\n}) {\n const from = statusFrom ? (statusMap[statusFrom] ?? { value: statusFrom, label: statusFrom, color: null, icon: null }) : null\n const to = statusTo ? (statusMap[statusTo] ?? { value: statusTo, label: statusTo, color: null, icon: null }) : null\n\n return (\n <div className=\"flex items-center gap-1.5 flex-wrap\">\n {from ? (\n <span className=\"inline-flex items-center gap-1 text-xs text-muted-foreground\">\n <StatusDot color={from.color} />\n <span>{from.label}</span>\n </span>\n ) : null}\n {from && to ? (\n <ArrowRightLeft className=\"h-3 w-3 text-muted-foreground/60 shrink-0\" />\n ) : null}\n {to ? (\n <span className=\"inline-flex items-center gap-1 text-xs font-medium text-foreground\">\n <StatusDot color={to.color} />\n <span>{to.label}</span>\n </span>\n ) : null}\n </div>\n )\n}\n\nfunction TimelineItem({\n entry,\n statusMap,\n isLast,\n}: {\n entry: TimelineEntry\n statusMap: Record<string, StatusOption>\n isLast: boolean\n}) {\n const KindIcon = KIND_ICONS[entry.kind]\n const relativeTime = formatRelativeTime(entry.occurredAt)\n const absoluteTime = formatDateTime(entry.occurredAt)\n\n const isStatusChange = entry.kind === 'status' && entry.metadata?.statusTo\n\n return (\n <div data-testid=\"timeline-entry\" className=\"relative flex gap-3\">\n {/* Vertical connector line */}\n {!isLast && (\n <div className=\"absolute left-[11px] top-6 bottom-0 w-px bg-border\" aria-hidden />\n )}\n\n {/* Icon circle */}\n <div\n className={cn(\n 'relative z-10 flex h-6 w-6 shrink-0 items-center justify-center rounded-full border border-border',\n KIND_BG_COLORS[entry.kind],\n )}\n >\n <KindIcon className={cn('h-3 w-3', KIND_ICON_COLORS[entry.kind])} aria-hidden />\n </div>\n\n {/* Content card */}\n <div className=\"flex-1 pb-4\">\n <div className=\"group rounded-lg border bg-card p-3 space-y-1.5\">\n {/* Header: actor + time */}\n <div className=\"flex items-center justify-between gap-2 flex-wrap\">\n <span className=\"inline-flex items-center gap-1.5 text-xs font-medium text-foreground\">\n <User className=\"h-3 w-3 text-muted-foreground\" aria-hidden />\n {entry.actor.label}\n </span>\n <span\n className=\"text-xs text-muted-foreground\"\n title={absoluteTime ?? undefined}\n >\n {relativeTime ?? absoluteTime}\n </span>\n </div>\n\n {/* Body */}\n {isStatusChange ? (\n <StatusTransition\n statusFrom={entry.metadata?.statusFrom}\n statusTo={entry.metadata?.statusTo}\n statusMap={statusMap}\n />\n ) : (\n <div className=\"text-sm text-foreground\">{entry.action}</div>\n )}\n </div>\n </div>\n </div>\n )\n}\n\ntype FilterKind = 'all' | 'status' | 'action' | 'comment'\n\ntype FilterOption = { value: FilterKind; label: string }\n\nfunction FilterDropdown({ filter, onChange }: { filter: FilterKind; onChange: (kind: FilterKind) => void }) {\n const t = useT()\n const [open, setOpen] = React.useState(false)\n const ref = React.useRef<HTMLDivElement>(null)\n\n React.useEffect(() => {\n if (!open) return\n function onDocClick(e: MouseEvent) {\n if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)\n }\n function onKey(e: KeyboardEvent) {\n if (e.key === 'Escape') setOpen(false)\n }\n document.addEventListener('mousedown', onDocClick)\n document.addEventListener('keydown', onKey)\n return () => {\n document.removeEventListener('mousedown', onDocClick)\n document.removeEventListener('keydown', onKey)\n }\n }, [open])\n\n const options: FilterOption[] = [\n { value: 'all', label: t('sales.documents.history.filter.all', 'All') },\n { value: 'status', label: t('sales.documents.history.filter.status', 'Status changes') },\n { value: 'action', label: t('sales.documents.history.filter.actions', 'Actions') },\n { value: 'comment', label: t('sales.documents.history.filter.comments', 'Comments') },\n ]\n\n const activeLabel = options.find(o => o.value === filter)?.label\n\n return (\n <div className=\"relative\" ref={ref}>\n <button\n type=\"button\"\n aria-haspopup=\"listbox\"\n aria-expanded={open}\n onClick={() => setOpen(prev => !prev)}\n className=\"inline-flex items-center gap-1.5 rounded-md border border-input bg-background px-2.5 py-1 text-xs font-medium shadow-sm hover:bg-accent hover:text-accent-foreground transition-colors select-none\"\n >\n <Filter className=\"h-3 w-3\" aria-hidden />\n {t('sales.documents.history.filter.label', 'Filters')}\n {filter !== 'all' && (\n <span className=\"text-muted-foreground\">: {activeLabel}</span>\n )}\n <ChevronDown className={cn('h-3 w-3 transition-transform duration-150', open && 'rotate-180')} aria-hidden />\n </button>\n {open && (\n <div\n role=\"listbox\"\n aria-label={t('sales.documents.history.filter.label', 'Filters')}\n className=\"absolute left-0 top-full mt-1 z-dropdown w-48 rounded-md border bg-background p-1 shadow-md\"\n >\n {options.map(opt => (\n <button\n key={opt.value}\n type=\"button\"\n role=\"option\"\n aria-selected={filter === opt.value}\n onClick={() => { onChange(opt.value); setOpen(false) }}\n className=\"flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-accent\"\n >\n <Check className={cn('h-3.5 w-3.5 shrink-0', filter === opt.value ? 'opacity-100' : 'opacity-0')} aria-hidden />\n {opt.label}\n </button>\n ))}\n </div>\n )}\n </div>\n )\n}\n\nexport const DocumentHistoryWidget: React.FC<InjectionWidgetComponentProps<unknown, unknown>> = ({ context }) => {\n const t = useT()\n const [entries, setEntries] = React.useState<TimelineEntry[]>([])\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [statusMap, setStatusMap] = React.useState<Record<string, StatusOption>>({})\n const [filter, setFilter] = React.useState<FilterKind>('all')\n\n React.useEffect(() => {\n const urls = [\n '/api/sales/order-statuses?pageSize=100',\n '/api/sales/shipment-statuses?pageSize=100',\n '/api/sales/payment-statuses?pageSize=100',\n ]\n const map: Record<string, StatusOption> = {}\n const merge = (items: unknown[]) => {\n if (!Array.isArray(items)) return\n for (const item of items) {\n if (!item || typeof item !== 'object') continue\n const d = item as Record<string, unknown>\n const value = typeof d.value === 'string' ? d.value : null\n if (!value) continue\n map[value] = {\n value,\n label: typeof d.label === 'string' && d.label.length ? d.label : value,\n color: typeof d.color === 'string' && d.color.length ? d.color : null,\n icon: typeof d.icon === 'string' && d.icon.length ? d.icon : null,\n }\n }\n }\n Promise.all(urls.map((url) => apiCall<{ items?: unknown[] }>(url)))\n .then((responses) => {\n for (const res of responses) {\n if (res.ok && Array.isArray(res.result?.items)) merge(res.result.items)\n }\n setStatusMap(map)\n })\n .catch(() => {})\n }, [])\n\n React.useEffect(() => {\n if (!isValidContext(context)) {\n setLoading(false)\n setError(t(\"sales.documents.history.error\", \"Failed to load history.\"))\n return\n }\n\n setLoading(true)\n setError(null)\n apiCall<{ items: TimelineEntry[] }>(\n `/api/sales/document-history?kind=${context.kind}&id=${context.record.id}`\n )\n .then((res) => {\n if (res.ok && Array.isArray(res.result?.items)) {\n setEntries(res.result.items)\n } else {\n setError(t(\"sales.documents.history.error\", \"Failed to load history.\"))\n }\n })\n .catch(() => setError(t(\"sales.documents.history.error\", \"Failed to load history.\")))\n .finally(() => setLoading(false))\n }, [context, t])\n\n const filtered = React.useMemo(\n () => filter === 'all' ? entries : entries.filter(e => e.kind === filter),\n [entries, filter]\n )\n\n return (\n <div className=\"space-y-4\">\n {/* Filter dropdown */}\n <div>\n <FilterDropdown filter={filter} onChange={setFilter} />\n </div>\n\n {/* Content */}\n {loading ? (\n <div className=\"flex items-center justify-center h-24\">\n <Spinner />\n </div>\n ) : error ? (\n <div className=\"text-destructive text-sm\">{error}</div>\n ) : !filtered.length ? (\n <div className=\"text-muted-foreground text-sm py-6 text-center\">\n {t(\"sales.documents.history.empty\", \"No history entries yet.\")}\n </div>\n ) : (\n <div className=\"relative\">\n {filtered.map((entry, index) => (\n <TimelineItem\n key={entry.id}\n entry={entry}\n statusMap={statusMap}\n isLast={index === filtered.length - 1}\n />\n ))}\n </div>\n )}\n </div>\n )\n}\n\nexport default DocumentHistoryWidget\n"],
5
- "mappings": ";AAoEqB,cAyBb,YAzBa;AAlErB,YAAY,WAAW;AACvB,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,oBAAoB,sBAAsB;AACnD,SAAS,UAAU;AAEnB,SAAS,gBAAgB,KAAK,eAAe,MAAM,QAAQ,aAAa,aAAa;AA6BrF,MAAM,iBAAiB,CAAC,QACtB,QAAQ,QACR,OAAO,QAAQ,YACf,UAAU,OACV,YAAY,QACV,IAAwB,SAAS,WAAY,IAAwB,SAAS,YAChF,OAAQ,IAAwB,WAAW,YAC1C,IAAwB,WAAW,QACpC,QAAS,IAAwB,UACjC,OAAQ,IAAwB,OAAO,OAAO;AAEhD,MAAM,aAAa;AAAA,EACjB,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AACX;AAEA,MAAM,mBAAmB;AAAA,EACvB,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AACX;AAEA,MAAM,iBAAiB;AAAA,EACrB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,yCACb,8BAAC,WAAQ,GACX,IACE,QACF,oBAAC,SAAI,WAAU,4BAA4B,iBAAM,IAC/C,CAAC,SAAS,SACZ,oBAAC,SAAI,WAAU,kDACZ,YAAE,iCAAiC,yBAAyB,GAC/D,IAEA,oBAAC,SAAI,WAAU,YACZ,mBAAS,IAAI,CAAC,OAAO,UACpB;AAAA,MAAC;AAAA;AAAA,QAEC;AAAA,QACA;AAAA,QACA,QAAQ,UAAU,SAAS,SAAS;AAAA;AAAA,MAH/B,MAAM;AAAA,IAIb,CACD,GACH;AAAA,KAEJ;AAEJ;AAEA,IAAO,wBAAQ;",
4
+ "sourcesContent": ["\"use client\"\n\nimport * as React from \"react\"\nimport { Spinner } from \"@open-mercato/ui/primitives/spinner\"\nimport { useT, type TranslateFn } from \"@open-mercato/shared/lib/i18n/context\"\nimport { apiCall } from \"@open-mercato/ui/backend/utils/apiCall\"\nimport { formatRelativeTime, formatDateTime } from \"@open-mercato/shared/lib/time\"\nimport { cn } from \"@open-mercato/shared/lib/utils\"\nimport type { InjectionWidgetComponentProps } from '@open-mercato/shared/modules/widgets/injection'\nimport { ArrowRightLeft, Zap, MessageSquare, User, Filter, ChevronDown, Check } from 'lucide-react'\n\nexport type TimelineEntry = {\n id: string\n occurredAt: string\n kind: \"status\" | \"action\" | \"comment\"\n action: string\n actor: { id: string | null; label: string }\n source: \"action_log\" | \"note\"\n metadata?: {\n statusFrom?: string | null\n statusTo?: string | null\n documentKind?: \"order\" | \"quote\"\n commandId?: string\n changedFields?: string[]\n }\n}\n\ntype StatusOption = {\n value: string\n label: string\n color: string | null\n icon: string | null\n}\n\ntype TimelineContext = {\n kind: \"order\" | \"quote\"\n record: { id: string }\n}\n\nconst isValidContext = (ctx: unknown): ctx is TimelineContext =>\n ctx !== null &&\n typeof ctx === 'object' &&\n 'kind' in ctx &&\n 'record' in ctx &&\n ((ctx as TimelineContext).kind === 'order' || (ctx as TimelineContext).kind === 'quote') &&\n typeof (ctx as TimelineContext).record === 'object' &&\n (ctx as TimelineContext).record !== null &&\n 'id' in (ctx as TimelineContext).record &&\n typeof (ctx as TimelineContext).record.id === 'string'\n\nconst KIND_ICONS = {\n status: ArrowRightLeft,\n action: Zap,\n comment: MessageSquare,\n}\n\nconst KIND_ICON_COLORS = {\n status: 'text-foreground',\n action: 'text-foreground',\n comment: 'text-foreground',\n}\n\nconst KIND_BG_COLORS = {\n status: 'bg-muted',\n action: 'bg-muted',\n comment: 'bg-muted',\n}\n\nconst CHANGED_FIELD_LABELS: Record<string, { key: string; fallback: string }> = {\n productId: { key: 'sales.documents.history.fields.product', fallback: 'Product' },\n productVariantId: { key: 'sales.documents.history.fields.variant', fallback: 'Variant' },\n name: { key: 'sales.documents.history.fields.name', fallback: 'Name' },\n description: { key: 'sales.documents.history.fields.description', fallback: 'Description' },\n comment: { key: 'sales.documents.history.fields.comment', fallback: 'Comment' },\n quantity: { key: 'sales.documents.history.fields.quantity', fallback: 'Quantity' },\n quantityUnit: { key: 'sales.documents.history.fields.quantityUnit', fallback: 'Quantity unit' },\n currencyCode: { key: 'sales.documents.history.fields.currency', fallback: 'Currency' },\n unitPriceNet: { key: 'sales.documents.history.fields.unitPriceNet', fallback: 'Net unit price' },\n unitPriceGross: { key: 'sales.documents.history.fields.unitPriceGross', fallback: 'Gross unit price' },\n discountAmount: { key: 'sales.documents.history.fields.discountAmount', fallback: 'Discount amount' },\n discountPercent: { key: 'sales.documents.history.fields.discountPercent', fallback: 'Discount percent' },\n taxRate: { key: 'sales.documents.history.fields.taxRate', fallback: 'Tax class' },\n configuration: { key: 'sales.documents.history.fields.configuration', fallback: 'Configuration' },\n promotionCode: { key: 'sales.documents.history.fields.promotionCode', fallback: 'Promotion code' },\n customFields: { key: 'sales.documents.history.fields.customFields', fallback: 'Custom fields' },\n statusEntryId: { key: 'sales.documents.history.fields.status', fallback: 'Status' },\n}\n\nfunction normalizeChangedField(field: string): string {\n const segment = field.split('.').pop() ?? field\n return segment.replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase())\n}\n\nfunction humanizeChangedField(field: string): string {\n const normalized = normalizeChangedField(field)\n const spaced = normalized.replace(/([a-z0-9])([A-Z])/g, '$1 $2').trim()\n return spaced.length > 0 ? `${spaced[0].toUpperCase()}${spaced.slice(1)}` : field\n}\n\nfunction translateChangedField(t: TranslateFn, field: string): string {\n const normalized = normalizeChangedField(field)\n const translation = CHANGED_FIELD_LABELS[normalized]\n return translation ? t(translation.key, translation.fallback) : humanizeChangedField(field)\n}\n\nfunction translateAction(t: TranslateFn, entry: TimelineEntry): string {\n const commandId = entry.metadata?.commandId\n if (!commandId?.startsWith('sales.')) return entry.action\n return t(`sales.audit.${commandId.slice('sales.'.length)}`, entry.action)\n}\n\nfunction StatusDot({ color, className }: { color: string | null | undefined; className?: string }) {\n if (!color) return <span className={cn('h-2.5 w-2.5 rounded-full bg-muted-foreground/40 border border-border inline-flex', className)} />\n return (\n <span\n className={cn('h-2.5 w-2.5 rounded-full border border-border/70 inline-flex', className)}\n style={{ backgroundColor: color }}\n aria-hidden\n />\n )\n}\n\nfunction StatusTransition({\n statusFrom,\n statusTo,\n statusMap,\n}: {\n statusFrom: string | null | undefined\n statusTo: string | null | undefined\n statusMap: Record<string, StatusOption>\n}) {\n const from = statusFrom ? (statusMap[statusFrom] ?? { value: statusFrom, label: statusFrom, color: null, icon: null }) : null\n const to = statusTo ? (statusMap[statusTo] ?? { value: statusTo, label: statusTo, color: null, icon: null }) : null\n\n return (\n <div className=\"flex items-center gap-1.5 flex-wrap\">\n {from ? (\n <span className=\"inline-flex items-center gap-1 text-xs text-muted-foreground\">\n <StatusDot color={from.color} />\n <span>{from.label}</span>\n </span>\n ) : null}\n {from && to ? (\n <ArrowRightLeft className=\"h-3 w-3 text-muted-foreground/60 shrink-0\" />\n ) : null}\n {to ? (\n <span className=\"inline-flex items-center gap-1 text-xs font-medium text-foreground\">\n <StatusDot color={to.color} />\n <span>{to.label}</span>\n </span>\n ) : null}\n </div>\n )\n}\n\nfunction TimelineItem({\n entry,\n statusMap,\n isLast,\n}: {\n entry: TimelineEntry\n statusMap: Record<string, StatusOption>\n isLast: boolean\n}) {\n const t = useT()\n const KindIcon = KIND_ICONS[entry.kind]\n const relativeTime = formatRelativeTime(entry.occurredAt)\n const absoluteTime = formatDateTime(entry.occurredAt)\n\n const isStatusChange = entry.kind === 'status' && entry.metadata?.statusTo\n const action = translateAction(t, entry)\n const actorLabel = entry.actor.id\n ? entry.actor.label\n : t('sales.documents.history.actor.system', 'System')\n const changedFieldLabels = Array.from(\n new Set((entry.metadata?.changedFields ?? []).map((field) => translateChangedField(t, field))),\n )\n\n return (\n <div data-testid=\"timeline-entry\" className=\"relative flex gap-3\">\n {/* Vertical connector line */}\n {!isLast && (\n <div className=\"absolute left-[11px] top-6 bottom-0 w-px bg-border\" aria-hidden />\n )}\n\n {/* Icon circle */}\n <div\n className={cn(\n 'relative z-10 flex h-6 w-6 shrink-0 items-center justify-center rounded-full border border-border',\n KIND_BG_COLORS[entry.kind],\n )}\n >\n <KindIcon className={cn('h-3 w-3', KIND_ICON_COLORS[entry.kind])} aria-hidden />\n </div>\n\n {/* Content card */}\n <div className=\"flex-1 pb-4\">\n <div className=\"group rounded-lg border bg-card p-3 space-y-1.5\">\n {/* Header: actor + time */}\n <div className=\"flex items-center justify-between gap-2 flex-wrap\">\n <span className=\"inline-flex items-center gap-1.5 text-xs font-medium text-foreground\">\n <User className=\"h-3 w-3 text-muted-foreground\" aria-hidden />\n {actorLabel}\n </span>\n <span\n className=\"text-xs text-muted-foreground\"\n title={absoluteTime ?? undefined}\n >\n {relativeTime ?? absoluteTime}\n </span>\n </div>\n\n {/* Body */}\n {isStatusChange ? (\n <StatusTransition\n statusFrom={entry.metadata?.statusFrom}\n statusTo={entry.metadata?.statusTo}\n statusMap={statusMap}\n />\n ) : (\n <div className=\"space-y-0.5\">\n <div className=\"text-sm text-foreground\">{action}</div>\n {entry.kind === 'action' && changedFieldLabels.length > 0 ? (\n <div className=\"text-xs text-muted-foreground\">\n {t('sales.documents.history.changedFields', 'Changed fields: {fields}', {\n fields: changedFieldLabels.join(', '),\n })}\n </div>\n ) : null}\n </div>\n )}\n </div>\n </div>\n </div>\n )\n}\n\ntype FilterKind = 'all' | 'status' | 'action' | 'comment'\n\ntype FilterOption = { value: FilterKind; label: string }\n\nfunction FilterDropdown({ filter, onChange }: { filter: FilterKind; onChange: (kind: FilterKind) => void }) {\n const t = useT()\n const [open, setOpen] = React.useState(false)\n const ref = React.useRef<HTMLDivElement>(null)\n\n React.useEffect(() => {\n if (!open) return\n function onDocClick(e: MouseEvent) {\n if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)\n }\n function onKey(e: KeyboardEvent) {\n if (e.key === 'Escape') setOpen(false)\n }\n document.addEventListener('mousedown', onDocClick)\n document.addEventListener('keydown', onKey)\n return () => {\n document.removeEventListener('mousedown', onDocClick)\n document.removeEventListener('keydown', onKey)\n }\n }, [open])\n\n const options: FilterOption[] = [\n { value: 'all', label: t('sales.documents.history.filter.all', 'All') },\n { value: 'status', label: t('sales.documents.history.filter.status', 'Status changes') },\n { value: 'action', label: t('sales.documents.history.filter.actions', 'Actions') },\n { value: 'comment', label: t('sales.documents.history.filter.comments', 'Comments') },\n ]\n\n const activeLabel = options.find(o => o.value === filter)?.label\n\n return (\n <div className=\"relative\" ref={ref}>\n <button\n type=\"button\"\n aria-haspopup=\"listbox\"\n aria-expanded={open}\n onClick={() => setOpen(prev => !prev)}\n className=\"inline-flex items-center gap-1.5 rounded-md border border-input bg-background px-2.5 py-1 text-xs font-medium shadow-sm hover:bg-accent hover:text-accent-foreground transition-colors select-none\"\n >\n <Filter className=\"h-3 w-3\" aria-hidden />\n {t('sales.documents.history.filter.label', 'Filters')}\n {filter !== 'all' && (\n <span className=\"text-muted-foreground\">: {activeLabel}</span>\n )}\n <ChevronDown className={cn('h-3 w-3 transition-transform duration-150', open && 'rotate-180')} aria-hidden />\n </button>\n {open && (\n <div\n role=\"listbox\"\n aria-label={t('sales.documents.history.filter.label', 'Filters')}\n className=\"absolute left-0 top-full mt-1 z-dropdown w-48 rounded-md border bg-background p-1 shadow-md\"\n >\n {options.map(opt => (\n <button\n key={opt.value}\n type=\"button\"\n role=\"option\"\n aria-selected={filter === opt.value}\n onClick={() => { onChange(opt.value); setOpen(false) }}\n className=\"flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-accent\"\n >\n <Check className={cn('h-3.5 w-3.5 shrink-0', filter === opt.value ? 'opacity-100' : 'opacity-0')} aria-hidden />\n {opt.label}\n </button>\n ))}\n </div>\n )}\n </div>\n )\n}\n\nexport const DocumentHistoryWidget: React.FC<InjectionWidgetComponentProps<unknown, unknown>> = ({ context }) => {\n const t = useT()\n const [entries, setEntries] = React.useState<TimelineEntry[]>([])\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [statusMap, setStatusMap] = React.useState<Record<string, StatusOption>>({})\n const [filter, setFilter] = React.useState<FilterKind>('all')\n\n React.useEffect(() => {\n const urls = [\n '/api/sales/order-statuses?pageSize=100',\n '/api/sales/shipment-statuses?pageSize=100',\n '/api/sales/payment-statuses?pageSize=100',\n ]\n const map: Record<string, StatusOption> = {}\n const merge = (items: unknown[]) => {\n if (!Array.isArray(items)) return\n for (const item of items) {\n if (!item || typeof item !== 'object') continue\n const d = item as Record<string, unknown>\n const value = typeof d.value === 'string' ? d.value : null\n if (!value) continue\n map[value] = {\n value,\n label: typeof d.label === 'string' && d.label.length ? d.label : value,\n color: typeof d.color === 'string' && d.color.length ? d.color : null,\n icon: typeof d.icon === 'string' && d.icon.length ? d.icon : null,\n }\n }\n }\n Promise.all(urls.map((url) => apiCall<{ items?: unknown[] }>(url)))\n .then((responses) => {\n for (const res of responses) {\n if (res.ok && Array.isArray(res.result?.items)) merge(res.result.items)\n }\n setStatusMap(map)\n })\n .catch(() => {})\n }, [])\n\n React.useEffect(() => {\n if (!isValidContext(context)) {\n setLoading(false)\n setError(t(\"sales.documents.history.error\", \"Failed to load history.\"))\n return\n }\n\n setLoading(true)\n setError(null)\n apiCall<{ items: TimelineEntry[] }>(\n `/api/sales/document-history?kind=${context.kind}&id=${context.record.id}`\n )\n .then((res) => {\n if (res.ok && Array.isArray(res.result?.items)) {\n setEntries(res.result.items)\n } else {\n setError(t(\"sales.documents.history.error\", \"Failed to load history.\"))\n }\n })\n .catch(() => setError(t(\"sales.documents.history.error\", \"Failed to load history.\")))\n .finally(() => setLoading(false))\n }, [context, t])\n\n const filtered = React.useMemo(\n () => filter === 'all' ? entries : entries.filter(e => e.kind === filter),\n [entries, filter]\n )\n\n return (\n <div className=\"space-y-4\">\n {/* Filter dropdown */}\n <div>\n <FilterDropdown filter={filter} onChange={setFilter} />\n </div>\n\n {/* Content */}\n {loading ? (\n <div className=\"flex items-center justify-center h-24\">\n <Spinner />\n </div>\n ) : error ? (\n <div className=\"text-destructive text-sm\">{error}</div>\n ) : !filtered.length ? (\n <div className=\"text-muted-foreground text-sm py-6 text-center\">\n {t(\"sales.documents.history.empty\", \"No history entries yet.\")}\n </div>\n ) : (\n <div className=\"relative\">\n {filtered.map((entry, index) => (\n <TimelineItem\n key={entry.id}\n entry={entry}\n statusMap={statusMap}\n isLast={index === filtered.length - 1}\n />\n ))}\n </div>\n )}\n </div>\n )\n}\n\nexport default DocumentHistoryWidget\n"],
5
+ "mappings": ";AAgHqB,cAyBb,YAzBa;AA9GrB,YAAY,WAAW;AACvB,SAAS,eAAe;AACxB,SAAS,YAA8B;AACvC,SAAS,eAAe;AACxB,SAAS,oBAAoB,sBAAsB;AACnD,SAAS,UAAU;AAEnB,SAAS,gBAAgB,KAAK,eAAe,MAAM,QAAQ,aAAa,aAAa;AA8BrF,MAAM,iBAAiB,CAAC,QACtB,QAAQ,QACR,OAAO,QAAQ,YACf,UAAU,OACV,YAAY,QACV,IAAwB,SAAS,WAAY,IAAwB,SAAS,YAChF,OAAQ,IAAwB,WAAW,YAC1C,IAAwB,WAAW,QACpC,QAAS,IAAwB,UACjC,OAAQ,IAA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oBAAC,SAAI,WAAU,kDACZ,YAAE,iCAAiC,yBAAyB,GAC/D,IAEA,oBAAC,SAAI,WAAU,YACZ,mBAAS,IAAI,CAAC,OAAO,UACpB;AAAA,MAAC;AAAA;AAAA,QAEC;AAAA,QACA;AAAA,QACA,QAAQ,UAAU,SAAS,SAAS;AAAA;AAAA,MAH/B,MAAM;AAAA,IAIb,CACD,GACH;AAAA,KAEJ;AAEJ;AAEA,IAAO,wBAAQ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/core",
3
- "version": "0.6.7-develop.6756.1.5300c41145",
3
+ "version": "0.6.7-develop.6758.1.697eade236",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -254,16 +254,16 @@
254
254
  "zod": "^4.4.3"
255
255
  },
256
256
  "peerDependencies": {
257
- "@open-mercato/ai-assistant": "0.6.7-develop.6756.1.5300c41145",
258
- "@open-mercato/shared": "0.6.7-develop.6756.1.5300c41145",
259
- "@open-mercato/ui": "0.6.7-develop.6756.1.5300c41145",
257
+ "@open-mercato/ai-assistant": "0.6.7-develop.6758.1.697eade236",
258
+ "@open-mercato/shared": "0.6.7-develop.6758.1.697eade236",
259
+ "@open-mercato/ui": "0.6.7-develop.6758.1.697eade236",
260
260
  "react": "^19.0.0",
261
261
  "react-dom": "^19.0.0"
262
262
  },
263
263
  "devDependencies": {
264
- "@open-mercato/ai-assistant": "0.6.7-develop.6756.1.5300c41145",
265
- "@open-mercato/shared": "0.6.7-develop.6756.1.5300c41145",
266
- "@open-mercato/ui": "0.6.7-develop.6756.1.5300c41145",
264
+ "@open-mercato/ai-assistant": "0.6.7-develop.6758.1.697eade236",
265
+ "@open-mercato/shared": "0.6.7-develop.6758.1.697eade236",
266
+ "@open-mercato/ui": "0.6.7-develop.6758.1.697eade236",
267
267
  "@testing-library/dom": "^10.4.1",
268
268
  "@testing-library/jest-dom": "^6.9.1",
269
269
  "@testing-library/react": "^16.3.1",
@@ -171,6 +171,7 @@ const historyEntrySchema = z.object({
171
171
  statusTo: z.string().nullable().optional(),
172
172
  documentKind: z.enum(['order', 'quote']).optional(),
173
173
  commandId: z.string().optional(),
174
+ changedFields: z.array(z.string()).optional(),
174
175
  }).optional(),
175
176
  })
176
177
 
@@ -927,8 +927,27 @@
927
927
  "sales.documents.form.submit": "Erstellen",
928
928
  "sales.documents.form.success": "Verkaufsdokument erstellt.",
929
929
  "sales.documents.form.title": "Verkaufsdokument erstellen",
930
+ "sales.documents.history.actor.system": "System",
931
+ "sales.documents.history.changedFields": "Geänderte Felder: {fields}",
930
932
  "sales.documents.history.empty": "Noch keine Verlaufseinträge.",
931
933
  "sales.documents.history.error": "Verlauf konnte nicht geladen werden.",
934
+ "sales.documents.history.fields.comment": "Kommentar",
935
+ "sales.documents.history.fields.configuration": "Konfiguration",
936
+ "sales.documents.history.fields.currency": "Währung",
937
+ "sales.documents.history.fields.customFields": "Benutzerdefinierte Felder",
938
+ "sales.documents.history.fields.description": "Beschreibung",
939
+ "sales.documents.history.fields.discountAmount": "Rabattbetrag",
940
+ "sales.documents.history.fields.discountPercent": "Rabattprozentsatz",
941
+ "sales.documents.history.fields.name": "Name",
942
+ "sales.documents.history.fields.product": "Produkt",
943
+ "sales.documents.history.fields.promotionCode": "Aktionscode",
944
+ "sales.documents.history.fields.quantity": "Menge",
945
+ "sales.documents.history.fields.quantityUnit": "Mengeneinheit",
946
+ "sales.documents.history.fields.status": "Status",
947
+ "sales.documents.history.fields.taxRate": "Steuerklasse",
948
+ "sales.documents.history.fields.unitPriceGross": "Bruttostückpreis",
949
+ "sales.documents.history.fields.unitPriceNet": "Nettostückpreis",
950
+ "sales.documents.history.fields.variant": "Variante",
932
951
  "sales.documents.history.filter.actions": "Aktionen",
933
952
  "sales.documents.history.filter.all": "Alle",
934
953
  "sales.documents.history.filter.comments": "Kommentare",
@@ -927,8 +927,27 @@
927
927
  "sales.documents.form.submit": "Create",
928
928
  "sales.documents.form.success": "Sales document created.",
929
929
  "sales.documents.form.title": "Create sales document",
930
+ "sales.documents.history.actor.system": "System",
931
+ "sales.documents.history.changedFields": "Changed fields: {fields}",
930
932
  "sales.documents.history.empty": "No history entries yet.",
931
933
  "sales.documents.history.error": "Failed to load history.",
934
+ "sales.documents.history.fields.comment": "Comment",
935
+ "sales.documents.history.fields.configuration": "Configuration",
936
+ "sales.documents.history.fields.currency": "Currency",
937
+ "sales.documents.history.fields.customFields": "Custom fields",
938
+ "sales.documents.history.fields.description": "Description",
939
+ "sales.documents.history.fields.discountAmount": "Discount amount",
940
+ "sales.documents.history.fields.discountPercent": "Discount percent",
941
+ "sales.documents.history.fields.name": "Name",
942
+ "sales.documents.history.fields.product": "Product",
943
+ "sales.documents.history.fields.promotionCode": "Promotion code",
944
+ "sales.documents.history.fields.quantity": "Quantity",
945
+ "sales.documents.history.fields.quantityUnit": "Quantity unit",
946
+ "sales.documents.history.fields.status": "Status",
947
+ "sales.documents.history.fields.taxRate": "Tax class",
948
+ "sales.documents.history.fields.unitPriceGross": "Gross unit price",
949
+ "sales.documents.history.fields.unitPriceNet": "Net unit price",
950
+ "sales.documents.history.fields.variant": "Variant",
932
951
  "sales.documents.history.filter.actions": "Actions",
933
952
  "sales.documents.history.filter.all": "All",
934
953
  "sales.documents.history.filter.comments": "Comments",
@@ -927,8 +927,27 @@
927
927
  "sales.documents.form.submit": "Crear",
928
928
  "sales.documents.form.success": "Documento de ventas creado.",
929
929
  "sales.documents.form.title": "Crear documento de ventas",
930
+ "sales.documents.history.actor.system": "Sistema",
931
+ "sales.documents.history.changedFields": "Campos modificados: {fields}",
930
932
  "sales.documents.history.empty": "Aún no hay entradas en el historial.",
931
933
  "sales.documents.history.error": "No se pudo cargar el historial.",
934
+ "sales.documents.history.fields.comment": "Comentario",
935
+ "sales.documents.history.fields.configuration": "Configuración",
936
+ "sales.documents.history.fields.currency": "Moneda",
937
+ "sales.documents.history.fields.customFields": "Campos personalizados",
938
+ "sales.documents.history.fields.description": "Descripción",
939
+ "sales.documents.history.fields.discountAmount": "Importe de descuento",
940
+ "sales.documents.history.fields.discountPercent": "Porcentaje de descuento",
941
+ "sales.documents.history.fields.name": "Nombre",
942
+ "sales.documents.history.fields.product": "Producto",
943
+ "sales.documents.history.fields.promotionCode": "Código promocional",
944
+ "sales.documents.history.fields.quantity": "Cantidad",
945
+ "sales.documents.history.fields.quantityUnit": "Unidad de cantidad",
946
+ "sales.documents.history.fields.status": "Estado",
947
+ "sales.documents.history.fields.taxRate": "Clase fiscal",
948
+ "sales.documents.history.fields.unitPriceGross": "Precio unitario bruto",
949
+ "sales.documents.history.fields.unitPriceNet": "Precio unitario neto",
950
+ "sales.documents.history.fields.variant": "Variante",
932
951
  "sales.documents.history.filter.actions": "Acciones",
933
952
  "sales.documents.history.filter.all": "Todos",
934
953
  "sales.documents.history.filter.comments": "Comentarios",
@@ -927,8 +927,27 @@
927
927
  "sales.documents.form.submit": "Utwórz",
928
928
  "sales.documents.form.success": "Dokument sprzedaży utworzony.",
929
929
  "sales.documents.form.title": "Utwórz dokument sprzedaży",
930
+ "sales.documents.history.actor.system": "System",
931
+ "sales.documents.history.changedFields": "Zmienione pola: {fields}",
930
932
  "sales.documents.history.empty": "Brak wpisów w historii.",
931
933
  "sales.documents.history.error": "Nie udało się załadować historii.",
934
+ "sales.documents.history.fields.comment": "Komentarz",
935
+ "sales.documents.history.fields.configuration": "Konfiguracja",
936
+ "sales.documents.history.fields.currency": "Waluta",
937
+ "sales.documents.history.fields.customFields": "Pola niestandardowe",
938
+ "sales.documents.history.fields.description": "Opis",
939
+ "sales.documents.history.fields.discountAmount": "Kwota rabatu",
940
+ "sales.documents.history.fields.discountPercent": "Procent rabatu",
941
+ "sales.documents.history.fields.name": "Nazwa",
942
+ "sales.documents.history.fields.product": "Produkt",
943
+ "sales.documents.history.fields.promotionCode": "Kod promocyjny",
944
+ "sales.documents.history.fields.quantity": "Ilość",
945
+ "sales.documents.history.fields.quantityUnit": "Jednostka ilości",
946
+ "sales.documents.history.fields.status": "Status",
947
+ "sales.documents.history.fields.taxRate": "Klasa podatkowa",
948
+ "sales.documents.history.fields.unitPriceGross": "Cena jednostkowa brutto",
949
+ "sales.documents.history.fields.unitPriceNet": "Cena jednostkowa netto",
950
+ "sales.documents.history.fields.variant": "Wariant",
932
951
  "sales.documents.history.filter.actions": "Akcje",
933
952
  "sales.documents.history.filter.all": "Wszystkie",
934
953
  "sales.documents.history.filter.comments": "Komentarze",
@@ -1,4 +1,5 @@
1
1
  import type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'
2
+ import { isRecord } from '@open-mercato/shared/lib/utils'
2
3
  import type { SalesNote } from '../data/entities'
3
4
 
4
5
  export type HistoryEntry = {
@@ -13,9 +14,90 @@ export type HistoryEntry = {
13
14
  statusTo?: string | null
14
15
  documentKind?: 'order' | 'quote'
15
16
  commandId?: string
17
+ changedFields?: string[]
16
18
  }
17
19
  }
18
20
 
21
+ const DOCUMENT_LINE_UPSERT_COMMANDS = new Set([
22
+ 'sales.orders.lines.upsert',
23
+ 'sales.quotes.lines.upsert',
24
+ ])
25
+
26
+ const USER_EDITABLE_LINE_FIELDS = [
27
+ 'productId',
28
+ 'productVariantId',
29
+ 'name',
30
+ 'description',
31
+ 'comment',
32
+ 'quantity',
33
+ 'quantityUnit',
34
+ 'currencyCode',
35
+ 'unitPriceNet',
36
+ 'unitPriceGross',
37
+ 'discountAmount',
38
+ 'discountPercent',
39
+ 'taxRate',
40
+ 'configuration',
41
+ 'promotionCode',
42
+ 'customFields',
43
+ 'statusEntryId',
44
+ ] as const
45
+
46
+ function readSnapshotLines(snapshot: unknown): Array<Record<string, unknown>> {
47
+ if (!isRecord(snapshot) || !Array.isArray(snapshot.lines)) return []
48
+ return snapshot.lines.filter(isRecord)
49
+ }
50
+
51
+ function historyValuesEqual(left: unknown, right: unknown): boolean {
52
+ if (Object.is(left, right)) return true
53
+ try {
54
+ return JSON.stringify(left) === JSON.stringify(right)
55
+ } catch {
56
+ return false
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Derives a concise, user-editable field list for document line updates.
62
+ * Calculated totals and normalized UoM fields are intentionally excluded so
63
+ * the timeline describes the user's edit rather than downstream recalculation.
64
+ */
65
+ function deriveDocumentLineChangedFields(log: ActionLog): string[] {
66
+ if (!DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) return []
67
+
68
+ const beforeById = new Map(
69
+ readSnapshotLines(log.snapshotBefore)
70
+ .filter((line) => typeof line.id === 'string')
71
+ .map((line) => [line.id as string, line]),
72
+ )
73
+ const afterLines = readSnapshotLines(log.snapshotAfter)
74
+
75
+ const changedFields = new Set<string>()
76
+ for (const afterLine of afterLines) {
77
+ if (typeof afterLine.id !== 'string') continue
78
+ const beforeLine = beforeById.get(afterLine.id)
79
+ if (!beforeLine) continue
80
+
81
+ for (const field of USER_EDITABLE_LINE_FIELDS) {
82
+ if (!historyValuesEqual(beforeLine[field], afterLine[field])) {
83
+ changedFields.add(field)
84
+ }
85
+ }
86
+ }
87
+
88
+ return USER_EDITABLE_LINE_FIELDS.filter((field) => changedFields.has(field))
89
+ }
90
+
91
+ export function deriveHistoryChangedFields(log: ActionLog): string[] {
92
+ if (DOCUMENT_LINE_UPSERT_COMMANDS.has(log.commandId)) {
93
+ return deriveDocumentLineChangedFields(log)
94
+ }
95
+ const persisted = Array.isArray(log.changedFields)
96
+ ? log.changedFields.filter((field): field is string => typeof field === 'string' && field.trim().length > 0)
97
+ : []
98
+ return Array.from(new Set(persisted))
99
+ }
100
+
19
101
  function extractStatusFromSnapshot(snapshot: unknown): string | null {
20
102
  if (!snapshot || typeof snapshot !== 'object') return null
21
103
  const s = snapshot as Record<string, unknown>
@@ -64,9 +146,14 @@ export function normalizeActionLogToHistoryEntry(
64
146
  displayUsers?: Record<string, string>,
65
147
  ): HistoryEntry {
66
148
  const statusChange = detectStatusChange(log)
149
+ const changedFields = deriveHistoryChangedFields(log)
67
150
  let entryKind: 'status' | 'action' = 'action'
68
151
  let action = log.actionLabel || log.commandId
69
- let metadata: HistoryEntry['metadata'] = { documentKind: kind, commandId: log.commandId }
152
+ let metadata: HistoryEntry['metadata'] = {
153
+ documentKind: kind,
154
+ commandId: log.commandId,
155
+ ...(changedFields.length > 0 ? { changedFields } : {}),
156
+ }
70
157
  if (statusChange) {
71
158
  const hasStatusValues = statusChange.statusFrom != null || statusChange.statusTo != null
72
159
  if (hasStatusValues) {
@@ -2,7 +2,7 @@
2
2
 
3
3
  import * as React from "react"
4
4
  import { Spinner } from "@open-mercato/ui/primitives/spinner"
5
- import { useT } from "@open-mercato/shared/lib/i18n/context"
5
+ import { useT, type TranslateFn } from "@open-mercato/shared/lib/i18n/context"
6
6
  import { apiCall } from "@open-mercato/ui/backend/utils/apiCall"
7
7
  import { formatRelativeTime, formatDateTime } from "@open-mercato/shared/lib/time"
8
8
  import { cn } from "@open-mercato/shared/lib/utils"
@@ -21,6 +21,7 @@ export type TimelineEntry = {
21
21
  statusTo?: string | null
22
22
  documentKind?: "order" | "quote"
23
23
  commandId?: string
24
+ changedFields?: string[]
24
25
  }
25
26
  }
26
27
 
@@ -65,6 +66,49 @@ const KIND_BG_COLORS = {
65
66
  comment: 'bg-muted',
66
67
  }
67
68
 
69
+ const CHANGED_FIELD_LABELS: Record<string, { key: string; fallback: string }> = {
70
+ productId: { key: 'sales.documents.history.fields.product', fallback: 'Product' },
71
+ productVariantId: { key: 'sales.documents.history.fields.variant', fallback: 'Variant' },
72
+ name: { key: 'sales.documents.history.fields.name', fallback: 'Name' },
73
+ description: { key: 'sales.documents.history.fields.description', fallback: 'Description' },
74
+ comment: { key: 'sales.documents.history.fields.comment', fallback: 'Comment' },
75
+ quantity: { key: 'sales.documents.history.fields.quantity', fallback: 'Quantity' },
76
+ quantityUnit: { key: 'sales.documents.history.fields.quantityUnit', fallback: 'Quantity unit' },
77
+ currencyCode: { key: 'sales.documents.history.fields.currency', fallback: 'Currency' },
78
+ unitPriceNet: { key: 'sales.documents.history.fields.unitPriceNet', fallback: 'Net unit price' },
79
+ unitPriceGross: { key: 'sales.documents.history.fields.unitPriceGross', fallback: 'Gross unit price' },
80
+ discountAmount: { key: 'sales.documents.history.fields.discountAmount', fallback: 'Discount amount' },
81
+ discountPercent: { key: 'sales.documents.history.fields.discountPercent', fallback: 'Discount percent' },
82
+ taxRate: { key: 'sales.documents.history.fields.taxRate', fallback: 'Tax class' },
83
+ configuration: { key: 'sales.documents.history.fields.configuration', fallback: 'Configuration' },
84
+ promotionCode: { key: 'sales.documents.history.fields.promotionCode', fallback: 'Promotion code' },
85
+ customFields: { key: 'sales.documents.history.fields.customFields', fallback: 'Custom fields' },
86
+ statusEntryId: { key: 'sales.documents.history.fields.status', fallback: 'Status' },
87
+ }
88
+
89
+ function normalizeChangedField(field: string): string {
90
+ const segment = field.split('.').pop() ?? field
91
+ return segment.replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase())
92
+ }
93
+
94
+ function humanizeChangedField(field: string): string {
95
+ const normalized = normalizeChangedField(field)
96
+ const spaced = normalized.replace(/([a-z0-9])([A-Z])/g, '$1 $2').trim()
97
+ return spaced.length > 0 ? `${spaced[0].toUpperCase()}${spaced.slice(1)}` : field
98
+ }
99
+
100
+ function translateChangedField(t: TranslateFn, field: string): string {
101
+ const normalized = normalizeChangedField(field)
102
+ const translation = CHANGED_FIELD_LABELS[normalized]
103
+ return translation ? t(translation.key, translation.fallback) : humanizeChangedField(field)
104
+ }
105
+
106
+ function translateAction(t: TranslateFn, entry: TimelineEntry): string {
107
+ const commandId = entry.metadata?.commandId
108
+ if (!commandId?.startsWith('sales.')) return entry.action
109
+ return t(`sales.audit.${commandId.slice('sales.'.length)}`, entry.action)
110
+ }
111
+
68
112
  function StatusDot({ color, className }: { color: string | null | undefined; className?: string }) {
69
113
  if (!color) return <span className={cn('h-2.5 w-2.5 rounded-full bg-muted-foreground/40 border border-border inline-flex', className)} />
70
114
  return (
@@ -118,11 +162,19 @@ function TimelineItem({
118
162
  statusMap: Record<string, StatusOption>
119
163
  isLast: boolean
120
164
  }) {
165
+ const t = useT()
121
166
  const KindIcon = KIND_ICONS[entry.kind]
122
167
  const relativeTime = formatRelativeTime(entry.occurredAt)
123
168
  const absoluteTime = formatDateTime(entry.occurredAt)
124
169
 
125
170
  const isStatusChange = entry.kind === 'status' && entry.metadata?.statusTo
171
+ const action = translateAction(t, entry)
172
+ const actorLabel = entry.actor.id
173
+ ? entry.actor.label
174
+ : t('sales.documents.history.actor.system', 'System')
175
+ const changedFieldLabels = Array.from(
176
+ new Set((entry.metadata?.changedFields ?? []).map((field) => translateChangedField(t, field))),
177
+ )
126
178
 
127
179
  return (
128
180
  <div data-testid="timeline-entry" className="relative flex gap-3">
@@ -148,7 +200,7 @@ function TimelineItem({
148
200
  <div className="flex items-center justify-between gap-2 flex-wrap">
149
201
  <span className="inline-flex items-center gap-1.5 text-xs font-medium text-foreground">
150
202
  <User className="h-3 w-3 text-muted-foreground" aria-hidden />
151
- {entry.actor.label}
203
+ {actorLabel}
152
204
  </span>
153
205
  <span
154
206
  className="text-xs text-muted-foreground"
@@ -166,7 +218,16 @@ function TimelineItem({
166
218
  statusMap={statusMap}
167
219
  />
168
220
  ) : (
169
- <div className="text-sm text-foreground">{entry.action}</div>
221
+ <div className="space-y-0.5">
222
+ <div className="text-sm text-foreground">{action}</div>
223
+ {entry.kind === 'action' && changedFieldLabels.length > 0 ? (
224
+ <div className="text-xs text-muted-foreground">
225
+ {t('sales.documents.history.changedFields', 'Changed fields: {fields}', {
226
+ fields: changedFieldLabels.join(', '),
227
+ })}
228
+ </div>
229
+ ) : null}
230
+ </div>
170
231
  )}
171
232
  </div>
172
233
  </div>