Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

11.1 KB
Raw Blame
import type { User } from '../../types';
import { isUpdateConflict } from '../common/conflictResult';
import { DomainError } from '../common/domain-error';
import { requireTripWrite, type TripWriter } from '../common/trip-writer';
import { NotificationsService } from '../notifications/notifications.service';
import { PermissionsService } from '../permissions/permissions.service';
import { PackingService, isInvalidBagRef, type PackingVisibility } from './packing.service';
import { Injectable } from '@nestjs/common';
import type { TrekWsPayload } from '@trek/shared';

/** The item fields a create takes, as PackingService.createItem reads them. */
export type PackingItemCreateInput = Parameters<PackingService['createItem']>[1];
/** The item fields an update takes, as PackingService.updateItem reads them. */
export type PackingItemUpdateInput = Parameters<PackingService['updateItem']>[2];

/** A packing item with the privacy fields that decide who an event reaches (#858). */
type PrivacyItem = { is_private?: number; owner_id?: number | null; recipients?: { user_id: number }[] };

const itemNotFound = () => new DomainError(404, 'Item not found', { mcpMessage: 'Packing item not found.' });
// The payload named a bag that is not on this trip (#2154): 400, as on create.
const bagNotFound = () => new DomainError(400, 'Bag not found', { mcpMessage: 'Bag not found.' });
// The path named a bag that is not on this trip: 404, as every bag route answers it.
const bagMissing = () => new DomainError(404, 'Bag not found', { mcpMessage: 'Bag not found.' });
const ownerOnly = () =>
  new DomainError(403, 'Only the owner can change sharing', { mcpMessage: 'Only the owner can change sharing.' });
const templateMissing = () =>
  new DomainError(404, 'Template not found or empty', { mcpMessage: 'Template not found.' });

/** Writes that can move a bag's weight, the only ones worth a bag-totals ping (#2191). */
const WEIGHT_KEYS = ['weight_grams', 'quantity', 'bag_id'];

/**
 * The packing item writes, one method per use case, for every surface.
 *
 * Each holds the trip gate, the reference and conflict checks, the write and
 * every event it sends; PackingController and PackingMcp are adapters over it.
 * Before this each surface ran its own copy of the gate and the broadcasts, and
 * they had drifted: the MCP update pinged bag totals on every write to a common
 * item, which the REST route had stopped doing on purpose.
 *
 * One difference stays, on purpose, because changing it would change who sees
 * what: an update to a restricted item reaches its owner only on REST
 * (PackingService.broadcastUpdate), and its owner plus recipients on MCP. Which
 * of the two is right is a product decision; until it is made, each surface
 * keeps the routing it had.
 *
 * The list writes below the item ones (sharing, order, bag delete, category
 * assignees, templates) came here where the two surfaces had drifted apart:
 * the REST reorder told nobody, the MCP sharing change pinged bag totals it
 * cannot move, the MCP bag delete reported success for a bag that was not
 * there, the MCP assignee change told none of the tagged travellers, and a
 * template applied over MCP always landed on the common list.
 */
@Injectable()
export class PackingWritesService {
  constructor(
    private readonly packing: PackingService,
    private readonly permissions: PermissionsService,
    private readonly notifications: NotificationsService,
  ) {}

  /**
   * The REST caller of a packing write. Its events go through PackingService's own
   * broadcast and broadcastToViewers, skipping the sender's socket, exactly the
   * calls emitToViewers made for the routes.
   */
  restWriter(tripId: string, user: User, socketId: string | undefined): TripWriter {
    return {
      userId: user.id,
      role: user.role,
      socketId,
      surface: 'rest',
      events: {
        emit: (event, payload, onlyUserIds) =>
          onlyUserIds == null
            ? this.packing.broadcast(tripId, event, payload, socketId)
            : this.packing.broadcastToViewers(tripId, event, payload, [...onlyUserIds], socketId),
        emitAll: (event, payload) => this.packing.broadcast(tripId, event, payload, undefined),
      },
    };
  }

  private gate(tripId: string | number, writer: TripWriter) {
    return requireTripWrite(
      {
        access: { findAccessible: (id, userId) => this.packing.verifyTripAccess(id, userId) },
        permissions: this.permissions,
      },
      'packing_edit',
      tripId,
      writer,
    );
  }

  async createItem(tripId: string | number, input: PackingItemCreateInput, writer: TripWriter) {
    await this.gate(tripId, writer);
    const item = await this.packing.createItem(tripId, input, writer.userId);
    if (isInvalidBagRef(item)) throw bagNotFound();
    // A restricted item reaches its owner and recipients only; a common one the room.
    writer.events.emit('packing:created', { item } as TrekWsPayload<'packing:created'>, this.packing.viewersOf(item));
    this.packing.broadcastBagTotals(String(tripId));
    return item;
  }

  /**
   * Change an item. `bodyKeys` names the fields the caller sent (an explicit null
   * clears one); `ifMatch` is REST's X-Base-Updated-At for a stale offline write.
   */
  async updateItem(
    tripId: string | number,
    itemId: number,
    input: PackingItemUpdateInput,
    bodyKeys: string[],
    writer: TripWriter,
    ifMatch?: string,
  ) {
    await this.gate(tripId, writer);
    // Read before the write: getting it wrong leaks a freshly privatized item.
    const wasPrivate = !!(await this.packing.getItemPrivacy(tripId, itemId))?.is_private;
    const updated = await this.packing.updateItem(tripId, itemId, input, bodyKeys, ifMatch, writer.userId);
    if (!updated) throw itemNotFound();
    // A stale offline overwrite: the client resolves it against the server row (#1135).
    if (isUpdateConflict(updated)) throw new DomainError(409, 'conflict', { details: { server: updated.server } });
    if (isInvalidBagRef(updated)) throw bagNotFound();
    const item = updated as PrivacyItem;
    if (writer.surface === 'rest')
      this.packing.broadcastUpdate(String(tripId), itemId, item, wasPrivate, writer.socketId);
    else this.announceUpdateToViewers(itemId, item, wasPrivate, writer);
    // Checking an item off is the most frequent packing write there is, and every
    // ping costs every connected client a listBags round trip.
    if (WEIGHT_KEYS.some((k) => bodyKeys.includes(k))) this.packing.broadcastBagTotals(String(tripId));
    return updated;
  }

  /**
   * The four privacy transitions of an update (#858) as the MCP tools always sent
   * them: a restricted item goes to its owner and recipients.
   */
  private announceUpdateToViewers(itemId: number, item: PrivacyItem, wasPrivate: boolean, writer: TripWriter) {
    const viewers = this.packing.viewersOf(item);
    const payload = { item } as TrekWsPayload<'packing:updated'>;
    if (item.is_private) {
      // Newly restricted: off the room's screens first, then back to who may see it.
      if (!wasPrivate) writer.events.emit('packing:deleted', { itemId });
      writer.events.emit(wasPrivate ? 'packing:updated' : 'packing:created', payload, viewers);
      return;
    }
    // Newly common: the members who never had the row need it created, not updated.
    if (wasPrivate) writer.events.emit('packing:created', payload);
    writer.events.emit('packing:updated', payload);
  }

  async deleteItem(tripId: string | number, itemId: number, writer: TripWriter) {
    await this.gate(tripId, writer);
    const deleted = await this.packing.deleteItem(tripId, itemId, writer.userId);
    if (!deleted) throw itemNotFound();
    // Scoped to the people who could see it (owner + recipients, #858, #1976).
    writer.events.emit('packing:deleted', { itemId }, this.packing.viewersOf(deleted));
    this.packing.broadcastBagTotals(String(tripId));
    return deleted;
  }

  /**
   * Move an item between the sharing tiers (#858). Only its owner may; an item
   * nobody owns yet is claimed by the writer. Visibility moves no weight, so
   * there is no bag-totals ping.
   */
  async setSharing(
    tripId: string | number,
    itemId: number,
    visibility: PackingVisibility,
    recipientIds: number[],
    writer: TripWriter,
  ) {
    await this.gate(tripId, writer);
    const item = await this.packing.setItemSharing(tripId, itemId, writer.userId, visibility, recipientIds);
    if (!item) throw itemNotFound();
    if ((item as { forbidden?: boolean }).forbidden) throw ownerOnly();
    // The viewer set just changed: off the whole room's screens first, then back
    // to whoever may see it now (owner and recipients, or the room for Common).
    writer.events.emit('packing:deleted', { itemId });
    writer.events.emit('packing:created', { item } as TrekWsPayload<'packing:created'>, this.packing.viewersOf(item));
    return item;
  }

  /** The list's new order, for every other tab on both surfaces. */
  async reorder(tripId: string | number, orderedIds: number[], writer: TripWriter) {
    await this.gate(tripId, writer);
    await this.packing.reorderItems(tripId, orderedIds);
    writer.events.emit('packing:reordered', { orderedIds });
  }

  /** Delete a bag; a bag that is not on the trip is refused and announced to nobody. */
  async deleteBag(tripId: string | number, bagId: number, writer: TripWriter) {
    await this.gate(tripId, writer);
    if (!(await this.packing.deleteBag(tripId, bagId))) throw bagMissing();
    writer.events.emit('packing:bag-deleted', { bagId });
    // bag_id is ON DELETE SET NULL, so everything that was in it just landed in
    // the unassigned pile: both figures moved (#2191).
    this.packing.broadcastBagTotals(String(tripId));
  }

  /**
   * Who looks after a packing category. The tagged travellers are notified on
   * every surface, the way the route always notified them.
   */
  async setCategoryAssignees(tripId: string | number, category: string, userIds: number[], writer: TripWriter) {
    await this.gate(tripId, writer);
    const assignees = await this.packing.updateCategoryAssignees(tripId, category, userIds);
    writer.events.emit('packing:assignees', { category, assignees });
    if (userIds.length > 0) {
      // The notification names its actor by name. A writer carries only the id,
      // so the name is read here; an account that is gone notifies nobody.
      const username = await this.notifications.actorName(writer.userId);
      if (username) await this.packing.notifyTagged(String(tripId), { id: writer.userId, username }, category, userIds);
    }
    return assignees;
  }

  /**
   * Add a template's items to the list, on the common list or as the writer's
   * own. Personal items reach their owner alone, common ones the room (#858).
   */
  async applyTemplate(
    tripId: string | number,
    templateId: number,
    visibility: 'common' | 'personal',
    writer: TripWriter,
  ) {
    await this.gate(tripId, writer);
    const items = await this.packing.applyTemplate(tripId, templateId, visibility, writer.userId);
    if (!items) throw templateMissing();
    writer.events.emit('packing:template-applied', { items }, this.packing.viewersOf(items[0]));
    this.packing.broadcastBagTotals(String(tripId));
    return items;
  }
}