init-source

This commit is contained in:
2026-06-15 16:21:53 -05:00
parent a12a3fc72e
commit 631890c5bd
1010 changed files with 107132 additions and 0 deletions
@@ -0,0 +1,35 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from '@/lib/prisma'
import { requireAuth, logAction, SHIPMENT_SEND_ROLES } from '@/lib/auth'
import { sendEmail, emailTemplate } from '@/lib/email'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const user = await requireAuth(req, res, SHIPMENT_SEND_ROLES)
if (!user) return
if (req.method !== 'POST') return res.status(405).end()
const { id } = req.query as { id: string }
const { clientEmail, subject, message } = req.body
if (!clientEmail || !subject || !message) {
return res.status(400).json({ error: 'clientEmail, subject, message required' })
}
const shipment = await prisma.shipment.findUnique({ where: { id }, include: { items: true } })
if (!shipment) return res.status(404).json({ error: 'Shipment not found' })
const itemsHtml = shipment.items.filter(i => i.included).map(i => `<li>${i.label}</li>`).join('')
await sendEmail(clientEmail, subject, emailTemplate('Quality Release Package', `
<p>${message.replace(/\n/g, '<br>')}</p>
<ul>${itemsHtml}</ul>
`))
const updated = await prisma.shipment.update({
where: { id },
data: { sentAt: new Date(), sentTo: clientEmail },
})
await logAction(user.id, 'UPDATE', 'Shipment', id, { sentAt: null }, { sentAt: updated.sentAt, sentTo: clientEmail })
return res.json({ data: updated })
}
@@ -0,0 +1,45 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from '@/lib/prisma'
import { requireAuth, logAction, generateRef } from '@/lib/auth'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const user = await requireAuth(req, res)
if (!user) return
if (req.method === 'GET') {
const shipments = await prisma.shipment.findMany({
include: { items: { orderBy: { order: 'asc' } }, _count: { select: { escapes: true } } },
orderBy: { createdAt: 'desc' },
})
return res.json({ data: shipments })
}
if (req.method === 'POST') {
const { product, batch, client, clientEmail, shippedAt, items } = req.body
if (!product || !batch || !client || !shippedAt) {
return res.status(400).json({ error: 'product, batch, client, shippedAt required' })
}
const count = await prisma.shipment.count()
const ref = generateRef('REL', count)
const shipment = await prisma.shipment.create({
data: {
ref, product, batch, client, clientEmail,
shippedAt: new Date(shippedAt),
createdById: user.id,
items: {
create: (items || []).map((it: any, i: number) => ({
label: it.label, type: it.type || 'OTHER', included: it.included !== false, order: i,
}))
}
},
include: { items: { orderBy: { order: 'asc' } } },
})
await logAction(user.id, 'CREATE', 'Shipment', shipment.id, null, { ref, product, batch })
return res.status(201).json({ data: shipment })
}
res.status(405).end()
}
@@ -0,0 +1,40 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from '@/lib/prisma'
import { requireAuth } from '@/lib/auth'
// Auto-suggests release items tied to a product: recent first-build form
// submission counts for that product, plus recently resolved NCR fixes.
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const user = await requireAuth(req, res)
if (!user) return
if (req.method !== 'GET') return res.status(405).end()
const { product } = req.query as { product?: string }
const items: { label: string; type: string; included: boolean }[] = []
if (product) {
const forms = await prisma.buildForm.findMany({
where: { product: { contains: product, mode: 'insensitive' } },
include: { _count: { select: { submissions: true } } },
})
for (const f of forms) {
if (f._count.submissions > 0) {
items.push({ label: `${f.name}${f._count.submissions} submissions`, type: 'FORM_DATA', included: true })
}
}
}
// Recently resolved NCRs (last 90 days) as candidate fixes to include
const since = new Date()
since.setDate(since.getDate() - 90)
const recentNcrs = await prisma.nCR.findMany({
where: { status: 'RESOLVED', resolvedAt: { gte: since } },
orderBy: { resolvedAt: 'desc' },
take: 5,
})
for (const n of recentNcrs) {
items.push({ label: `${n.ref} fix — ${n.category || 'resolved'}`, type: 'NCR_FIX', included: false })
}
return res.json({ data: items })
}