diff --git a/backend/scripts/reconcile.js b/backend/scripts/reconcile.js index 04a8dd8..eb08ec1 100644 --- a/backend/scripts/reconcile.js +++ b/backend/scripts/reconcile.js @@ -47,9 +47,6 @@ async function reconcile() { // Get batch ID at index const batchIdBytes32 = await contract.getBatchIdByIndex(i); - // Convert bytes32 to string (remove padding) - const batchId = ethers.zeroPadValue(batchIdBytes32, 32).toString(); - // Get full batch data const onChainBatch = await contract.getBatch(batchIdBytes32); @@ -59,14 +56,13 @@ async function reconcile() { const readableBatchId = ethers.toUtf8String(batchIdBytes32); // Map blockchain stage (uint8) to string - const stageName = getStageName(Number(onChainBatch.quantity) > 0 ? 0 : 0); // Stage is in updates, not CropBatch struct + const stageName = getStageName(0); // Update or insert batch in MongoDB await Batch.updateOne( { batchId: readableBatchId }, { $set: { - // Keep local data but update sync status syncStatus: 'synced', lastSyncedAt: new Date(), onChainData: { diff --git a/backend/server.js b/backend/server.js index 64a885b..2a6761e 100644 --- a/backend/server.js +++ b/backend/server.js @@ -60,9 +60,9 @@ const PORT = process.env.PORT || 3001; // ==================== MIDDLEWARE FUNCTIONS ==================== -// Authentication is handled by middleware imported from './middleware/auth' -// - protect: Verifies JWT and fetches full user from - adminOnly: MongoDB -// Checks if user has admin role +// Authentication is handled by middleware imported from './middleware/auth': +// - protect: Verifies JWT and fetches full user from MongoDB +// - adminOnly: Checks if user has admin role // - authorizeBatchOwner: Verifies user owns the batch // - authorizeRoles: Role-based authorization @@ -274,7 +274,7 @@ async function generateBatchId() { ); const currentYear = new Date().getFullYear(); - const batchId = `CROP-${currentYear}-${String(counter.seq).padStart(4, '0')}`; + const batchId = `CROP-${currentYear}-${String(counter.seq).padStart(3, '0')}`; await session.commitTransaction(); session.endSession(); @@ -308,6 +308,7 @@ async function generateBatchId(session = null) { return `CROP-${currentYear}-${String(counter.seq).padStart(4, '0')}`; } + async function generateQRCode(batchId) { try { return await QRCode.toDataURL(batchId, {