Feat: Filter price lists by customer group (#1431)

* add customer groups to price list factory

* add integration test for filtering price lists by customer group

* normalize list price list query

* add customer groups to list-price-list queryparameters

* query based on customergroups if they exist for price lists

* remove verbose flag

* add another price list with a customer group

* remove console.log

* pr feedback

* add query type to repository

* add query type to repository

* set groups to undefined instead of deleting parameter

* remove wildcard destructing

* make buildQuery type specific to price lists

* steal Adriens types

* fix(medusa): support searching for price lists (#1407)

* delete instead of settting groups to undefined

* add groups to query with q

* use simple customer group factory instead of manual creation

* Update simple-customer-group-factory.ts

* remove comma that breaks integration-tests

Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2022-05-12 04:22:46 +02:00
committed by GitHub
co-authored by Zakaria El Asri
parent 0b2a3a0f0e
commit a69b52e031
7 changed files with 129 additions and 44 deletions
@@ -1,3 +1,4 @@
const { PriceList, CustomerGroup } = require("@medusajs/medusa")
const path = require("path")
const setupServer = require("../../../helpers/setup-server")
@@ -8,6 +9,9 @@ const {
simpleProductFactory,
simplePriceListFactory,
} = require("../../factories")
const {
simpleCustomerGroupFactory,
} = require("../../factories/simple-customer-group-factory")
const adminSeeder = require("../../helpers/admin-seeder")
const customerSeeder = require("../../helpers/customer-seeder")
const priceListSeeder = require("../../helpers/price-list-seeder")
@@ -322,6 +326,48 @@ describe("/admin/price-lists", () => {
)
expect(response.data.count).toEqual(1)
})
it("lists only price lists with customer_group", async () => {
await customerSeeder(dbConnection)
await simplePriceListFactory(dbConnection, {
id: "test-list-cgroup-1",
customer_groups: ["customer-group-1"],
})
await simplePriceListFactory(dbConnection, {
id: "test-list-cgroup-2",
customer_groups: ["customer-group-2"],
})
await simplePriceListFactory(dbConnection, {
id: "test-list-cgroup-3",
customer_groups: ["customer-group-3"],
})
await simplePriceListFactory(dbConnection, {
id: "test-list-no-cgroup",
})
const api = useApi()
const response = await api
.get(
`/admin/price-lists?customer_groups[]=customer-group-1,customer-group-2`,
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.warn(err.response.data)
})
expect(response.status).toEqual(200)
expect(response.data.price_lists.length).toEqual(2)
expect(response.data.price_lists).toEqual([
expect.objectContaining({ id: "test-list-cgroup-1" }),
expect.objectContaining({ id: "test-list-cgroup-2" }),
])
})
})
describe("POST /admin/price-lists/:id", () => {
@@ -22,7 +22,7 @@ export const simpleCustomerGroupFactory = async (
data.id || `simple-customer-group-${Math.random() * 1000}`
const c = manager.create(CustomerGroup, {
id: customerGroupId,
name: data.name,
name: data.name || faker.company.companyName(),
})
const group = await manager.save(c)
@@ -7,6 +7,7 @@ import {
} from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
import { simpleCustomerGroupFactory } from "./simple-customer-group-factory"
type ProductListPrice = {
variant_id: string
@@ -42,22 +43,10 @@ export const simplePriceListFactory = async (
let customerGroups = []
if (typeof data.customer_groups !== "undefined") {
await manager
.createQueryBuilder()
.insert()
.into(CustomerGroup)
.values(
data.customer_groups.map((group) => ({
id: group,
name: faker.company.companyName(),
}))
customerGroups = await Promise.all(
data.customer_groups.map((group) =>
simpleCustomerGroupFactory(connection, { id: group })
)
.orIgnore()
.execute()
customerGroups = await manager.findByIds(
CustomerGroup,
data.customer_groups
)
}