-
Notifications
You must be signed in to change notification settings - Fork 64
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 (#286)
Also fixes the issue where importing logs sometimes did not work
- Loading branch information
Showing
12 changed files
with
326 additions
and
31 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
import { | ||
DocumentVersion, | ||
Providers, | ||
User, | ||
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' | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
getSession: vi.fn(), | ||
} | ||
}) | ||
vi.mock('$/services/auth/getSession', () => ({ | ||
getSession: mocks.getSession, | ||
})) | ||
|
||
describe('GET handler for documents/[projectId]/for-import', () => { | ||
let mockRequest: NextRequest | ||
let mockParams: { projectId: string } | ||
let mockWorkspace: Workspace | ||
let mockDocuments: DocumentVersion[] | ||
let mockUser: User | ||
|
||
beforeEach(async () => { | ||
mockRequest = new NextRequest('http://localhost:3000') | ||
const { workspace, documents, project, user } = await createProject({ | ||
providers: [{ type: Providers.OpenAI, name: 'openai' }], | ||
documents: { | ||
foo: { | ||
content: helpers.createPrompt({ provider: 'openai', content: 'foo' }), | ||
}, | ||
}, | ||
}) | ||
mockParams = { projectId: String(project.id) } | ||
|
||
mockUser = user | ||
mockWorkspace = workspace | ||
mockDocuments = documents | ||
}) | ||
|
||
describe('unauthorized', () => { | ||
it('should return 401 if user is not authenticated', async () => { | ||
const response = await GET(mockRequest, { | ||
params: mockParams, | ||
workspace: mockWorkspace, | ||
} as any) | ||
|
||
expect(response.status).toBe(401) | ||
expect(await response.json()).toEqual({ | ||
message: 'Unauthorized', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('authorized', () => { | ||
beforeEach(async () => { | ||
mocks.getSession.mockReturnValue({ user: mockUser }) | ||
}) | ||
|
||
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
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 } | ||
} |
Oops, something went wrong.