diff --git a/apps/web/src/actions/commits/fetchCommitsByProjectAction.ts b/apps/web/src/actions/commits/fetchCommitsByProjectAction.ts new file mode 100644 index 000000000..2bdc2ffc1 --- /dev/null +++ b/apps/web/src/actions/commits/fetchCommitsByProjectAction.ts @@ -0,0 +1,32 @@ +'use server' + +import { CommitsRepository, CommitStatus } from '@latitude-data/core' +import commitPresenter from '$/presenters/commitPresenter' + +import { z } from 'zod' +import { withProject } from '../procedures' + +// Not really using pagination yet +const RANDOM_ULTRA_LARGE_PAGE_SIZE = 1000 + +export const fetchCommitsByProjectAction = withProject + .createServerAction() + .input( + z.object({ + status: z.nativeEnum(CommitStatus), + page: z.number().optional(), + pageSize: z.number().optional(), + }), + { type: 'json' }, + ) + .handler(async ({ input, ctx }) => { + return new CommitsRepository(ctx.workspace.id) + .getCommitsByProject({ + project: ctx.project, + filterByStatus: input.status, + page: input.page ?? 1, + pageSize: input.pageSize ?? RANDOM_ULTRA_LARGE_PAGE_SIZE, + }) + .then((r) => r.unwrap()) + .then((r) => r.map(commitPresenter)) + }) diff --git a/apps/web/src/actions/users/fetch.test.ts b/apps/web/src/actions/users/fetch.test.ts index 75c485189..db1a6834c 100644 --- a/apps/web/src/actions/users/fetch.test.ts +++ b/apps/web/src/actions/users/fetch.test.ts @@ -1,12 +1,16 @@ -import { getSession } from '$/services/auth/getSession' -import { createWorkspace } from '$core/tests/factories/workspaces' +import { factories } from '@latitude-data/core' import useTestDatabase from '$core/tests/useTestDatabase' -import { beforeEach, describe, expect, it, Mock, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { getUsersActions } from './fetch' +const mocks = vi.hoisted(() => { + return { + getSession: vi.fn(), + } +}) vi.mock('$/services/auth/getSession', () => ({ - getSession: vi.fn(), + getSession: mocks.getSession, })) describe('getUsersAction', () => { @@ -22,9 +26,12 @@ describe('getUsersAction', () => { describe('authorized', () => { beforeEach(async () => { - const session = await createWorkspace() - ;(getSession as Mock).mockReturnValue({ - user: session.userData, + const { workspace, userData } = await factories.createWorkspace({ + name: 'test', + }) + mocks.getSession.mockReturnValue({ + user: userData, + workspace: { id: workspace.id, name: workspace.name }, }) }) diff --git a/apps/web/src/actions/users/fetch.ts b/apps/web/src/actions/users/fetch.ts index 5b4747016..3f6ab09e6 100644 --- a/apps/web/src/actions/users/fetch.ts +++ b/apps/web/src/actions/users/fetch.ts @@ -10,7 +10,7 @@ export const getUsersActions = authProcedure .handler(async ({ ctx }) => { const usersScope = new UsersRepository(ctx.workspace.id) - return await usersScope + return usersScope .findAll() .then((r) => r.unwrap()) .then((r) => r.map(userPresenter)) diff --git a/apps/web/src/presenters/commitPresenter.ts b/apps/web/src/presenters/commitPresenter.ts new file mode 100644 index 000000000..069ed3bcb --- /dev/null +++ b/apps/web/src/presenters/commitPresenter.ts @@ -0,0 +1,5 @@ +import { Commit } from '@latitude-data/core' + +export default function commitPresenter(commit: Commit) { + return commit +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 9e285fb53..726f53c85 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -5,3 +5,6 @@ export * from './lib' export * from './schema' export * from './services' export * from './repositories' +import * as factories from './tests/factories' + +export { factories } diff --git a/packages/core/src/repositories/commitsRepository/index.ts b/packages/core/src/repositories/commitsRepository/index.ts index 7caa3502a..483778c91 100644 --- a/packages/core/src/repositories/commitsRepository/index.ts +++ b/packages/core/src/repositories/commitsRepository/index.ts @@ -2,12 +2,12 @@ import { CommitStatus, HEAD_COMMIT } from '$core/constants' import { NotFoundError, Result } from '$core/lib' import { commits, Project, projects } from '$core/schema' import { + and, desc, eq, getTableColumns, isNotNull, isNull, - and, or, } from 'drizzle-orm' @@ -126,7 +126,10 @@ export class CommitsRepository extends Repository { filterByStatus = CommitStatus.All, pageSize = 20, }: { project: Project; filterByStatus?: CommitStatus } & PaginationArgs) { - const filter = filterByStatusQuery({ scope: this.scope, status: filterByStatus }) + const filter = filterByStatusQuery({ + scope: this.scope, + status: filterByStatus, + }) const query = this.db .select({ id: this.scope.id, @@ -135,11 +138,17 @@ export class CommitsRepository extends Repository { description: this.scope.description, projectId: this.scope.projectId, mergedAt: this.scope.mergedAt, + createdAt: this.scope.createdAt, + updatedAt: this.scope.updatedAt, }) .from(this.scope) .where(and(eq(this.scope.projectId, project.id), filter)) - const result = Repository.paginateQuery({ query: query.$dynamic(), page, pageSize }) - return result + const result = await Repository.paginateQuery({ + query: query.$dynamic(), + page, + pageSize, + }) + return Result.ok(result) } }