Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 'use server'; import { revalidatePath } from 'next/cache'; import type { ClaimKind } from '@prisma/client'; import { requireSession } from '@/lib/session'; import { createClaim } from '@/lib/claims'; import { logActivity } from '@/lib/activity'; import { getSystemConfig } from '@/lib/system-config'; export type ActionState = { error?: string; success?: string }; function parseKind(v: unknown): ClaimKind | null { return v === 'CLAIM' || v === 'DEFECTIVE' || v === 'REFUND' ? v : null; } export async function createClaimAction(_prev: ActionState, formData: FormData): Promise<ActionState> { const session = await requireSession(); const kind = parseKind(formData.get('kind')); if (!kind) return { error: 'Invalid claim kind' }; const productCode = String(formData.get('productCode') ?? '').trim() || null; const issueTypeId = String(formData.get('issueTypeId') ?? '').trim() || null; const customerName = String(formData.get('customerName') ?? '').trim() || null; const description = String(formData.get('description') ?? '').trim() || null; const amountRaw = String(formData.get('amountTHB') ?? '').trim(); const amountTHB = amountRaw ? Number(amountRaw) : null; if (kind === 'REFUND') { if (amountTHB == null || !Number.isFinite(amountTHB) || amountTHB <= 0) { return { error: 'กรุณาระบุจำนวนเงินชดเชย' }; } const { refundPerItemCapTHB } = await getSystemConfig(); if (amountTHB > refundPerItemCapTHB) { return { error: `จำนวนเงินเกิน ฿${refundPerItemCapTHB.toLocaleString()} ต่อรายการ` }; } } await createClaim({ kind, productCode, issueTypeId, customerName, amountTHB, description, filedByUserId: session.sub, }); await logActivity({ userId: session.sub, action: `claim.create:${kind.toLowerCase()}`, detail: productCode ?? customerName ?? description ?? '(no detail)', }); revalidatePath('/claims'); return { success: 'บันทึกข้อมูลเรียบร้อย' }; } |