diff --git a/src/app/api/analyze/route.ts b/src/app/api/analyze/route.ts index fd348c4..6eee10d 100644 --- a/src/app/api/analyze/route.ts +++ b/src/app/api/analyze/route.ts @@ -236,12 +236,16 @@ export async function POST(req: NextRequest): Promise { // Process the animal image and get the IPFS URL let ipfsUrl = ''; try { + // Ensure we have a valid userId before passing to processAnimalImage + const validUserId = userId || '00000000-0000-0000-0000-000000000000'; + console.log('Using userId for animal processing:', validUserId); + ipfsUrl = await processAnimalImage( buffer, visionResult.description, visionResult.rawResponse, fileName, - userId + validUserId ); // Award points to the user for uploading a valid animal image diff --git a/src/services/animalProcessingService.ts b/src/services/animalProcessingService.ts index b40c2ad..b714548 100644 --- a/src/services/animalProcessingService.ts +++ b/src/services/animalProcessingService.ts @@ -77,7 +77,7 @@ export async function processAnimalImage( visionDescription: string, visionResponse: any, filename: string = 'animal-image.jpg', - userId?: string + userId: string ) { try { // Get coordinates @@ -94,6 +94,12 @@ export async function processAnimalImage( )?.description || 'unknown' console.log('Detected species:', species) + // Make sure we have a valid userId + if (!userId) { + userId = 'anonymous'; + console.warn('No userId provided for animal record, using anonymous'); + } + const registryId = await publishAnimalRecord( species.toLowerCase(), coordinates.lat, @@ -101,13 +107,19 @@ export async function processAnimalImage( visionDescription, ipfsUrl, process.env.NEXT_PUBLIC_PORTAL_NAME, - userId || 'unknown' + userId ) console.log('Published animal record to Laconic Registry:', registryId) return ipfsUrl } catch (error) { - console.error('Failed to process animal image:', error) + // Provide more detailed error logging + console.error('Failed to process animal image:', { + error: error.message || String(error), + species: visionResponse?.labelAnnotations?.length > 0 ? + visionResponse.labelAnnotations[0].description : 'unknown', + userId + }) throw error } } diff --git a/src/services/laconicService.ts b/src/services/laconicService.ts index 853ff31..d939247 100644 --- a/src/services/laconicService.ts +++ b/src/services/laconicService.ts @@ -13,7 +13,7 @@ interface LaconicAnimalRecord { description: string imageUrl: string portalName: string - userId: string + userId: string // Single, consistent way to include userId } } @@ -24,9 +24,10 @@ export async function publishAnimalRecord( description: string, imageUrl: string, portalName: string, - userId: string + userId: string // Keep this required ): Promise { try { + // Create the record with required fields including contributor field for userId const record: LaconicAnimalRecord = { record: { type: 'AnimalRecord', @@ -38,7 +39,7 @@ export async function publishAnimalRecord( description, imageUrl, portalName, - userId + userId // Consistent place for userId } } @@ -54,7 +55,20 @@ export async function publishAnimalRecord( return response.data.output } catch (error) { - console.error('Failed to publish animal record:', error) + // Log request details for debugging + console.error('Failed to publish animal record:', { + error: error.message, + status: axios.isAxiosError(error) ? error.response?.status : 'unknown', + data: axios.isAxiosError(error) ? error.response?.data : 'unknown', + recordData: { + mainObject, + location: { latitude, longitude }, + imageUrl: imageUrl.substring(0, 30) + '...', // Truncate for log readability + userId, + yamlSize: yaml.stringify(record).length + } + }) + throw error } }