Spaces:
Sleeping
Sleeping
| // File: tasks/pricing/source.js | |
| // Source JavaScript code for the pricing migration task. | |
| const TAX_RATES_BPS = { | |
| "US-CA": 875, | |
| "US-TX": 625, | |
| "US-NY": 800, | |
| UK: 2000, | |
| }; | |
| function taxRateBps(regionId) { | |
| return TAX_RATES_BPS[regionId] ?? 0; | |
| } | |
| function subtotal(order) { | |
| return order.items.reduce( | |
| (total, item) => total + item.unitPrice * item.quantity, | |
| 0, | |
| ); | |
| } | |
| function couponDiscount(order) { | |
| const sub = subtotal(order); | |
| const rawDiscount = order.coupons.reduce( | |
| (total, coupon) => total + Math.floor((sub * coupon.discountPercent) / 100), | |
| 0, | |
| ); | |
| return Math.min(rawDiscount, Math.floor(sub / 2)); | |
| } | |
| function loyaltyDiscount(order) { | |
| const sub = subtotal(order); | |
| return Math.min(order.loyaltyPoints, Math.floor(sub / 10)); | |
| } | |
| function totalDiscount(order) { | |
| return couponDiscount(order) + loyaltyDiscount(order); | |
| } | |
| function tax(order) { | |
| const afterDiscount = subtotal(order) - totalDiscount(order); | |
| return Math.floor((afterDiscount * taxRateBps(order.regionId)) / 10000); | |
| } | |
| function finalPrice(order) { | |
| return subtotal(order) - totalDiscount(order) + tax(order); | |
| } | |
| module.exports = { | |
| subtotal, | |
| taxRateBps, | |
| couponDiscount, | |
| loyaltyDiscount, | |
| totalDiscount, | |
| tax, | |
| finalPrice, | |
| }; |