feat(order): tambah poin saat update

This commit is contained in:
Yoga Pangestu 2025-11-05 13:10:21 +07:00
parent ac59863be3
commit 464ce3df39

View File

@ -111,11 +111,15 @@ public function store()
// Calculate total (subtotal - manual discount - voucher discount)
$total = $afterManual - $voucherDiscount;
$member = User::whereHas('customer', fn ($q) => $q->where('id', $this->member_id))->first();
$pointsEarned = (int) floor($total / 5000);
// Wrap the entire operation in a database transaction
// Ensures atomicity — if any step fails, all changes are rolled back
DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $member) {
DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $pointsEarned) {
$member = User::whereHas('customer', fn ($q) => $q->where('id', $this->member_id))
->with(['membership' => fn ($q) => $q->lockForUpdate()])
->first();
$order = Order::create([
'outlet_id' => $this->outlet_id,
'user_id' => auth()->id(),
@ -174,6 +178,14 @@ public function store()
}
}
// Update member tier points
if ($member && $member->membership) {
$member->membership()->update([
'tier_points' => DB::raw("tier_points + $pointsEarned"),
'reward_points' => DB::raw("reward_points + $pointsEarned"),
'last_transaction_at' => now(),
]);
}
});
return [