feat(items): edit an item's quantity by swiping or right-clicking it
The PATCH endpoint now accepts quantity alongside completed (at least one field is required), and the row shows the quantity only when its leading number is above one. A small dialog edits it, opened by a left swipe on touch or by right-clicking the row on desktop.
This commit is contained in:
@@ -9,7 +9,7 @@ import type { AppEnv } from '@/types'
|
||||
import { ItemParamsSchema, UpdateItemSchema } from './schema'
|
||||
import { UpdateItem } from './use-case'
|
||||
|
||||
/** PATCH /lists/:listId/items/:itemId — marks an item as bought or pending. */
|
||||
/** PATCH /lists/:listId/items/:itemId — updates an item's bought state and/or quantity. */
|
||||
export const updateItemRoute = new Hono<AppEnv>().patch(
|
||||
'/:itemId',
|
||||
requireSession,
|
||||
@@ -17,12 +17,13 @@ export const updateItemRoute = new Hono<AppEnv>().patch(
|
||||
validate.json(UpdateItemSchema),
|
||||
async (c) => {
|
||||
const { listId, itemId } = c.req.valid('param')
|
||||
const { completed } = c.req.valid('json')
|
||||
const { completed, quantity } = c.req.valid('json')
|
||||
const result = await new UpdateItem(prisma).execute({
|
||||
listId,
|
||||
itemId,
|
||||
userId: c.get('user').id,
|
||||
completed,
|
||||
quantity,
|
||||
})
|
||||
|
||||
return resultJson(c, result)
|
||||
|
||||
@@ -8,9 +8,14 @@ export const ItemParamsSchema = z.object({
|
||||
|
||||
export type ItemParams = z.infer<typeof ItemParamsSchema>
|
||||
|
||||
/** Body of `PATCH /lists/:listId/items/:itemId`. */
|
||||
export const UpdateItemSchema = z.object({
|
||||
completed: z.boolean(),
|
||||
})
|
||||
/** Body of `PATCH /lists/:listId/items/:itemId` — at least one field is required. */
|
||||
export const UpdateItemSchema = z
|
||||
.object({
|
||||
completed: z.boolean().optional(),
|
||||
quantity: z.string().trim().max(40, 'Máximo 40 caracteres').nullable().optional(),
|
||||
})
|
||||
.refine((value) => value.completed !== undefined || value.quantity !== undefined, {
|
||||
message: 'Indica al menos un campo para actualizar.',
|
||||
})
|
||||
|
||||
export type UpdateItemInput = z.infer<typeof UpdateItemSchema>
|
||||
|
||||
@@ -45,9 +45,13 @@ function createDb(options: { membership: unknown; item: ItemRow | null }) {
|
||||
},
|
||||
listItem: {
|
||||
findFirst: async () => options.item,
|
||||
update: async (args: { data: { completed: boolean; boughtAt: Date | null } }) => {
|
||||
// The use case only sends the fields it changes, so the stored row is
|
||||
// simulated by merging those fields into the row the query returned.
|
||||
update: async (args: {
|
||||
data: Partial<Pick<ItemRow, 'completed' | 'boughtAt' | 'quantity'>>
|
||||
}) => {
|
||||
updates.push(args)
|
||||
return itemRow(args.data.completed, args.data.boughtAt)
|
||||
return { ...options.item, ...args.data } as ItemRow
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -122,6 +126,50 @@ describe('UpdateItem', () => {
|
||||
expect(published).toEqual([{ type: 'item.updated', listId: 'list-1', item: result.value }])
|
||||
})
|
||||
|
||||
it('updates only the quantity, leaving the bought state untouched', async () => {
|
||||
const { db, updates } = createDb({ membership: { role: 'owner' }, item: itemRow(false, null) })
|
||||
const { events, published } = createBus()
|
||||
const result = await new UpdateItem(db, events).execute({
|
||||
listId: 'list-1',
|
||||
itemId: 'item-1',
|
||||
userId: 'user-1',
|
||||
quantity: ' 3 ',
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error('expected the use case to succeed')
|
||||
|
||||
expect(updates).toHaveLength(1)
|
||||
const data = updates[0] as {
|
||||
data: { completed?: boolean; boughtAt?: Date | null; quantity?: string | null }
|
||||
}
|
||||
expect(data.data).toEqual({ quantity: '3' })
|
||||
expect(data.data.completed).toBeUndefined()
|
||||
|
||||
expect(result.value.completed).toBe(false)
|
||||
expect(result.value.quantity).toBe('3')
|
||||
|
||||
expect(published).toEqual([{ type: 'item.updated', listId: 'list-1', item: result.value }])
|
||||
})
|
||||
|
||||
it('clears the quantity when an empty value is sent', async () => {
|
||||
const { db, updates } = createDb({ membership: { role: 'owner' }, item: itemRow(false, null) })
|
||||
const { events } = createBus()
|
||||
const result = await new UpdateItem(db, events).execute({
|
||||
listId: 'list-1',
|
||||
itemId: 'item-1',
|
||||
userId: 'user-1',
|
||||
quantity: '',
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error('expected the use case to succeed')
|
||||
|
||||
const data = updates[0] as { data: { quantity?: string | null } }
|
||||
expect(data.data).toEqual({ quantity: null })
|
||||
expect(result.value.quantity).toBeNull()
|
||||
})
|
||||
|
||||
it('marks the item back to pending, clears boughtAt and publishes item.updated', async () => {
|
||||
const { db, updates } = createDb({
|
||||
membership: { role: 'collaborator' },
|
||||
|
||||
@@ -12,9 +12,19 @@ import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { toListItem } from '../serialize'
|
||||
|
||||
type UpdateItemFields = {
|
||||
listId: string
|
||||
itemId: string
|
||||
userId: string
|
||||
/** New bought state; when omitted the state stays untouched. */
|
||||
completed?: boolean
|
||||
/** New quantity (`null` or an empty string clears it); when omitted it stays untouched. */
|
||||
quantity?: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks an item as bought (or back to pending). Only members can do it.
|
||||
* `boughtAt` records the moment it was marked as bought, for future
|
||||
* Updates an item's bought state and/or its quantity. Only members can do it.
|
||||
* `boughtAt` records the moment an item was marked as bought, for future
|
||||
* statistics, and is cleared when the item goes back to pending.
|
||||
*/
|
||||
export class UpdateItem {
|
||||
@@ -23,12 +33,7 @@ export class UpdateItem {
|
||||
private readonly events: ListEvents = listEvents,
|
||||
) {}
|
||||
|
||||
async execute(input: {
|
||||
listId: string
|
||||
itemId: string
|
||||
userId: string
|
||||
completed: boolean
|
||||
}): Promise<Result<ListItem, ProblemDetails>> {
|
||||
async execute(input: UpdateItemFields): Promise<Result<ListItem, ProblemDetails>> {
|
||||
const membership = await this.db.shoppingListMember.findUnique({
|
||||
where: { listId_userId: { listId: input.listId, userId: input.userId } },
|
||||
})
|
||||
@@ -45,12 +50,20 @@ export class UpdateItem {
|
||||
return Failure(problems.notFound('El artículo no existe en esta lista.'))
|
||||
}
|
||||
|
||||
const data: { completed?: boolean; boughtAt?: Date | null; quantity?: string | null } = {}
|
||||
|
||||
if (input.completed !== undefined) {
|
||||
data.completed = input.completed
|
||||
data.boughtAt = input.completed ? new Date() : null
|
||||
}
|
||||
|
||||
if (input.quantity !== undefined) {
|
||||
data.quantity = input.quantity?.trim() || null
|
||||
}
|
||||
|
||||
const updated = await this.db.listItem.update({
|
||||
where: { id: item.id },
|
||||
data: {
|
||||
completed: input.completed,
|
||||
boughtAt: input.completed ? new Date() : null,
|
||||
},
|
||||
data,
|
||||
})
|
||||
|
||||
const dto = toListItem(updated)
|
||||
|
||||
Reference in New Issue
Block a user