-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quickfix: allow to import logs from any document in the project
Also fixes the issue where importing logs sometimes did not work
- Loading branch information
Showing
7 changed files
with
178 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
apps/web/src/app/api/documents/[projectId]/for-import/route.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
DocumentVersion, | ||
Providers, | ||
Workspace, | ||
} from '@latitude-data/core/browser' | ||
import { createProject, helpers } from '@latitude-data/core/factories' | ||
import { NextRequest } from 'next/server' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
import { GET } from './route' | ||
|
||
vi.mock('$/middlewares/authHandler', () => ({ | ||
authHandler: (fn: Function) => fn, | ||
})) | ||
|
||
describe('GET handler for documents/[projectId]/for-import', () => { | ||
let mockRequest: NextRequest | ||
let mockParams: { projectId: string } | ||
let mockWorkspace: Workspace | ||
let mockDocuments: DocumentVersion[] | ||
|
||
beforeEach(async () => { | ||
mockRequest = new NextRequest('http://localhost:3000') | ||
const { workspace, documents, project } = await createProject({ | ||
providers: [{ type: Providers.OpenAI, name: 'openai' }], | ||
documents: { | ||
foo: { | ||
content: helpers.createPrompt({ provider: 'openai', content: 'foo' }), | ||
}, | ||
}, | ||
}) | ||
mockParams = { projectId: String(project.id) } | ||
|
||
mockWorkspace = workspace | ||
mockDocuments = documents | ||
}) | ||
|
||
it('should return 400 if projectId is missing', async () => { | ||
const response = await GET(mockRequest, { | ||
params: {}, | ||
workspace: mockWorkspace, | ||
} as any) | ||
|
||
expect(response.status).toBe(400) | ||
expect(await response.json()).toEqual({ | ||
message: 'Project ID is required', | ||
details: {}, | ||
}) | ||
}) | ||
|
||
it('should return documents for import when valid params are provided', async () => { | ||
const response = await GET(mockRequest, { | ||
// @ts-expect-error | ||
params: mockParams, | ||
workspace: mockWorkspace, | ||
}) | ||
|
||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual([ | ||
{ | ||
documentUuid: mockDocuments[0]!.documentUuid, | ||
path: mockDocuments[0]!.path, | ||
}, | ||
]) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
apps/web/src/app/api/documents/[projectId]/for-import/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Workspace } from '@latitude-data/core/browser' | ||
import { BadRequestError } from '@latitude-data/core/lib/errors' | ||
import { DocumentVersionsRepository } from '@latitude-data/core/repositories' | ||
import { authHandler } from '$/middlewares/authHandler' | ||
import { errorHandler } from '$/middlewares/errorHandler' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export const GET = errorHandler( | ||
authHandler( | ||
async ( | ||
_: NextRequest, | ||
{ | ||
params, | ||
workspace, | ||
}: { | ||
params: { projectId: string } | ||
workspace: Workspace | ||
}, | ||
) => { | ||
const { projectId } = params | ||
|
||
if (!projectId) { | ||
throw new BadRequestError('Project ID is required') | ||
} | ||
|
||
const docsScope = new DocumentVersionsRepository(workspace.id) | ||
const documents = await docsScope | ||
.getDocumentsForImport(Number(projectId)) | ||
.then((r) => r.unwrap()) | ||
|
||
return NextResponse.json(documents, { status: 200 }) | ||
}, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { useToast } from '@latitude-data/web-ui' | ||
import useSWR, { SWRConfiguration } from 'swr' | ||
|
||
interface DocumentForImport { | ||
documentUuid: string | ||
path: string | ||
} | ||
|
||
export default function useDocumentsForImport( | ||
{ projectId }: { projectId?: number }, | ||
opts?: SWRConfiguration, | ||
) { | ||
const { toast } = useToast() | ||
const fetcher = useCallback(async () => { | ||
if (!projectId) return [] | ||
|
||
const response = await fetch(`/api/documents/${projectId}/for-import`, { | ||
credentials: 'include', | ||
}) | ||
|
||
if (!response.ok) { | ||
const error = await response.json() | ||
toast({ | ||
title: 'Error fetching documents for import', | ||
description: error.message || 'An error occurred', | ||
variant: 'destructive', | ||
}) | ||
return [] | ||
} | ||
|
||
return response.json() | ||
}, [projectId, toast]) | ||
|
||
const { | ||
data = [], | ||
mutate, | ||
...rest | ||
} = useSWR<DocumentForImport[]>( | ||
['api/documents/for-import', projectId], | ||
fetcher, | ||
opts, | ||
) | ||
|
||
return { data, mutate, ...rest } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters