Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect to gemini aich #2081

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0+10",
"version": "0.8.0+11",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
4 changes: 4 additions & 0 deletions src/constants/aich.ts
Original file line number Diff line number Diff line change
@@ -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',
}
17 changes: 17 additions & 0 deletions src/lib/utils/aich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • api naming issue

);

expect(result).toEqual({ payload: 'testPayload' });
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/v1/company/[companyId]/ocr/[resultId]/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/v1/company/[companyId]/ocr/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('POST OCR', () => {
expect(promiseJson).toBeInstanceOf(Promise);

expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining('/ocr/upload'),
expect.stringContaining('/gemini/upload'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • api naming issue

expect.objectContaining({ method: 'POST', body: expect.any(FormData) })
);
});
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('POST OCR', () => {
expect(resultJson).toEqual(resultJsonArrayExpect);

expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining('/ocr/upload'),
expect.stringContaining('/gemini/upload'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • api naming issue

expect.objectContaining({ method: 'POST', body: expect.any(FormData) })
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/v1/company/[companyId]/ocr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare module 'next-auth' {
user: {
id: string;
hasReadAgreement: boolean;
} & DefaultSession['user'] ;
} & DefaultSession['user'];
}

interface User {
Expand Down