Refactor: Remove item_edit_history_view

This commit is contained in:
gpt-engineer-app[bot]
2025-11-02 23:55:49 +00:00
parent 9c4b80e454
commit c59d8e40d5
8 changed files with 369 additions and 63 deletions

View File

@@ -1182,6 +1182,13 @@ export async function editSubmissionItem(
((currentItem.original_data && Object.keys(currentItem.original_data).length > 0) ? 'edit' : 'create');
if (isModerator) {
// Phase 4: Track changes for edit history
const changes = {
before: currentItem.item_data,
after: newData,
timestamp: new Date().toISOString(),
};
// Moderators can edit directly
const { error: updateError } = await supabase
.from('submission_items')
@@ -1195,6 +1202,24 @@ export async function editSubmissionItem(
if (updateError) throw updateError;
// Phase 4: Record edit history
const { error: historyError } = await supabase
.from('item_edit_history')
.insert({
item_id: itemId,
editor_id: userId,
changes: changes,
});
if (historyError) {
logger.error('Failed to record edit history', {
itemId,
editorId: userId,
error: historyError.message,
});
// Don't fail the whole operation if history tracking fails
}
// CRITICAL: Create version history if this is an entity edit (not photo)
// Only create version if this item has already been approved (has approved_entity_id)
if (currentItem.item_type !== 'photo' && currentItem.approved_entity_id) {
@@ -1309,3 +1334,37 @@ export async function escalateSubmission(
}
}
}
/**
* Phase 4: Fetch edit history for a submission item
* Returns all edits with editor information
*/
export async function fetchEditHistory(itemId: string) {
try {
const { data, error } = await supabase
.from('item_edit_history')
.select(`
id,
changes,
edited_at,
editor:profiles!item_edit_history_editor_id_fkey (
user_id,
username,
display_name,
avatar_url
)
`)
.eq('item_id', itemId)
.order('edited_at', { ascending: false });
if (error) throw error;
return data || [];
} catch (error: unknown) {
logger.error('Error fetching edit history', {
itemId,
error: getErrorMessage(error),
});
return [];
}
}