Fix: Add Customer id in generate line item for customer specific pricing (#1245)
* initial * add logged in item to cart gets correct price * customer id or cart_id customer id * move customer_id to a config * update unit test * run pipelines
This commit is contained in:
@@ -264,6 +264,44 @@ describe("/store/carts", () => {
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item to cart time customer pricing", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// customer with customer-group 5
|
||||
const authResponse = await api.post("/store/auth", {
|
||||
email: "test5@email.com",
|
||||
password: "test",
|
||||
})
|
||||
|
||||
const [authCookie] = authResponse.headers["set-cookie"][0].split(";")
|
||||
|
||||
// Add standard line item to cart
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart/line-items",
|
||||
{
|
||||
variant_id: "test-variant-sale-customer",
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
// withCredentials: true,
|
||||
headers: {
|
||||
Cookie: authCookie,
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart",
|
||||
unit_price: 700,
|
||||
variant_id: "test-variant-sale-customer",
|
||||
quantity: 1,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item with quantity to cart with quantity discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -72,6 +72,38 @@ module.exports = async (connection, data = {}) => {
|
||||
`UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'dk'`
|
||||
)
|
||||
|
||||
const customer5 = await manager.create(Customer, {
|
||||
id: "test-customer-5",
|
||||
email: "test5@email.com",
|
||||
first_name: "John",
|
||||
last_name: "Deere",
|
||||
password_hash:
|
||||
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
|
||||
has_account: true,
|
||||
})
|
||||
await manager.save(customer5)
|
||||
|
||||
const c_group_5 = await manager.create(CustomerGroup, {
|
||||
id: "test-group-5",
|
||||
name: "test-group-5",
|
||||
})
|
||||
await manager.save(c_group_5)
|
||||
|
||||
customer5.groups = [c_group_5]
|
||||
await manager.save(customer5)
|
||||
|
||||
const priceList_customer = await manager.create(PriceList, {
|
||||
id: "pl_customer",
|
||||
name: "VIP winter sale",
|
||||
description: "Winter sale for VIP customers.",
|
||||
type: "sale",
|
||||
status: "active",
|
||||
})
|
||||
|
||||
priceList_customer.customer_groups = [c_group_5]
|
||||
|
||||
await manager.save(priceList_customer)
|
||||
|
||||
const freeRule = manager.create(DiscountRule, {
|
||||
id: "free-shipping-rule",
|
||||
description: "Free shipping rule",
|
||||
@@ -390,6 +422,19 @@ module.exports = async (connection, data = {}) => {
|
||||
],
|
||||
})
|
||||
|
||||
await manager.insert(ProductVariant, {
|
||||
id: "test-variant-sale-customer",
|
||||
title: "test variant",
|
||||
product_id: "test-product",
|
||||
inventory_quantity: 1000,
|
||||
options: [
|
||||
{
|
||||
option_id: "test-option",
|
||||
value: "Size",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await manager.insert(ProductVariant, {
|
||||
id: "test-variant",
|
||||
title: "test variant",
|
||||
@@ -437,9 +482,31 @@ module.exports = async (connection, data = {}) => {
|
||||
amount: 800,
|
||||
price_list_id: "pl_current",
|
||||
})
|
||||
|
||||
await manager.save(ma_sale_1)
|
||||
|
||||
const ma_sale_customer = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant-sale-customer",
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
})
|
||||
await manager.save(ma_sale_customer)
|
||||
|
||||
const ma_sale_customer_1 = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant-sale-customer",
|
||||
currency_code: "usd",
|
||||
amount: 700,
|
||||
price_list_id: "pl_customer",
|
||||
})
|
||||
await manager.save(ma_sale_customer_1)
|
||||
|
||||
const ma_sale_customer_2 = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant-sale-customer",
|
||||
currency_code: "usd",
|
||||
amount: 800,
|
||||
price_list_id: "pl_current",
|
||||
})
|
||||
await manager.save(ma_sale_customer_2)
|
||||
|
||||
const ma_quantity = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant-quantity",
|
||||
currency_code: "usd",
|
||||
|
||||
@@ -101,12 +101,14 @@ describe("POST /store/carts", () => {
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
IdMap.getId("testRegion"),
|
||||
3
|
||||
3,
|
||||
{ customer_id: undefined }
|
||||
)
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant1"),
|
||||
IdMap.getId("testRegion"),
|
||||
1
|
||||
1,
|
||||
{ customer_id: undefined }
|
||||
)
|
||||
expect(CartServiceMock.addLineItem).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
@@ -129,7 +129,9 @@ export default async (req, res) => {
|
||||
validated.items.map(async (i) => {
|
||||
const lineItem = await lineItemService
|
||||
.withTransaction(manager)
|
||||
.generate(i.variant_id, regionId, i.quantity)
|
||||
.generate(i.variant_id, regionId, i.quantity, {
|
||||
customer_id: req.user?.customer_id,
|
||||
})
|
||||
await cartService
|
||||
.withTransaction(manager)
|
||||
.addLineItem(cart.id, lineItem)
|
||||
|
||||
@@ -30,6 +30,7 @@ import { validator } from "../../../../utils/validator"
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const customerId = req.user?.customer_id
|
||||
const validated = await validator(StorePostCartsCartLineItemsReq, req.body)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
@@ -43,6 +44,7 @@ export default async (req, res) => {
|
||||
const line = await lineItemService
|
||||
.withTransaction(m)
|
||||
.generate(validated.variant_id, cart.region_id, validated.quantity, {
|
||||
customer_id: customerId || cart.customer_id,
|
||||
metadata: validated.metadata,
|
||||
})
|
||||
await txCartService.addLineItem(id, line)
|
||||
|
||||
@@ -168,7 +168,7 @@ class LineItemService extends BaseService {
|
||||
.getRegionPrice(variant.id, {
|
||||
regionId: region.id,
|
||||
quantity: quantity,
|
||||
customer_id: undefined,
|
||||
customer_id: config.customer_id,
|
||||
include_discount_prices: true,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user