mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
Add drag-to-reorder to the packing and to-do lists, mirroring the budget panel's native HTML5 drag pattern. A drag within a filtered/grouped view is mapped back onto the global order so untouched items keep their place, and the order persists optimistically via the existing reorder endpoints. Packing items can now be marked private (#858): a private item is visible only to its owner. createItem/bulkImport stamp the owner, listItems filters by the viewer, and the WebSocket broadcasts are scoped to the owner so a private item never reaches another member's screen — including the public/private toggle transitions. Owners get a lock toggle and a private indicator on their items.
This commit is contained in:
@@ -3132,6 +3132,21 @@ function runMigrations(db: Database.Database): void {
|
||||
if (!err.message?.includes('duplicate column name')) throw err;
|
||||
}
|
||||
},
|
||||
// Private packing items (#858): an item can be hidden from other trip members.
|
||||
// is_private toggles the visibility; owner_id records who it belongs to so the
|
||||
// listing can show it only to them. owner_id is NULL on legacy rows (shared).
|
||||
() => {
|
||||
for (const stmt of [
|
||||
'ALTER TABLE packing_items ADD COLUMN is_private INTEGER NOT NULL DEFAULT 0',
|
||||
'ALTER TABLE packing_items ADD COLUMN owner_id INTEGER REFERENCES users(id) ON DELETE SET NULL',
|
||||
]) {
|
||||
try {
|
||||
db.exec(stmt);
|
||||
} catch (err: any) {
|
||||
if (!err.message?.includes('duplicate column name')) throw err;
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
if (currentVersion < migrations.length) {
|
||||
|
||||
@@ -127,7 +127,8 @@ export function registerResources(server: McpServer, userId: number, scopes: str
|
||||
async (uri, { tripId }) => {
|
||||
const id = parseId(tripId);
|
||||
if (id === null || !canAccessTrip(id, userId)) return accessDenied(uri.href);
|
||||
const items = listPackingItems(id);
|
||||
// Hide other members' private items (#858) from the requesting user.
|
||||
const items = listPackingItems(id, userId);
|
||||
return jsonContent(uri.href, items);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -44,7 +44,7 @@ export function registerPackingTools(server: McpServer, userId: number, scopes:
|
||||
if (isDemoUser(userId)) return demoDenied();
|
||||
if (!canAccessTrip(tripId, userId)) return noAccess();
|
||||
if (!hasTripPermission('packing_edit', tripId, userId)) return permissionDenied();
|
||||
const item = createPackingItem(tripId, { name, category: category || 'General' });
|
||||
const item = createPackingItem(tripId, { name, category: category || 'General' }, userId);
|
||||
safeBroadcast(tripId, 'packing:created', { item });
|
||||
return ok({ item });
|
||||
}
|
||||
@@ -386,7 +386,7 @@ export function registerPackingTools(server: McpServer, userId: number, scopes:
|
||||
if (isDemoUser(userId)) return demoDenied();
|
||||
if (!canAccessTrip(tripId, userId)) return noAccess();
|
||||
if (!hasTripPermission('packing_edit', tripId, userId)) return permissionDenied();
|
||||
const created = bulkImport(tripId, items);
|
||||
const created = bulkImport(tripId, items, userId);
|
||||
for (const item of created) safeBroadcast(tripId, 'packing:created', { item });
|
||||
return ok({ items: created, count: created.length });
|
||||
}
|
||||
|
||||
@@ -77,7 +77,8 @@ ${days?.map((d: any, i: number) => `Day ${i + 1} (${d.date}): ${d.assignments?.l
|
||||
if (!canAccessTrip(tripId, userId)) {
|
||||
return { messages: [{ role: 'user', content: { type: 'text', text: 'Trip not found or access denied.' } }] };
|
||||
}
|
||||
const items = listPackingItems(tripId);
|
||||
// Hide other members' private items (#858) from the requesting user.
|
||||
const items = listPackingItems(tripId, userId);
|
||||
if (!items.length) {
|
||||
return { messages: [{ role: 'user', content: { type: 'text', text: 'No packing items found for this trip.' } }] };
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ import { isUpdateConflict } from '../../services/conflictResult';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
|
||||
/** A packing item row carrying the privacy fields (#858) used to scope broadcasts. */
|
||||
type PackingItemRow = { is_private?: number; owner_id?: number | null; [key: string]: unknown };
|
||||
|
||||
/**
|
||||
* /api/trips/:tripId/packing — trip-scoped packing list (items, bags, templates,
|
||||
* assignees).
|
||||
@@ -51,7 +54,8 @@ export class PackingController {
|
||||
@Get()
|
||||
list(@CurrentUser() user: User, @Param('tripId') tripId: string) {
|
||||
this.requireTrip(tripId, user);
|
||||
return { items: this.packing.listItems(tripId) };
|
||||
// Pass the viewer so private items (#858) owned by other members are hidden.
|
||||
return { items: this.packing.listItems(tripId, user.id) };
|
||||
}
|
||||
|
||||
@Post('import')
|
||||
@@ -66,9 +70,9 @@ export class PackingController {
|
||||
if (!Array.isArray(items) || items.length === 0) {
|
||||
throw new HttpException({ error: 'items must be a non-empty array' }, 400);
|
||||
}
|
||||
const created = this.packing.bulkImport(tripId, items);
|
||||
const created = this.packing.bulkImport(tripId, items, user.id);
|
||||
for (const item of created) {
|
||||
this.packing.broadcast(tripId, 'packing:created', { item }, socketId);
|
||||
this.packing.broadcastItem(tripId, 'packing:created', { item }, item, socketId);
|
||||
}
|
||||
return { items: created, count: created.length };
|
||||
}
|
||||
@@ -77,7 +81,7 @@ export class PackingController {
|
||||
create(
|
||||
@CurrentUser() user: User,
|
||||
@Param('tripId') tripId: string,
|
||||
@Body() body: { name?: string; category?: string; checked?: boolean },
|
||||
@Body() body: { name?: string; category?: string; checked?: boolean; is_private?: boolean },
|
||||
@Headers('x-socket-id') socketId?: string,
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
@@ -85,8 +89,8 @@ export class PackingController {
|
||||
if (!body.name) {
|
||||
throw new HttpException({ error: 'Item name is required' }, 400);
|
||||
}
|
||||
const item = this.packing.createItem(tripId, { name: body.name, category: body.category, checked: body.checked });
|
||||
this.packing.broadcast(tripId, 'packing:created', { item }, socketId);
|
||||
const item = this.packing.createItem(tripId, { name: body.name, category: body.category, checked: body.checked, is_private: body.is_private }, user.id);
|
||||
this.packing.broadcastItem(tripId, 'packing:created', { item }, item, socketId);
|
||||
return { item };
|
||||
}
|
||||
|
||||
@@ -114,8 +118,11 @@ export class PackingController {
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
this.requireEdit(trip, user);
|
||||
const { name, checked, category, weight_grams, bag_id, quantity } = body as Record<string, never>;
|
||||
const updated = this.packing.updateItem(tripId, id, { name, checked, category, weight_grams, bag_id, quantity }, Object.keys(body), ifMatch);
|
||||
// Privacy state before the change, so a public↔private toggle (#858) can route
|
||||
// the broadcast correctly instead of leaking a freshly-privatized item.
|
||||
const before = this.packing.getItemPrivacy(tripId, id);
|
||||
const { name, checked, category, weight_grams, bag_id, quantity, is_private } = body as Record<string, never>;
|
||||
const updated = this.packing.updateItem(tripId, id, { name, checked, category, weight_grams, bag_id, quantity, is_private }, Object.keys(body), ifMatch, user.id);
|
||||
if (!updated) {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
@@ -123,10 +130,41 @@ export class PackingController {
|
||||
if (isUpdateConflict(updated)) {
|
||||
throw new HttpException({ error: 'conflict', server: updated.server }, 409);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:updated', { item: updated }, socketId);
|
||||
this.broadcastUpdate(tripId, id, updated as PackingItemRow, !!before?.is_private, socketId);
|
||||
return { item: updated };
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes a packing-item update over WebSocket so private items (#858) stay
|
||||
* scoped to their owner across the four public↔private transitions:
|
||||
* - stays private → owner-only update
|
||||
* - public→private → drop it from the whole room, re-add for the owner
|
||||
* - private→public → create for members who lacked it, then update for all
|
||||
* - stays public → plain update to all
|
||||
*/
|
||||
private broadcastUpdate(
|
||||
tripId: string,
|
||||
id: string,
|
||||
item: PackingItemRow,
|
||||
wasPrivate: boolean,
|
||||
socketId: string | undefined,
|
||||
): void {
|
||||
const nowPrivate = !!item.is_private;
|
||||
if (nowPrivate) {
|
||||
if (wasPrivate) {
|
||||
this.packing.broadcastItem(tripId, 'packing:updated', { item }, item, socketId);
|
||||
} else {
|
||||
this.packing.broadcast(tripId, 'packing:deleted', { itemId: Number(id) }, socketId);
|
||||
this.packing.broadcastItem(tripId, 'packing:created', { item }, item, socketId);
|
||||
}
|
||||
} else {
|
||||
if (wasPrivate) {
|
||||
this.packing.broadcast(tripId, 'packing:created', { item }, socketId);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:updated', { item }, socketId);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(
|
||||
@CurrentUser() user: User,
|
||||
@@ -136,10 +174,12 @@ export class PackingController {
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
this.requireEdit(trip, user);
|
||||
if (!this.packing.deleteItem(tripId, id)) {
|
||||
const deleted = this.packing.deleteItem(tripId, id);
|
||||
if (!deleted) {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:deleted', { itemId: Number(id) }, socketId);
|
||||
// Scope the delete to the owner when the item was private (#858).
|
||||
this.packing.broadcastItem(tripId, 'packing:deleted', { itemId: Number(id) }, deleted, socketId);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ import { checkPermission } from '../../services/permissions';
|
||||
import type { User } from '../../types';
|
||||
import * as svc from '../../services/packingService';
|
||||
|
||||
/** Privacy fields stamped on a packing item (#858). */
|
||||
type PrivacyFields = { is_private?: number; owner_id?: number | null };
|
||||
|
||||
type Trip = NonNullable<ReturnType<typeof svc.verifyTripAccess>>;
|
||||
|
||||
/**
|
||||
@@ -27,24 +30,40 @@ export class PackingService {
|
||||
broadcast(tripId, event, payload, socketId);
|
||||
}
|
||||
|
||||
listItems(tripId: string) {
|
||||
return svc.listItems(tripId);
|
||||
/**
|
||||
* Broadcast an item event, but keep private items (#858) off other members'
|
||||
* screens: when the item is private the event is delivered only to its owner's
|
||||
* sockets. Shared items broadcast to the whole trip room as before.
|
||||
*/
|
||||
broadcastItem(tripId: string, event: string, payload: Record<string, unknown>, item: PrivacyFields | null | undefined, socketId: string | undefined): void {
|
||||
const onlyUserId = item?.is_private && item.owner_id != null ? item.owner_id : undefined;
|
||||
broadcast(tripId, event, payload, socketId, onlyUserId);
|
||||
}
|
||||
|
||||
createItem(tripId: string, data: { name: string; category?: string; checked?: boolean }) {
|
||||
return svc.createItem(tripId, data);
|
||||
listItems(tripId: string, userId?: number) {
|
||||
return svc.listItems(tripId, userId);
|
||||
}
|
||||
|
||||
updateItem(tripId: string, id: string, data: Parameters<typeof svc.updateItem>[2], changedKeys: string[], ifMatch?: string) {
|
||||
return svc.updateItem(tripId, id, data, changedKeys, ifMatch);
|
||||
/** Reads an item's current privacy fields (#858) before an update, so the
|
||||
* controller can detect a public↔private transition and route the broadcast. */
|
||||
getItemPrivacy(tripId: string, id: string): PrivacyFields | undefined {
|
||||
return db.prepare('SELECT is_private, owner_id FROM packing_items WHERE id = ? AND trip_id = ?').get(id, tripId) as PrivacyFields | undefined;
|
||||
}
|
||||
|
||||
deleteItem(tripId: string, id: string): boolean {
|
||||
createItem(tripId: string, data: { name: string; category?: string; checked?: boolean; is_private?: boolean }, ownerId?: number) {
|
||||
return svc.createItem(tripId, data, ownerId);
|
||||
}
|
||||
|
||||
updateItem(tripId: string, id: string, data: Parameters<typeof svc.updateItem>[2], changedKeys: string[], ifMatch?: string, actingUserId?: number) {
|
||||
return svc.updateItem(tripId, id, data, changedKeys, ifMatch, actingUserId);
|
||||
}
|
||||
|
||||
deleteItem(tripId: string, id: string) {
|
||||
return svc.deleteItem(tripId, id);
|
||||
}
|
||||
|
||||
bulkImport(tripId: string, items: Parameters<typeof svc.bulkImport>[1]) {
|
||||
return svc.bulkImport(tripId, items);
|
||||
bulkImport(tripId: string, items: Parameters<typeof svc.bulkImport>[1], ownerId?: number) {
|
||||
return svc.bulkImport(tripId, items, ownerId);
|
||||
}
|
||||
|
||||
reorderItems(tripId: string, orderedIds: Parameters<typeof svc.reorderItems>[1]): void {
|
||||
|
||||
@@ -8,20 +8,28 @@ export { verifyTripAccess } from './tripAccess';
|
||||
|
||||
// ── Items ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export function listItems(tripId: string | number) {
|
||||
export function listItems(tripId: string | number, userId?: number) {
|
||||
// Private items (#858) are visible only to their owner; shared items
|
||||
// (is_private = 0) are visible to everyone. Without a userId (internal callers
|
||||
// such as trip export) the unfiltered list is returned for back-compat.
|
||||
if (userId == null) {
|
||||
return db.prepare(
|
||||
'SELECT * FROM packing_items WHERE trip_id = ? ORDER BY sort_order ASC, created_at ASC'
|
||||
).all(tripId);
|
||||
}
|
||||
return db.prepare(
|
||||
'SELECT * FROM packing_items WHERE trip_id = ? ORDER BY sort_order ASC, created_at ASC'
|
||||
).all(tripId);
|
||||
'SELECT * FROM packing_items WHERE trip_id = ? AND (is_private = 0 OR owner_id = ?) ORDER BY sort_order ASC, created_at ASC'
|
||||
).all(tripId, userId);
|
||||
}
|
||||
|
||||
export function createItem(tripId: string | number, data: { name: string; category?: string; checked?: boolean; quantity?: number }) {
|
||||
export function createItem(tripId: string | number, data: { name: string; category?: string; checked?: boolean; quantity?: number; is_private?: boolean }, ownerId?: number) {
|
||||
const maxOrder = db.prepare('SELECT MAX(sort_order) as max FROM packing_items WHERE trip_id = ?').get(tripId) as { max: number | null };
|
||||
const sortOrder = (maxOrder.max !== null ? maxOrder.max : -1) + 1;
|
||||
const qty = Math.max(1, Math.min(999, Number(data.quantity) || 1));
|
||||
|
||||
const result = db.prepare(
|
||||
'INSERT INTO packing_items (trip_id, name, checked, category, sort_order, quantity, updated_at) VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)'
|
||||
).run(tripId, data.name, data.checked ? 1 : 0, data.category || 'Allgemein', sortOrder, qty);
|
||||
'INSERT INTO packing_items (trip_id, name, checked, category, sort_order, quantity, is_private, owner_id, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)'
|
||||
).run(tripId, data.name, data.checked ? 1 : 0, data.category || 'Allgemein', sortOrder, qty, data.is_private ? 1 : 0, ownerId ?? null);
|
||||
|
||||
return db.prepare('SELECT * FROM packing_items WHERE id = ?').get(result.lastInsertRowid);
|
||||
}
|
||||
@@ -29,11 +37,12 @@ export function createItem(tripId: string | number, data: { name: string; catego
|
||||
export function updateItem(
|
||||
tripId: string | number,
|
||||
id: string | number,
|
||||
data: { name?: string; checked?: number; category?: string; weight_grams?: number | null; bag_id?: number | null; quantity?: number },
|
||||
data: { name?: string; checked?: number; category?: string; weight_grams?: number | null; bag_id?: number | null; quantity?: number; is_private?: boolean },
|
||||
bodyKeys: string[],
|
||||
ifMatch?: string,
|
||||
actingUserId?: number,
|
||||
): unknown | UpdateConflict | null {
|
||||
const item = db.prepare('SELECT * FROM packing_items WHERE id = ? AND trip_id = ?').get(id, tripId) as { updated_at?: string | null } | undefined;
|
||||
const item = db.prepare('SELECT * FROM packing_items WHERE id = ? AND trip_id = ?').get(id, tripId) as { updated_at?: string | null; owner_id?: number | null } | undefined;
|
||||
if (!item) return null;
|
||||
|
||||
// Optimistic concurrency (#1135): reject a stale offline overwrite. Absent
|
||||
@@ -42,6 +51,10 @@ export function updateItem(
|
||||
return { conflict: true, server: db.prepare('SELECT * FROM packing_items WHERE id = ?').get(id) };
|
||||
}
|
||||
|
||||
// Privatizing an unowned (legacy) item stamps the acting user as its owner so
|
||||
// the visibility filter still has someone to match (#858).
|
||||
const claimOwner = bodyKeys.includes('is_private') && !!data.is_private && item.owner_id == null && actingUserId != null;
|
||||
|
||||
db.prepare(`
|
||||
UPDATE packing_items SET
|
||||
name = COALESCE(?, name),
|
||||
@@ -50,6 +63,8 @@ export function updateItem(
|
||||
weight_grams = CASE WHEN ? THEN ? ELSE weight_grams END,
|
||||
bag_id = CASE WHEN ? THEN ? ELSE bag_id END,
|
||||
quantity = CASE WHEN ? THEN ? ELSE quantity END,
|
||||
is_private = CASE WHEN ? THEN ? ELSE is_private END,
|
||||
owner_id = CASE WHEN ? THEN ? ELSE owner_id END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
@@ -63,6 +78,10 @@ export function updateItem(
|
||||
data.bag_id ?? null,
|
||||
bodyKeys.includes('quantity') ? 1 : 0,
|
||||
data.quantity ? Math.max(1, Math.min(999, Number(data.quantity))) : 1,
|
||||
bodyKeys.includes('is_private') ? 1 : 0,
|
||||
data.is_private ? 1 : 0,
|
||||
claimOwner ? 1 : 0,
|
||||
actingUserId ?? null,
|
||||
id
|
||||
);
|
||||
|
||||
@@ -70,11 +89,13 @@ export function updateItem(
|
||||
}
|
||||
|
||||
export function deleteItem(tripId: string | number, id: string | number) {
|
||||
const item = db.prepare('SELECT id FROM packing_items WHERE id = ? AND trip_id = ?').get(id, tripId);
|
||||
if (!item) return false;
|
||||
// Return the deleted row (not just a boolean) so callers can target the
|
||||
// delete broadcast at the owner when the item was private (#858).
|
||||
const item = db.prepare('SELECT * FROM packing_items WHERE id = ? AND trip_id = ?').get(id, tripId) as { is_private?: number; owner_id?: number | null } | undefined;
|
||||
if (!item) return null;
|
||||
|
||||
db.prepare('DELETE FROM packing_items WHERE id = ?').run(id);
|
||||
return true;
|
||||
return item;
|
||||
}
|
||||
|
||||
// ── Bulk Import ────────────────────────────────────────────────────────────
|
||||
@@ -86,13 +107,14 @@ interface ImportItem {
|
||||
weight_grams?: string | number;
|
||||
bag?: string;
|
||||
quantity?: number;
|
||||
is_private?: boolean;
|
||||
}
|
||||
|
||||
export function bulkImport(tripId: string | number, items: ImportItem[]) {
|
||||
export function bulkImport(tripId: string | number, items: ImportItem[], ownerId?: number) {
|
||||
const maxOrder = db.prepare('SELECT MAX(sort_order) as max FROM packing_items WHERE trip_id = ?').get(tripId) as { max: number | null };
|
||||
let sortOrder = (maxOrder.max !== null ? maxOrder.max : -1) + 1;
|
||||
|
||||
const stmt = db.prepare('INSERT INTO packing_items (trip_id, name, checked, category, weight_grams, bag_id, sort_order, quantity, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)');
|
||||
const stmt = db.prepare('INSERT INTO packing_items (trip_id, name, checked, category, weight_grams, bag_id, sort_order, quantity, is_private, owner_id, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)');
|
||||
const created: any[] = [];
|
||||
|
||||
const insertAll = db.transaction(() => {
|
||||
@@ -116,7 +138,7 @@ export function bulkImport(tripId: string | number, items: ImportItem[]) {
|
||||
}
|
||||
|
||||
const qty = Math.max(1, Math.min(999, Number(item.quantity) || 1));
|
||||
const result = stmt.run(tripId, item.name.trim(), checked, item.category?.trim() || 'Other', weight, bagId, sortOrder++, qty);
|
||||
const result = stmt.run(tripId, item.name.trim(), checked, item.category?.trim() || 'Other', weight, bagId, sortOrder++, qty, item.is_private ? 1 : 0, ownerId ?? null);
|
||||
created.push(db.prepare('SELECT * FROM packing_items WHERE id = ?').get(result.lastInsertRowid));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -186,8 +186,11 @@ function leaveRoom(ws: NomadWebSocket, tripId: number): void {
|
||||
|
||||
/**
|
||||
* Broadcast an event to all sockets in a trip room, optionally excluding a socket.
|
||||
* When `onlyUserId` is given the event is delivered only to that user's sockets in
|
||||
* the room — used to keep private packing items (#858) off other members' screens
|
||||
* while still syncing the owner's own tabs.
|
||||
*/
|
||||
function broadcast(tripId: number | string, eventType: string, payload: Record<string, unknown>, excludeSid?: number | string): void {
|
||||
function broadcast(tripId: number | string, eventType: string, payload: Record<string, unknown>, excludeSid?: number | string, onlyUserId?: number): void {
|
||||
tripId = Number(tripId);
|
||||
const room = rooms.get(tripId);
|
||||
if (!room || room.size === 0) return;
|
||||
@@ -198,6 +201,7 @@ function broadcast(tripId: number | string, eventType: string, payload: Record<s
|
||||
if (ws.readyState !== 1) continue; // WebSocket.OPEN === 1
|
||||
// Exclude the specific socket that triggered the change
|
||||
if (excludeNum && socketId.get(ws) === excludeNum) continue;
|
||||
if (onlyUserId != null && socketUser.get(ws)?.id !== onlyUserId) continue;
|
||||
ws.send(JSON.stringify({ type: eventType, tripId, ...payload }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user