diff --git a/package.json b/package.json index 9fc29467e..1fe80b5fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iSunFA", - "version": "0.8.0+10", + "version": "0.8.0+11", "private": false, "scripts": { "dev": "next dev", diff --git a/src/constants/aich.ts b/src/constants/aich.ts index b75e37cf8..78362b69a 100644 --- a/src/constants/aich.ts +++ b/src/constants/aich.ts @@ -1,4 +1,8 @@ export enum AICH_APIS_TYPES { UPLOAD_OCR = 'upload_ocr', GET_OCR_RESULT_ID = 'get_ocr_result_id', + GET_OCR_RESULT = 'get_ocr_result', + UPLOAD_GEMINI = 'upload_gemini', + GET_GEMINI_RESULT_ID = 'get_gemini_result_id', + GET_GEMINI_RESULT = 'get_gemini_result', } diff --git a/src/lib/utils/aich.ts b/src/lib/utils/aich.ts index a38a3bf03..624c48457 100644 --- a/src/lib/utils/aich.ts +++ b/src/lib/utils/aich.ts @@ -18,6 +18,23 @@ export function getAichUrl(endPoint: AICH_APIS_TYPES, aichResultId?: string): st throw new Error('AICH Result ID is required'); } return `${AICH_URI}/api/v1/ocr/${aichResultId}/process_status`; + case AICH_APIS_TYPES.GET_OCR_RESULT: + if (!aichResultId) { + throw new Error('AICH Result ID is required'); + } + return `${AICH_URI}/api/v1/ocr/${aichResultId}/result`; + case AICH_APIS_TYPES.UPLOAD_GEMINI: + return `${AICH_URI}/api/v1/gemini/upload`; + case AICH_APIS_TYPES.GET_GEMINI_RESULT_ID: + if (!aichResultId) { + throw new Error('AICH Result ID is required'); + } + return `${AICH_URI}/api/v1/gemini/${aichResultId}/process_status`; + case AICH_APIS_TYPES.GET_GEMINI_RESULT: + if (!aichResultId) { + throw new Error('AICH Result ID is required'); + } + return `${AICH_URI}/api/v1/gemini/${aichResultId}/result`; default: throw new Error('Invalid AICH API Type'); } diff --git a/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.test.ts b/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.test.ts index f8f2c69c1..53ee70f49 100644 --- a/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.test.ts +++ b/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.test.ts @@ -58,7 +58,7 @@ describe('fetchOCRResult', () => { const result = await module.fetchOCRResult(resultId); expect(global.fetch).toHaveBeenCalledWith( - expect.stringContaining(`/api/v1/ocr/${resultId}/result`) + expect.stringContaining(`/api/v1/gemini/${resultId}/result`) ); expect(result).toEqual({ payload: 'testPayload' }); diff --git a/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts b/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts index 4ee0be301..e3cdc8941 100644 --- a/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts +++ b/src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts @@ -1,6 +1,5 @@ // Info Murky (20240416): this is mock api need to migrate to microservice import type { NextApiRequest, NextApiResponse } from 'next'; -import { AICH_URI } from '@/constants/config'; import { IResponseData } from '@/interfaces/response_data'; import { IInvoice } from '@/interfaces/invoice'; import { @@ -17,6 +16,8 @@ import { ProgressStatus } from '@/constants/account'; import { getSession } from '@/lib/utils/session'; import { checkAuthorization } from '@/lib/utils/auth_check'; import { AuthFunctionsKeys } from '@/interfaces/auth'; +import { getAichUrl } from '@/lib/utils/aich'; +import { AICH_APIS_TYPES } from '@/constants/aich'; // Info (20240522 - Murky): This OCR now can only be used on Invoice @@ -31,7 +32,8 @@ export async function fetchOCRResult(resultId: string) { let response: Response; try { - response = await fetch(`${AICH_URI}/api/v1/ocr/${resultId}/result`); + const fetchURL = getAichUrl(AICH_APIS_TYPES.GET_GEMINI_RESULT, resultId); + response = await fetch(fetchURL); } catch (error) { throw new Error(STATUS_MESSAGE.INTERNAL_SERVICE_ERROR_AICH_FAILED); } diff --git a/src/pages/api/v1/company/[companyId]/ocr/index.test.ts b/src/pages/api/v1/company/[companyId]/ocr/index.test.ts index da8b44bfe..5262dea54 100644 --- a/src/pages/api/v1/company/[companyId]/ocr/index.test.ts +++ b/src/pages/api/v1/company/[companyId]/ocr/index.test.ts @@ -123,7 +123,7 @@ describe('POST OCR', () => { expect(promiseJson).toBeInstanceOf(Promise); expect(global.fetch).toHaveBeenCalledWith( - expect.stringContaining('/ocr/upload'), + expect.stringContaining('/gemini/upload'), expect.objectContaining({ method: 'POST', body: expect.any(FormData) }) ); }); @@ -227,7 +227,7 @@ describe('POST OCR', () => { expect(resultJson).toEqual(resultJsonArrayExpect); expect(global.fetch).toHaveBeenCalledWith( - expect.stringContaining('/ocr/upload'), + expect.stringContaining('/gemini/upload'), expect.objectContaining({ method: 'POST', body: expect.any(FormData) }) ); }); diff --git a/src/pages/api/v1/company/[companyId]/ocr/index.ts b/src/pages/api/v1/company/[companyId]/ocr/index.ts index 613e2ac0c..6b947292b 100644 --- a/src/pages/api/v1/company/[companyId]/ocr/index.ts +++ b/src/pages/api/v1/company/[companyId]/ocr/index.ts @@ -57,7 +57,7 @@ export async function uploadImageToAICH(imageBlob: Blob, imageName: string) { const formData = createImageFormData(imageBlob, imageName); let response: Response; - const uploadUrl = getAichUrl(AICH_APIS_TYPES.UPLOAD_OCR); + const uploadUrl = getAichUrl(AICH_APIS_TYPES.UPLOAD_GEMINI); try { response = await fetch(uploadUrl, { method: 'POST', @@ -203,7 +203,7 @@ export async function fetchStatus(aichResultId: string) { if (aichResultId.length > 0) { try { - const fetchUrl = getAichUrl(AICH_APIS_TYPES.GET_OCR_RESULT_ID, aichResultId); + const fetchUrl = getAichUrl(AICH_APIS_TYPES.GET_GEMINI_RESULT_ID, aichResultId); const result = await fetch(fetchUrl); if (!result.ok) { diff --git a/src/types/next-auth.d.ts b/src/types/next-auth.d.ts index f5053d7a1..9bb8194eb 100644 --- a/src/types/next-auth.d.ts +++ b/src/types/next-auth.d.ts @@ -8,7 +8,7 @@ declare module 'next-auth' { user: { id: string; hasReadAgreement: boolean; - } & DefaultSession['user'] ; + } & DefaultSession['user']; } interface User {