Skip to content

Commit

Permalink
feature: tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos committed Jul 26, 2024
1 parent a4b833e commit 32508d3
Show file tree
Hide file tree
Showing 49 changed files with 913 additions and 647 deletions.
16 changes: 8 additions & 8 deletions apps/web/src/actions/documents/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use server'

import { createNewDocument } from '@latitude-data/core'
import { findCommit } from '$/app/(private)/_data-access'
import { CommitsRepository, createNewDocument } from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'
Expand All @@ -15,14 +14,15 @@ export const createDocumentVersionAction = withProject
}),
{ type: 'json' },
)
.handler(async ({ input }) => {
const commit = await findCommit({
projectId: input.projectId,
uuid: input.commitUuid,
})
.handler(async ({ input, ctx }) => {
const commit = await new CommitsRepository(ctx.project.workspaceId)
.getCommitByUuid({ uuid: input.commitUuid, projectId: ctx.project.id })
.then((r) => r.unwrap())

const result = await createNewDocument({
commitId: commit.id,
commit,
path: input.path,
})

return result.unwrap()
})
9 changes: 7 additions & 2 deletions apps/web/src/actions/documents/getContentByPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use server'

import { CommitsRepository } from '@latitude-data/core'
import { getDocumentByPath } from '$/app/(private)/_data-access'
import { z } from 'zod'

Expand All @@ -14,9 +15,13 @@ export const getDocumentContentByPathAction = withProject
}),
{ type: 'json' },
)
.handler(async ({ input }) => {
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.then((r) => r.unwrap())
const document = await getDocumentByPath({
commitId: input.commitId,
commit,
path: input.path,
})
return document.content
Expand Down
25 changes: 21 additions & 4 deletions apps/web/src/actions/documents/updateContent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use server'

import { updateDocument } from '@latitude-data/core'
import {
CommitsRepository,
DocumentVersionsRepository,
updateDocument,
} from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'
Expand All @@ -15,11 +19,24 @@ export const updateDocumentContentAction = withProject
}),
{ type: 'json' },
)
.handler(async ({ input }) => {
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const document = await docsScope
.getDocumentByUuid({
commitId: input.commitId,
documentUuid: input.documentUuid,
})
.then((r) => r.unwrap())
const commit = await commitsScope
.getCommitById(input.commitId)
.then((r) => r.unwrap())

const result = await updateDocument({
commitId: input.commitId,
documentUuid: input.documentUuid,
commit,
document,
content: input.content,
})

return result.unwrap()
})
10 changes: 5 additions & 5 deletions apps/web/src/actions/procedures/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findProject } from '@latitude-data/core'
import { ProjectsRepository } from '@latitude-data/core'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { z } from 'zod'
import { createServerActionProcedure } from 'zsa'
Expand All @@ -16,11 +16,11 @@ export const authProcedure = createServerActionProcedure().handler(async () => {
export const withProject = createServerActionProcedure(authProcedure)
.input(z.object({ projectId: z.number() }))
.handler(async ({ input, ctx }) => {
const { workspace } = ctx
const projectScope = new ProjectsRepository(workspace.id)
const project = (
await findProject({
projectId: input.projectId,
workspaceId: ctx.workspace.id,
})
await projectScope.getProjectById(input.projectId)
).unwrap()

return { ...ctx, project }
})
1 change: 0 additions & 1 deletion apps/web/src/actions/user/setupAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const setupAction = createServerAction()
)
.handler(async ({ input }) => {
const itWasAlreadySetup = await isWorkspaceCreated()

if (itWasAlreadySetup) {
throw new Error('Workspace already created')
}
Expand Down
67 changes: 43 additions & 24 deletions apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,81 @@
import { cache } from 'react'

import {
getDocumentAtCommit,
Commit,
CommitsRepository,
DocumentVersionsRepository,
findWorkspaceFromCommit,
NotFoundError,
findCommitByUuid as originalfindCommit,
findProject as originalFindProject,
getDocumentsAtCommit as originalGetDocumentsAtCommit,
getFirstProject as originalGetFirstProject,
type FindCommitByUuidProps,
type FindProjectProps,
type GetDocumentAtCommitProps,
Project,
ProjectsRepository,
} from '@latitude-data/core'

export const getFirstProject = cache(
async ({ workspaceId }: { workspaceId: number }) => {
const result = await originalGetFirstProject({ workspaceId })
const projectsScope = new ProjectsRepository(workspaceId)
const result = await projectsScope.getFirstProject()
const project = result.unwrap()

return project
},
)

export const findProject = cache(
async ({ projectId, workspaceId }: FindProjectProps) => {
const result = await originalFindProject({ projectId, workspaceId })
async ({
projectId,
workspaceId,
}: {
projectId: number
workspaceId: number
}) => {
const projectsScope = new ProjectsRepository(workspaceId)
const result = await projectsScope.getProjectById(projectId)
const project = result.unwrap()

return project
},
)

export const findCommit = cache(
async ({ uuid, projectId }: FindCommitByUuidProps) => {
const result = await originalfindCommit({ uuid, projectId })
async ({ uuid, project }: { uuid: string; project: Project }) => {
const commitsScope = new CommitsRepository(project.workspaceId)
const result = await commitsScope.getCommitByUuid({
projectId: project.id,
uuid,
})
const commit = result.unwrap()

return commit
},
)

export const getDocumentByUuid = cache(
async ({ documentUuid, commitId }: GetDocumentAtCommitProps) => {
const result = await getDocumentAtCommit({ documentUuid, commitId })
const document = result.unwrap()
async ({
documentUuid,
commit,
}: {
documentUuid: string
commit: Commit
}) => {
const workspace = await findWorkspaceFromCommit(commit)
const scope = new DocumentVersionsRepository(workspace!.id)
const result = await scope.getDocumentAtCommit({ documentUuid, commit })

return document
return result.unwrap()
},
)

export const getDocumentByPath = cache(
async ({ commitId, path }: { commitId: number; path: string }) => {
const documents = (
await originalGetDocumentsAtCommit({ commitId })
).unwrap()
async ({ commit, path }: { commit: Commit; path: string }) => {
const workspace = await findWorkspaceFromCommit(commit)
const docsScope = new DocumentVersionsRepository(workspace!.id)
const documents = await docsScope
.getDocumentsAtCommit(commit)
.then((r) => r.unwrap())

const document = documents.find((d) => d.path === path)
if (!document) {
throw new NotFoundError('Document not found')
}
if (!document) throw new NotFoundError('Document not found')

return document
},
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HEAD_COMMIT, mergeCommit } from '@latitude-data/core'
import { LatitudeRequest } from '$/middleware'
import useTestDatabase from '$core/tests/useTestDatabase'
import { NextRequest } from 'next/server'
import { describe, expect, test } from 'vitest'

import { GET } from './route'
Expand All @@ -10,26 +10,26 @@ useTestDatabase()
describe('GET documentVersion', () => {
test('returns the document by path', async (ctx) => {
const { project } = await ctx.factories.createProject()
const { commit } = await ctx.factories.createDraft({ project })
let { commit } = await ctx.factories.createDraft({ project })
const { documentVersion: doc } = await ctx.factories.createDocumentVersion({
commit,
})

await mergeCommit({ commitId: commit.id })

const response = await GET(
new NextRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
),
{
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: doc.path.split('/'),
},
},
commit = await mergeCommit(commit).then((r) => r.unwrap())
const req = new LatitudeRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
)

req.workspaceId = project.workspaceId

const response = await GET(req, {
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: doc.path.split('/'),
},
})

expect(response.status).toBe(200)
const responseDoc = await response.json()
expect(responseDoc.documentUuid).toEqual(doc.documentUuid)
Expand All @@ -38,25 +38,24 @@ describe('GET documentVersion', () => {

test('returns the document in main branch if commitUuid is HEAD', async (ctx) => {
const { project } = await ctx.factories.createProject()
const { commit } = await ctx.factories.createDraft({ project })
let { commit } = await ctx.factories.createDraft({ project })
const { documentVersion: doc } = await ctx.factories.createDocumentVersion({
commit,
})

await mergeCommit({ commitId: commit.id })

const response = await GET(
new NextRequest(
'http://localhost/api/projects/projectId/commits/HEAD/path/to/doc',
),
{
params: {
projectId: project.id,
commitUuid: HEAD_COMMIT,
documentPath: doc.path.split('/'),
},
},
commit = await mergeCommit(commit).then((r) => r.unwrap())
const req = new LatitudeRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
)
req.workspaceId = project.workspaceId

const response = await GET(req, {
params: {
projectId: project.id,
commitUuid: HEAD_COMMIT,
documentPath: doc.path.split('/'),
},
})

expect(response.status).toBe(200)
const responseDoc = await response.json()
Expand All @@ -66,22 +65,21 @@ describe('GET documentVersion', () => {

test('returns 404 if document is not found', async (ctx) => {
const { project } = await ctx.factories.createProject()
const { commit } = await ctx.factories.createDraft({ project })
let { commit } = await ctx.factories.createDraft({ project })

await mergeCommit({ commitId: commit.id })

const response = await GET(
new NextRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
),
{
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: ['path', 'to', 'doc'],
},
},
commit = await mergeCommit(commit).then((r) => r.unwrap())
const req = new LatitudeRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
)
req.workspaceId = project.workspaceId

const response = await GET(req, {
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: ['path', 'to', 'doc'],
},
})

expect(response.status).toBe(404)
})
Expand All @@ -93,18 +91,18 @@ describe('GET documentVersion', () => {
commit,
})

const response = await GET(
new NextRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
),
{
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: doc.path.split('/'),
},
},
const req = new LatitudeRequest(
'http://localhost/api/projects/projectId/commits/commitUuid/path/to/doc',
)
req.workspaceId = project.workspaceId

const response = await GET(req, {
params: {
projectId: project.id,
commitUuid: commit.uuid,
documentPath: doc.path.split('/'),
},
})

expect(response.status).toBe(200)
const responseDoc = await response.json()
Expand Down
Loading

0 comments on commit 32508d3

Please sign in to comment.