feat(items): move an item to another list with a long swipe

A longer swipe left on an item (past the edit threshold) opens a dialog
with the user's other lists; picking one moves the item there while the
user stays on the current list. On desktop, right-click now opens a
context menu with "Editar" and "Mover a otra lista…".

The API gets POST /api/lists/:listId/items/:itemId/move, which requires
membership in both lists and publishes item.removed on the source list
and item.created on the target, so other members see the change live.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-20 20:24:38 -03:00
parent 86d4796d88
commit feb8df14c0
13 changed files with 807 additions and 71 deletions

View File

@@ -4,6 +4,7 @@ import type { AppEnv } from '@/types'
import { createItemRoute } from './create/route'
import { listItemsRoute } from './list/route'
import { moveItemRoute } from './move/route'
import { updateItemRoute } from './update/route'
/** `items` sub-module of `lists`: mounted at `/lists/:listId/items`. */
@@ -11,3 +12,4 @@ export const listItemsModule = new Hono<AppEnv>()
.route('/', listItemsRoute)
.route('/', createItemRoute)
.route('/', updateItemRoute)
.route('/', moveItemRoute)

View File

@@ -0,0 +1,31 @@
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 { MoveItemSchema } from './schema'
import { MoveItem } from './use-case'
/** POST /lists/:listId/items/:itemId/move — moves an item to another list of the user. */
export const moveItemRoute = new Hono<AppEnv>().post(
'/:itemId/move',
requireSession,
validate.param(ItemParamsSchema),
validate.json(MoveItemSchema),
async (c) => {
const { listId, itemId } = c.req.valid('param')
const { targetListId } = c.req.valid('json')
const result = await new MoveItem(prisma).execute({
listId,
itemId,
userId: c.get('user').id,
targetListId,
})
return resultJson(c, result)
},
)

View File

@@ -0,0 +1,8 @@
import { z } from 'zod'
/** Body of `POST /lists/:listId/items/:itemId/move`. */
export const MoveItemSchema = z.object({
targetListId: z.uuid('El identificador de la lista de destino no es válido'),
})
export type MoveItemInput = z.infer<typeof MoveItemSchema>

View File

@@ -0,0 +1,141 @@
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 { MoveItem } from './use-case'
type ItemRow = {
id: string
shoppingListId: string
name: string
completed: boolean
boughtAt: Date | null
quantity: string | null
notes: string | null
order: number
createdAt: Date
updatedAt: Date
}
const now = new Date('2026-10-20T10:00:00.000Z')
const item: ItemRow = {
id: 'item-1',
shoppingListId: 'list-1',
name: 'Leche',
completed: false,
boughtAt: null,
quantity: '1 L',
notes: 'sin lactosa',
order: 0,
createdAt: now,
updatedAt: now,
}
function createDb(options: { memberOf: string[]; item: ItemRow | null }) {
const updates: unknown[] = []
const db = {
shoppingListMember: {
findUnique: async (args: { where: { listId_userId: { listId: string } } }) =>
options.memberOf.includes(args.where.listId_userId.listId) ? { role: 'owner' } : null,
},
listItem: {
findFirst: async () => options.item,
update: async (args: { data: Partial<ItemRow> }) => {
updates.push(args)
return { ...options.item, ...args.data } as ItemRow
},
},
}
return { db: db as unknown as Database, updates }
}
function createBus() {
const events = new ListEvents()
const published: ListEvent[] = []
events.subscribe('list-1', (event) => published.push(event))
events.subscribe('list-2', (event) => published.push(event))
return { events, published }
}
const input = { listId: 'list-1', itemId: 'item-1', userId: 'user-1', targetListId: 'list-2' }
describe('MoveItem', () => {
it('answers 400 when the target is the same list', async () => {
const { db, updates } = createDb({ memberOf: ['list-1'], item })
const { events, published } = createBus()
const result = await new MoveItem(db, events).execute({ ...input, targetListId: 'list-1' })
expect(result.ok).toBe(false)
if (result.ok) throw new Error('expected the use case to fail')
expect(result.error.status).toBe(400)
expect(updates).toHaveLength(0)
expect(published).toHaveLength(0)
})
it('answers 404 when the user does not belong to the source list', async () => {
const { db, updates } = createDb({ memberOf: ['list-2'], item })
const { events, published } = createBus()
const result = await new MoveItem(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(updates).toHaveLength(0)
expect(published).toHaveLength(0)
})
it('answers 404 when the user does not belong to the target list', async () => {
const { db, updates } = createDb({ memberOf: ['list-1'], item })
const { events, published } = createBus()
const result = await new MoveItem(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(result.error.detail).toBe('La lista de destino no existe o no tienes acceso.')
expect(updates).toHaveLength(0)
expect(published).toHaveLength(0)
})
it('answers 404 when the item does not belong to the source list', async () => {
const { db, updates } = createDb({ memberOf: ['list-1', 'list-2'], item: null })
const { events, published } = createBus()
const result = await new MoveItem(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(updates).toHaveLength(0)
expect(published).toHaveLength(0)
})
it('moves the item, keeping its details, and notifies both lists', async () => {
const { db, updates } = createDb({ memberOf: ['list-1', 'list-2'], item })
const { events, published } = createBus()
const result = await new MoveItem(db, events).execute(input)
expect(result.ok).toBe(true)
if (!result.ok) throw new Error('expected the use case to succeed')
expect(updates).toEqual([{ where: { id: 'item-1' }, data: { shoppingListId: 'list-2' } }])
expect(result.value).toMatchObject({
id: 'item-1',
listId: 'list-2',
name: 'Leche',
quantity: '1 L',
notes: 'sin lactosa',
completed: false,
})
expect(published).toEqual([
{ type: 'item.removed', listId: 'list-1', itemId: 'item-1' },
{ type: 'item.created', listId: 'list-2', item: result.value },
])
})
})

View File

@@ -0,0 +1,72 @@
import {
Failure,
type ListItem,
Ok,
type ProblemDetails,
problems,
type Result,
} from '@superlist/shared'
import { type ListEvents, listEvents } from '@/lib/events'
import type { Database } from '@/lib/prisma'
import { toListItem } from '../serialize'
type MoveItemFields = {
listId: string
itemId: string
userId: string
targetListId: string
}
/**
* Moves an item to another list, keeping its name, quantity, notes and bought
* state. The user has to be a member of both lists.
*/
export class MoveItem {
constructor(
private readonly db: Database,
private readonly events: ListEvents = listEvents,
) {}
async execute(input: MoveItemFields): Promise<Result<ListItem, ProblemDetails>> {
if (input.targetListId === input.listId) {
return Failure(problems.badRequest('El artículo ya está en esa lista.'))
}
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 targetMembership = await this.db.shoppingListMember.findUnique({
where: { listId_userId: { listId: input.targetListId, userId: input.userId } },
})
if (!targetMembership) {
return Failure(problems.notFound('La lista de destino 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.'))
}
const moved = await this.db.listItem.update({
where: { id: item.id },
data: { shoppingListId: input.targetListId },
})
const dto = toListItem(moved)
this.events.publish({ type: 'item.removed', listId: input.listId, itemId: item.id })
this.events.publish({ type: 'item.created', listId: input.targetListId, item: dto })
return Ok(dto)
}
}