mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
618f070675
Distinguish omitted references from explicit clears on stays and their linked bookings. Reserve automatic confirmation for a new reference with a booking code format. Announce linked changes after booking edits. Closes #3301
214 lines
8.0 KiB
TypeScript
214 lines
8.0 KiB
TypeScript
import { buildApp } from '../../src/bootstrap';
|
|
import { Reservations } from '../../src/db/entities/Reservations.entity';
|
|
import { AccommodationsService } from '../../src/nest/accommodations/accommodations.service';
|
|
import { legacyBoundIntegerText } from '../../src/nest/common/row-id';
|
|
import { withRequestContext } from '../../src/nest/database/request-context';
|
|
import { ReservationsService } from '../../src/nest/reservations/reservations.service';
|
|
import { makePlace } from '../helpers/factories/places';
|
|
import { makeReservation } from '../helpers/factories/reservations';
|
|
import { findRow, findRows, updateRows } from '../helpers/factories/rows';
|
|
import { makeDay, makeTrip } from '../helpers/factories/trips';
|
|
import { makeUser } from '../helpers/factories/users';
|
|
import { MikroORM } from '@mikro-orm/core';
|
|
import type { INestApplication } from '@nestjs/common';
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
|
|
let app: INestApplication;
|
|
let orm: MikroORM;
|
|
let svc: AccommodationsService;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildApp();
|
|
orm = app.get(MikroORM);
|
|
svc = app.get(AccommodationsService);
|
|
}, 60_000);
|
|
|
|
afterAll(async () => {
|
|
await app?.close();
|
|
});
|
|
|
|
function linkedReservation(accommodationId: number) {
|
|
return findRow(orm, Reservations, { accommodation_id: legacyBoundIntegerText(accommodationId) });
|
|
}
|
|
|
|
function editStay(id: number, tripId: number, fields: Parameters<AccommodationsService['updateAccommodation']>[2]) {
|
|
return withRequestContext(orm, async () => {
|
|
const existing = await svc.getAccommodation(id, tripId);
|
|
if (!existing) throw new Error('Stay was not found');
|
|
return svc.updateAccommodation(id, existing, fields);
|
|
});
|
|
}
|
|
|
|
describe('Accommodation confirmation references', () => {
|
|
async function stayForConfirmation(confirmation?: string) {
|
|
const { user } = await makeUser(orm);
|
|
const trip = await makeTrip(orm, user.id);
|
|
const day = await makeDay(orm, trip.id);
|
|
const place = await makePlace(orm, trip.id);
|
|
const { accommodation } = await withRequestContext(orm, () =>
|
|
svc.createAccommodation(trip.id, {
|
|
place_id: place.id,
|
|
start_day_id: day.id,
|
|
end_day_id: day.id,
|
|
confirmation,
|
|
}),
|
|
);
|
|
if (!accommodation) throw new Error('Stay was not created');
|
|
return { accommodation, trip, day, place };
|
|
}
|
|
|
|
it.each(['Waiting for the hotel', 'tBd', 'N/a', 'NOT-BOOKED-YET'])(
|
|
'ACC-3301: creates a pending booking with placeholder %s',
|
|
async (confirmation) => {
|
|
const { accommodation } = await stayForConfirmation(confirmation);
|
|
|
|
expect(accommodation.confirmation).toBe(confirmation);
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({
|
|
confirmation_number: confirmation,
|
|
status: 'pending',
|
|
});
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['NOT BOOKED YET: pick a hotel', 'ABC-12345'],
|
|
['not_booked_yet', '1234 567 890'],
|
|
['T.B.D', 'XYZ'],
|
|
])('ACC-3301: confirms a booking after placeholder %s is replaced with %s', async (confirmation, reference) => {
|
|
const { accommodation, trip } = await stayForConfirmation();
|
|
const { accommodation: updated } = await editStay(accommodation.id, trip.id, { confirmation });
|
|
|
|
expect(updated?.confirmation).toBe(confirmation);
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({
|
|
confirmation_number: confirmation,
|
|
status: 'pending',
|
|
});
|
|
await editStay(accommodation.id, trip.id, { confirmation: ` ${reference} ` });
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({
|
|
confirmation_number: reference,
|
|
status: 'confirmed',
|
|
});
|
|
});
|
|
|
|
it.each([null, '', ' \t '])(
|
|
'ACC-3301: clearing with %j clears each booking reference and reopens its confirmation',
|
|
async (confirmation) => {
|
|
const { accommodation, trip } = await stayForConfirmation('ABC123');
|
|
const accommodation_id = legacyBoundIntegerText(accommodation.id);
|
|
await makeReservation(orm, trip.id, {
|
|
accommodation_id,
|
|
type: 'hotel',
|
|
status: 'cancelled',
|
|
confirmation_number: 'ABC123',
|
|
});
|
|
await makeReservation(orm, trip.id, { accommodation_id, type: 'hotel', status: 'confirmed' });
|
|
const { accommodation: updated } = await editStay(accommodation.id, trip.id, { confirmation });
|
|
|
|
expect(updated?.confirmation).toBeNull();
|
|
expect(await findRows(orm, Reservations, { accommodation_id }, { id: 'asc' })).toMatchObject([
|
|
{ confirmation_number: null, status: 'pending' },
|
|
{ confirmation_number: null, status: 'cancelled' },
|
|
{ confirmation_number: null, status: 'confirmed' },
|
|
]);
|
|
},
|
|
);
|
|
|
|
it('ACC-3301: omitting the code preserves the linked booking reference and its chosen status', async () => {
|
|
const { accommodation, trip } = await stayForConfirmation('STAY-1');
|
|
await updateRows(
|
|
orm,
|
|
Reservations,
|
|
{ accommodation_id: legacyBoundIntegerText(accommodation.id) },
|
|
{
|
|
confirmation_number: 'BOOKING-1',
|
|
status: 'pending',
|
|
},
|
|
);
|
|
const { accommodation: updated } = await editStay(accommodation.id, trip.id, {
|
|
notes: 'Late arrival',
|
|
});
|
|
|
|
expect(updated?.confirmation).toBe('STAY-1');
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({
|
|
confirmation_number: 'BOOKING-1',
|
|
status: 'pending',
|
|
});
|
|
});
|
|
|
|
it('ACC-3301: resubmitting the same stay reference reconciles the booking while preserving its pending status', async () => {
|
|
const { accommodation, trip } = await stayForConfirmation('STAY-1');
|
|
await updateRows(
|
|
orm,
|
|
Reservations,
|
|
{ accommodation_id: legacyBoundIntegerText(accommodation.id) },
|
|
{
|
|
confirmation_number: 'BOOKING-1',
|
|
status: 'pending',
|
|
},
|
|
);
|
|
await editStay(accommodation.id, trip.id, { confirmation: 'STAY-1', notes: 'Late arrival' });
|
|
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({
|
|
confirmation_number: 'STAY-1',
|
|
status: 'pending',
|
|
});
|
|
});
|
|
|
|
it('ACC-3301: explicitly clearing an empty stay clears a stale booking reference', async () => {
|
|
const { accommodation, trip } = await stayForConfirmation();
|
|
await updateRows(
|
|
orm,
|
|
Reservations,
|
|
{ accommodation_id: legacyBoundIntegerText(accommodation.id) },
|
|
{
|
|
confirmation_number: 'STALE',
|
|
status: 'confirmed',
|
|
},
|
|
);
|
|
await editStay(accommodation.id, trip.id, { confirmation: null });
|
|
|
|
expect(await linkedReservation(accommodation.id)).toMatchObject({ confirmation_number: null, status: 'pending' });
|
|
});
|
|
|
|
it.each([
|
|
{ nested: null, expected: null },
|
|
{ nested: ' \t ', expected: null },
|
|
{ nested: 'NEW-REF', expected: 'NEW-REF' },
|
|
{ top: null, nested: 'OLD-REF', expected: null },
|
|
{ top: 'TOP-REF', nested: null, expected: 'TOP-REF' },
|
|
])(
|
|
'ACC-3301: syncs the selected reference across a complete booking update (%j)',
|
|
async ({ top, nested, expected }) => {
|
|
const { accommodation, trip, day, place } = await stayForConfirmation('OLD-REF');
|
|
const booking = await linkedReservation(accommodation.id);
|
|
if (!booking) throw new Error('Booking was not found');
|
|
const bookings = app.get(ReservationsService);
|
|
|
|
await withRequestContext(orm, async () => {
|
|
const current = await bookings.getReservation(booking.id, trip.id);
|
|
if (!current) throw new Error('Booking was not found');
|
|
const { reservation, accommodationChanged } = await bookings.update(
|
|
booking.id,
|
|
trip.id,
|
|
{
|
|
type: 'hotel',
|
|
status: 'confirmed',
|
|
confirmation_number: top,
|
|
create_accommodation: {
|
|
place_id: place.id,
|
|
start_day_id: day.id,
|
|
end_day_id: day.id,
|
|
confirmation: nested,
|
|
},
|
|
},
|
|
current,
|
|
);
|
|
expect(accommodationChanged).toBe(true);
|
|
expect(reservation).toMatchObject({ confirmation_number: expected, status: 'confirmed' });
|
|
expect(await svc.getAccommodation(accommodation.id, trip.id)).toMatchObject({ confirmation: expected });
|
|
});
|
|
},
|
|
);
|
|
});
|