Refactor log_request_metadata function

This commit is contained in:
gpt-engineer-app[bot]
2025-11-03 20:58:52 +00:00
parent 50e560f7cd
commit 19b1451f32
11 changed files with 992 additions and 63 deletions

View File

@@ -191,3 +191,68 @@ export async function readItemChangeFields(
return acc;
}, {} as Record<string, { old_value: string | null; new_value: string | null }>);
}
/**
* Write profile change fields to relational table
* Replaces JSONB profile_audit_log.changes column
*/
export async function writeProfileChangeFields(
auditLogId: string,
changes: Record<string, { old_value?: unknown; new_value?: unknown }>
): Promise<void> {
if (!changes || Object.keys(changes).length === 0) return;
const entries = Object.entries(changes).map(([fieldName, change]) => ({
audit_log_id: auditLogId,
field_name: fieldName,
old_value: change.old_value !== undefined
? (typeof change.old_value === 'object' ? JSON.stringify(change.old_value) : String(change.old_value))
: null,
new_value: change.new_value !== undefined
? (typeof change.new_value === 'object' ? JSON.stringify(change.new_value) : String(change.new_value))
: null,
}));
const { error } = await supabase
.from('profile_change_fields')
.insert(entries);
if (error) {
logger.error('Failed to write profile change fields', { error, auditLogId });
throw error;
}
}
/**
* Write conflict detail fields to relational table
* Replaces JSONB conflict_resolutions.conflict_details column
*/
export async function writeConflictDetailFields(
conflictResolutionId: string,
conflictData: Record<string, unknown>
): Promise<void> {
if (!conflictData || Object.keys(conflictData).length === 0) return;
const entries = Object.entries(conflictData).map(([fieldName, value]) => ({
conflict_resolution_id: conflictResolutionId,
field_name: fieldName,
conflicting_value_1: typeof value === 'object' && value !== null && 'v1' in value
? String((value as any).v1)
: null,
conflicting_value_2: typeof value === 'object' && value !== null && 'v2' in value
? String((value as any).v2)
: null,
resolved_value: typeof value === 'object' && value !== null && 'resolved' in value
? String((value as any).resolved)
: null,
}));
const { error } = await supabase
.from('conflict_detail_fields')
.insert(entries);
if (error) {
logger.error('Failed to write conflict detail fields', { error, conflictResolutionId });
throw error;
}
}