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.

13.8 KB
Raw Blame
import type { StoreApi } from 'zustand'
import type { TripStoreState } from '../tripStore'
import type { Assignment, AssignmentsMap } from '../../types'
import { getApiErrorMessage } from '../../types'
import { assignmentRepo, type AssignmentTimes } from '../../repo/assignmentRepo'
import { mergeAssignmentPlace } from './placesSlice'
import { nextTempId } from '../../sync/mutationQueue'

type SetState = StoreApi<TripStoreState>['setState']
type GetState = StoreApi<TripStoreState>['getState']

export interface AssignmentsSlice {
  setAssignmentEndDay: (tripId: number | string, dayId: number, assignmentId: number, endDay: boolean) => Promise<void>
  /** The stay of one visit (#3092); null reads the place's duration_minutes again. */
  setAssignmentStay: (tripId: number | string, dayId: number, assignmentId: number, stayMinutes: number | null) => Promise<void>
  setAssignmentRouteExcluded: (tripId: number | string, dayId: number, assignmentId: number, excluded: boolean) => Promise<void>
  setAssignmentTimes: (tripId: number | string, dayId: number, assignmentId: number, times: AssignmentTimes) => Promise<void>
  setAssignmentNotes: (tripId: number | string, dayId: number, assignmentId: number, notes: string | null) => Promise<void>
  setAssignmentParticipants: (tripId: number | string, dayId: number, assignmentId: number, userIds: number[]) => Promise<void>
  setAssignmentLegMode: (tripId: number | string, dayId: number, assignmentId: number, mode: string | null, direction?: 'outgoing' | 'incoming') => Promise<void>
  /** `tourStageKey` puts one stage of a multi-day tour on the day instead of the whole tour (MDT-02). */
  assignPlaceToDay: (tripId: number | string, dayId: number | string, placeId: number | string, position?: number | null, tourStageKey?: string) => Promise<Assignment | undefined>
  removeAssignment: (tripId: number | string, dayId: number | string, assignmentId: number) => Promise<void>
  clearDayAssignments: (tripId: number | string, dayId: number) => Promise<void>
  reorderAssignments: (tripId: number | string, dayId: number | string, orderedIds: number[]) => Promise<void>
  moveAssignment: (tripId: number | string, assignmentId: number, fromDayId: number | string, toDayId: number | string, toOrderIndex?: number | null) => Promise<void>
  setAssignments: (assignments: AssignmentsMap) => void
}

/**
 * One field of a saved visit on its day, written optimistically: shown at once, settled
 * on what the save answers, and put back when the save is refused. `read` is how the
 * field is read off a row, so an absent value settles the same way every time.
 */
async function writeVisitField<K extends keyof Assignment>(
  set: SetState,
  get: GetState,
  { dayId, assignmentId, field, value, read, save }: {
    dayId: number
    assignmentId: number
    field: K
    value: Assignment[K]
    read: (a: Assignment) => Assignment[K]
    save: (assignment: Assignment) => Promise<Assignment>
  },
): Promise<void> {
  const key = String(dayId)
  const assignment = get().assignments[key]?.find(a => a.id === assignmentId)
  if (!assignment || assignmentId < 0) return
  const apply = (next: Assignment[K]) => set(state => ({
    assignments: { ...state.assignments, [key]: (state.assignments[key] || []).map(a => a.id === assignmentId ? { ...a, [field]: next } : a) },
  }))
  apply(value)
  try {
    apply(read(await save(assignment)))
  } catch (err: unknown) {
    apply(read(assignment))
    throw err
  }
}

export const createAssignmentsSlice = (set: SetState, get: GetState): AssignmentsSlice => ({
  setAssignmentEndDay: async (tripId, dayId, assignmentId, endDay) => {
    const assignment = get().assignments[String(dayId)]?.find(a => a.id === assignmentId)
    if (!assignment || assignmentId < 0) return
    const apply = (value: boolean) => set(state => ({
      assignments: Object.fromEntries(Object.entries(state.assignments).map(([key, items]) => [
        key, items.map(a => a.id === assignmentId ? { ...a, end_day: value } : a),
      ])),
    }))
    apply(endDay)
    try {
      const saved = await assignmentRepo.setEndDay(tripId, assignment, endDay)
      apply(saved.end_day === true)
    } catch (err: unknown) {
      apply(assignment.end_day === true)
      throw err
    }
  },
  setAssignmentStay: (tripId, dayId, assignmentId, stayMinutes) => writeVisitField(set, get, {
    dayId, assignmentId, field: 'stay_minutes', value: stayMinutes, read: a => a.stay_minutes ?? null,
    save: assignment => assignmentRepo.setStay(tripId, assignment, stayMinutes),
  }),
  setAssignmentRouteExcluded: (tripId, dayId, assignmentId, excluded) => writeVisitField(set, get, {
    dayId, assignmentId, field: 'route_excluded', value: excluded, read: a => a.route_excluded === true,
    save: assignment => assignmentRepo.setRouteExcluded(tripId, assignment, excluded),
  }),
  setAssignmentTimes: async (tripId, dayId, assignmentId, times) => {
    const assignment = get().assignments[String(dayId)]?.find(a => a.id === assignmentId)
    if (!assignment || assignmentId < 0) return
    const replace = (next: Assignment) => set(state => ({
      assignments: Object.fromEntries(Object.entries(state.assignments).map(([key, items]) => [
        key, items.map(a => a.id === assignmentId ? next : a),
      ])),
    }))
    // The embedded place carries the times the visit shows, its own or else the place's,
    // so it is merged again from the pool rather than patched field by field. The merged
    // row is also what an offline save caches, or the old end time would come back from
    // the cache on the next read.
    const pool = get().places.find(p => p.id === assignment.place_id)
    const merge = (next: Assignment) => (pool ? mergeAssignmentPlace(next, pool) : next)
    const draft = merge({ ...assignment, assignment_time: times.place_time, assignment_end_time: times.end_time })
    replace(draft)
    try {
      replace(merge({ ...draft, ...await assignmentRepo.setTimes(tripId, draft, times) }))
    } catch (err: unknown) {
      replace(assignment)
      throw err
    }
  },
  setAssignmentNotes: async (tripId, dayId, assignmentId, notes) => {
    const assignment = get().assignments[String(dayId)]?.find(a => a.id === assignmentId)
    if (!assignment || assignmentId < 0) return
    // Only the note is patched: the saved row carries the place as the server has it,
    // and the visit's times may still wait in the queue ahead of this write.
    const apply = (value: string | null) => set(state => ({
      assignments: Object.fromEntries(Object.entries(state.assignments).map(([key, items]) => [
        key, items.map(a => a.id === assignmentId ? { ...a, notes: value } : a),
      ])),
    }))
    apply(notes)
    try {
      const saved = await assignmentRepo.setNotes(tripId, assignment, notes)
      apply(saved.notes ?? null)
    } catch (err: unknown) {
      apply(assignment.notes ?? null)
      throw err
    }
  },
  // Who joins the visit and how its legs are travelled; offline both wait in the
  // queue. The participants the repo answers with are written into the day.
  setAssignmentParticipants: async (tripId, dayId, assignmentId, userIds) => {
    const participants = await assignmentRepo.setParticipants(tripId, assignmentId, userIds)
    set(state => ({
      assignments: { ...state.assignments, [String(dayId)]: (state.assignments[String(dayId)] || []).map(a => a.id === assignmentId ? { ...a, participants } : a) },
    }))
  },
  setAssignmentLegMode: async (tripId, dayId, assignmentId, mode, direction = 'outgoing') => {
    await assignmentRepo.setLegMode(tripId, assignmentId, mode, direction)
    const field = direction === 'incoming' ? 'incoming_leg_transport_mode' : 'leg_transport_mode'
    set(state => ({
      assignments: { ...state.assignments, [String(dayId)]: (state.assignments[String(dayId)] || []).map(a => a.id === assignmentId ? { ...a, [field]: mode } : a) },
    }))
  },
  assignPlaceToDay: async (tripId, dayId, placeId, position, tourStageKey) => {
    const state = get()
    const place = state.places.find(p => p.id === Number.parseInt(String(placeId)))
    if (!place) return

    const tempId = nextTempId()
    // Sorted first, as moveAssignment does: the position is an index in the day as
    // it is shown, and that day is sorted by order_index. The stored list is not
    // always, a stop that arrived over the socket is appended wherever its
    // order_index says it belongs, so a splice into the raw list landed the new
    // row behind the wrong neighbour and the reorder that follows wrote that order.
    const current = (state.assignments[String(dayId)] || []).slice().sort((a, b) => a.order_index - b.order_index)
    const insertIdx = position != null ? position : current.length
    const tempAssignment: Assignment = {
      id: tempId,
      day_id: Number.parseInt(String(dayId)),
      place_id: place.id,
      order_index: insertIdx,
      notes: null,
      tour_stage_key: tourStageKey ?? null,
      place,
    }

    current.splice(insertIdx, 0, tempAssignment)
    set(state => ({
      assignments: {
        ...state.assignments,
        [String(dayId)]: current,
      }
    }))

    try {
      const created = await assignmentRepo.create(tripId, Number(dayId), place, tourStageKey)
      const newAssignment: Assignment = {
        ...created,
        place: created.place || place,
        order_index: position != null ? insertIdx : created.order_index,
      }
      set(state => ({
        assignments: {
          ...state.assignments,
          [String(dayId)]: state.assignments[String(dayId)].map(
            a => a.id === tempId ? newAssignment : a
          ),
        }
      }))
      if (position != null) {
        const updated = get().assignments[String(dayId)] || []
        const orderedIds = updated.map(a => a.id)
        if (orderedIds.length > 0) {
          try {
            await assignmentRepo.reorder(tripId, Number(dayId), orderedIds)
            set(state => {
              const items = state.assignments[String(dayId)] || []
              const reordered = orderedIds.map((id, idx) => {
                const item = items.find(a => a.id === id)
                return item ? { ...item, order_index: idx } : null
              }).filter((item): item is Assignment => item !== null)
              return {
                assignments: {
                  ...state.assignments,
                  [String(dayId)]: reordered,
                }
              }
            })
          } catch {}
        }
      }
      return created
    } catch (err: unknown) {
      set(state => ({
        assignments: {
          ...state.assignments,
          [String(dayId)]: state.assignments[String(dayId)].filter(a => a.id !== tempId),
        }
      }))
      throw new Error(getApiErrorMessage(err, 'Error assigning place'))
    }
  },

  removeAssignment: async (tripId, dayId, assignmentId) => {
    const prevAssignments = get().assignments

    set(state => ({
      assignments: {
        ...state.assignments,
        [String(dayId)]: state.assignments[String(dayId)].filter(a => a.id !== assignmentId),
      }
    }))

    try {
      await assignmentRepo.remove(tripId, Number(dayId), assignmentId)
    } catch (err: unknown) {
      set({ assignments: prevAssignments })
      throw new Error(getApiErrorMessage(err, 'Error removing assignment'))
    }
  },

  clearDayAssignments: async (tripId, dayId) => {
    const prevAssignments = get().assignments
    set(state => ({ assignments: { ...state.assignments, [String(dayId)]: [] } }))
    try {
      await assignmentRepo.clearDay(tripId, dayId)
    } catch (err: unknown) {
      set({ assignments: prevAssignments })
      throw new Error(getApiErrorMessage(err, 'Error removing assignment'))
    }
  },

  reorderAssignments: async (tripId, dayId, orderedIds) => {
    const prevAssignments = get().assignments
    const dayItems = get().assignments[String(dayId)] || []
    const reordered = orderedIds.map((id, idx) => {
      const item = dayItems.find(a => a.id === id)
      return item ? { ...item, order_index: idx } : null
    }).filter((item): item is Assignment => item !== null)

    set(state => ({
      assignments: {
        ...state.assignments,
        [String(dayId)]: reordered,
      }
    }))

    try {
      await assignmentRepo.reorder(tripId, Number(dayId), orderedIds)
    } catch (err: unknown) {
      set({ assignments: prevAssignments })
      throw new Error(getApiErrorMessage(err, 'Error reordering'))
    }
  },

  moveAssignment: async (tripId, assignmentId, fromDayId, toDayId, toOrderIndex = null) => {
    const state = get()
    const prevAssignments = state.assignments
    const assignment = (state.assignments[String(fromDayId)] || []).find(a => a.id === assignmentId)
    if (!assignment) return

    const toItems = (state.assignments[String(toDayId)] || []).slice().sort((a, b) => a.order_index - b.order_index)
    const insertAt = toOrderIndex !== null ? toOrderIndex : toItems.length

    const withMoved = [...toItems]
    withMoved.splice(insertAt, 0, { ...assignment, day_id: Number.parseInt(String(toDayId)) })
    // Renumber into copies. Assigning `order_index` in place would mutate the very objects
    // still held in `prevAssignments`, so the rollback below would restore a list whose
    // indices had already been overwritten — a failed move left the order wrong instead of
    // putting it back.
    const newToItems = withMoved.map((a, i) => ({ ...a, order_index: i }))

    set(s => ({
      assignments: {
        ...s.assignments,
        [String(fromDayId)]: s.assignments[String(fromDayId)].filter(a => a.id !== assignmentId),
        [String(toDayId)]: newToItems,
      }
    }))

    try {
      await assignmentRepo.move(tripId, assignment, Number(toDayId), insertAt, newToItems.map(a => a.id))
    } catch (err: unknown) {
      set({ assignments: prevAssignments })
      throw new Error(getApiErrorMessage(err, 'Error moving assignment'))
    }
  },

  setAssignments: (assignments) => {
    set({ assignments })
  },
})