feat: line item adjustments (#1319)

* add: crud services + model + totals

* fix: enforce unique constraint on line item adjustment model and update service (#1241)

* add: unique constraint on model + fix service

* fix: unique constraint

* fix: add cascade on delete + fix discount relation

* fix: remove optional unique prop

* add: tests for ensuring line item adjustment db constraints (#1279)

* add: tests for ensuring db constraints

* fix: use given when then

* feat: adjust cart to include line item adjustments (#1242)

* fix: cart service + cart tests

* fix: remaining tests

* fix: swap tests

* fix: add relationship + fix oas

* refactor: applyDiscount

* fix: refactor applyDiscount and fix + add unit tests

* fix: plugins tests

* feat: line item adjustments draft orders (#1243)

* fix: draft order tests

* fix: constraint

* fix: wrong variable name

* fix: unique constraint

* progress: add tests

* fix: add cascade on delete + fix discount relation

* fix: remove optional unique prop

* fix: cart removeLineItem + tests

* fix: cart unit tests

* fix: update snapshot

* remove: verbose option

* rename arg

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

* add: create adjustments for swap additional_items

* add: create adjustments for return lines

* fix: unit test for creating adjustment for additional_items

* fix: create adjustments only for non return items + no deletion when item is a return item

* add: integration tests

* refactor: use refreshAdjustments method

* refactor test

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
Zakaria El Asri
2022-04-12 15:49:31 +02:00
committed by GitHub
co-authored by Sebastian Rindom
parent 607a382b4e
commit 1cfeb5dbd8
45 changed files with 3290 additions and 581 deletions
@@ -144,6 +144,69 @@ module.exports = async (connection, data = {}) => {
tenPercent.rule = tenPercentRule
await manager.save(tenPercent)
const totalFixed100Rule = manager.create(DiscountRule, {
id: "total-fixed-100-rule",
description: "Fixed 100 total",
type: "fixed",
value: 100,
allocation: "total",
})
const totalFixed100 = manager.create(Discount, {
id: "total-fixed-100",
code: "FIXED100",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
totalFixed100.regions = [r]
totalFixed100.rule = totalFixed100Rule
await manager.save(totalFixed100)
const itemFixed200Rule = manager.create(DiscountRule, {
id: "item-fixed-200-rule",
description: "Item 200 fixed",
type: "fixed",
value: 200,
allocation: "item",
})
const itemFixed200 = manager.create(Discount, {
id: "item-fixed-200",
code: "FIXED200",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
itemFixed200.regions = [r]
itemFixed200.rule = itemFixed200Rule
await manager.save(itemFixed200)
const itemPerc15Rule = manager.create(DiscountRule, {
id: "item-percentage-15-rule",
description: "Item 15 percentage",
type: "percentage",
value: 15,
allocation: "item",
})
const itemPerc15 = manager.create(Discount, {
id: "item-percentage-15",
code: "15PERCENT",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
itemPerc15.regions = [r]
itemPerc15.rule = itemPerc15Rule
await manager.save(itemPerc15)
const dUsageLimit = await manager.create(Discount, {
id: "test-discount-usage-limit",
code: "SPENT",
@@ -570,6 +633,74 @@ module.exports = async (connection, data = {}) => {
await manager.save(cart)
const cartWithTotalFixedDiscount = manager.create(Cart, {
id: "test-cart-w-total-fixed-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [totalFixed100],
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithTotalFixedDiscount)
const cartWithItemFixedDiscount = manager.create(Cart, {
id: "test-cart-w-item-fixed-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [itemFixed200],
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithItemFixedDiscount)
const cartWithTotalPercDiscount = manager.create(Cart, {
id: "test-cart-w-total-percentage-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [tenPercent],
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithTotalPercDiscount)
const cartWithItemPercDiscount = manager.create(Cart, {
id: "test-cart-w-item-percentage-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [itemPerc15],
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithItemPercDiscount)
const cart2 = manager.create(Cart, {
id: "test-cart-2",
customer_id: "some-customer",
@@ -173,6 +173,14 @@ module.exports = async (connection, data = {}) => {
quantity: 1,
variant_id: "test-variant",
order_id: "test-order",
adjustments: [
{
amount: 800,
discount_id: "test-discount",
description: "discount",
item_id: "test-item",
},
],
})
await manager.save(li)
+10 -1
View File
@@ -349,7 +349,7 @@ const createSwap = async (options, manager) => {
is_disabled: false,
rule: dRule,
})
await manager.save(discount)
let discountDb = await manager.save(discount)
const cart = manager.create(Cart, {
id: `${swapId}-cart`,
@@ -396,6 +396,14 @@ const createSwap = async (options, manager) => {
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
adjustments: [
{
amount: -800,
description: "discount",
discount_id: discountDb.id,
item_id: `${swapId}-return-item-1`,
},
],
variant_id: "test-variant",
order_id: "order-with-swap",
cart_id: cart.id,
@@ -419,6 +427,7 @@ const createSwap = async (options, manager) => {
const return_item1 = manager.create(LineItem, {
...li,
is_return: true,
unit_price: -1 * li.unit_price,
})