import { env } from '../config/env.js'; import { createLogger } from './logger.js'; const log = createLogger('trip-store'); export interface Trip { id: string; userId: string; title: string; vehicleModel: string; rangeMi: number; itinerary: unknown; status: string; isPublic: boolean; shareSlug: string | null; createdAt: Date; updatedAt: Date; } export type TripInput = Omit & { shareSlug?: string | null; }; interface TripStore { list(userId: string): Promise; get(id: string, userId: string): Promise; create(input: TripInput): Promise; update(id: string, userId: string, patch: Partial): Promise; remove(id: string, userId: string): Promise; } class MemoryTripStore implements TripStore { private trips = new Map(); async list(userId: string): Promise { return [...this.trips.values()] .filter(t => t.userId === userId) .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()); } async get(id: string, userId: string): Promise { const trip = this.trips.get(id); if (!trip || trip.userId !== userId) return null; return trip; } async create(input: TripInput): Promise { const id = crypto.randomUUID(); const now = new Date(); const trip: Trip = { id, ...input, shareSlug: input.shareSlug ?? null, createdAt: now, updatedAt: now, }; this.trips.set(id, trip); return trip; } async update(id: string, userId: string, patch: Partial): Promise { const trip = this.trips.get(id); if (!trip || trip.userId !== userId) return null; const updated: Trip = { ...trip, ...patch, updatedAt: new Date() }; this.trips.set(id, updated); return updated; } async remove(id: string, userId: string): Promise { const trip = this.trips.get(id); if (!trip || trip.userId !== userId) return false; return this.trips.delete(id); } } let store: TripStore | null = null; export function getTripStore(): TripStore { if (store) return store; if (!env.databaseUrl) { log.warn('DATABASE_URL not set — using in-memory trip store (data lost on restart)'); store = new MemoryTripStore(); return store; } log.info('Using in-memory trip store (Prisma adapter not yet wired)'); store = new MemoryTripStore(); return store; }