feat: improve AI resilience, add background enrichment loading states, and fix duplicate identifier in TagSelector

This commit is contained in:
2025-12-23 11:38:16 +01:00
parent 1d98bb9947
commit c134c78a2c
37 changed files with 1906 additions and 786 deletions

View File

@@ -14,12 +14,12 @@ export async function saveBottle(
try {
const metadata = BottleMetadataSchema.parse(rawMetadata);
const { data: { session } } = await supabase.auth.getSession();
if (!session) {
const { data: { user } } = await supabase.auth.getUser();
if (!user) {
throw new Error('Nicht autorisiert oder Session abgelaufen.');
}
const userId = session.user.id;
const userId = user.id;
let finalImageUrl = preUploadedUrl;
// 1. Upload Image to Storage if not already uploaded
@@ -50,6 +50,26 @@ export async function saveBottle(
throw new Error('Kein Bild zum Speichern vorhanden.');
}
// 1.5 Deduplication Check
// If a bottle with the same name/distillery was created by the same user in the last 5 minutes,
// we treat it as a duplicate (likely from a race condition or double sync).
const fiveMinutesAgo = new Date(Date.now() - 5 * 60 * 1000).toISOString();
const { data: existingBottle } = await supabase
.from('bottles')
.select('*')
.eq('user_id', userId)
.eq('name', metadata.name)
.eq('distillery', metadata.distillery)
.gte('created_at', fiveMinutesAgo)
.order('created_at', { ascending: false })
.limit(1)
.maybeSingle();
if (existingBottle) {
console.log('[saveBottle] Potential duplicate detected, returning existing bottle:', existingBottle.id);
return { success: true, data: existingBottle };
}
// 2. Save Metadata to Database
const { data: bottleData, error: dbError } = await supabase
.from('bottles')
@@ -64,7 +84,7 @@ export async function saveBottle(
image_url: finalImageUrl,
status: 'sealed',
is_whisky: metadata.is_whisky ?? true,
confidence: metadata.confidence ?? 100,
confidence: metadata.confidence ? Math.round(metadata.confidence * 100) : 100,
distilled_at: metadata.distilled_at,
bottled_at: metadata.bottled_at,
batch_info: metadata.batch_info,