feat(items): delete an item with a swipe right, undoable for 3 seconds
A long swipe right on an item (or "Eliminar" in the desktop context menu) hides it and shows a toast with "Deshacer". The DELETE is only sent once the 3 seconds are over, so undoing just cancels it; pending deletions are flushed with keepalive if the page is closed in the meantime. The API gets DELETE /api/lists/:listId/items/:itemId, members only, which publishes item.removed so other members see the item disappear. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
27
apps/api/src/modules/lists/items/delete/route.ts
Normal file
27
apps/api/src/modules/lists/items/delete/route.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Hono } from 'hono'
|
||||
|
||||
import { requireSession } from '@/lib/auth-session'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { resultJson } from '@/lib/result-json'
|
||||
import { validate } from '@/lib/validate'
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
import { ItemParamsSchema } from '../update/schema'
|
||||
import { DeleteItem } from './use-case'
|
||||
|
||||
/** DELETE /lists/:listId/items/:itemId — deletes an item from a list the user belongs to. */
|
||||
export const deleteItemRoute = new Hono<AppEnv>().delete(
|
||||
'/:itemId',
|
||||
requireSession,
|
||||
validate.param(ItemParamsSchema),
|
||||
async (c) => {
|
||||
const { listId, itemId } = c.req.valid('param')
|
||||
const result = await new DeleteItem(prisma).execute({
|
||||
listId,
|
||||
itemId,
|
||||
userId: c.get('user').id,
|
||||
})
|
||||
|
||||
return resultJson(c, result, { status: 204 })
|
||||
},
|
||||
)
|
||||
75
apps/api/src/modules/lists/items/delete/use-case.test.ts
Normal file
75
apps/api/src/modules/lists/items/delete/use-case.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it } from 'bun:test'
|
||||
import type { ListEvent } from '@superlist/shared'
|
||||
|
||||
import { ListEvents } from '@/lib/events'
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { DeleteItem } from './use-case'
|
||||
|
||||
function createDb(options: { membership: unknown; item: { id: string } | null }) {
|
||||
const deletes: unknown[] = []
|
||||
|
||||
const db = {
|
||||
shoppingListMember: {
|
||||
findUnique: async () => options.membership,
|
||||
},
|
||||
listItem: {
|
||||
findFirst: async () => options.item,
|
||||
delete: async (args: unknown) => {
|
||||
deletes.push(args)
|
||||
return options.item
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return { db: db as unknown as Database, deletes }
|
||||
}
|
||||
|
||||
function createBus() {
|
||||
const events = new ListEvents()
|
||||
const published: ListEvent[] = []
|
||||
events.subscribe('list-1', (event) => published.push(event))
|
||||
|
||||
return { events, published }
|
||||
}
|
||||
|
||||
const input = { listId: 'list-1', itemId: 'item-1', userId: 'user-1' }
|
||||
|
||||
describe('DeleteItem', () => {
|
||||
it('answers 404 when the user does not belong to the list', async () => {
|
||||
const { db, deletes } = createDb({ membership: null, item: { id: 'item-1' } })
|
||||
const { events, published } = createBus()
|
||||
const result = await new DeleteItem(db, events).execute(input)
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
if (result.ok) throw new Error('expected the use case to fail')
|
||||
expect(result.error.status).toBe(404)
|
||||
expect(deletes).toHaveLength(0)
|
||||
expect(published).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('answers 404 when the item does not belong to the list', async () => {
|
||||
const { db, deletes } = createDb({ membership: { role: 'owner' }, item: null })
|
||||
const { events, published } = createBus()
|
||||
const result = await new DeleteItem(db, events).execute(input)
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
if (result.ok) throw new Error('expected the use case to fail')
|
||||
expect(result.error.status).toBe(404)
|
||||
expect(deletes).toHaveLength(0)
|
||||
expect(published).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('deletes the item and publishes item.removed', async () => {
|
||||
const { db, deletes } = createDb({
|
||||
membership: { role: 'collaborator' },
|
||||
item: { id: 'item-1' },
|
||||
})
|
||||
const { events, published } = createBus()
|
||||
const result = await new DeleteItem(db, events).execute(input)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(deletes).toEqual([{ where: { id: 'item-1' } }])
|
||||
expect(published).toEqual([{ type: 'item.removed', listId: 'list-1', itemId: 'item-1' }])
|
||||
})
|
||||
})
|
||||
42
apps/api/src/modules/lists/items/delete/use-case.ts
Normal file
42
apps/api/src/modules/lists/items/delete/use-case.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Failure, Ok, type ProblemDetails, problems, type Result } from '@superlist/shared'
|
||||
|
||||
import { type ListEvents, listEvents } from '@/lib/events'
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
type DeleteItemFields = {
|
||||
listId: string
|
||||
itemId: string
|
||||
userId: string
|
||||
}
|
||||
|
||||
/** Deletes an item from a list. Only members can do it. */
|
||||
export class DeleteItem {
|
||||
constructor(
|
||||
private readonly db: Database,
|
||||
private readonly events: ListEvents = listEvents,
|
||||
) {}
|
||||
|
||||
async execute(input: DeleteItemFields): Promise<Result<void, ProblemDetails>> {
|
||||
const membership = await this.db.shoppingListMember.findUnique({
|
||||
where: { listId_userId: { listId: input.listId, userId: input.userId } },
|
||||
})
|
||||
|
||||
if (!membership) {
|
||||
return Failure(problems.notFound('La lista no existe o no tienes acceso.'))
|
||||
}
|
||||
|
||||
const item = await this.db.listItem.findFirst({
|
||||
where: { id: input.itemId, shoppingListId: input.listId },
|
||||
})
|
||||
|
||||
if (!item) {
|
||||
return Failure(problems.notFound('El artículo no existe en esta lista.'))
|
||||
}
|
||||
|
||||
await this.db.listItem.delete({ where: { id: item.id } })
|
||||
|
||||
this.events.publish({ type: 'item.removed', listId: input.listId, itemId: item.id })
|
||||
|
||||
return Ok(undefined)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { Hono } from 'hono'
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
import { createItemRoute } from './create/route'
|
||||
import { deleteItemRoute } from './delete/route'
|
||||
import { listItemsRoute } from './list/route'
|
||||
import { moveItemRoute } from './move/route'
|
||||
import { updateItemRoute } from './update/route'
|
||||
@@ -13,3 +14,4 @@ export const listItemsModule = new Hono<AppEnv>()
|
||||
.route('/', createItemRoute)
|
||||
.route('/', updateItemRoute)
|
||||
.route('/', moveItemRoute)
|
||||
.route('/', deleteItemRoute)
|
||||
|
||||
Reference in New Issue
Block a user