mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
refactor(extract): dedupe currency/day helpers, drop redundant casts, support JPY vouchers
Code-audit clean-ups: share one normCurrency between the router and the templates, lift the duplicated nearest-day resolver into formatters.resolveDayId, drop two needless as-unknown-as casts at the fillBookingWideFields call sites, restore routeExtraction's doc comment, and give the broker template readable names. Plus recognise ¥/JPY and fall back to a standalone symbol amount, so a Klook-style voucher whose price sits far from any label still yields a cost.
This commit is contained in:
@@ -1,5 +1,29 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { splitReservationDateTime } from './formatters'
|
||||
import { splitReservationDateTime, resolveDayId } from './formatters'
|
||||
import type { Day } from '../types'
|
||||
|
||||
const days = [
|
||||
{ id: 10, date: '2026-05-03' },
|
||||
{ id: 11, date: '2026-05-04' },
|
||||
{ id: 12, date: '2026-05-22' },
|
||||
] as Day[]
|
||||
|
||||
describe('resolveDayId', () => {
|
||||
it('returns the exact-match day id', () => {
|
||||
expect(resolveDayId(days, '2026-05-04')).toBe(11)
|
||||
})
|
||||
it('accepts a full ISO timestamp', () => {
|
||||
expect(resolveDayId(days, '2026-05-22T13:30:00')).toBe(12)
|
||||
})
|
||||
it('falls back to the nearest day when there is no exact match', () => {
|
||||
expect(resolveDayId(days, '2026-05-05')).toBe(11)
|
||||
})
|
||||
it('returns "" for a missing/invalid date or no days', () => {
|
||||
expect(resolveDayId(days, null)).toBe('')
|
||||
expect(resolveDayId(days, 'not a date')).toBe('')
|
||||
expect(resolveDayId([], '2026-05-04')).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('splitReservationDateTime', () => {
|
||||
it('parses full ISO datetime', () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { AssignmentsMap } from '../types'
|
||||
import type { AssignmentsMap, Day } from '../types'
|
||||
|
||||
// Collapses verbose Nominatim display_name strings (e.g. "Place, 1, Road, Neighbourhood,
|
||||
// City, County, State, Country, Postcode, Country") into "Place, Postcode, Country".
|
||||
@@ -129,6 +129,27 @@ export function splitReservationDateTime(value?: string | null): { date: string
|
||||
return { date: null, time: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a date (YYYY-MM-DD or an ISO timestamp) to a trip day id: exact match, else the
|
||||
* nearest day so an out-of-range booking still lands on one. Returns '' when there is no
|
||||
* usable date or the trip has no days — callers read that as "no day selected".
|
||||
*/
|
||||
export function resolveDayId(days: Day[], value: string | null | undefined): Day['id'] | '' {
|
||||
const date = value ? String(value).slice(0, 10) : ''
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || days.length === 0) return ''
|
||||
const exact = days.find(d => d.date === date)
|
||||
if (exact) return exact.id
|
||||
const target = new Date(date).getTime()
|
||||
let best: Day['id'] | '' = ''
|
||||
let bestDiff = Infinity
|
||||
for (const d of days) {
|
||||
if (!d.date) continue
|
||||
const diff = Math.abs(new Date(d.date).getTime() - target)
|
||||
if (diff < bestDiff) { bestDiff = diff; best = d.id }
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
export function dayTotalCost(dayId: number, assignments: AssignmentsMap, currency: string): string | null {
|
||||
const da = assignments[String(dayId)] || []
|
||||
const total = da.reduce((s, a) => s + (parseFloat(String(a.place?.price ?? '')) || 0), 0)
|
||||
|
||||
Reference in New Issue
Block a user