Skip to content

Commit

Permalink
Remix: list deleted projects (#4877)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggi authored Feb 12, 2024
1 parent 12aeac1 commit 27f3b2e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
33 changes: 33 additions & 0 deletions utopia-remix/app/models/project.server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment'
import { prisma } from '../db.server'
import { createTestProject, createTestUser, truncateTables } from '../test-util'
import {
listDeletedProjects,
listProjects,
renameProject,
restoreDeletedProject,
Expand Down Expand Up @@ -180,4 +181,36 @@ describe('project model', () => {
expect(got?.deleted).toEqual(null)
})
})

describe('listDeletedProjects', () => {
describe('when the user is not found', () => {
it('returns an empty array', async () => {
const got = await listDeletedProjects({ ownerId: 'not-found' })
expect(got.length).toBe(0)
})
})

describe('when the user is passed as undefined', () => {
it('throws an error', async () => {
const fn = async () => listDeletedProjects({ ownerId: undefined as any })
await expect(fn).rejects.toThrow()
})
})

describe('when the user is found', () => {
it('returns the user deleted projects', async () => {
await createTestProject(prisma, { id: 'foo', ownerId: 'bob' })
await createTestProject(prisma, { id: 'bar', ownerId: 'bob', deleted: true })
await createTestProject(prisma, { id: 'baz', ownerId: 'alice' })
await createTestProject(prisma, { id: 'qux', ownerId: 'bob', deleted: true })

const bobProjects = await listDeletedProjects({ ownerId: 'bob' })
expect(bobProjects.length).toBe(2)
expect(bobProjects.map((p) => p.proj_id)).toEqual(['qux', 'bar'])

const aliceProjects = await listDeletedProjects({ ownerId: 'alice' })
expect(aliceProjects.length).toBe(0)
})
})
})
})
13 changes: 13 additions & 0 deletions utopia-remix/app/models/project.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ export async function restoreDeletedProject(params: { id: string; userId: string
data: { deleted: null },
})
}

export async function listDeletedProjects(params: {
ownerId: string
}): Promise<ProjectWithoutContent[]> {
return await prisma.project.findMany({
select: selectProjectWithoutContent,
where: {
owner_id: params.ownerId,
deleted: true,
},
orderBy: { modified_at: 'desc' },
})
}
64 changes: 64 additions & 0 deletions utopia-remix/app/routes-test/projects.deleted.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { prisma } from '../db.server'
import { handleListDeletedProjects } from '../routes/projects.deleted'
import {
createTestProject,
createTestSession,
createTestUser,
newTestRequest,
truncateTables,
} from '../test-util'
import { ApiError } from '../util/api.server'

describe('handleDeleteProject', () => {
afterEach(async () => {
await truncateTables([
prisma.userDetails,
prisma.persistentSession,
prisma.project,
prisma.projectID,
])
})

beforeEach(async () => {
await createTestUser(prisma, { id: 'foo' })
await createTestUser(prisma, { id: 'bar' })
await createTestSession(prisma, { key: 'the-key', userId: 'foo' })
await createTestProject(prisma, { id: 'one', ownerId: 'foo', title: 'project-one' })
await createTestProject(prisma, {
id: 'two',
ownerId: 'foo',
title: 'project-two',
deleted: true,
})
await createTestProject(prisma, { id: 'three', ownerId: 'bar', title: 'project-three' })
await createTestProject(prisma, {
id: 'four',
ownerId: 'foo',
title: 'project-four',
deleted: true,
})
await createTestProject(prisma, {
id: 'five',
ownerId: 'foo',
title: 'project-five',
deleted: true,
})
})

it('requires a user', async () => {
const fn = async () =>
handleListDeletedProjects(newTestRequest({ method: 'POST', authCookie: 'wrong-key' }), {})
await expect(fn).rejects.toThrow(ApiError)
await expect(fn).rejects.toThrow('session not found')
})
it('returns deleted projects', async () => {
const fn = async () => {
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' })
return handleListDeletedProjects(req, {})
}

const got = await fn()
expect(got.projects).toHaveLength(3)
expect(got.projects.map((p) => p.proj_id)).toEqual(['five', 'four', 'two'])
})
})
18 changes: 18 additions & 0 deletions utopia-remix/app/routes/projects.deleted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LoaderFunctionArgs } from '@remix-run/node'
import { Params } from '@remix-run/react'
import { listDeletedProjects } from '../models/project.server'
import { handle, requireUser } from '../util/api.server'

export async function loader(args: LoaderFunctionArgs) {
return handle(args, { GET: handleListDeletedProjects })
}

export async function handleListDeletedProjects(req: Request, params: Params<string>) {
const user = await requireUser(req)

const projects = await listDeletedProjects({ ownerId: user.user_id })

return {
projects,
}
}

0 comments on commit 27f3b2e

Please sign in to comment.