97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth, logAction, generateRef } from '@/lib/auth'
|
|
import { NCRSeverity, EscapeStatus } from '@prisma/client'
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const user = await requireAuth(req, res, ['ADMIN', 'QC'])
|
|
if (!user) return
|
|
|
|
const { id } = req.query as { id: string }
|
|
|
|
const escape = await prisma.qualityEscape.findUnique({
|
|
where: { id },
|
|
include: { shipment: true, capa: { select: { ref: true } } },
|
|
})
|
|
if (!escape) return res.status(404).json({ error: 'Escape not found' })
|
|
|
|
if (req.method === 'GET') {
|
|
return res.json({ data: escape })
|
|
}
|
|
|
|
if (req.method === 'PATCH') {
|
|
const before = { ...escape }
|
|
const { severity, status, resolution, category, standardItem, escalate, capaForm } = req.body as {
|
|
severity?: NCRSeverity
|
|
status?: EscapeStatus
|
|
resolution?: string
|
|
category?: string
|
|
standardItem?: string
|
|
escalate?: boolean
|
|
capaForm?: { title: string; priority: string; ownerId: string; dueDate: string }
|
|
}
|
|
|
|
const updateData: any = {}
|
|
if (severity !== undefined) updateData.severity = severity
|
|
if (status !== undefined) updateData.status = status
|
|
|
|
if (resolution !== undefined && category !== undefined) {
|
|
updateData.resolution = resolution
|
|
updateData.category = category
|
|
updateData.status = 'RESOLVED'
|
|
updateData.resolvedAt = new Date()
|
|
}
|
|
|
|
if (standardItem !== undefined) {
|
|
updateData.standardItemAdded = standardItem || '—'
|
|
if (standardItem) {
|
|
const maxOrder = await prisma.shippingStandardItem.count()
|
|
await prisma.shippingStandardItem.create({
|
|
data: { text: standardItem, source: escape.ref, order: maxOrder },
|
|
})
|
|
}
|
|
}
|
|
|
|
let capaRef: string | undefined
|
|
if (escalate && capaForm) {
|
|
const count = await prisma.cAPA.count()
|
|
capaRef = generateRef('CAPA', count)
|
|
const capa = await prisma.cAPA.create({
|
|
data: {
|
|
ref: capaRef, title: capaForm.title,
|
|
description: `Client-reported quality escape ${escape.ref} (${escape.shipment.ref}): ${escape.description}`,
|
|
priority: capaForm.priority as any,
|
|
ownerId: capaForm.ownerId, raisedById: user.id,
|
|
dueDate: new Date(capaForm.dueDate),
|
|
},
|
|
})
|
|
await prisma.cAPAEvent.create({
|
|
data: { capaId: capa.id, event: 'CAPA raised', note: `Escalated from quality escape ${escape.ref} by ${user.name}` }
|
|
})
|
|
updateData.status = 'ESCALATED'
|
|
updateData.capaId = capa.id
|
|
}
|
|
|
|
const updated = await prisma.qualityEscape.update({
|
|
where: { id },
|
|
data: updateData,
|
|
include: { shipment: true, capa: { select: { ref: true } } },
|
|
})
|
|
|
|
// File to resolutions library
|
|
if (resolution !== undefined && category !== undefined) {
|
|
await prisma.resolution.create({
|
|
data: {
|
|
title: escape.description.length > 80 ? escape.description.slice(0, 80) + '…' : escape.description,
|
|
category, resolution, linkedRef: escape.ref,
|
|
}
|
|
})
|
|
}
|
|
|
|
await logAction(user.id, 'UPDATE', 'QualityEscape', id, before, updated)
|
|
return res.json({ data: updated })
|
|
}
|
|
|
|
res.status(405).end()
|
|
}
|