mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
feat(costs): let a trip keep its costs as totals only (#1714)
Travellers who track what a trip costs but never split it saw an Unfinished warning on every expense and a settle-up they had no use for. A trip now has a costs mode (appended migration, trips.costs_mode, split by default); totals leaves payers, shares and settling up out of Costs on desktop and phone through the same gate as a trip of one, and saves new expenses without a payer. PUT /api/trips/:id and update_trip take it on budget_edit. The split chips move into their own component.
This commit is contained in:
@@ -106,6 +106,7 @@ const UNNUMBERED = [
|
||||
'Migration20200101046360_a_new_install_starts_with_more_addons_on',
|
||||
'Migration20200101046370_a_deleted_chat_message_keeps_no_text',
|
||||
'Migration20200101046380_ireland_gained_its_counties',
|
||||
'Migration20200101046390_a_trip_can_keep_its_costs_as_totals_only',
|
||||
];
|
||||
// The last step the positional runner ever had; planLegacyBaseline refuses a
|
||||
// schema_version past it.
|
||||
|
||||
@@ -560,6 +560,7 @@ describe('TripsRepository.findForViewer / listForUser / activeTrip (Plan 3c Task
|
||||
is_archived: 0,
|
||||
cover_image: null,
|
||||
reminder_days: 3,
|
||||
costs_mode: 'split',
|
||||
});
|
||||
// Seen INSIDE the open transaction — the write and the read share
|
||||
// one connection.
|
||||
@@ -609,6 +610,7 @@ describe('TripsRepository.insertTrip / updateTripRow / setCoverImage / deleteByI
|
||||
is_archived: 1,
|
||||
cover_image: 'c.png',
|
||||
reminder_days: 7,
|
||||
costs_mode: 'totals',
|
||||
});
|
||||
const row = await tripRow(trip.id);
|
||||
expect(row).toMatchObject({
|
||||
@@ -620,6 +622,7 @@ describe('TripsRepository.insertTrip / updateTripRow / setCoverImage / deleteByI
|
||||
is_archived: 1,
|
||||
cover_image: 'c.png',
|
||||
reminder_days: 7,
|
||||
costs_mode: 'totals',
|
||||
});
|
||||
// CURRENT_TIMESTAMP has one-second resolution — a same-second before/after
|
||||
// comparison is flaky, not a real assertion; the column being non-null and
|
||||
@@ -647,7 +650,7 @@ describe('TripsRepository.insertTrip / updateTripRow / setCoverImage / deleteByI
|
||||
// ── Plan 3c Task 8 (`TripsService.copy`, TP37) — additive ───────────────────
|
||||
|
||||
describe('TripsRepository.insertTripCopy (TP37)', () => {
|
||||
it('TRIPREPO-039: writes the 8-column copy set, is_archived hard-coded to 0 regardless of the caller', async () => {
|
||||
it('TRIPREPO-039: writes the 9-column copy set, is_archived hard-coded to 0 regardless of the caller', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: newOwner } = createUser(testDb);
|
||||
const newId = await trips.insertTripCopy({
|
||||
@@ -659,9 +662,20 @@ describe('TripsRepository.insertTripCopy (TP37)', () => {
|
||||
currency: 'USD',
|
||||
cover_image: 'c.png',
|
||||
reminder_days: 7,
|
||||
costs_mode: 'totals',
|
||||
});
|
||||
const { user_id, title, description, start_date, end_date, currency, cover_image, is_archived, reminder_days } =
|
||||
await tripRow(newId);
|
||||
const {
|
||||
user_id,
|
||||
title,
|
||||
description,
|
||||
start_date,
|
||||
end_date,
|
||||
currency,
|
||||
cover_image,
|
||||
is_archived,
|
||||
reminder_days,
|
||||
costs_mode,
|
||||
} = await tripRow(newId);
|
||||
const row = {
|
||||
user_id,
|
||||
title,
|
||||
@@ -672,6 +686,7 @@ describe('TripsRepository.insertTripCopy (TP37)', () => {
|
||||
cover_image,
|
||||
is_archived,
|
||||
reminder_days,
|
||||
costs_mode,
|
||||
};
|
||||
expect(row).toEqual({
|
||||
user_id: newOwner.id,
|
||||
@@ -683,6 +698,7 @@ describe('TripsRepository.insertTripCopy (TP37)', () => {
|
||||
cover_image: 'c.png',
|
||||
is_archived: 0,
|
||||
reminder_days: 7,
|
||||
costs_mode: 'totals',
|
||||
});
|
||||
// The source trip (never touched) proves is_archived: 0 is a literal, not a copied value.
|
||||
await setTrip(owner.id, { is_archived: 1 });
|
||||
@@ -701,6 +717,7 @@ describe('TripsRepository.insertTripCopy (TP37)', () => {
|
||||
currency: null,
|
||||
cover_image: null,
|
||||
reminder_days: 3,
|
||||
costs_mode: 'split',
|
||||
});
|
||||
expect((await tripRow(newId)).is_archived).toBe(0);
|
||||
});
|
||||
|
||||
@@ -1179,4 +1179,35 @@ describe('Trip writes, as REST makes them', () => {
|
||||
});
|
||||
expect(await tripRow(trip.id)).toMatchObject({ title: 'After', is_archived: 1 });
|
||||
});
|
||||
|
||||
it('MCP-TRIPS-005: update_trip sets the costs mode on budget_edit, as the route does (#1714)', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: member } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
expect((await tripRow(trip.id)).costs_mode).toBe('split');
|
||||
// A member may edit the costs by default, though not the trip's own details.
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'update_trip',
|
||||
arguments: { tripId: trip.id, costs_mode: 'totals' },
|
||||
});
|
||||
expect(result.isError).toBeFalsy();
|
||||
expect((parseToolResult(result) as { trip: { costs_mode: string } }).trip.costs_mode).toBe('totals');
|
||||
});
|
||||
await setPermission('budget_edit', 'trip_owner');
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'update_trip',
|
||||
arguments: { tripId: trip.id, costs_mode: 'split' },
|
||||
});
|
||||
expect(result.isError).toBe(true);
|
||||
const bogus = await h.client.callTool({
|
||||
name: 'update_trip',
|
||||
arguments: { tripId: trip.id, costs_mode: 'shares' },
|
||||
});
|
||||
expect(bogus.isError).toBe(true);
|
||||
});
|
||||
expect((await tripRow(trip.id)).costs_mode).toBe('totals');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -385,6 +385,25 @@ describe('TripsController (parity with the legacy /api/trips route)', async () =
|
||||
});
|
||||
});
|
||||
|
||||
it('the costs mode needs budget_edit and nothing else (#1714)', async () => {
|
||||
const noBudget = svc({ can: vi.fn().mockImplementation((a: string) => a !== 'budget_edit') });
|
||||
expect(await thrownAsync(() => tc(noBudget).update(user, '9', { costs_mode: 'totals' }, req))).toEqual({
|
||||
status: 403,
|
||||
body: { error: 'No permission to change how this trip keeps its costs' },
|
||||
});
|
||||
const update = vi.fn().mockReturnValue({
|
||||
updatedTrip: { id: 9, costs_mode: 'totals' },
|
||||
changes: { costs_mode: 'totals' },
|
||||
newTitle: 'b',
|
||||
newReminder: 0,
|
||||
oldReminder: 0,
|
||||
});
|
||||
const onlyBudget = svc({ update, can: vi.fn().mockImplementation((a: string) => a === 'budget_edit') });
|
||||
expect(await tc(onlyBudget).update(user, '9', { costs_mode: 'totals' }, req)).toEqual({
|
||||
trip: { id: 9, costs_mode: 'totals' },
|
||||
});
|
||||
});
|
||||
|
||||
it('403 on an edit field without trip_edit', async () => {
|
||||
const s = svc({ can: vi.fn().mockImplementation((a: string) => a !== 'trip_edit') });
|
||||
expect(await thrownAsync(() => tc(s).update(user, '9', { title: 'b' }, req))).toEqual({
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
/**
|
||||
* The pure helpers of the trips domain: the date range a trip may span and the
|
||||
* feed token a trip row loses on its way out. TRIP-HELPERS-001 to 003.
|
||||
* feed token a trip row loses on its way out, and the rights a trip update
|
||||
* needs. TRIP-HELPERS-001 to 004.
|
||||
*/
|
||||
import { assertTripSpan, withoutFeedToken } from '../../../src/nest/trips/trips.helpers';
|
||||
import { assertTripSpan, tripUpdatePermissions, withoutFeedToken } from '../../../src/nest/trips/trips.helpers';
|
||||
import { MAX_TRIP_DAYS, addIsoDays } from '@trek/shared';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
@@ -32,3 +33,16 @@ describe('withoutFeedToken', () => {
|
||||
expect(withoutFeedToken(42)).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tripUpdatePermissions', () => {
|
||||
it('TRIP-HELPERS-004: the costs mode asks for budget_edit, not trip_edit (#1714)', () => {
|
||||
expect(tripUpdatePermissions({ costs_mode: 'totals' }).map((p) => p.action)).toEqual(['budget_edit']);
|
||||
expect(tripUpdatePermissions({ costs_mode: 'split', title: 'Japan' }).map((p) => p.action)).toEqual([
|
||||
'budget_edit',
|
||||
'trip_edit',
|
||||
]);
|
||||
expect(tripUpdatePermissions({ title: 'Japan', costs_mode: undefined }).map((p) => p.action)).toEqual([
|
||||
'trip_edit',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user