mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +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:
@@ -5,6 +5,7 @@ import {
|
||||
tripUpdateRequestSchema,
|
||||
tripAddMemberRequestSchema,
|
||||
activeTripResponseSchema,
|
||||
tripSchema,
|
||||
} from './trip.schema';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
@@ -55,6 +56,23 @@ describe('tripUpdateRequestSchema', () => {
|
||||
);
|
||||
expect(tripUpdateRequestSchema.safeParse({ cover_image: 'x'.repeat(2049) }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('takes the costs mode, split or totals and nothing else (#1714)', () => {
|
||||
expect(tripUpdateRequestSchema.safeParse({ costs_mode: 'totals' }).success).toBe(true);
|
||||
expect(tripUpdateRequestSchema.safeParse({ costs_mode: 'split' }).success).toBe(true);
|
||||
expect(tripUpdateRequestSchema.safeParse({ costs_mode: 'none' }).success).toBe(false);
|
||||
expect(tripUpdateRequestSchema.safeParse({ costs_mode: null }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tripSchema', () => {
|
||||
const trip = { id: 1, user_id: 1, title: 'Japan', currency: 'EUR', is_archived: 0, reminder_days: 3 };
|
||||
|
||||
it('reads the costs mode, and a trip cached without one still parses (#1714)', () => {
|
||||
expect(tripSchema.parse({ ...trip, costs_mode: 'totals' }).costs_mode).toBe('totals');
|
||||
expect(tripSchema.safeParse(trip).success).toBe(true);
|
||||
expect(tripSchema.safeParse({ ...trip, costs_mode: 'shares' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tripAddMemberRequestSchema', () => {
|
||||
|
||||
@@ -32,6 +32,16 @@ export function tripSpanDays(startDate: string, endDate: string): number {
|
||||
return Math.floor((utcDay(endDate) - utcDay(startDate)) / 86400000) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* How a trip keeps its costs (#1714): `split` tracks who paid and who shares
|
||||
* each expense and settles up between the travellers, `totals` keeps only what
|
||||
* the trip costs. Who paid and the shares stay stored either way; `totals` only
|
||||
* leaves them out of Costs. Trips start out as `split`.
|
||||
*/
|
||||
export const TRIP_COSTS_MODES = ['split', 'totals'] as const;
|
||||
export const tripCostsModeSchema = z.enum(TRIP_COSTS_MODES);
|
||||
export type TripCostsMode = z.infer<typeof tripCostsModeSchema>;
|
||||
|
||||
/**
|
||||
* Trip entity as returned by the trip list / get / create / update endpoints
|
||||
* (server/src/services/tripService.ts -> TRIP_SELECT). Columns of the `trips`
|
||||
@@ -49,6 +59,8 @@ export const tripSchema = z.object({
|
||||
cover_image: z.string().nullable().optional(),
|
||||
is_archived: z.number(),
|
||||
reminder_days: z.number(),
|
||||
// Optional: a trip cached before the column existed has none, and reads as `split`.
|
||||
costs_mode: tripCostsModeSchema.optional(),
|
||||
created_at: z.string().optional(),
|
||||
updated_at: z.string().optional(),
|
||||
// computed in TRIP_SELECT (list/get)
|
||||
@@ -121,6 +133,8 @@ export const tripUpdateRequestSchema = z.object({
|
||||
// Bounded like a place's image url: a replaced cover is queued for release,
|
||||
// and every upload path the old value spells out is one queued object.
|
||||
cover_image: z.string().max(2048).nullable().optional(),
|
||||
/** Costs as totals only or split between the travellers; needs `budget_edit`, not `trip_edit`. */
|
||||
costs_mode: tripCostsModeSchema.optional(),
|
||||
});
|
||||
export type TripUpdateRequest = z.infer<typeof tripUpdateRequestSchema>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user