feat: Implement photo change detection

This commit is contained in:
gpt-engineer-app[bot]
2025-10-03 15:54:46 +00:00
parent 86fb99c696
commit b047291eb6
3 changed files with 117 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import type { SubmissionItemData } from '@/types/submissions';
import { supabase } from '@/integrations/supabase/client';
export interface FieldChange {
field: string;
@@ -17,11 +18,16 @@ export interface ImageChange {
export interface PhotoChange {
type: 'added' | 'edited' | 'deleted';
photoUrl: string;
title?: string;
caption?: string;
oldCaption?: string;
newCaption?: string;
photos?: Array<{ url: string; title?: string; caption?: string }>;
photo?: {
url: string;
title?: string;
caption?: string;
oldCaption?: string;
newCaption?: string;
oldTitle?: string;
newTitle?: string;
};
}
export interface ChangesSummary {
@@ -35,10 +41,54 @@ export interface ChangesSummary {
totalChanges: number;
}
/**
* Detects photo changes for a submission
*/
async function detectPhotoChanges(submissionId: string): Promise<PhotoChange[]> {
const changes: PhotoChange[] = [];
try {
// Fetch photo submission with items
const { data: photoSubmission, error } = await supabase
.from('photo_submissions')
.select(`
*,
items:photo_submission_items(*)
`)
.eq('submission_id', submissionId)
.maybeSingle();
if (error) {
console.error('Error fetching photo submissions:', error);
return changes;
}
if (photoSubmission?.items && photoSubmission.items.length > 0) {
// For now, treat all photos as additions
// TODO: Implement edit/delete detection by comparing with existing entity photos
changes.push({
type: 'added',
photos: photoSubmission.items.map((item: any) => ({
url: item.cloudflare_image_url,
title: item.title,
caption: item.caption
}))
});
}
} catch (err) {
console.error('Error detecting photo changes:', err);
}
return changes;
}
/**
* Detects what changed between original_data and item_data
*/
export function detectChanges(item: { item_data?: any; original_data?: any; item_type: string }): ChangesSummary {
export async function detectChanges(
item: { item_data?: any; original_data?: any; item_type: string },
submissionId?: string
): Promise<ChangesSummary> {
const itemData = item.item_data || {};
const originalData = item.original_data || {};
@@ -124,15 +174,18 @@ export function detectChanges(item: { item_data?: any; original_data?: any; item
// Get entity name
const entityName = itemData.name || originalData?.name || 'Unknown';
// Detect photo changes if submissionId provided
const photoChanges = submissionId ? await detectPhotoChanges(submissionId) : [];
return {
action,
entityType: item.item_type,
entityName,
fieldChanges,
imageChanges,
photoChanges: [], // Will be populated by component with submissionId
photoChanges,
hasLocationChange,
totalChanges: fieldChanges.length + imageChanges.length + (hasLocationChange ? 1 : 0)
totalChanges: fieldChanges.length + imageChanges.length + photoChanges.length + (hasLocationChange ? 1 : 0)
};
}