Skip to content

Commit

Permalink
feat(medusa,types,workflows,utils,product): PricingModule Integration…
Browse files Browse the repository at this point in the history
… of PriceLists into Core (#5536)
  • Loading branch information
pKorsholm authored Nov 21, 2023
1 parent 9c7ac73 commit dc5750d
Show file tree
Hide file tree
Showing 94 changed files with 5,693 additions and 876 deletions.
9 changes: 9 additions & 0 deletions .changeset/young-items-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@medusajs/workflows": patch
"@medusajs/product": patch
"@medusajs/medusa": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---

feat(medusa,types,workflows,utils,product): PricingModule Integration of PriceLists into Core
14 changes: 9 additions & 5 deletions integration-tests/environment-helpers/use-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const keepTables = [
"payment_provider",
"country",
"currency",
"migrations",
"mikro_orm_migrations",
]

const DbTestUtil = {
Expand All @@ -52,21 +54,23 @@ const DbTestUtil = {

teardown: async function ({ forceDelete } = {}) {
forceDelete = forceDelete || []
const entities = this.db_.entityMetadatas
const manager = this.db_.manager

await manager.query(`SET session_replication_role = 'replica';`)
const tableNames = await manager.query(`SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';`)

for (const entity of entities) {
for (const { table_name } of tableNames) {
if (
keepTables.includes(entity.tableName) &&
!forceDelete.includes(entity.tableName)
keepTables.includes(table_name) &&
!forceDelete.includes(table_name)
) {
continue
}

await manager.query(`DELETE
FROM "${entity.tableName}";`)
FROM "${table_name}";`)
}

await manager.query(`SET session_replication_role = 'origin';`)
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/factories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./simple-batch-job-factory"
export * from "./simple-cart-factory"
export * from "./simple-custom-shipping-option-factory"
export * from "./simple-customer-factory"
export * from "./simple-customer-group-factory"
export * from "./simple-discount-factory"
export * from "./simple-gift-card-factory"
export * from "./simple-line-item-factory"
Expand All @@ -22,5 +23,5 @@ export * from "./simple-shipping-method-factory"
export * from "./simple-shipping-option-factory"
export * from "./simple-shipping-profile-factory"
export * from "./simple-shipping-tax-rate-factory"
export * from "./simple-tax-rate-factory"
export * from "./simple-store-factory"
export * from "./simple-tax-rate-factory"
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import {
simpleProductFactory,
simpleRegionFactory,
} from "../../../../factories"

import {
IPricingModuleService,
PriceListStatus,
PriceListType,
} from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"

jest.setTimeout(50000)

const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}

const env = {
MEDUSA_FF_MEDUSA_V2: true,
}

describe("POST /admin/price-lists/:id/prices/batch", () => {
let dbConnection
let appContainer
let shutdownServer
let product
let variant
let pricingModuleService: IPricingModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
pricingModuleService = appContainer.resolve("pricingModuleService")
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
await createDefaultRuleTypes(appContainer)

await simpleRegionFactory(dbConnection, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})

product = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant",
variants: [
{
options: [{ option_id: "test-product-option-1", value: "test" }],
},
],
options: [
{
id: "test-product-option-1",
title: "Test option 1",
},
],
})

variant = product.variants[0]
})

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

it("should update money amounts if variant id is present in prices", async () => {
const [priceList] = await pricingModuleService.createPriceLists([
{
title: "test price list",
description: "test",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
},
])

await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
rules: {},
},
],
})

const api = useApi() as any
const data = {
prices: [
{
variant_id: variant.id,
amount: 5000,
currency_code: "usd",
},
],
}

await api.post(
`admin/price-lists/${priceList.id}/prices/batch`,
data,
adminHeaders
)

const response = await api.get(
`/admin/price-lists/${priceList.id}`,
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
name: "test price list",
description: "test",
type: "override",
status: "active",
starts_at: expect.any(String),
ends_at: expect.any(String),
customer_groups: [],
prices: [
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
currency_code: "usd",
amount: 5000,
min_quantity: null,
max_quantity: null,
price_list_id: expect.any(String),
region_id: null,
variant: expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
title: expect.any(String),
product_id: expect.any(String),
sku: null,
barcode: null,
ean: null,
upc: null,
variant_rank: 0,
inventory_quantity: 10,
allow_backorder: false,
manage_inventory: true,
hs_code: null,
origin_country: null,
mid_code: null,
material: null,
weight: null,
length: null,
height: null,
width: null,
metadata: null,
}),
variant_id: expect.any(String),
}),
],
})
)
})
})
Loading

0 comments on commit dc5750d

Please sign in to comment.