File size: 1,775 Bytes
57a889c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Injectable } from '@nestjs/common';
import { broadcast } from '../../websocket';
import { checkPermission } from '../../services/permissions';
import type { User } from '../../types';
import * as dayNoteService from '../../services/dayNoteService';

type Trip = NonNullable<ReturnType<typeof dayNoteService.verifyTripAccess>>;

/**
 * Thin Nest wrapper around the existing day-note service. Trip access + the
 * 'day_edit' permission reuse the legacy checks; the SQL is unchanged.
 */
@Injectable()
export class DayNotesService {
  verifyTripAccess(tripId: string, userId: number) {
    return dayNoteService.verifyTripAccess(tripId, userId);
  }

  canEdit(trip: Trip, user: User): boolean {
    return checkPermission('day_edit', user.role, trip.user_id, user.id, trip.user_id !== user.id);
  }

  broadcast(tripId: string, event: string, payload: Record<string, unknown>, socketId: string | undefined): void {
    broadcast(tripId, event, payload, socketId);
  }

  list(dayId: string, tripId: string) {
    return dayNoteService.listNotes(dayId, tripId);
  }

  dayExists(dayId: string, tripId: string) {
    return dayNoteService.dayExists(dayId, tripId);
  }

  getNote(id: string, dayId: string, tripId: string) {
    return dayNoteService.getNote(id, dayId, tripId);
  }

  create(dayId: string, tripId: string, text: string, time?: string, icon?: string, sortOrder?: number) {
    return dayNoteService.createNote(dayId, tripId, text, time, icon, sortOrder);
  }

  update(id: string, current: Parameters<typeof dayNoteService.updateNote>[1], fields: { text?: string; time?: string; icon?: string; sort_order?: number }) {
    return dayNoteService.updateNote(id, current, fields);
  }

  remove(id: string): void {
    dayNoteService.deleteNote(id);
  }
}