localgreenchain/pages/api/transport/harvest.ts
Claude 2502308bcb
Add complete API routes for transport, demand, and vertical farming systems
Transport API (11 endpoints):
- POST /api/transport/seed-acquisition - Record seed acquisition events
- POST /api/transport/planting - Record planting events
- POST /api/transport/growing - Record growing transport events
- POST /api/transport/harvest - Record harvest events
- POST /api/transport/distribution - Record distribution events
- POST /api/transport/seed-saving - Record seed saving events
- POST /api/transport/seed-sharing - Record seed sharing events
- GET /api/transport/journey/[plantId] - Get plant journey
- GET /api/transport/footprint/[userId] - Get environmental impact
- GET /api/transport/verify/[blockHash] - Verify block integrity
- GET /api/transport/qr/[id] - Generate QR code data

Demand API (6 endpoints):
- POST/GET /api/demand/preferences - Consumer preferences
- POST /api/demand/signal - Generate demand signal
- GET /api/demand/recommendations - Get planting recommendations
- GET /api/demand/forecast - Get demand forecast
- POST /api/demand/supply - Register supply commitment
- POST /api/demand/match - Create market match

Vertical Farm API (9 endpoints):
- POST /api/vertical-farm/register - Register new farm
- GET /api/vertical-farm/[farmId] - Get farm details
- GET/POST /api/vertical-farm/[farmId]/zones - Manage zones
- GET /api/vertical-farm/[farmId]/analytics - Get farm analytics
- POST /api/vertical-farm/batch/start - Start crop batch
- GET /api/vertical-farm/batch/[batchId] - Get batch details
- PUT /api/vertical-farm/batch/[batchId]/environment - Record environment
- POST /api/vertical-farm/batch/[batchId]/harvest - Complete harvest
- GET /api/vertical-farm/recipes - List growing recipes
2025-11-22 18:31:21 +00:00

103 lines
3 KiB
TypeScript

/**
* API Route: Record harvest event
* POST /api/transport/harvest
*/
import type { NextApiRequest, NextApiResponse } from 'next';
import { getTransportChain } from '../../../lib/transport/tracker';
import { HarvestEvent, TransportLocation, TransportMethod } from '../../../lib/transport/types';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'POST') {
return res.status(405).json({ success: false, error: 'Method not allowed' });
}
try {
const {
plantIds,
harvestBatchId,
harvestType,
produceType,
grossWeight,
netWeight,
weightUnit,
itemCount,
qualityGrade,
packagingType,
temperatureRequired,
shelfLifeHours,
fromLocation,
toLocation,
transportMethod,
senderId,
receiverId,
distanceKm,
durationMinutes,
seedsSaved,
seedBatchIdCreated,
notes
} = req.body;
// Validate required fields
if (!plantIds || !harvestBatchId || !harvestType || !produceType || !grossWeight || !netWeight || !fromLocation || !toLocation || !senderId || !receiverId) {
return res.status(400).json({
success: false,
error: 'Missing required fields: plantIds, harvestBatchId, harvestType, produceType, grossWeight, netWeight, fromLocation, toLocation, senderId, receiverId'
});
}
const transportChain = getTransportChain();
const event: HarvestEvent = {
id: `harvest-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
timestamp: new Date().toISOString(),
eventType: 'harvest',
plantIds: Array.isArray(plantIds) ? plantIds : [plantIds],
harvestBatchId,
harvestType,
produceType,
grossWeight,
netWeight,
weightUnit: weightUnit || 'kg',
itemCount,
qualityGrade,
packagingType: packagingType || 'bulk',
temperatureRequired: temperatureRequired || { min: 2, max: 8, optimal: 4, unit: 'celsius' },
shelfLifeHours: shelfLifeHours || 168,
fromLocation: fromLocation as TransportLocation,
toLocation: toLocation as TransportLocation,
distanceKm: distanceKm || 0,
durationMinutes: durationMinutes || 0,
transportMethod: (transportMethod as TransportMethod) || 'walking',
carbonFootprintKg: 0,
senderId,
receiverId,
status: 'verified',
seedsSaved: seedsSaved || false,
seedBatchIdCreated,
notes
};
const block = transportChain.recordEvent(event);
res.status(201).json({
success: true,
data: {
event,
block: {
index: block.index,
hash: block.hash,
timestamp: block.timestamp,
cumulativeCarbonKg: block.cumulativeCarbonKg,
cumulativeFoodMiles: block.cumulativeFoodMiles
}
}
});
} catch (error: any) {
console.error('Error recording harvest event:', error);
res.status(500).json({ success: false, error: error.message || 'Internal server error' });
}
}