|
| 1 | +import type { Request, Response } from 'express' |
| 2 | +import { z } from 'zod' |
| 3 | + |
| 4 | +import { NotFoundError } from '@crowd/common' |
| 5 | +import { |
| 6 | + INACTIVE_REASONS, |
| 7 | + STEWARDSHIP_UPDATABLE_STATUSES, |
| 8 | + updateStewardshipStatus, |
| 9 | +} from '@crowd/data-access-layer' |
| 10 | + |
| 11 | +import { getPackagesQx } from '@/db/packagesDb' |
| 12 | +import { ok } from '@/utils/api' |
| 13 | +import { validateOrThrow } from '@/utils/validation' |
| 14 | + |
| 15 | +import { stewardshipIdParamsSchema } from './schemas' |
| 16 | + |
| 17 | +const bodySchema = z |
| 18 | + .object({ |
| 19 | + status: z.enum(STEWARDSHIP_UPDATABLE_STATUSES), |
| 20 | + inactiveReason: z.enum(INACTIVE_REASONS).optional(), |
| 21 | + notes: z.string().trim().min(1).optional(), |
| 22 | + }) |
| 23 | + .refine((d) => d.status !== 'inactive' || !!d.inactiveReason, { |
| 24 | + message: 'inactiveReason is required when status is inactive', |
| 25 | + path: ['inactiveReason'], |
| 26 | + }) |
| 27 | + |
| 28 | +export async function updateStatusHandler(req: Request, res: Response): Promise<void> { |
| 29 | + const { id } = validateOrThrow(stewardshipIdParamsSchema, req.params) |
| 30 | + const { status, inactiveReason, notes } = validateOrThrow(bodySchema, req.body) |
| 31 | + |
| 32 | + const qx = await getPackagesQx() |
| 33 | + const stewardship = await updateStewardshipStatus(qx, id, { |
| 34 | + status, |
| 35 | + inactiveReason, |
| 36 | + notes, |
| 37 | + actorUserId: req.actor.id, |
| 38 | + }) |
| 39 | + |
| 40 | + if (!stewardship) { |
| 41 | + throw new NotFoundError(`Stewardship not found: ${id}`) |
| 42 | + } |
| 43 | + |
| 44 | + ok(res, { stewardship }) |
| 45 | +} |
0 commit comments