Skip to content

Commit

Permalink
ocr unify input check #2337
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyMurky committed Sep 11, 2024
1 parent 89770b5 commit 2786cbd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.1+11",
"version": "0.8.1+12",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
1 change: 1 addition & 0 deletions src/constants/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const AVERAGE_OCR_PROCESSING_TIME = 30 * ONE_MINUTE_IN_MS;

export enum ocrTypes {
INVOICE = 'invoice',
CONTRACT = 'contract',
}
9 changes: 8 additions & 1 deletion src/constants/zod_schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { APIName } from '@/constants/api_connection';
import { ocrListValidator, ocrUploadValidator } from '@/lib/utils/zod_schema/ocr';
import {
ocrDeleteValidator,
ocrListValidator,
ocrResultGetByIdValidator,
ocrUploadValidator,
} from '@/lib/utils/zod_schema/ocr';
import { zodExampleValidator } from '@/lib/utils/zod_schema/zod_example';

/*
Expand All @@ -17,4 +22,6 @@ export const API_ZOD_SCHEMA = {
[APIName.ZOD_EXAMPLE]: zodExampleValidator,
[APIName.OCR_LIST]: ocrListValidator,
[APIName.OCR_UPLOAD]: ocrUploadValidator,
[APIName.OCR_RESULT_GET_BY_ID]: ocrResultGetByIdValidator,
[APIName.OCR_DELETE]: ocrDeleteValidator,
};
29 changes: 29 additions & 0 deletions src/lib/utils/zod_schema/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,32 @@ export const ocrUploadValidator: IZodValidator<
query: ocrUploadQueryValidator,
body: ocrUploadBodyValidator,
};

const ocrResultGetByIdQueryValidator = z.object({
resultId: z.string(),
ocrType: z.nativeEnum(ocrTypes).or(z.undefined()),
});

const ocrResultGetByIdBodyValidator = z.object({});

export const ocrResultGetByIdValidator: IZodValidator<
(typeof ocrResultGetByIdQueryValidator)['shape'],
(typeof ocrResultGetByIdBodyValidator)['shape']
> = {
query: ocrResultGetByIdQueryValidator,
body: ocrResultGetByIdBodyValidator,
};

const ocrDeleteQueryValidator = z.object({
resultId: z.string(),
});

const ocrDeleteBodyValidator = z.object({});

export const ocrDeleteValidator: IZodValidator<
(typeof ocrDeleteQueryValidator)['shape'],
(typeof ocrDeleteBodyValidator)['shape']
> = {
query: ocrDeleteQueryValidator,
body: ocrDeleteBodyValidator,
};
20 changes: 0 additions & 20 deletions src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@ afterEach(() => {
jest.clearAllMocks();
});

describe('isResultIdValid', () => {
it('should return true if resultId is valid', () => {
const resultId = 'validResultId';
const result = module.isResultIdValid(resultId);
expect(result).toBe(true);
});

it('should return false if resultId is undefined', () => {
const resultId = undefined;
const result = module.isResultIdValid(resultId);
expect(result).toBe(false);
});

it('should return false if resultId is an array', () => {
const resultId = ['validResultId'];
const result = module.isResultIdValid(resultId);
expect(result).toBe(false);
});
});

describe('fetchOCRResult', () => {
it('should fetch OCR result with GET', async () => {
const resultId = 'validResultId';
Expand Down
40 changes: 20 additions & 20 deletions src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ import { AuthFunctionsKeys } from '@/interfaces/auth';
import { getAichUrl } from '@/lib/utils/aich';
import { AICH_APIS_TYPES } from '@/constants/aich';
import loggerBack, { loggerError } from '@/lib/utils/logger_back';
import { ocrTypes } from '@/constants/ocr';
import { validateRequest } from '@/lib/utils/request_validator';
import { APIName } from '@/constants/api_connection';

// Info: (20240522 - Murky) This OCR now can only be used on Invoice

export function isResultIdValid(resultId: string | string[] | undefined): resultId is string {
if (Array.isArray(resultId) || !resultId || typeof resultId !== 'string') {
return false;
}
return true;
}

export async function fetchOCRResult(resultId: string) {
let response: Response;

Expand Down Expand Up @@ -99,19 +95,19 @@ export function formatOCRResultDate(ocrResult: IInvoice) {
return newOcrResult;
}

export async function handleGetRequest(resultId: string, type: string = 'invoice') {
export async function handleGetRequest(resultId: string, ocrType: ocrTypes = ocrTypes.INVOICE) {
let ocrResult: IInvoice | IContract | null = null;

const isResultIdError = resultId && resultId.slice(0, 5) === 'error';
if (resultId && resultId.length >= 0 && !isResultIdError) {
const fetchResult = fetchOCRResult(resultId);
switch (type) {
case 'contract': {
switch (ocrType) {
case ocrTypes.CONTRACT: {
ocrResult = {} as IContract;

break;
}
case 'invoice': {
case ocrTypes.INVOICE: {
ocrResult = await getPayloadFromResponseJSON(fetchResult);
if (ocrResult) {
let newOcr: IInvoice | null = setOCRResultJournalId(ocrResult, null);
Expand Down Expand Up @@ -169,22 +165,26 @@ export default async function handler(
let payload: APIReturnType = null;
let status: string = STATUS_MESSAGE.BAD_REQUEST;

const { resultId, type } = req.query;
if (isAuth) {
try {
if (!isResultIdValid(resultId)) {
throw new Error(STATUS_MESSAGE.INVALID_INPUT_PARAMETER);
}

switch (req.method) {
case 'GET': {
payload = await handleGetRequest(resultId, type as string);
status = STATUS_MESSAGE.SUCCESS;
const { query } = validateRequest(APIName.OCR_RESULT_GET_BY_ID, req, userId);
if (query) {
const { resultId, ocrType } = query;
payload = await handleGetRequest(resultId, ocrType);
status = STATUS_MESSAGE.SUCCESS;
}
break;
}
case 'DELETE': {
payload = await handleDeleteRequest(resultId);
status = STATUS_MESSAGE.SUCCESS_DELETE;
const { query } = validateRequest(APIName.OCR_DELETE, req, userId);

if (query) {
const { resultId } = query;
payload = await handleDeleteRequest(resultId);
status = STATUS_MESSAGE.SUCCESS_DELETE;
}
break;
}
default: {
Expand Down

0 comments on commit 2786cbd

Please sign in to comment.