48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { z } from 'zod'
|
|
import { pikkuFunc } from '#pikku/pikku-types.gen.js'
|
|
import { setAuditContext } from '../../lib/audit-context.js'
|
|
|
|
export const CreateTaskInstanceInput = z.object({
|
|
templateId: z.string().uuid().optional().describe('ID of the task template to base this on'),
|
|
title: z.string().describe('Title of the task instance'),
|
|
description: z.string().optional().describe('Description of the task'),
|
|
category: z.enum(['housekeeping', 'kitchen', 'maintenance', 'operations', 'retreat', 'transport']).describe('Task category'),
|
|
location: z.string().optional().describe('Location for the task'),
|
|
scheduledStartAt: z.string().optional().describe('Scheduled start date/time'),
|
|
scheduledEndAt: z.string().optional().describe('Scheduled end date/time'),
|
|
})
|
|
|
|
export const CreateTaskInstanceOutput = z.object({
|
|
taskId: z.string().describe('Created task instance ID'),
|
|
status: z.string().describe('Task status'),
|
|
})
|
|
|
|
export const createTaskInstance = pikkuFunc({
|
|
expose: true,
|
|
approvalRequired: true,
|
|
tags: ['tasks.manage'],
|
|
input: CreateTaskInstanceInput,
|
|
output: CreateTaskInstanceOutput,
|
|
func: async ({ kysely }, { templateId, title, description, category, location, scheduledStartAt, scheduledEndAt }, { session }) => {
|
|
await setAuditContext(kysely, session!.userId)
|
|
|
|
const result = await kysely
|
|
.insertInto('taskInstance')
|
|
.values({
|
|
templateId: templateId ?? null,
|
|
title,
|
|
description: description ?? null,
|
|
category,
|
|
location: location ?? null,
|
|
scheduledStartAt: scheduledStartAt ? new Date(scheduledStartAt) : null,
|
|
scheduledEndAt: scheduledEndAt ? new Date(scheduledEndAt) : null,
|
|
status: 'open',
|
|
createdByUserId: session!.userId,
|
|
})
|
|
.returning(['taskId', 'status'])
|
|
.executeTakeFirstOrThrow()
|
|
|
|
return result
|
|
},
|
|
})
|