import type { AuditFieldChanges } from '@admin/actions/audit/types' import { getChangedFieldNames } from '@admin/utils/audit/get-changed-field-names' import { shouldTrackField } from '@admin/utils/audit/should-track-field' import { toAuditFieldValue } from '@admin/utils/audit/to-audit-field-value' export function buildAuditFieldChanges( previous: Record | null | undefined, next: Record | null | undefined ): AuditFieldChanges { const changed: AuditFieldChanges['changed'] = {} for (const fieldName of getChangedFieldNames(previous, next)) { changed[fieldName] = { before: toAuditFieldValue(previous?.[fieldName]), after: toAuditFieldValue(next?.[fieldName]) } } const fieldNames = new Set( [...Object.keys(previous ?? {}), ...Object.keys(next ?? {})].filter(shouldTrackField) ) return { changed, fieldCount: fieldNames.size } }