44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { z } from 'zod'
|
|
import { pikkuFunc } from '#pikku/pikku-types.gen.js'
|
|
import { setAuditContext } from '../../lib/audit-context.js'
|
|
|
|
export const CreateBoatTripInput = z.object({
|
|
boatId: z.string().uuid().optional().describe('ID of the boat assigned to this trip'),
|
|
routeId: z.string().uuid().describe('ID of the route for this trip'),
|
|
scheduledDepartureAt: z.string().describe('Scheduled departure date/time'),
|
|
scheduledArrivalAt: z.string().optional().describe('Scheduled arrival date/time'),
|
|
notes: z.string().optional().describe('Additional notes about the trip'),
|
|
})
|
|
|
|
export const CreateBoatTripOutput = z.object({
|
|
tripId: z.string().describe('Created trip ID'),
|
|
status: z.string().describe('Trip status'),
|
|
})
|
|
|
|
export const createBoatTrip = pikkuFunc({
|
|
expose: true,
|
|
approvalRequired: true,
|
|
tags: ['boats.manage'],
|
|
input: CreateBoatTripInput,
|
|
output: CreateBoatTripOutput,
|
|
func: async ({ kysely }, { boatId, routeId, scheduledDepartureAt, scheduledArrivalAt, notes }, { session }) => {
|
|
await setAuditContext(kysely, session!.userId)
|
|
|
|
const result = await kysely
|
|
.insertInto('boatTrip')
|
|
.values({
|
|
boatId: boatId ?? null,
|
|
routeId,
|
|
scheduledDepartureAt: new Date(scheduledDepartureAt),
|
|
scheduledArrivalAt: scheduledArrivalAt ? new Date(scheduledArrivalAt) : null,
|
|
notes: notes ?? null,
|
|
status: 'planned',
|
|
createdByUserId: session!.userId,
|
|
})
|
|
.returning(['tripId', 'status'])
|
|
.executeTakeFirstOrThrow()
|
|
|
|
return result
|
|
},
|
|
})
|