From 03ad66b2a21b05d785274ba323a8fc7060e20c54 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Fri, 20 Feb 2026 09:06:11 -0800 Subject: [PATCH] IO-3515 PR Comments addressed. --- .../bill-enter-ai-scan.component.jsx | 6 +- .../bill-form/bill-form.lines.component.jsx | 6 +- .../bill-form.lines.confidence.component.jsx | 2 +- server.js | 15 ++-- server/ai/bill-ocr/bill-ocr-generator.js | 3 +- server/ai/bill-ocr/bill-ocr-helpers.js | 3 +- server/ai/bill-ocr/bill-ocr.js | 74 ++++++++----------- 7 files changed, 45 insertions(+), 64 deletions(-) diff --git a/client/src/components/bill-enter-ai-scan/bill-enter-ai-scan.component.jsx b/client/src/components/bill-enter-ai-scan/bill-enter-ai-scan.component.jsx index 0eff900d7..653c85892 100644 --- a/client/src/components/bill-enter-ai-scan/bill-enter-ai-scan.component.jsx +++ b/client/src/components/bill-enter-ai-scan/bill-enter-ai-scan.component.jsx @@ -58,11 +58,11 @@ function BillEnterAiScan({ setScanLoading(false); // Update form with the extracted data - if (data.data && data.data.billForm) { + if (data?.data?.billForm) { form.setFieldsValue(data.data.billForm); await form.validateFields(["billlines"], { recursive: true }); notification.success({ - title: t(".bills.labels.ai.scancomplete") + title: t("bills.labels.ai.scancomplete") }); } } else if (data.status === "FAILED") { @@ -155,8 +155,6 @@ function BillEnterAiScan({ } } catch (error) { setScanLoading(false); - console.log("*** ~ BillEnterAiScan ~ error:", error, error.response?.data?.message); - notification.error({ title: t("bills.labels.ai.scanfailed"), description: error.response?.data?.message || error.message || t("bills.labels.ai.generic_failure") diff --git a/client/src/components/bill-form/bill-form.lines.component.jsx b/client/src/components/bill-form/bill-form.lines.component.jsx index 56508fad7..0cacef891 100644 --- a/client/src/components/bill-form/bill-form.lines.component.jsx +++ b/client/src/components/bill-form/bill-form.lines.component.jsx @@ -280,11 +280,7 @@ export function BillEnterModalLinesComponent({ { required: true }, { validator: (_, value) => { - if (Math.abs(parseFloat(value)) < 0.01) { - return Promise.reject(); - } else { - return Promise.resolve(); - } + return Math.abs(parseFloat(value)) < 0.01 ? Promise.reject() : Promise.resolve(); }, warningOnly: true } diff --git a/client/src/components/bill-form/bill-form.lines.confidence.component.jsx b/client/src/components/bill-form/bill-form.lines.confidence.component.jsx index 23ce5b702..4fb0b50f8 100644 --- a/client/src/components/bill-form/bill-form.lines.confidence.component.jsx +++ b/client/src/components/bill-form/bill-form.lines.confidence.component.jsx @@ -26,7 +26,7 @@ const ConfidenceDisplay = ({ rowValue: { confidence, actual_price, actual_cost } const parsed_actual_price = parseFloat(actual_price); const parsed_actual_cost = parseFloat(actual_cost); if (!parsed) { - return N/A; + return N/A; } const { total, ocr, jobMatch } = parsed; diff --git a/server.js b/server.js index 8b900c766..90f16f33d 100644 --- a/server.js +++ b/server.js @@ -427,6 +427,12 @@ const loadQueues = async ({ pubClient, logger, redisHelpers, ioRedis }) => { chatterApiQueue.on("error", (error) => { logger.log(`Error in chatterApiQueue: ${error}`, "ERROR", "queue", "api", null, { error: error?.message }); }); + + // Initialize bill-ocr with Redis client + const { initializeBillOcr, startSQSPolling } = require("./server/ai/bill-ocr/bill-ocr"); + initializeBillOcr(pubClient); + // Start SQS polling for Textract notifications + startSQSPolling(); }; /** @@ -456,14 +462,7 @@ const main = async () => { try { await server.listen(port); logger.log(`Server started on port ${port}`, "INFO", "api"); - - // Initialize bill-ocr with Redis client - const { initializeBillOcr, startSQSPolling } = require("./server/ai/bill-ocr/bill-ocr"); - initializeBillOcr(pubClient); - - // Start SQS polling for Textract notifications - startSQSPolling(); - logger.log(`Started SQS polling for Textract notifications`, "INFO", "api"); + } catch (error) { logger.log(`Server failed to start on port ${port}`, "ERROR", "api", null, { error: error.message }); } diff --git a/server/ai/bill-ocr/bill-ocr-generator.js b/server/ai/bill-ocr/bill-ocr-generator.js index 31d265111..9cc34a18f 100644 --- a/server/ai/bill-ocr/bill-ocr-generator.js +++ b/server/ai/bill-ocr/bill-ocr-generator.js @@ -287,9 +287,8 @@ async function generateBillFormData({ processedData, jobid: jobidFromProps, body if (jobs.length === 0) { throw new Error("No job found for the detected RO/PO number."); - } else { - jobid = jobs[0].id; } + jobid = jobs[0].id; } const jobData = await client.request(` diff --git a/server/ai/bill-ocr/bill-ocr-helpers.js b/server/ai/bill-ocr/bill-ocr-helpers.js index 6a4ef9086..8f8cf58d9 100644 --- a/server/ai/bill-ocr/bill-ocr-helpers.js +++ b/server/ai/bill-ocr/bill-ocr-helpers.js @@ -1,4 +1,5 @@ const PDFDocument = require('pdf-lib').PDFDocument; +const logger = require("../../utils/logger"); const TEXTRACT_REDIS_PREFIX = `textract:${process.env?.NODE_ENV}` const TEXTRACT_JOB_TTL = 10 * 60; @@ -141,7 +142,7 @@ async function hasActiveJobs({ redisPubClient }) { return false; } catch (error) { - console.error('Error checking for active jobs:', error); + logger.log("bill-ocr-job-check-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); return false; } } diff --git a/server/ai/bill-ocr/bill-ocr.js b/server/ai/bill-ocr/bill-ocr.js index b04898171..2e55e4919 100644 --- a/server/ai/bill-ocr/bill-ocr.js +++ b/server/ai/bill-ocr/bill-ocr.js @@ -6,6 +6,7 @@ const { getTextractJobKey, setTextractJob, getTextractJob, getFileType, getPdfPa const { extractInvoiceData, processScanData } = require("./bill-ocr-normalize"); const { generateBillFormData } = require("./bill-ocr-generator"); const logger = require("../../utils/logger"); + // Initialize AWS clients const awsConfig = { region: process.env.AWS_AI_REGION || "ca-central-1", @@ -39,25 +40,19 @@ async function jobExists(textractJobId) { if (!redisPubClient) { throw new Error('Redis client not initialized. Call initializeBillOcr first.'); } - - console.log('Checking if job exists for Textract job ID:', textractJobId); const key = getTextractJobKey(textractJobId); const exists = await redisPubClient.exists(key); if (exists) { - console.log(`Job found: ${textractJobId}`); return true; } - - console.log('No matching job found in Redis'); return false; } async function handleBillOcr(req, res) { // Check if file was uploaded if (!req.file) { - res.status(400).send({ error: 'No file uploaded.' }); - return; + return res.status(400).send({ error: 'No file uploaded.' }); } // The uploaded file is available in request file @@ -73,7 +68,7 @@ async function handleBillOcr(req, res) { const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: req }); logger.log("bill-ocr-single-complete", "DEBUG", req.user.email, jobid, { ...processedData, billForm }); - res.status(200).send({ + return res.status(200).json({ success: true, status: 'COMPLETED', data: { ...processedData, billForm }, @@ -88,35 +83,35 @@ async function handleBillOcr(req, res) { const processedData = await processSinglePageDocument(uploadedFile.buffer); const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: req }); logger.log("bill-ocr-single-complete", "DEBUG", req.user.email, jobid, { ...processedData, billForm }); - res.status(200).send({ + return res.status(200).json({ success: true, status: 'COMPLETED', data: { ...processedData, billForm }, message: 'Invoice processing completed' }); - } else { - // Start the Textract job (non-blocking) for multi-page documents - const jobInfo = await startTextractJob(uploadedFile.buffer, { jobid, bodyshopid, partsorderid }); - logger.log("bill-ocr-multipage-start", "DEBUG", req.user.email, jobid, jobInfo); - - res.status(202).send({ - success: true, - textractJobId: jobInfo.jobId, - message: 'Invoice processing started', - statusUrl: `/ai/bill-ocr/status/${jobInfo.jobId}` - }); } + // Start the Textract job (non-blocking) for multi-page documents + const jobInfo = await startTextractJob(uploadedFile.buffer, { jobid, bodyshopid, partsorderid }); + logger.log("bill-ocr-multipage-start", "DEBUG", req.user.email, jobid, jobInfo); + + return res.status(202).json({ + success: true, + textractJobId: jobInfo.jobId, + message: 'Invoice processing started', + statusUrl: `/ai/bill-ocr/status/${jobInfo.jobId}` + }); + } else { logger.log("bill-ocr-unsupported-filetype", "WARN", req.user.email, jobid, { fileType }); - res.status(400).send({ + return res.status(400).json({ error: 'Unsupported file type', message: 'Please upload a PDF or supported image file (JPEG, PNG, TIFF)' }); } } catch (error) { logger.log("bill-ocr-error", "ERROR", req.user.email, jobid, { error: error.message, stack: error.stack }); - res.status(500).send({ + return res.status(500).json({ error: 'Failed to start invoice processing', message: error.message }); @@ -128,15 +123,13 @@ async function handleBillOcrStatus(req, res) { if (!textractJobId) { logger.log("bill-ocr-status-error", "WARN", req.user.email, null, { error: 'No textractJobId found in params' }); - res.status(400).send({ error: 'Job ID is required' }); - res.status(400).send({ error: 'Job ID is required' }); - return; + return res.status(400).json({ error: 'Job ID is required' }); + } const jobStatus = await getTextractJob({ redisPubClient, textractJobId }); if (!jobStatus) { - res.status(404).send({ error: 'Job not found' }); - return; + return res.status(404).json({ error: 'Job not found' }); } if (jobStatus.status === 'COMPLETED') { @@ -169,17 +162,17 @@ async function handleBillOcrStatus(req, res) { } catch (error) { logger.log("bill-ocr-multipage-error", "ERROR", req.user.email, jobStatus.context.jobid, { ...jobStatus.data, error: error.message, stack: error.stack }); - res.status(500).send({ + return res.status(500).send({ status: 'COMPLETED', error: 'Data processed but failed to generate bill form', message: error.message, data: jobStatus.data // Still return the raw processed data }); - return; + } } - res.status(200).send({ + return res.status(200).send({ status: 'COMPLETED', data: { ...jobStatus.data, @@ -189,12 +182,12 @@ async function handleBillOcrStatus(req, res) { } else if (jobStatus.status === 'FAILED') { logger.log("bill-ocr-multipage-failed", "ERROR", req.user.email, jobStatus.context.jobid, { ...jobStatus.data, error: jobStatus.error, }); - res.status(500).send({ + return res.status(500).json({ status: 'FAILED', error: jobStatus.error }); } else { - res.status(200).send({ + return res.status(200).json({ status: jobStatus.status }); } @@ -294,14 +287,13 @@ async function processSQSMessages() { const queueUrl = process.env.AWS_TEXTRACT_SQS_QUEUE_URL; if (!queueUrl) { - console.error('AWS_TEXTRACT_SQS_QUEUE_URL not configured'); + logger.log("bill-ocr-error", "ERROR", "api", null, { message: "AWS_TEXTRACT_SQS_QUEUE_URL not configured" }); return; } // Only poll if there are active mutli page jobs in progress const hasActive = await hasActiveJobs({ redisPubClient }); if (!hasActive) { - console.log('No active jobs in progress, skipping SQS poll'); return; } @@ -316,7 +308,7 @@ async function processSQSMessages() { const result = await sqsClient.send(receiveCommand); if (result.Messages && result.Messages.length > 0) { - console.log('Processing', result.Messages.length, 'messages from SQS'); + logger.log("bill-ocr-sqs-processing", "DEBUG", "api", null, { message: `Processing ${result.Messages.length} messages from SQS` }); for (const message of result.Messages) { try { // Environment-level filtering: check if this message belongs to this environment @@ -330,8 +322,6 @@ async function processSQSMessages() { ReceiptHandle: message.ReceiptHandle }); await sqsClient.send(deleteCommand); - } else { - console.log('Ignoring message - job not found in this environment'); } } catch (error) { @@ -342,7 +332,6 @@ async function processSQSMessages() { } } catch (error) { logger.log("bill-ocr-sqs-receiving-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); - } } @@ -361,7 +350,7 @@ async function shouldProcessMessage(message) { const exists = await jobExists(textractJobId); return exists; } catch (error) { - console.error('Error checking if message should be processed:', error); + logger.log("bill-ocr-message-check-error", "DEBUG", "api", null, { message: "Error checking if message should be processed", error: error.message, stack: error.stack }); // If we can't parse the message, don't process it return false; } @@ -373,8 +362,7 @@ async function handleTextractNotification(message) { try { snsMessage = JSON.parse(body.Message); } catch (error) { - console.error('Error parsing SNS message:', error); - console.log('Invalid message format:', body); + logger.log("bill-ocr-handle-textract-error", "DEBUG", "api", null, { message: "Error parsing SNS message - invalid message format.", error: error.message, stack: error.stack, body }); return; } @@ -385,7 +373,7 @@ async function handleTextractNotification(message) { const jobInfo = await getTextractJob({ redisPubClient, textractJobId }); if (!jobInfo) { - console.warn(`Job info not found in Redis for Textract job ID: ${textractJobId}`); + logger.log("bill-ocr-job-not-found", "DEBUG", "api", null, { message: `Job info not found in Redis for Textract job ID: ${textractJobId}`, textractJobId, snsMessage }); return; } @@ -461,7 +449,7 @@ async function retrieveTextractResults(textractJobId) { function startSQSPolling() { const pollInterval = setInterval(() => { processSQSMessages().catch(error => { - console.error('SQS polling error:', error); + logger.log("bill-ocr-sqs-poll-error", "ERROR", "api", null, { message: error.message, stack: error.stack }); }); }, 10000); // Poll every 10 seconds return pollInterval;