Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Jul 30, 2024
1 parent 3bba50e commit 41bc538
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
32 changes: 32 additions & 0 deletions apps/web/src/actions/commits/fetchCommitsByProjectAction.ts
Original file line number Diff line number Diff line change
@@ -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))
})
21 changes: 14 additions & 7 deletions apps/web/src/actions/users/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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 },
})
})

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/actions/users/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/presenters/commitPresenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Commit } from '@latitude-data/core'

export default function commitPresenter(commit: Commit) {
return commit
}
3 changes: 3 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export * from './lib'
export * from './schema'
export * from './services'
export * from './repositories'
import * as factories from './tests/factories'

export { factories }
17 changes: 13 additions & 4 deletions packages/core/src/repositories/commitsRepository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}

0 comments on commit 41bc538

Please sign in to comment.