mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
Give accommodations one home instead of two halves
The routes lived in reservations/ while the SQL lived on DaysService, so the controller delegated every call across a domain boundary to reach its own data, and neither module owned the thing outright. The seven statements move into AccommodationsService unchanged and the domain gets its own directory, module, DTO, RPC and MCP surface. What stays in days/ is the reorder side: assertNoInvertedAccommodation and resyncAccommodationDays are about re-dating day rows, and the stay is the thing being carried, not the thing doing the carrying. Two edges fall out of this. AccommodationsService drops its DaysService dependency, and DaysMcp drops DatabaseService and PlacesService, because create_place_accommodation was their only user - which lets DaysModule stop importing PlacesModule and closes one leg of the latent Days -> Places -> Assignments -> Days cycle. DAY-SVC-016..026, 029 and 035 move to accommodations.service.test.ts with their case ids intact. The reservations e2e container assembles both modules now: it builds a single domain module on purpose, which is exactly what catches a move like this, and it caught it.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
import { AccommodationsController } from '../../../src/nest/reservations/accommodations.controller';
|
||||
import type { AccommodationsService } from '../../../src/nest/reservations/accommodations.service';
|
||||
import { AccommodationsController } from '../../../src/nest/accommodations/accommodations.controller';
|
||||
import type { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
|
||||
import type { User } from '../../../src/types';
|
||||
|
||||
const user = { id: 1, role: 'user', email: 'u@example.test' } as User;
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
/**
|
||||
* Unit tests for AccommodationsService. DAY-SVC-016..026, 029 and 035 moved here
|
||||
* 1:1 with the SQL they cover when accommodations became their own domain; the
|
||||
* case ids are unchanged so the diff reads as a move. Real in-memory SQLite.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
|
||||
const { testDb, dbMock } = vi.hoisted(() => {
|
||||
const Database = require('better-sqlite3');
|
||||
const db = new Database(':memory:');
|
||||
db.exec('PRAGMA journal_mode = WAL');
|
||||
db.exec('PRAGMA foreign_keys = ON');
|
||||
db.exec('PRAGMA busy_timeout = 5000');
|
||||
return {
|
||||
testDb: db,
|
||||
dbMock: {
|
||||
db,
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
getPlaceWithTags: () => null,
|
||||
canAccessTrip: (tripId: any, userId: number) =>
|
||||
db.prepare(`
|
||||
SELECT t.id, t.user_id FROM trips t
|
||||
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ?
|
||||
WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)
|
||||
`).get(userId, tripId, userId),
|
||||
isOwner: (tripId: any, userId: number) =>
|
||||
!!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('../../../src/db/database', () => dbMock);
|
||||
const { broadcast } = vi.hoisted(() => ({ broadcast: vi.fn() }));
|
||||
vi.mock('../../../src/websocket', () => ({ broadcast }));
|
||||
|
||||
import { createTables } from '../../../src/db/schema';
|
||||
import { runMigrations } from '../../../src/db/migrations';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createUser, createTrip, createDay, createPlace, createDayAccommodation } from '../../helpers/factories';
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import { PermissionsService } from '../../../src/nest/permissions/permissions.service';
|
||||
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
||||
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
|
||||
import { AccommodationsModule } from '../../../src/nest/accommodations/accommodations.module';
|
||||
import { AccommodationsController } from '../../../src/nest/accommodations/accommodations.controller';
|
||||
import { expectRegisteredProvider, expectRegisteredController } from '../../helpers/module-providers';
|
||||
|
||||
// Named `svc` so the moved cases read exactly as they did on DaysService.
|
||||
const svc = new AccommodationsService(
|
||||
new DatabaseService(testDb),
|
||||
new PermissionsService(new DatabaseService(testDb)),
|
||||
new RealtimeService(),
|
||||
);
|
||||
|
||||
beforeAll(() => {
|
||||
createTables(testDb);
|
||||
runMigrations(testDb);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
describe('validateAccommodationRefs', () => {
|
||||
it('DAY-SVC-016 — returns no errors when all refs are valid', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, place.id, day.id, day.id);
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('DAY-SVC-017 — returns error when place does not exist in trip', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, 99999, day.id, day.id);
|
||||
expect(errors.some((e: any) => e.field === 'place_id')).toBe(true);
|
||||
});
|
||||
|
||||
it('DAY-SVC-018 — returns error when start_day_id is invalid', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, place.id, 99999, day.id);
|
||||
expect(errors.some((e: any) => e.field === 'start_day_id')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createAccommodation', () => {
|
||||
it('DAY-SVC-019 — creates accommodation and returns it with place info', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' }) as any;
|
||||
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id,
|
||||
start_day_id: day.id,
|
||||
end_day_id: day.id,
|
||||
check_in: '15:00',
|
||||
check_out: '11:00',
|
||||
}) as any;
|
||||
|
||||
expect(accom).toBeDefined();
|
||||
expect(accom.place_name).toBe('Grand Hotel');
|
||||
});
|
||||
|
||||
it('DAY-SVC-020 — auto-creates a linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'City Hotel' }) as any;
|
||||
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const reservation = testDb.prepare('SELECT * FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
expect(reservation).toBeDefined();
|
||||
expect(reservation.type).toBe('hotel');
|
||||
expect(reservation.status).toBe('confirmed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAccommodation', () => {
|
||||
it('DAY-SVC-021 — returns accommodation for valid id and trip', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
|
||||
const found = svc.getAccommodation(accom.id, trip.id) as any;
|
||||
expect(found).toBeDefined();
|
||||
expect(found.id).toBe(accom.id);
|
||||
});
|
||||
|
||||
it('DAY-SVC-022 — returns undefined for non-existent accommodation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
expect(svc.getAccommodation(99999, trip.id)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateAccommodation', () => {
|
||||
it('DAY-SVC-023 — updates check-in and check-out times', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const existing = svc.getAccommodation(accom.id, trip.id)!;
|
||||
const updated = svc.updateAccommodation(accom.id, existing as any, { check_in: '16:00', check_out: '12:00' }) as any;
|
||||
expect(updated).toBeDefined();
|
||||
|
||||
// Verify linked reservation metadata was synced
|
||||
const reservation = testDb.prepare('SELECT * FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
expect(reservation).toBeDefined();
|
||||
const meta = JSON.parse(reservation.metadata || '{}');
|
||||
expect(meta.check_in_time).toBe('16:00');
|
||||
expect(meta.check_out_time).toBe('12:00');
|
||||
});
|
||||
|
||||
it('DAY-SVC-024 — preserves existing fields when not updated', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
confirmation: 'ABC123',
|
||||
}) as any;
|
||||
|
||||
const existing = svc.getAccommodation(accom.id, trip.id)!;
|
||||
svc.updateAccommodation(accom.id, existing as any, { check_in: '14:00' });
|
||||
|
||||
const row = svc.getAccommodation(accom.id, trip.id) as any;
|
||||
expect(row.confirmation).toBe('ABC123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteAccommodation', () => {
|
||||
it('DAY-SVC-025 — deletes accommodation and its linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const reservation = testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
|
||||
const result = svc.deleteAccommodation(accom.id);
|
||||
expect(result.linkedReservationId).toBe(reservation.id);
|
||||
|
||||
// Accommodation is gone
|
||||
expect(svc.getAccommodation(accom.id, trip.id)).toBeUndefined();
|
||||
|
||||
// Reservation is gone
|
||||
const deletedRes = testDb.prepare('SELECT id FROM reservations WHERE id = ?').get(reservation.id);
|
||||
expect(deletedRes).toBeUndefined();
|
||||
});
|
||||
|
||||
it('DAY-SVC-026 — returns null linkedReservationId when no reservation was linked', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
|
||||
|
||||
// Remove the auto-created reservation so there's no linked one
|
||||
testDb.prepare('DELETE FROM reservations WHERE accommodation_id = ?').run(accom.id);
|
||||
|
||||
const result = svc.deleteAccommodation(accom.id);
|
||||
expect(result.linkedReservationId).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('listAccommodations', () => {
|
||||
it('DAY-SVC-029 — listAccommodations returns the hydrated stays', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Ryokan' });
|
||||
createDayAccommodation(testDb, trip.id, place.id, day.id, day.id);
|
||||
const rows = svc.listAccommodations(trip.id) as { place_name: string }[];
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].place_name).toBe('Ryokan');
|
||||
});
|
||||
});
|
||||
|
||||
describe('quirk fixes', () => {
|
||||
it('DAY-SVC-035 — deleteAccommodation is atomic: a failed stay delete keeps the linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' });
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as { id: number };
|
||||
testDb.exec("CREATE TRIGGER boom BEFORE DELETE ON day_accommodations BEGIN SELECT RAISE(ABORT, 'boom'); END");
|
||||
try {
|
||||
expect(() => svc.deleteAccommodation(accom.id)).toThrow();
|
||||
// The earlier reservation delete inside the transaction rolled back.
|
||||
expect(testDb.prepare('SELECT COUNT(*) as n FROM reservations WHERE accommodation_id = ?').get(accom.id)).toMatchObject({ n: 1 });
|
||||
} finally {
|
||||
testDb.exec('DROP TRIGGER boom');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('AccommodationsService wiring', () => {
|
||||
it('ACC-001: the module carries the controller, the service and the RPC surface', () => {
|
||||
expectRegisteredController(AccommodationsModule, AccommodationsController);
|
||||
expectRegisteredProvider(AccommodationsModule, AccommodationsService);
|
||||
const exports = Reflect.getMetadata('exports', AccommodationsModule) as unknown[];
|
||||
expect(Array.isArray(exports)).toBe(true);
|
||||
expect(exports).toEqual(expect.arrayContaining([AccommodationsService]));
|
||||
});
|
||||
|
||||
it('ACC-002: it does not import the days or reservations modules', () => {
|
||||
const imports = (Reflect.getMetadata('imports', AccommodationsModule) as { name?: string }[]) ?? [];
|
||||
expect(Array.isArray(imports)).toBe(true);
|
||||
const names = imports.map((m) => m?.name);
|
||||
expect(names).not.toContain('DaysModule');
|
||||
expect(names).not.toContain('ReservationsModule');
|
||||
});
|
||||
});
|
||||
@@ -99,6 +99,7 @@ const tripsSvc = new TripsService(
|
||||
undefined as never, // places — not exercised here
|
||||
undefined as never, // unsplash — not exercised here
|
||||
new UserCleanupService(dbs()),
|
||||
undefined as never, // accommodations — not exercised here
|
||||
);
|
||||
const createGuest = tripsSvc.createGuest.bind(tripsSvc);
|
||||
const deleteGuest = tripsSvc.deleteGuest.bind(tripsSvc);
|
||||
|
||||
@@ -231,175 +231,14 @@ describe('remove', () => {
|
||||
|
||||
// ── validateAccommodationRefs ─────────────────────────────────────────────────
|
||||
|
||||
describe('validateAccommodationRefs', () => {
|
||||
it('DAY-SVC-016 — returns no errors when all refs are valid', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, place.id, day.id, day.id);
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('DAY-SVC-017 — returns error when place does not exist in trip', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, 99999, day.id, day.id);
|
||||
expect(errors.some((e: any) => e.field === 'place_id')).toBe(true);
|
||||
});
|
||||
|
||||
it('DAY-SVC-018 — returns error when start_day_id is invalid', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const errors = svc.validateAccommodationRefs(trip.id, place.id, 99999, day.id);
|
||||
expect(errors.some((e: any) => e.field === 'start_day_id')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ── createAccommodation ───────────────────────────────────────────────────────
|
||||
|
||||
describe('createAccommodation', () => {
|
||||
it('DAY-SVC-019 — creates accommodation and returns it with place info', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' }) as any;
|
||||
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id,
|
||||
start_day_id: day.id,
|
||||
end_day_id: day.id,
|
||||
check_in: '15:00',
|
||||
check_out: '11:00',
|
||||
}) as any;
|
||||
|
||||
expect(accom).toBeDefined();
|
||||
expect(accom.place_name).toBe('Grand Hotel');
|
||||
});
|
||||
|
||||
it('DAY-SVC-020 — auto-creates a linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'City Hotel' }) as any;
|
||||
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const reservation = testDb.prepare('SELECT * FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
expect(reservation).toBeDefined();
|
||||
expect(reservation.type).toBe('hotel');
|
||||
expect(reservation.status).toBe('confirmed');
|
||||
});
|
||||
});
|
||||
|
||||
// ── getAccommodation ──────────────────────────────────────────────────────────
|
||||
|
||||
describe('getAccommodation', () => {
|
||||
it('DAY-SVC-021 — returns accommodation for valid id and trip', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
|
||||
const found = svc.getAccommodation(accom.id, trip.id) as any;
|
||||
expect(found).toBeDefined();
|
||||
expect(found.id).toBe(accom.id);
|
||||
});
|
||||
|
||||
it('DAY-SVC-022 — returns undefined for non-existent accommodation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
expect(svc.getAccommodation(99999, trip.id)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── updateAccommodation ───────────────────────────────────────────────────────
|
||||
|
||||
describe('updateAccommodation', () => {
|
||||
it('DAY-SVC-023 — updates check-in and check-out times', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const existing = svc.getAccommodation(accom.id, trip.id)!;
|
||||
const updated = svc.updateAccommodation(accom.id, existing as any, { check_in: '16:00', check_out: '12:00' }) as any;
|
||||
expect(updated).toBeDefined();
|
||||
|
||||
// Verify linked reservation metadata was synced
|
||||
const reservation = testDb.prepare('SELECT * FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
expect(reservation).toBeDefined();
|
||||
const meta = JSON.parse(reservation.metadata || '{}');
|
||||
expect(meta.check_in_time).toBe('16:00');
|
||||
expect(meta.check_out_time).toBe('12:00');
|
||||
});
|
||||
|
||||
it('DAY-SVC-024 — preserves existing fields when not updated', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
confirmation: 'ABC123',
|
||||
}) as any;
|
||||
|
||||
const existing = svc.getAccommodation(accom.id, trip.id)!;
|
||||
svc.updateAccommodation(accom.id, existing as any, { check_in: '14:00' });
|
||||
|
||||
const row = svc.getAccommodation(accom.id, trip.id) as any;
|
||||
expect(row.confirmation).toBe('ABC123');
|
||||
});
|
||||
});
|
||||
|
||||
// ── deleteAccommodation ───────────────────────────────────────────────────────
|
||||
|
||||
describe('deleteAccommodation', () => {
|
||||
it('DAY-SVC-025 — deletes accommodation and its linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as any;
|
||||
|
||||
const reservation = testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
|
||||
const result = svc.deleteAccommodation(accom.id);
|
||||
expect(result.linkedReservationId).toBe(reservation.id);
|
||||
|
||||
// Accommodation is gone
|
||||
expect(svc.getAccommodation(accom.id, trip.id)).toBeUndefined();
|
||||
|
||||
// Reservation is gone
|
||||
const deletedRes = testDb.prepare('SELECT id FROM reservations WHERE id = ?').get(reservation.id);
|
||||
expect(deletedRes).toBeUndefined();
|
||||
});
|
||||
|
||||
it('DAY-SVC-026 — returns null linkedReservationId when no reservation was linked', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id) as any;
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
|
||||
|
||||
// Remove the auto-created reservation so there's no linked one
|
||||
testDb.prepare('DELETE FROM reservations WHERE accommodation_id = ?').run(accom.id);
|
||||
|
||||
const result = svc.deleteAccommodation(accom.id);
|
||||
expect(result.linkedReservationId).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── days.bridge delegation (out-of-container consumers) ───────────────────────
|
||||
// The listAccommodations / restampReservationDates / resyncAccommodationDays /
|
||||
// addDays bridge exports were pruned when their last outside-container
|
||||
@@ -424,17 +263,6 @@ describe('days.bridge', () => {
|
||||
expect(Array.isArray(result.days[0].assignments)).toBe(true);
|
||||
});
|
||||
|
||||
it('DAY-SVC-029 — listAccommodations returns the hydrated stays', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Ryokan' });
|
||||
createDayAccommodation(testDb, trip.id, place.id, day.id, day.id);
|
||||
const rows = svc.listAccommodations(trip.id) as { place_name: string }[];
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].place_name).toBe('Ryokan');
|
||||
});
|
||||
|
||||
it('DAY-SVC-030 — addDays stays UTC-only across month and year rollovers', () => {
|
||||
expect(addDays('2026-02-27', 3)).toBe('2026-03-02');
|
||||
expect(addDays('2026-12-31', 1)).toBe('2027-01-01');
|
||||
@@ -516,24 +344,6 @@ describe('quirk fixes', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('DAY-SVC-035 — deleteAccommodation is atomic: a failed stay delete keeps the linked reservation', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' });
|
||||
const accom = svc.createAccommodation(trip.id, {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
|
||||
}) as { id: number };
|
||||
testDb.exec("CREATE TRIGGER boom BEFORE DELETE ON day_accommodations BEGIN SELECT RAISE(ABORT, 'boom'); END");
|
||||
try {
|
||||
expect(() => svc.deleteAccommodation(accom.id)).toThrow();
|
||||
// The earlier reservation delete inside the transaction rolled back.
|
||||
expect(testDb.prepare('SELECT COUNT(*) as n FROM reservations WHERE accommodation_id = ?').get(accom.id)).toMatchObject({ n: 1 });
|
||||
} finally {
|
||||
testDb.exec('DROP TRIGGER boom');
|
||||
}
|
||||
});
|
||||
|
||||
it('DAY-SVC-036 — getAssignmentsForDay returns full tag rows from the batched load', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
|
||||
@@ -65,6 +65,7 @@ import { VacayService } from '../../../src/nest/vacay/vacay.service';
|
||||
import { TripsService } from '../../../src/nest/trips/trips.service';
|
||||
import { PlacesService } from '../../../src/nest/places/places.service';
|
||||
import { UserCleanupService } from '../../../src/nest/auth/user-cleanup.service';
|
||||
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
|
||||
import { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import { getTripOwner, listMembers as bridgeListMembers } from '../../../src/nest/trips/trips.bridge';
|
||||
import { QueryHelpersService } from '../../../src/nest/query-helpers/query-helpers.service';
|
||||
@@ -76,7 +77,8 @@ const dbs = () => new DatabaseService(testDb);
|
||||
const budgetSvc = new BudgetService(dbs(), new PermissionsService(dbs()), new ExchangeRatesService(), new RealtimeService());
|
||||
const daysSvc = new DaysService(dbs(), new PermissionsService(dbs()), new RealtimeService(), new QueryHelpersService(dbs()));
|
||||
const placesSvc = new PlacesService(dbs(), new PermissionsService(dbs()), new RealtimeService(), new MapsService(dbs()), new QueryHelpersService(dbs()));
|
||||
const createAccommodation = daysSvc.createAccommodation.bind(daysSvc);
|
||||
const accommodationsSvc = new AccommodationsService(dbs(), new PermissionsService(dbs()), new RealtimeService());
|
||||
const createAccommodation = accommodationsSvc.createAccommodation.bind(accommodationsSvc);
|
||||
|
||||
const svc = new TripsService(
|
||||
dbs(),
|
||||
@@ -93,6 +95,7 @@ const svc = new TripsService(
|
||||
placesSvc,
|
||||
undefined as never, // unsplash — not exercised here
|
||||
new UserCleanupService(dbs()),
|
||||
accommodationsSvc,
|
||||
);
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -912,6 +915,7 @@ describe('quirk fixes', () => {
|
||||
placesSvc,
|
||||
undefined as never, // unsplash — not exercised here
|
||||
new UserCleanupService(dbs()),
|
||||
new AccommodationsService(fdbs, new PermissionsService(dbs()), new RealtimeService()),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import { PlacesRpc } from '../../../src/nest/places/places.rpc';
|
||||
import { PackingRpc } from '../../../src/nest/packing/packing.rpc';
|
||||
import { FilesRpc } from '../../../src/nest/files/files.rpc';
|
||||
import { TripsRpc } from '../../../src/nest/trips/trips.rpc';
|
||||
import { AccommodationsRpc } from '../../../src/nest/days/accommodations.rpc';
|
||||
import { AccommodationsRpc } from '../../../src/nest/accommodations/accommodations.rpc';
|
||||
import { ReservationsRpc } from '../../../src/nest/reservations/reservations.rpc';
|
||||
import { DaysRpc } from '../../../src/nest/days/days.rpc';
|
||||
import { VacayRpc } from '../../../src/nest/vacay/vacay.rpc';
|
||||
@@ -124,13 +124,13 @@ describe('a service that says no becomes a refusal, not a crash', () => {
|
||||
|
||||
it('FALLBACK-008 an accommodation whose refs do not validate is refused with the validator message', async () => {
|
||||
const { guards } = guardsFor();
|
||||
const days = {
|
||||
const accommodations = {
|
||||
validateAccommodationRefs: vi.fn(() => [{ message: 'day 9 is not on this trip' }]),
|
||||
getAccommodation: vi.fn(() => ({ id: 11 })),
|
||||
createAccommodation: vi.fn(),
|
||||
updateAccommodation: vi.fn(),
|
||||
} as never;
|
||||
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new AccommodationsRpc(days, realtime(), guards)]));
|
||||
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new AccommodationsRpc(accommodations, realtime(), guards)]));
|
||||
expect(err(await host.dispatch(req('accommodations.update', { tripId: 1, accommodationId: 11, input: { start_day_id: 9 } }), 42)).message)
|
||||
.toBe('day 9 is not on this trip');
|
||||
});
|
||||
@@ -145,7 +145,7 @@ describe('service errors are translated into the RPC taxonomy', () => {
|
||||
create: vi.fn(() => { if (createThrows) throw createThrows; return { trip: { id: 99 } }; }),
|
||||
removeMember: vi.fn(),
|
||||
} as never;
|
||||
const rpc = new TripsRpc(trips, {} as never, {} as never, {} as never, db, realtime(), guards);
|
||||
const rpc = new TripsRpc(trips, {} as never, {} as never, {} as never, db, realtime(), guards, {} as never);
|
||||
return new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([rpc]));
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ describe('every schema-validated method rejects a payload its schema refuses', (
|
||||
new PlacesRpc(anything, anything, realtime(), guards),
|
||||
new DaysRpc(anything, realtime(), guards),
|
||||
new PackingRpc(anything, realtime(), guards),
|
||||
new TripsRpc(anything, anything, anything, anything, db, realtime(), guards),
|
||||
new TripsRpc(anything, anything, anything, anything, db, realtime(), guards, anything),
|
||||
new AccommodationsRpc(anything, realtime(), guards),
|
||||
new ReservationsRpc(anything, realtime(), guards),
|
||||
]);
|
||||
|
||||
@@ -32,7 +32,7 @@ const tripsRegistry = () => {
|
||||
} as unknown as DatabaseService;
|
||||
const guards = new PluginGuards(db, {} as never, {} as never);
|
||||
return createTestPluginRegistry([
|
||||
new TripsRpc({} as never, {} as never, {} as never, {} as never, db, {} as never, guards),
|
||||
new TripsRpc({} as never, {} as never, {} as never, {} as never, db, {} as never, guards, {} as never),
|
||||
]);
|
||||
};
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ import { createTestPluginRegistry } from '../../../src/nest/plugins/host/rpc-kit
|
||||
import { PluginGuards } from '../../../src/nest/plugins/host/plugin-guards.service';
|
||||
import { ReservationsRpc } from '../../../src/nest/reservations/reservations.rpc';
|
||||
import { ReservationsModule } from '../../../src/nest/reservations/reservations.module';
|
||||
import { AccommodationsRpc } from '../../../src/nest/days/accommodations.rpc';
|
||||
import { DaysModule } from '../../../src/nest/days/days.module';
|
||||
import { AccommodationsRpc } from '../../../src/nest/accommodations/accommodations.rpc';
|
||||
import { AccommodationsModule } from '../../../src/nest/accommodations/accommodations.module';
|
||||
import type { ReservationsService } from '../../../src/nest/reservations/reservations.service';
|
||||
import type { DaysService } from '../../../src/nest/days/days.service';
|
||||
import type { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
||||
@@ -243,6 +243,6 @@ describe('AccommodationsRpc', () => {
|
||||
});
|
||||
|
||||
it('BOOK-RPC-016 the class is listed in its module providers', () => {
|
||||
expectRegisteredProvider(DaysModule, AccommodationsRpc);
|
||||
expectRegisteredProvider(AccommodationsModule, AccommodationsRpc);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,6 +18,7 @@ import { NotFoundError, ValidationError } from '../../../src/nest/trips/trips.se
|
||||
import type { TripsService } from '../../../src/nest/trips/trips.service';
|
||||
import type { ReservationsService } from '../../../src/nest/reservations/reservations.service';
|
||||
import type { DaysService } from '../../../src/nest/days/days.service';
|
||||
import type { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
|
||||
import type { TripMembershipService } from '../../../src/nest/trip-membership/trip-membership.service';
|
||||
import type { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
||||
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
@@ -44,8 +45,11 @@ export function build(opts: { allow?: (action: string) => boolean; updateThrows?
|
||||
const reservations = { list: vi.fn(() => [{ id: 5 }]) } as unknown as ReservationsService & Record<string, ReturnType<typeof vi.fn>>;
|
||||
const days = {
|
||||
list: vi.fn(() => ({ days: [{ id: 3 }] })),
|
||||
listAccommodations: vi.fn(() => [{ id: 11 }]),
|
||||
} as unknown as DaysService & Record<string, ReturnType<typeof vi.fn>>;
|
||||
// The stays left the day service when accommodations became their own domain.
|
||||
const accommodations = {
|
||||
list: vi.fn(() => [{ id: 11 }]),
|
||||
} as unknown as AccommodationsService & Record<string, ReturnType<typeof vi.fn>>;
|
||||
const membership = { joinTripAsMember: vi.fn(() => ({ joined: true })) } as unknown as TripMembershipService & Record<string, ReturnType<typeof vi.fn>>;
|
||||
const db = {
|
||||
canAccessTrip: vi.fn((tripId: number, userId: number) => (tripId === 1 && userId === 42 ? { id: 1, user_id: 42 } : undefined)),
|
||||
@@ -61,10 +65,10 @@ export function build(opts: { allow?: (action: string) => boolean; updateThrows?
|
||||
const permissions = { checkPermission: vi.fn((a: string) => (opts.allow ? opts.allow(a) : true)) } as unknown as PermissionsService;
|
||||
const realtime = { broadcast: vi.fn() } as unknown as RealtimeService & { broadcast: ReturnType<typeof vi.fn> };
|
||||
const guards = new PluginGuards(db, permissions, { isAddonEnabled: vi.fn(() => true) } as unknown as AddonsService);
|
||||
const rpc = new TripsRpc(trips, reservations, days, membership, db, realtime, guards);
|
||||
const rpc = new TripsRpc(trips, reservations, days, membership, db, realtime, guards, accommodations);
|
||||
const host = (...grants: string[]) =>
|
||||
new PluginRpcHost('p', new Set(grants.length ? grants : ALL_TRIP_GRANTS), makeDeps(), createTestPluginRegistry([rpc]));
|
||||
return { trips, reservations, days, membership, realtime, permissions, host };
|
||||
return { trips, reservations, days, accommodations, membership, realtime, permissions, host };
|
||||
}
|
||||
|
||||
describe('TripsRpc reads', () => {
|
||||
|
||||
Reference in New Issue
Block a user