IO-3515 Add translations and logging.

This commit is contained in:
Patrick Fic
2026-02-19 13:54:39 -08:00
parent ae1408012f
commit b2bc19c5c9
7 changed files with 463 additions and 78 deletions

View File

@@ -3487,6 +3487,289 @@
</translation> </translation>
</translations> </translations>
</concept_node> </concept_node>
<folder_node>
<name>ai</name>
<children>
<concept_node>
<name>accept_and_continue</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<folder_node>
<name>confidence</name>
<children>
<concept_node>
<name>breakdown</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>match</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>missing_data</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>ocr</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>overall</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
</children>
</folder_node>
<concept_node>
<name>disclaimer_title</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>multipage</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>processing</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>scan</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>scancomplete</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>scanfailed</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>scanstarted</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
</children>
</folder_node>
<concept_node> <concept_node>
<name>bill_lines</name> <name>bill_lines</name>
<definition_loaded>false</definition_loaded> <definition_loaded>false</definition_loaded>
@@ -21167,6 +21450,27 @@
</translation> </translation>
</translations> </translations>
</concept_node> </concept_node>
<concept_node>
<name>beta</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node> <concept_node>
<name>cancel</name> <name>cancel</name>
<definition_loaded>false</definition_loaded> <definition_loaded>false</definition_loaded>

View File

@@ -7,7 +7,7 @@ import { createStructuredSelector } from "reselect";
import { useNotification } from "../../contexts/Notifications/notificationContext"; import { useNotification } from "../../contexts/Notifications/notificationContext";
import { selectBillEnterModal } from "../../redux/modals/modals.selectors"; import { selectBillEnterModal } from "../../redux/modals/modals.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors"; import { selectBodyshop } from "../../redux/user/user.selectors";
import { useApolloClient } from "@apollo/client/react"; import { useTranslation } from "react-i18next";
const mapStateToProps = createStructuredSelector({ const mapStateToProps = createStructuredSelector({
billEnterModal: selectBillEnterModal, billEnterModal: selectBillEnterModal,
@@ -26,23 +26,20 @@ function BillEnterAiScan({
setIsAiScan setIsAiScan
}) { }) {
const notification = useNotification(); const notification = useNotification();
const { t } = useTranslation();
const [showBetaModal, setShowBetaModal] = useState(false); const [showBetaModal, setShowBetaModal] = useState(false);
const BETA_ACCEPTANCE_KEY = "ai_scan_beta_acceptance"; const BETA_ACCEPTANCE_KEY = "ai_scan_beta_acceptance";
const client = useApolloClient();
const handleBetaAcceptance = () => { const handleBetaAcceptance = () => {
localStorage.setItem(BETA_ACCEPTANCE_KEY, "true"); localStorage.setItem(BETA_ACCEPTANCE_KEY, "true");
setShowBetaModal(false); setShowBetaModal(false);
// Trigger the file input after acceptance
fileInputRef.current?.click(); fileInputRef.current?.click();
}; };
const checkBetaAcceptance = () => { const checkBetaAcceptance = () => {
const hasAccepted = localStorage.getItem(BETA_ACCEPTANCE_KEY); const hasAccepted = localStorage.getItem(BETA_ACCEPTANCE_KEY);
if (hasAccepted) { if (hasAccepted) {
// User has already accepted, proceed with file selection
fileInputRef.current?.click(); fileInputRef.current?.click();
} else { } else {
// Show beta modal
setShowBetaModal(true); setShowBetaModal(true);
} }
}; };
@@ -65,8 +62,7 @@ function BillEnterAiScan({
form.setFieldsValue(data.data.billForm); form.setFieldsValue(data.data.billForm);
await form.validateFields(["billlines"], { recursive: true }); await form.validateFields(["billlines"], { recursive: true });
notification.success({ notification.success({
title: "AI Scan Complete", title: t(".bills.labels.ai.scancomplete")
message: "Invoice data has been extracted successfully"
}); });
} }
} else if (data.status === "FAILED") { } else if (data.status === "FAILED") {
@@ -78,8 +74,8 @@ function BillEnterAiScan({
setScanLoading(false); setScanLoading(false);
notification.error({ notification.error({
title: "AI Scan Failed", title: t("bills.labels.ai.scanfailed"),
message: data.error || "Failed to process the invoice" message: data.error || ""
}); });
} }
// If status is IN_PROGRESS, continue polling // If status is IN_PROGRESS, continue polling
@@ -92,7 +88,7 @@ function BillEnterAiScan({
setScanLoading(false); setScanLoading(false);
notification.error({ notification.error({
title: "AI Scan Error", title: t("bills.labels.ai.scanfailed"),
message: error.response?.data?.message || error.message || "Failed to check scan status" message: error.response?.data?.message || error.message || "Failed to check scan status"
}); });
} }
@@ -123,8 +119,8 @@ function BillEnterAiScan({
if (status === 202) { if (status === 202) {
// Multipage PDF - start polling // Multipage PDF - start polling
notification.info({ notification.info({
title: "Processing Invoice", title: t("bills.labels.ai.scanstarted"),
message: "This is a multipage document. Processing may take a few moments..." message: t("bills.labels.ai.multipage")
}); });
//Workaround needed to bypass react-compiler error about manipulating refs in child components. Refactor may be needed in the future to clean this up. //Workaround needed to bypass react-compiler error about manipulating refs in child components. Refactor may be needed in the future to clean this up.
@@ -144,14 +140,13 @@ function BillEnterAiScan({
await form.validateFields(["billlines"], { recursive: true }); await form.validateFields(["billlines"], { recursive: true });
notification.success({ notification.success({
title: "AI Scan Complete", title: t("bills.labels.ai.scancomplete")
message: "Invoice data has been extracted successfully"
}); });
} }
} catch (error) { } catch (error) {
setScanLoading(false); setScanLoading(false);
notification.error({ notification.error({
title: "AI Scan Failed", title: t("bills.labels.ai.scanfailed"),
message: error.response?.data?.message || error.message || "Failed to process invoice" message: error.response?.data?.message || error.message || "Failed to process invoice"
}); });
} }
@@ -162,20 +157,21 @@ function BillEnterAiScan({
/> />
<Button onClick={checkBetaAcceptance} icon={<FaWandMagicSparkles />} loading={scanLoading} disabled={scanLoading}> <Button onClick={checkBetaAcceptance} icon={<FaWandMagicSparkles />} loading={scanLoading} disabled={scanLoading}>
{scanLoading ? "Processing Invoice..." : "AI Scan"} {scanLoading ? t("bills.labels.ai.processing") : t("bills.labels.ai.scan")}
<Tag color="red">BETA</Tag> <Tag color="red">{t("general.labels.beta")}</Tag>
</Button> </Button>
<Modal <Modal
title="AI Scan Beta Disclaimer" title={t("bills.labels.ai.disclaimer_title")}
open={showBetaModal} open={showBetaModal}
onOk={handleBetaAcceptance} onOk={handleBetaAcceptance}
onCancel={() => setShowBetaModal(false)} onCancel={() => setShowBetaModal(false)}
okText="Accept and Continue" okText={t("bills.labels.ai.accept_and_continue")}
cancelText="Cancel" cancelText={t("general.labels.cancel")}
> >
<Typography.Title level={2}>AI Usage Disclaimer</Typography.Title> {
//This is explicitly not translated.
}
<Typography.Text> <Typography.Text>
This AI scanning feature is currently in <strong>beta</strong>. While it can accelerate data entry, you{" "} This AI scanning feature is currently in <strong>beta</strong>. While it can accelerate data entry, you{" "}
<strong>must carefully review all extracted results</strong> for accuracy. <strong>must carefully review all extracted results</strong> for accuracy.

View File

@@ -36,11 +36,9 @@ const ConfidenceDisplay = ({ rowValue: { confidence, actual_price, actual_cost }
<Tooltip <Tooltip
title={ title={
<div style={{ padding: "4px 0" }}> <div style={{ padding: "4px 0" }}>
<div style={{ marginBottom: 8, fontWeight: 600 }}> <div style={{ marginBottom: 8, fontWeight: 600 }}>{t("bills.labels.ai.confidence.breakdown")}</div>
{t("billlines.confidence.breakdown", { defaultValue: "Confidence Breakdown" })}
</div>
<div style={{ marginBottom: 4 }}> <div style={{ marginBottom: 4 }}>
<strong>{t("billlines.confidence.overall", { defaultValue: "Overall" })}:</strong> {total.toFixed(1)}% <strong>{t("bills.labels.ai.confidence.overall")}:</strong> {total.toFixed(1)}%
<Progress <Progress
percent={total} percent={total}
size="small" size="small"
@@ -50,7 +48,7 @@ const ConfidenceDisplay = ({ rowValue: { confidence, actual_price, actual_cost }
/> />
</div> </div>
<div style={{ marginBottom: 4 }}> <div style={{ marginBottom: 4 }}>
<strong>{t("billlines.confidence.ocr", { defaultValue: "OCR" })}:</strong> {ocr.toFixed(1)}% <strong>{t("bills.labels.ai.confidence.ocr")}:</strong> {ocr.toFixed(1)}%
<Progress <Progress
percent={ocr} percent={ocr}
size="small" size="small"
@@ -60,7 +58,7 @@ const ConfidenceDisplay = ({ rowValue: { confidence, actual_price, actual_cost }
/> />
</div> </div>
<div> <div>
<strong>{t("billlines.confidence.match", { defaultValue: "Job Match" })}:</strong> {jobMatch.toFixed(1)}% <strong>{t("bills.labels.ai.confidence.match")}:</strong> {jobMatch.toFixed(1)}%
<Progress <Progress
percent={jobMatch} percent={jobMatch}
size="small" size="small"
@@ -75,7 +73,7 @@ const ConfidenceDisplay = ({ rowValue: { confidence, actual_price, actual_cost }
<Space size="small"> <Space size="small">
{!parsed_actual_cost || !parsed_actual_price || parsed_actual_cost === 0 || parsed_actual_price === 0 ? ( {!parsed_actual_cost || !parsed_actual_price || parsed_actual_cost === 0 || parsed_actual_price === 0 ? (
<Tag color="red" style={{ margin: 0, cursor: "help", userSelect: "none" }}> <Tag color="red" style={{ margin: 0, cursor: "help", userSelect: "none" }}>
{t("billlines.confidence.missing_data", { defaultValue: "Missing Data" })} {t("bills.labels.ai.confidence.missing_data")}
</Tag> </Tag>
) : null} ) : null}
<Tag color={color} style={{ margin: 0, cursor: "help", userSelect: "none" }}> <Tag color={color} style={{ margin: 0, cursor: "help", userSelect: "none" }}>

View File

@@ -218,6 +218,23 @@
}, },
"labels": { "labels": {
"actions": "Actions", "actions": "Actions",
"ai": {
"accept_and_continue": "Accept and Continue",
"confidence": {
"breakdown": "Confidence Breakdown",
"match": "Jobline Match",
"missing_data": "Missing Data",
"ocr": "Optical Character Recognition",
"overall": "Overall"
},
"disclaimer_title": "AI Scan Beta Disclaimer",
"multipage": "The is a multi-page document. Processing will take a few moments.",
"processing": "Analyzing Bill",
"scan": "AI Bill Scanner",
"scancomplete": "AI Scan Complete",
"scanfailed": "AI Scan Failed",
"scanstarted": "AI Scan Started"
},
"bill_lines": "Bill Lines", "bill_lines": "Bill Lines",
"bill_total": "Bill Total Amount", "bill_total": "Bill Total Amount",
"billcmtotal": "Credit Memos", "billcmtotal": "Credit Memos",
@@ -1296,6 +1313,7 @@
"apply": "Apply", "apply": "Apply",
"areyousure": "Are you sure?", "areyousure": "Are you sure?",
"barcode": "Barcode", "barcode": "Barcode",
"beta": "BETA",
"cancel": "Are you sure you want to cancel? Your changes will not be saved.", "cancel": "Are you sure you want to cancel? Your changes will not be saved.",
"changelog": "Change Log", "changelog": "Change Log",
"clear": "Clear", "clear": "Clear",

View File

@@ -218,6 +218,23 @@
}, },
"labels": { "labels": {
"actions": "", "actions": "",
"ai": {
"accept_and_continue": "",
"confidence": {
"breakdown": "",
"match": "",
"missing_data": "",
"ocr": "",
"overall": ""
},
"disclaimer_title": "",
"multipage": "",
"processing": "",
"scan": "",
"scancomplete": "",
"scanfailed": "",
"scanstarted": ""
},
"bill_lines": "", "bill_lines": "",
"bill_total": "", "bill_total": "",
"billcmtotal": "", "billcmtotal": "",
@@ -1296,6 +1313,7 @@
"apply": "", "apply": "",
"areyousure": "", "areyousure": "",
"barcode": "código de barras", "barcode": "código de barras",
"beta": "",
"cancel": "", "cancel": "",
"changelog": "", "changelog": "",
"clear": "", "clear": "",

View File

@@ -218,6 +218,23 @@
}, },
"labels": { "labels": {
"actions": "", "actions": "",
"ai": {
"accept_and_continue": "",
"confidence": {
"breakdown": "",
"match": "",
"missing_data": "",
"ocr": "",
"overall": ""
},
"disclaimer_title": "",
"multipage": "",
"processing": "",
"scan": "",
"scancomplete": "",
"scanfailed": "",
"scanstarted": ""
},
"bill_lines": "", "bill_lines": "",
"bill_total": "", "bill_total": "",
"billcmtotal": "", "billcmtotal": "",
@@ -1296,6 +1313,7 @@
"apply": "", "apply": "",
"areyousure": "", "areyousure": "",
"barcode": "code à barre", "barcode": "code à barre",
"beta": "",
"cancel": "", "cancel": "",
"changelog": "", "changelog": "",
"clear": "", "clear": "",

View File

@@ -5,7 +5,7 @@ const { v4: uuidv4 } = require('uuid');
const { getTextractJobKey, setTextractJob, getTextractJob, getFileType, getPdfPageCount, hasActiveJobs } = require("./bill-ocr-helpers"); const { getTextractJobKey, setTextractJob, getTextractJob, getFileType, getPdfPageCount, hasActiveJobs } = require("./bill-ocr-helpers");
const { extractInvoiceData, processScanData } = require("./bill-ocr-normalize"); const { extractInvoiceData, processScanData } = require("./bill-ocr-normalize");
const { generateBillFormData } = require("./bill-ocr-generator"); const { generateBillFormData } = require("./bill-ocr-generator");
const logger = require("../../utils/logger");
// Initialize AWS clients // Initialize AWS clients
const awsConfig = { const awsConfig = {
region: process.env.AWS_AI_REGION || "ca-central-1", region: process.env.AWS_AI_REGION || "ca-central-1",
@@ -53,24 +53,27 @@ async function jobExists(textractJobId) {
return false; return false;
} }
async function handleBillOcr(request, response) { async function handleBillOcr(req, res) {
// Check if file was uploaded // Check if file was uploaded
if (!request.file) { if (!req.file) {
response.status(400).send({ error: 'No file uploaded.' }); res.status(400).send({ error: 'No file uploaded.' });
return; return;
} }
// The uploaded file is available in request.file // The uploaded file is available in request file
const uploadedFile = request.file; const uploadedFile = req.file;
const { jobid, bodyshopid, partsorderid } = request.body; const { jobid, bodyshopid, partsorderid } = req.body;
logger.log("bill-ocr-start", "DEBUG", req.user.email, jobid, null);
try { try {
const fileType = getFileType(uploadedFile); const fileType = getFileType(uploadedFile);
// Images are always processed synchronously (single page) // Images are always processed synchronously (single page)
if (fileType === 'image') { if (fileType === 'image') {
const processedData = await processSinglePageDocument(uploadedFile.buffer); const processedData = await processSinglePageDocument(uploadedFile.buffer);
const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: request }); const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: req });
response.status(200).send({ logger.log("bill-ocr-single-complete", "DEBUG", req.user.email, jobid, { ...processedData, billForm });
res.status(200).send({
success: true, success: true,
status: 'COMPLETED', status: 'COMPLETED',
data: { ...processedData, billForm }, data: { ...processedData, billForm },
@@ -83,9 +86,9 @@ async function handleBillOcr(request, response) {
if (pageCount === 1) { if (pageCount === 1) {
// Process synchronously for single-page documents // Process synchronously for single-page documents
const processedData = await processSinglePageDocument(uploadedFile.buffer); const processedData = await processSinglePageDocument(uploadedFile.buffer);
const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: request }); const billForm = await generateBillFormData({ processedData: processedData, jobid, bodyshopid, partsorderid, req: req });
//const billResult = await generateBillFormData({ result, }); logger.log("bill-ocr-single-complete", "DEBUG", req.user.email, jobid, { ...processedData, billForm });
response.status(200).send({ res.status(200).send({
success: true, success: true,
status: 'COMPLETED', status: 'COMPLETED',
data: { ...processedData, billForm }, data: { ...processedData, billForm },
@@ -94,8 +97,9 @@ async function handleBillOcr(request, response) {
} else { } else {
// Start the Textract job (non-blocking) for multi-page documents // Start the Textract job (non-blocking) for multi-page documents
const jobInfo = await startTextractJob(uploadedFile.buffer, { jobid, bodyshopid, partsorderid }); const jobInfo = await startTextractJob(uploadedFile.buffer, { jobid, bodyshopid, partsorderid });
logger.log("bill-ocr-multipage-start", "DEBUG", req.user.email, jobid, jobInfo);
response.status(202).send({ res.status(202).send({
success: true, success: true,
textractJobId: jobInfo.jobId, textractJobId: jobInfo.jobId,
message: 'Invoice processing started', message: 'Invoice processing started',
@@ -103,32 +107,35 @@ async function handleBillOcr(request, response) {
}); });
} }
} else { } else {
response.status(400).send({ logger.log("bill-ocr-unsupported-filetype", "WARN", req.user.email, jobid, { fileType });
res.status(400).send({
error: 'Unsupported file type', error: 'Unsupported file type',
message: 'Please upload a PDF or supported image file (JPEG, PNG, TIFF)' message: 'Please upload a PDF or supported image file (JPEG, PNG, TIFF)'
}); });
} }
} catch (error) { } catch (error) {
console.error('Error starting invoice processing:', error); logger.log("bill-ocr-error", "ERROR", req.user.email, jobid, { error: error.message, stack: error.stack });
response.status(500).send({ res.status(500).send({
error: 'Failed to start invoice processing', error: 'Failed to start invoice processing',
message: error.message message: error.message
}); });
} }
} }
async function handleBillOcrStatus(request, response) { async function handleBillOcrStatus(req, res) {
const { textractJobId } = request.params; const { textractJobId } = req.params;
if (!textractJobId) { if (!textractJobId) {
console.log('No textractJobId found in params'); logger.log("bill-ocr-status-error", "WARN", req.user.email, null, { error: 'No textractJobId found in params' });
response.status(400).send({ error: 'Job ID is required' }); res.status(400).send({ error: 'Job ID is required' });
res.status(400).send({ error: 'Job ID is required' });
return; return;
} }
const jobStatus = await getTextractJob({ redisPubClient, textractJobId }); const jobStatus = await getTextractJob({ redisPubClient, textractJobId });
if (!jobStatus) { if (!jobStatus) {
response.status(404).send({ error: 'Job not found' }); res.status(404).send({ error: 'Job not found' });
return; return;
} }
@@ -143,8 +150,9 @@ async function handleBillOcrStatus(request, response) {
jobid: jobStatus.context.jobid, jobid: jobStatus.context.jobid,
bodyshopid: jobStatus.context.bodyshopid, bodyshopid: jobStatus.context.bodyshopid,
partsorderid: jobStatus.context.partsorderid, partsorderid: jobStatus.context.partsorderid,
req: request // Now we have request context! req: req // Now we have request context!
}); });
logger.log("bill-ocr-multipage-complete", "DEBUG", req.user.email, jobStatus.context.jobid, { ...jobStatus.data, billForm });
// Cache the billForm back to Redis for future requests // Cache the billForm back to Redis for future requests
await setTextractJob({ await setTextractJob({
@@ -159,7 +167,9 @@ async function handleBillOcrStatus(request, response) {
} }
}); });
} catch (error) { } catch (error) {
response.status(500).send({ 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({
status: 'COMPLETED', status: 'COMPLETED',
error: 'Data processed but failed to generate bill form', error: 'Data processed but failed to generate bill form',
message: error.message, message: error.message,
@@ -169,7 +179,7 @@ async function handleBillOcrStatus(request, response) {
} }
} }
response.status(200).send({ res.status(200).send({
status: 'COMPLETED', status: 'COMPLETED',
data: { data: {
...jobStatus.data, ...jobStatus.data,
@@ -177,12 +187,14 @@ async function handleBillOcrStatus(request, response) {
} }
}); });
} else if (jobStatus.status === 'FAILED') { } else if (jobStatus.status === 'FAILED') {
response.status(500).send({ logger.log("bill-ocr-multipage-failed", "ERROR", req.user.email, jobStatus.context.jobid, { ...jobStatus.data, error: jobStatus.error, });
res.status(500).send({
status: 'FAILED', status: 'FAILED',
error: jobStatus.error error: jobStatus.error
}); });
} else { } else {
response.status(200).send({ res.status(200).send({
status: jobStatus.status status: jobStatus.status
}); });
} }
@@ -307,22 +319,51 @@ async function processSQSMessages() {
console.log('Processing', result.Messages.length, 'messages from SQS'); console.log('Processing', result.Messages.length, 'messages from SQS');
for (const message of result.Messages) { for (const message of result.Messages) {
try { try {
//TODO: Add environment level filtering here. // Environment-level filtering: check if this message belongs to this environment
await handleTextractNotification(message); const shouldProcess = await shouldProcessMessage(message);
// Delete message after successful processing if (shouldProcess) {
const deleteCommand = new DeleteMessageCommand({ await handleTextractNotification(message);
QueueUrl: queueUrl, // Delete message after successful processing
ReceiptHandle: message.ReceiptHandle const deleteCommand = new DeleteMessageCommand({
}); QueueUrl: queueUrl,
await sqsClient.send(deleteCommand); ReceiptHandle: message.ReceiptHandle
});
await sqsClient.send(deleteCommand);
} else {
console.log('Ignoring message - job not found in this environment');
}
} catch (error) { } catch (error) {
console.error('Error processing message:', error);
logger.log("bill-ocr-sqs-processing-error", "ERROR", "api", null, { message, error: error.message, stack: error.stack });
} }
} }
} }
} catch (error) { } catch (error) {
console.error('Error receiving SQS messages:', error); logger.log("bill-ocr-sqs-receiving-error", "ERROR", "api", null, { error: error.message, stack: error.stack });
}
}
/**
* Check if a message should be processed by this environment
* @param {Object} message - SQS message
* @returns {Promise<boolean>}
*/
async function shouldProcessMessage(message) {
try {
const body = JSON.parse(message.Body);
const snsMessage = JSON.parse(body.Message);
const textractJobId = snsMessage.JobId;
// Check if job exists in Redis for this environment (using environment-specific prefix)
const exists = await jobExists(textractJobId);
return exists;
} catch (error) {
console.error('Error checking if message should be processed:', error);
// If we can't parse the message, don't process it
return false;
} }
} }
@@ -332,30 +373,22 @@ async function handleTextractNotification(message) {
try { try {
snsMessage = JSON.parse(body.Message); snsMessage = JSON.parse(body.Message);
} catch (error) { } catch (error) {
//Delete the message so it doesn't clog the queue
const deleteCommand = new DeleteMessageCommand({
QueueUrl: process.env.AWS_TEXTRACT_SQS_QUEUE_URL,
ReceiptHandle: message.ReceiptHandle
});
await sqsClient.send(deleteCommand);
console.error('Error parsing SNS message:', error); console.error('Error parsing SNS message:', error);
console.log('Message Deleted:', body); console.log('Invalid message format:', body);
return; return;
} }
const textractJobId = snsMessage.JobId; const textractJobId = snsMessage.JobId;
const status = snsMessage.Status; const status = snsMessage.Status;
// Check if job exists in Redis // Get job info from Redis
const exists = await jobExists(textractJobId); const jobInfo = await getTextractJob({ redisPubClient, textractJobId });
if (!exists) { if (!jobInfo) {
console.warn(`Job not found for Textract job ID: ${textractJobId}`); console.warn(`Job info not found in Redis for Textract job ID: ${textractJobId}`);
return; return;
} }
const jobInfo = await getTextractJob({ redisPubClient, textractJobId });
if (status === 'SUCCEEDED') { if (status === 'SUCCEEDED') {
// Retrieve the results // Retrieve the results
const { processedData, originalResponse } = await retrieveTextractResults(textractJobId); const { processedData, originalResponse } = await retrieveTextractResults(textractJobId);