fix(wallet, pet-profile): ensure wallet balance deduction and map petId in order history sync

This commit is contained in:
parsa aghaei committed 2026-07-25 12:14:08 +03:30
1 parent 6d6824c3ca
commit 38e86d755b
2 files changed
+22 -19

No files matched your search

@@ -78,25 +78,26 @@ export default function CheckoutPage() {
const activePet = getActivePet();
try {
// 1. If using wallet, deduct available balance (up to totalAmount)
let walletDeducted = 0;
if (paymentMethod === 'wallet' && profile.walletBalance > 0) {
walletDeducted = Math.min(profile.walletBalance, totalAmount);
const newBalance = profile.walletBalance - walletDeducted;
const newTransaction = {
id: `TR-${Math.floor(Math.random() * 90000) + 10000}`,
type: 'purchase' as const,
amount: walletDeducted,
date: new Date().toISOString(),
status: 'success' as const
};
useUserStore.setState((state) => ({
profile: {
...state.profile,
walletBalance: newBalance,
transactions: [newTransaction, ...(state.profile.transactions || [])]
}
}));
// 1. Deduct wallet balance if payment method is wallet
if (paymentMethod === 'wallet') {
const walletDeducted = Math.min(profile.walletBalance || 0, totalAmount);
if (walletDeducted > 0) {
const newBalance = (profile.walletBalance || 0) - walletDeducted;
const newTransaction = {
id: `TR-${Math.floor(Math.random() * 90000) + 10000}`,
type: 'purchase' as const,
amount: walletDeducted,
date: new Date().toISOString(),
status: 'success' as const
};
useUserStore.setState((state) => ({
profile: {
...state.profile,
walletBalance: newBalance,
transactions: [newTransaction, ...(state.profile.transactions || [])]
}
}));
}
}
// 2. Register the order in the backend
@@ -143,6 +143,8 @@ export const useCartStore = create<CartStore>()(
total: Number(bo.totalAmount),
charityDonation: Number(bo.charityDonation),
status: bo.status,
petId: bo.petId || bo.pet?.id,
shippingAddress: bo.shippingAddress,
trackingNumber: bo.trackingNumber || `CN-${Math.floor(Math.random() * 90000) + 10000}`
}));
set({ orders: mappedOrders });