mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
fix(packing): anyone on the trip can be put down as the bringer of a shared item, and taken off again (#3098)
A common item only let you pledge yourself. Nobody else could be named, and a bringer could never be removed, so an item that went personal and back kept its owner's avatar for good. The item menu now lists every member to tick on or off, a click on the avatar opens it there, and the add route takes an optional user_id limited to the trip's members. Taking the owner off clears the owner. The remove route now checks visibility for the caller, not for the user named in the path.
This commit is contained in:
@@ -407,6 +407,12 @@ export class PackingItemsRepository extends TrekRepository<PackingItems> {
|
||||
await this.nativeUpdate({ id }, { is_private, ...data, updated_at: currentTimestamp(platform) });
|
||||
}
|
||||
|
||||
/** `removeContributor` on a Common item's owner (#3098): the item stays on the list, nobody's in particular. */
|
||||
async clearOwner(id: number): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.nativeUpdate({ id }, { owner: null, updated_at: currentTimestamp(platform) });
|
||||
}
|
||||
|
||||
/** PK16 (`setItemSharing`'s replace-all) — `DELETE FROM packing_item_recipients WHERE item_id = ?`. */
|
||||
async deleteRecipientsForItem(item_id: number): Promise<void> {
|
||||
await this.db().deleteFrom('packing_item_recipients').where('item_id', '=', item_id).execute();
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
PackingReorderDto,
|
||||
PackingSaveTemplateDto,
|
||||
PackingSetSharingDto,
|
||||
PackingAddContributorDto,
|
||||
PackingUpdateBagDto,
|
||||
PackingUpdateItemDto,
|
||||
} from './packing.dto';
|
||||
@@ -248,6 +249,7 @@ export class PackingController {
|
||||
@Param('tripId') tripId: string,
|
||||
@Param('id') id: string,
|
||||
@Headers('x-socket-id') socketId?: string,
|
||||
@Body() body?: PackingAddContributorDto,
|
||||
) {
|
||||
// Plan 4 Task 8b (U6) — :id parsed ONCE here (toRowId). #858's
|
||||
// contributors/sharing routes are native Nest code with no pre-ORM
|
||||
@@ -258,7 +260,8 @@ export class PackingController {
|
||||
if (itemId === null) {
|
||||
throw new HttpException({ error: 'Item not found or not a shared list item' }, 404);
|
||||
}
|
||||
const item = await this.packing.addContributor(tripId, itemId, user.id);
|
||||
// No body is the caller's own pledge; a user_id puts someone else down (#3098).
|
||||
const item = await this.packing.addContributor(tripId, itemId, body?.user_id ?? user.id, user.id);
|
||||
if (!item) {
|
||||
throw new HttpException({ error: 'Item not found or not a shared list item' }, 404);
|
||||
}
|
||||
@@ -286,7 +289,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
// You can drop your own pledge; the owner can remove anyone's.
|
||||
const item = await this.packing.removeContributor(tripId, itemId, target);
|
||||
const item = await this.packing.removeContributor(tripId, itemId, target, user.id);
|
||||
if (!item) {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
packingCreateItemRequestSchema,
|
||||
packingUpdateItemRequestSchema,
|
||||
packingSetSharingRequestSchema,
|
||||
packingAddContributorRequestSchema,
|
||||
packingImportRequestSchema,
|
||||
packingReorderRequestSchema,
|
||||
packingCreateBagRequestSchema,
|
||||
@@ -24,6 +25,7 @@ import { createZodDto } from 'nestjs-zod';
|
||||
export class PackingCreateItemDto extends createZodDto(packingCreateItemRequestSchema) {}
|
||||
export class PackingUpdateItemDto extends createZodDto(packingUpdateItemRequestSchema) {}
|
||||
export class PackingSetSharingDto extends createZodDto(packingSetSharingRequestSchema) {}
|
||||
export class PackingAddContributorDto extends createZodDto(packingAddContributorRequestSchema) {}
|
||||
export class PackingImportDto extends createZodDto(packingImportRequestSchema) {}
|
||||
export class PackingReorderDto extends createZodDto(packingReorderRequestSchema) {}
|
||||
export class PackingCreateBagDto extends createZodDto(packingCreateBagRequestSchema) {}
|
||||
|
||||
@@ -406,18 +406,23 @@ export class PackingService {
|
||||
* getItemPrivacy} (`PackingController.addContributor`, native Nest code
|
||||
* with no pre-ORM Express precedent — #858 landed post-migration).
|
||||
*/
|
||||
async addContributor(tripId: string | number, id: number, userId: number) {
|
||||
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, userId);
|
||||
async addContributor(tripId: string | number, id: number, userId: number, actingUserId: number = userId) {
|
||||
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, actingUserId);
|
||||
if (!item || item.is_private !== 0) return null; // co-contribution is a Common-list concept
|
||||
if (item.owner_id === userId) return null; // the bringer is already covering it
|
||||
// Putting someone else down as a bringer (#3098) only reaches people on the trip.
|
||||
if (userId !== actingUserId && !(await this.tripRosterIds(tripId)).has(userId)) return null;
|
||||
await this.contributorsRepo.insertIgnore(id, userId);
|
||||
return (await this.enrichItems([await this.itemsRepo.findById(id)]))[0];
|
||||
}
|
||||
|
||||
/** `id: number` — same Plan 4 Task 8b (U6) narrowing as {@link addContributor}; `userId` here is the target being removed, not the acting viewer (`PackingController.removeContributor` now parses both `:id`/`:userId` via `toRowId` instead of a bare `Number.parseInt`). */
|
||||
async removeContributor(tripId: string | number, id: number, userId: number) {
|
||||
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, userId);
|
||||
async removeContributor(tripId: string | number, id: number, userId: number, actingUserId: number = userId) {
|
||||
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, actingUserId);
|
||||
if (!item) return null;
|
||||
// On a Common item the owner is its first bringer, so taking them off the
|
||||
// item has to clear the owner too; otherwise their avatar stuck for good (#3098).
|
||||
if (item.is_private === 0 && item.owner_id === userId) await this.itemsRepo.clearOwner(id);
|
||||
await this.contributorsRepo.deleteOne(id, userId);
|
||||
return (await this.enrichItems([await this.itemsRepo.findById(id)]))[0];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user