chore: move day backend to proper DI

This commit is contained in:
jubnl
2026-07-27 20:57:01 +02:00
parent e62628bf93
commit 2d1eec76c3
35 changed files with 1491 additions and 1123 deletions
@@ -3,19 +3,22 @@ import { broadcast } from '../../websocket';
import { DatabaseService } from '../database/database.service';
import { checkPermission } from '../../services/permissions';
import type { User } from '../../types';
import * as dayService from '../../services/dayService';
import { DaysService } from '../days/days.service';
type Trip = { user_id: number };
/**
* Thin Nest wrapper around the accommodation parts of the existing day service.
* Thin Nest wrapper around the accommodation parts of the day domain.
* Accommodations are gated by the 'day_edit' permission (same as days) and the
* SQL + cascade (linked reservation / budget cleanup on delete) reuse the legacy
* code unchanged.
* SQL + cascade (linked reservation / budget cleanup on delete) live on the
* injected DaysService (the DI-native home of the legacy dayService).
*/
@Injectable()
export class AccommodationsService {
constructor(private readonly dbs: DatabaseService) {}
constructor(
private readonly dbs: DatabaseService,
private readonly days: DaysService,
) {}
/** Mirrors the requireTripAccess middleware (owner or member), returning the trip. */
verifyTripAccess(tripId: string, userId: number) {
@@ -31,26 +34,26 @@ export class AccommodationsService {
}
list(tripId: string) {
return dayService.listAccommodations(tripId);
return this.days.listAccommodations(tripId);
}
validateRefs(tripId: string, placeId?: number, startDayId?: number, endDayId?: number) {
return dayService.validateAccommodationRefs(tripId, placeId, startDayId, endDayId);
return this.days.validateAccommodationRefs(tripId, placeId, startDayId, endDayId);
}
get(id: string, tripId: string) {
return dayService.getAccommodation(id, tripId);
return this.days.getAccommodation(id, tripId);
}
create(tripId: string, data: Parameters<typeof dayService.createAccommodation>[1]) {
return dayService.createAccommodation(tripId, data);
create(tripId: string, data: Parameters<DaysService['createAccommodation']>[1]) {
return this.days.createAccommodation(tripId, data);
}
update(id: string, existing: Parameters<typeof dayService.updateAccommodation>[1], fields: Parameters<typeof dayService.updateAccommodation>[2]) {
return dayService.updateAccommodation(id, existing, fields);
update(id: string, existing: Parameters<DaysService['updateAccommodation']>[1], fields: Parameters<DaysService['updateAccommodation']>[2]) {
return this.days.updateAccommodation(id, existing, fields);
}
remove(id: string) {
return dayService.deleteAccommodation(id);
return this.days.deleteAccommodation(id);
}
}
@@ -6,10 +6,10 @@ import {
import { z } from 'zod';
import { isDemoUser } from '../../services/authService';
import { linkBudgetItemToReservation } from '../../services/budgetService';
import { getDay } from '../../services/dayService';
import { placeExists, getAssignmentForTrip } from '../assignments/assignments.bridge';
import { safeBroadcast, noAccess, hasTripPermission, permissionDenied } from '../../mcp/tools/_shared';
import { ReservationsService } from './reservations.service';
import { DaysService } from '../days/days.service';
function parseId(value: string | string[]): number | null {
const n = Number(Array.isArray(value) ? value[0] : value);
@@ -28,7 +28,10 @@ function parseId(value: string | string[]): number | null {
*/
@McpController()
export class ReservationsMcp {
constructor(private readonly reservations: ReservationsService) {}
constructor(
private readonly reservations: ReservationsService,
private readonly days: DaysService,
) {}
@Tool({
name: 'create_reservation',
@@ -68,13 +71,13 @@ export class ReservationsMcp {
if (!hasTripPermission('reservation_edit', tripId, ctx.userId)) return permissionDenied();
// Validate that all referenced IDs belong to this trip
if (day_id && !getDay(day_id, tripId))
if (day_id && !this.days.getDay(day_id, tripId))
return errorResult('day_id does not belong to this trip.');
if (place_id && !placeExists(place_id, tripId))
return errorResult('place_id does not belong to this trip.');
if (start_day_id && !getDay(start_day_id, tripId))
if (start_day_id && !this.days.getDay(start_day_id, tripId))
return errorResult('start_day_id does not belong to this trip.');
if (end_day_id && !getDay(end_day_id, tripId))
if (end_day_id && !this.days.getDay(end_day_id, tripId))
return errorResult('end_day_id does not belong to this trip.');
if (assignment_id && !getAssignmentForTrip(assignment_id, tripId))
return errorResult('assignment_id does not belong to this trip.');
@@ -237,9 +240,9 @@ export class ReservationsMcp {
if (!placeExists(place_id, tripId))
return errorResult('place_id does not belong to this trip.');
if (!getDay(start_day_id, tripId))
if (!this.days.getDay(start_day_id, tripId))
return errorResult('start_day_id does not belong to this trip.');
if (!getDay(end_day_id, tripId))
if (!this.days.getDay(end_day_id, tripId))
return errorResult('end_day_id does not belong to this trip.');
const isNewAccommodation = !current.accommodation_id;
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { DaysModule } from '../days/days.module';
import { ReservationsController } from './reservations.controller';
import { ReservationsService } from './reservations.service';
import { ReservationsMcp } from './reservations.mcp';
@@ -13,6 +14,8 @@ import { UpcomingReservationsController } from './upcoming-reservations.controll
* decorator-registered MCP tools + resource for the domain.
*/
@Module({
// DaysModule: AccommodationsService + ReservationsMcp inject DaysService.
imports: [DaysModule],
controllers: [ReservationsController, AccommodationsController, UpcomingReservationsController],
providers: [ReservationsService, AccommodationsService, ReservationsMcp],
// For in-container consumers (PluginHostDepsFactory, TripsService, BookingImportService).