fix: code review fixes + unit/API tests for repair-POS integration
Code review fixes: - Wrap createFromRepairTicket() in DB transaction for atomicity - Wrap complete() inventory + status updates in DB transaction - Repair ticket status update now atomic with transaction completion - Add Zod validation on from-repair route body - Fix requiresDiscountOverride: threshold and manual_discount are independent checks - Order discount distributes proportionally across line items (not first-only) - Extract shared receipt calculations into useReceiptData/useBarcode hooks - Add error handling for barcode generation Tests: - Unit: consumable tax category mapping, exempt rate short-circuit - API: ready-for-pickup listing + search, from-repair transaction creation, consumable exclusion from line items, tax rate verification (labor=service, part=goods), duplicate prevention, ticket auto-pickup on payment completion, isConsumable product filter Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -26,6 +26,7 @@ export function POSCartPanel({ transaction }: POSCartPanelProps) {
|
||||
const [overrideOpen, setOverrideOpen] = useState(false)
|
||||
const [priceItemId, setPriceItemId] = useState<string | null>(null)
|
||||
const [pendingDiscount, setPendingDiscount] = useState<{ lineItemId: string; amount: number; reason: string } | null>(null)
|
||||
const [pendingOrderDiscount, setPendingOrderDiscount] = useState<{ amount: number; reason: string } | null>(null)
|
||||
const [discountOverrideOpen, setDiscountOverrideOpen] = useState(false)
|
||||
const lineItems = transaction?.lineItems ?? []
|
||||
|
||||
@@ -52,6 +53,28 @@ export function POSCartPanel({ transaction }: POSCartPanelProps) {
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const orderDiscountMutation = useMutation({
|
||||
mutationFn: async ({ amount, reason }: { amount: number; reason: string }) => {
|
||||
// Distribute discount proportionally across all line items
|
||||
let remaining = amount
|
||||
for (let i = 0; i < lineItems.length; i++) {
|
||||
const item = lineItems[i]
|
||||
const itemTotal = parseFloat(item.unitPrice) * item.qty
|
||||
const isLast = i === lineItems.length - 1
|
||||
const share = isLast ? remaining : Math.round((itemTotal / subtotal) * amount * 100) / 100
|
||||
remaining -= share
|
||||
if (share > 0) {
|
||||
await posMutations.applyDiscount(currentTransactionId!, { lineItemId: item.id, amount: share, reason })
|
||||
}
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: posKeys.transaction(currentTransactionId!) })
|
||||
toast.success('Order discount applied')
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const voidMutation = useMutation({
|
||||
mutationFn: () => posMutations.void(currentTransactionId!),
|
||||
onSuccess: () => {
|
||||
@@ -204,13 +227,13 @@ export function POSCartPanel({ transaction }: POSCartPanelProps) {
|
||||
onApply={(amount, reason) => {
|
||||
const pct = subtotal > 0 ? (amount / subtotal) * 100 : 0
|
||||
if (requiresDiscountOverride(pct)) {
|
||||
setPendingDiscount({ lineItemId: lineItems[0].id, amount, reason })
|
||||
setPendingOrderDiscount({ amount, reason })
|
||||
setDiscountOverrideOpen(true)
|
||||
} else {
|
||||
discountMutation.mutate({ lineItemId: lineItems[0].id, amount, reason })
|
||||
orderDiscountMutation.mutate({ amount, reason })
|
||||
}
|
||||
}}
|
||||
isPending={discountMutation.isPending}
|
||||
isPending={orderDiscountMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
@@ -296,6 +319,9 @@ export function POSCartPanel({ transaction }: POSCartPanelProps) {
|
||||
if (pendingDiscount) {
|
||||
discountMutation.mutate(pendingDiscount)
|
||||
setPendingDiscount(null)
|
||||
} else if (pendingOrderDiscount) {
|
||||
orderDiscountMutation.mutate(pendingOrderDiscount)
|
||||
setPendingOrderDiscount(null)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user