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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 'use server'; import { revalidatePath } from 'next/cache'; import { prisma } from '@/lib/db'; import { requireSession } from '@/lib/session'; import { canManageSettings } from '@/lib/permissions'; import { cacheInvalidatePattern, invalidateJoinedDisplayCaches } from '@/lib/cache'; import { logActivity } from '@/lib/activity'; import { setSystemConfig } from '@/lib/system-config'; export type ActionState = { error?: string; success?: string }; async function gate() { const session = await requireSession(); if (!canManageSettings(session.role)) return { error: 'ไม่มีสิทธิ์จัดการการตั้งค่า' as string, session: null }; return { error: null, session }; } function str(v: FormDataEntryValue | null, max = 64): string { return String(v ?? '').trim().slice(0, max); } // ===== BRANCH ===== export async function upsertBranchAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const id = str(fd.get('id')); const code = str(fd.get('code')).toUpperCase(); const name = str(fd.get('name'), 128); const active = str(fd.get('active')) !== 'false'; if (!code) return { error: 'กรุณาระบุ Code' }; if (!name) return { error: 'กรุณาระบุชื่อสาขา' }; if (id) { await prisma.branch.update({ where: { id }, data: { code, name, active } }); } else { const dupe = await prisma.branch.findUnique({ where: { code } }); if (dupe) return { error: 'Code นี้มีอยู่แล้ว' }; await prisma.branch.create({ data: { code, name, active } }); } await cacheInvalidatePattern('taxonomy:*'); await invalidateJoinedDisplayCaches(); // product/transfer lists embed branchCode await logActivity({ userId: session!.sub, action: id ? 'branch.update' : 'branch.create', detail: `${code} — ${name}` }); revalidatePath('/settings'); revalidatePath('/inventory'); revalidatePath('/transfer'); revalidatePath('/products'); revalidatePath('/dashboard'); return { success: 'บันทึกสาขาเรียบร้อย' }; } export async function toggleBranchActiveAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const id = str(fd.get('id')); const b = await prisma.branch.findUnique({ where: { id } }); if (!b) return { error: 'ไม่พบสาขา' }; await prisma.branch.update({ where: { id }, data: { active: !b.active } }); await cacheInvalidatePattern('taxonomy:*'); await invalidateJoinedDisplayCaches(); await logActivity({ userId: session!.sub, action: 'branch.toggle', detail: `${b.code} → ${!b.active}` }); revalidatePath('/settings'); revalidatePath('/inventory'); revalidatePath('/transfer'); revalidatePath('/products'); return { success: 'อัพเดตสถานะสาขา' }; } // ===== PRODUCT CATEGORY ===== export async function upsertCategoryAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const id = str(fd.get('id')); const code = str(fd.get('code')).toUpperCase().replace(/\s+/g, '_'); const name = str(fd.get('name'), 128); const icon = str(fd.get('icon'), 128) || null; const active = str(fd.get('active')) !== 'false'; if (!code) return { error: 'กรุณาระบุ Code' }; if (!name) return { error: 'กรุณาระบุชื่อหมวด' }; if (id) { await prisma.productCategory.update({ where: { id }, data: { code, name, icon, active } }); } else { const dupe = await prisma.productCategory.findUnique({ where: { code } }); if (dupe) return { error: 'Code นี้มีอยู่แล้ว' }; await prisma.productCategory.create({ data: { code, name, icon, active } }); } await cacheInvalidatePattern('taxonomy:*'); await invalidateJoinedDisplayCaches(); await logActivity({ userId: session!.sub, action: id ? 'category.update' : 'category.create', detail: `${code} — ${name}` }); revalidatePath('/settings'); revalidatePath('/categorization'); revalidatePath('/products'); return { success: 'บันทึกหมวดหมู่เรียบร้อย' }; } // ===== PRODUCT SUBTYPE ===== export async function upsertSubtypeAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const id = str(fd.get('id')); const categoryId = str(fd.get('categoryId')); const code = str(fd.get('code')).toUpperCase().replace(/\s+/g, '_'); const name = str(fd.get('name'), 128); const active = str(fd.get('active')) !== 'false'; if (!categoryId) return { error: 'กรุณาเลือกหมวดหลัก' }; if (!code) return { error: 'กรุณาระบุ Code' }; if (!name) return { error: 'กรุณาระบุชื่อ subtype' }; if (id) { await prisma.productSubtype.update({ where: { id }, data: { categoryId, code, name, active } }); } else { const dupe = await prisma.productSubtype.findUnique({ where: { categoryId_code: { categoryId, code } } }); if (dupe) return { error: 'Code นี้มีอยู่แล้วในหมวดเดียวกัน' }; await prisma.productSubtype.create({ data: { categoryId, code, name, active } }); } await cacheInvalidatePattern('taxonomy:*'); await invalidateJoinedDisplayCaches(); // product+transfer lists embed subtype name await logActivity({ userId: session!.sub, action: id ? 'subtype.update' : 'subtype.create', detail: `${code} — ${name}` }); revalidatePath('/settings'); revalidatePath('/inventory'); revalidatePath('/categorization'); revalidatePath('/products'); revalidatePath('/transfer'); return { success: 'บันทึก subtype เรียบร้อย' }; } // ===== SYSTEM CONFIG ===== export async function updateSystemConfigAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const dailyRaw = String(fd.get('refundDailyBudgetTHB') ?? '').trim(); const perItemRaw = String(fd.get('refundPerItemCapTHB') ?? '').trim(); const daily = dailyRaw ? Number(dailyRaw) : NaN; const perItem = perItemRaw ? Number(perItemRaw) : NaN; if (!Number.isFinite(daily) || daily < 0) return { error: 'งบชดเชยรายวันต้องเป็นจำนวนบวก' }; if (!Number.isFinite(perItem) || perItem < 0) return { error: 'เพดานต่อรายการต้องเป็นจำนวนบวก' }; if (perItem > daily) return { error: 'เพดานต่อรายการต้องไม่เกินงบรายวัน' }; await setSystemConfig({ refundDailyBudgetTHB: daily, refundPerItemCapTHB: perItem, }); await logActivity({ userId: session!.sub, action: 'system-config.update', detail: `daily=${daily} perItem=${perItem}`, }); revalidatePath('/settings'); revalidatePath('/claims'); revalidatePath('/dashboard'); return { success: 'บันทึกค่าระบบเรียบร้อย' }; } // ===== ISSUE TYPE ===== export async function upsertIssueTypeAction(_p: ActionState, fd: FormData): Promise<ActionState> { const { error, session } = await gate(); if (error) return { error }; const id = str(fd.get('id')); const code = str(fd.get('code')).toUpperCase().replace(/\s+/g, '_'); const name = str(fd.get('name'), 128); const active = str(fd.get('active')) !== 'false'; if (!code) return { error: 'กรุณาระบุ Code' }; if (!name) return { error: 'กรุณาระบุชื่อ' }; if (id) { await prisma.issueType.update({ where: { id }, data: { code, name, active } }); } else { const dupe = await prisma.issueType.findUnique({ where: { code } }); if (dupe) return { error: 'Code นี้มีอยู่แล้ว' }; await prisma.issueType.create({ data: { code, name, active } }); } await cacheInvalidatePattern('taxonomy:*'); await cacheInvalidatePattern('claim:*'); // claim list embeds issueType.name await cacheInvalidatePattern('transfer:list:*'); // problem options menu await logActivity({ userId: session!.sub, action: id ? 'issueType.update' : 'issueType.create', detail: `${code} — ${name}` }); revalidatePath('/settings'); revalidatePath('/claims'); revalidatePath('/transfer'); return { success: 'บันทึก IssueType เรียบร้อย' }; } |