feat(medusa, admin-ui): Improve gift card application (#4944)

Fix for the problems identified in issue #4892 

Bugfix: admin-ui order summary no longer uses gift card total from order when displaying how much has been withdrawn from each giftcard.

Bugfix(?): no longer keep applying gift cards (at 0 value) when sufficient balance has been reached

Feature: multiple giftcards are now applied in ordered fashion. First by end_date (supports null), then by remaining balance. In order to ensure that customers ends up with as long lasting and few remaining gift cards as possible after the transaction.
This commit is contained in:
mortenengel authored and GitHub committed 2023-09-07 16:56:36 +00:00
1 parent 9f16c90f99
commit 11fb523051
4 files changed
+199 -6

No files matched your search

@@ -221,20 +221,20 @@ const SummaryCard: React.FC<SummaryCardProps> = ({ order, reservations }) => {
}
/>
))}
{order?.gift_cards?.map((giftCard, index) => (
{order?.gift_card_transactions?.map((gcTransaction, index) => (
<DisplayTotal
key={index}
currency={order.currency_code}
totalAmount={-1 * order.gift_card_total}
totalAmount={-1 * gcTransaction.amount}
totalTitle={
<div className="inter-small-regular text-grey-90 flex items-center">
Gift card:
<Badge className="ml-3" variant="default">
{giftCard.code}
{gcTransaction.gift_card.code}
</Badge>
<div className="ml-2">
<CopyToClipboard
value={giftCard.code}
value={gcTransaction.gift_card.code}
showValue={false}
iconSize={16}
/>
+10 -1
View File
@@ -733,7 +733,13 @@ class OrderService extends TransactionBaseService {
let giftCardableAmountBalance = giftCardableAmount
const giftCardService = this.giftCardService_.withTransaction(manager)
for (const giftCard of cart.gift_cards) {
//Order the gift cards by first ends_at date, then remaining amount. To ensure largest possible amount left, for longest possible time.
const orderedGiftCards = cart.gift_cards.sort((a, b) => {
let aEnd = a.ends_at ?? new Date(2100, 1, 1)
let bEnd = b.ends_at ?? new Date(2100, 1, 1)
return aEnd.getTime() - bEnd.getTime() || a.balance - b.balance
})
for (const giftCard of orderedGiftCards) {
const newGiftCardBalance = Math.max(
0,
giftCard.balance - giftCardableAmountBalance
@@ -755,6 +761,9 @@ class OrderService extends TransactionBaseService {
giftCardableAmountBalance =
giftCardableAmountBalance - giftCardBalanceUsed
if (giftCardableAmountBalance == 0)
break;
}
const shippingOptionServiceTx =