IO-3515 PR Comments addressed.
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 <span style={{ color: "#999", fontSize: "0.85em" }}>N/A</span>;
|
||||
return <span style={{ color: "#959595", fontSize: "0.85em" }}>N/A</span>;
|
||||
}
|
||||
|
||||
const { total, ocr, jobMatch } = parsed;
|
||||
|
||||
15
server.js
15
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 });
|
||||
}
|
||||
|
||||
@@ -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(`
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user