Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa, medusa-js): Use price selection strategy for GET /admin/variants #2270

Merged
merged 14 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/few-maps-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/medusa-js": patch
---

Adds the use of price selection strategy to retrieving variants in the admin API. This moves the responsibility of tax calculations from the frontend (admin) to the backend.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/admin/products GET /admin/variants price selection strategy selects prices based on the passed currency code 1`] = `
Object {
"count": 1,
"limit": 20,
"offset": 0,
"variants": Array [
Object {
"allow_backorder": false,
"barcode": "test-barcode",
"calculated_price": 80,
"calculated_price_incl_tax": null,
"calculated_price_type": "sale",
"calculated_tax": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean",
"height": null,
"hs_code": null,
"id": "test-variant",
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant",
"variant_id": "test-variant",
},
],
"origin_country": null,
"original_price": 100,
"original_price_incl_tax": null,
"original_tax": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": "test-price",
"max_quantity": null,
"min_quantity": null,
"price_list": null,
"price_list_id": null,
"region_id": null,
"updated_at": Any<String>,
"variant_id": "test-variant",
},
Object {
"amount": 80,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": "test-price-discount",
"max_quantity": null,
"min_quantity": null,
"price_list": Object {
"created_at": Any<String>,
"deleted_at": null,
"description": "Winter sale for VIP customers.",
"ends_at": null,
"id": "pl",
"name": "VIP winter sale",
"starts_at": null,
"status": "active",
"type": "sale",
"updated_at": Any<String>,
},
"price_list_id": "pl",
"region_id": null,
"updated_at": Any<String>,
"variant_id": "test-variant",
},
],
"product": Any<Object>,
"product_id": "test-product",
"sku": "test-sku",
"tax_rates": null,
"title": "Test variant",
"upc": "test-upc",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
}
`;
122 changes: 122 additions & 0 deletions integration-tests/api/__tests__/admin/variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const path = require("path")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const { simpleProductFactory } = require("../../factories")

const adminSeeder = require("../../helpers/admin-seeder")
const productSeeder = require("../../helpers/product-seeder")
const storeProductSeeder = require("../../helpers/store-product-seeder")

jest.setTimeout(30000)

Expand Down Expand Up @@ -145,4 +147,124 @@ describe("/admin/products", () => {
)
})
})

describe("GET /admin/variants price selection strategy", () => {
beforeEach(async () => {
await storeProductSeeder(dbConnection)
await adminSeeder(dbConnection)

await simpleProductFactory(
dbConnection,
{
title: "prod",
variants: [
{
title: "test1",
inventory_quantity: 10,
},
{
title: "test2",
inventory_quantity: 12,
},
],
},
100
)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("selects prices based on the passed currency code", async () => {
kasperkristensen marked this conversation as resolved.
Show resolved Hide resolved
const api = useApi()

const response = await api.get(
"/admin/variants?id=test-variant&currency_code=usd",
{
headers: {
Authorization: "Bearer test_token",
},
}
)

console.warn("response", response.data.variants)
kasperkristensen marked this conversation as resolved.
Show resolved Hide resolved

expect(response.data).toMatchSnapshot({
variants: [
{
allow_backorder: false,
barcode: "test-barcode",
created_at: expect.any(String),
deleted_at: null,
ean: "test-ean",
height: null,
hs_code: null,
id: "test-variant",
inventory_quantity: 10,
length: null,
manage_inventory: true,
material: null,
metadata: null,
mid_code: null,
origin_country: null,
product_id: "test-product",
sku: "test-sku",
title: "Test variant",
upc: "test-upc",
updated_at: expect.any(String),
weight: null,
width: null,
options: [
{
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
prices: [
{
created_at: expect.any(String),
updated_at: expect.any(String),
amount: 100,
currency_code: "usd",
deleted_at: null,
id: "test-price",
region_id: null,
min_quantity: null,
max_quantity: null,
price_list_id: null,
variant_id: "test-variant",
},
{
created_at: expect.any(String),
updated_at: expect.any(String),
amount: 80,
currency_code: "usd",
deleted_at: null,
price_list_id: "pl",
id: "test-price-discount",
region_id: null,
variant_id: "test-variant",
price_list: {
created_at: expect.any(String),
updated_at: expect.any(String),
id: "pl",
},
},
],
product: expect.any(Object),
original_price: 100,
calculated_price: 80,
calculated_price_type: "sale",
original_price_incl_tax: null,
calculated_price_incl_tax: null,
original_tax: null,
calculated_tax: null,
tax_rates: null,
},
],
})
})
})
})
6 changes: 3 additions & 3 deletions integration-tests/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.3.7-dev-1662369149992",
"@medusajs/medusa": "1.4.1-dev-1664382371714",
"faker": "^5.5.3",
"medusa-interfaces": "1.3.3-dev-1662369149992",
"medusa-interfaces": "1.3.3-dev-1664382371714",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"babel-preset-medusa-package": "1.1.19-dev-1662369149992",
"babel-preset-medusa-package": "1.1.19-dev-1664382371714",
"jest": "^26.6.3"
}
}
Loading