Skip to content

Commit

Permalink
Bug/ocr override inputted data (#2766)
Browse files Browse the repository at this point in the history
* add ocr invalidations

* feat: fix OCR functionality

* updated notification message for ocr

* feat: updated timeout for ocr

* updated timout logic

---------

Co-authored-by: Alon Peretz <8467965+alonp99@users.noreply.github.com>
  • Loading branch information
Blokh and alonp99 authored Oct 20, 2024
1 parent 56238f0 commit 5448ecd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/backoffice-v2/public/locales/en/toast.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
},
"document_ocr": {
"success": "OCR performed successfully.",
"empty_extraction": "Unable to extract the document's relevant fields.",
"error": "Failed to perform OCR on the document."
},
"business_report_creation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { toast } from 'sonner';
import { t } from 'i18next';
import { workflowsQueryKeys } from '@/domains/workflows/query-keys';
import { useFilterId } from '@/common/hooks/useFilterId/useFilterId';
import { isEmptyObject } from '@ballerine/common';

export const useDocumentOcr = ({ workflowId }: { workflowId: string }) => {
const filterId = useFilterId();
Expand All @@ -19,6 +20,17 @@ export const useDocumentOcr = ({ workflowId }: { workflowId: string }) => {
},
onSuccess: (data, variables) => {
void queryClient.invalidateQueries(workflowsQueryKeys._def);

if (
!data.parsedData ||
isEmptyObject(data.parsedData) ||
Object.entries(data.parsedData).every(([_, value]) => {
return (typeof value === 'string' && value.trim() === '') || !value;
})
) {
return toast.info(t('toast:document_ocr.empty_extraction'));
}

toast.success(t('toast:document_ocr.success'));
},
onError: (error, variables) => {
Expand Down
1 change: 1 addition & 0 deletions apps/backoffice-v2/src/domains/workflows/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export const fetchWorkflowDocumentOCRResult = async ({
env.VITE_API_URL,
)}/api/v1/internal/workflows/${workflowRuntimeId}/documents/${documentId}/run-ocr`,
schema: z.any(),
timeout: 40_000,
});

return handleZodError(error, workflow);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MotionButton } from '@/common/components/molecules/MotionButton/MotionButton';
import { checkIsBusiness } from '@/common/utils/check-is-business/check-is-business';
import { ctw } from '@/common/utils/ctw/ctw';
import { CommonWorkflowStates, isObject, StateTag, valueOrNA } from '@ballerine/common';
import { CommonWorkflowStates, StateTag, valueOrNA } from '@ballerine/common';
import { useApproveTaskByIdMutation } from '@/domains/entities/hooks/mutations/useApproveTaskByIdMutation/useApproveTaskByIdMutation';
import { useRejectTaskByIdMutation } from '@/domains/entities/hooks/mutations/useRejectTaskByIdMutation/useRejectTaskByIdMutation';
import { useRemoveDecisionTaskByIdMutation } from '@/domains/entities/hooks/mutations/useRemoveDecisionTaskByIdMutation/useRemoveDecisionTaskByIdMutation';
Expand Down Expand Up @@ -373,16 +373,6 @@ export const useDocumentBlocks = ({
...propertiesSchema?.properties,
} ?? {},
).map(([title, formattedValue]) => {
if (isObject(formattedValue)) {
return [
title,
{
...formattedValue,
value: formattedValue.value || ocrResult?.parsedData?.[title],
},
];
}

return [title, formattedValue];
});

Expand Down Expand Up @@ -418,6 +408,18 @@ export const useDocumentBlocks = ({
return value;
}

if (ocrResult?.parsedData?.[title]) {
const isOcrValueString = typeof ocrResult.parsedData[title] === 'string';

if (isOcrValueString && ocrResult.parsedData[title].length > 0) {
return ocrResult.parsedData[title];
}

if (!isOcrValueString) {
return ocrResult.parsedData[title];
}
}

if (
typeof properties?.[title] === 'undefined' &&
typeof defaultValue !== 'undefined'
Expand Down

0 comments on commit 5448ecd

Please sign in to comment.