feat(medusa): Allow to filter customer groups by discount condition id (#2346)
This commit is contained in:
committed by
GitHub
parent
19ca18e71c
commit
94c242f476
@@ -1,14 +1,29 @@
|
||||
const path = require("path")
|
||||
|
||||
const { IdMap } = require("medusa-test-utils")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { useDb, initDb } = require("../../../helpers/use-db")
|
||||
|
||||
const customerSeeder = require("../../helpers/customer-seeder")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const {
|
||||
DiscountRuleType,
|
||||
AllocationType,
|
||||
DiscountConditionType,
|
||||
DiscountConditionOperator,
|
||||
} = require("@medusajs/medusa")
|
||||
const { simpleDiscountFactory } = require("../../factories")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const adminReqConfig = {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
|
||||
describe("/admin/customer-groups", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
@@ -43,11 +58,11 @@ describe("/admin/customer-groups", () => {
|
||||
name: "test group",
|
||||
}
|
||||
|
||||
const response = await api.post("/admin/customer-groups", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const response = await api.post(
|
||||
"/admin/customer-groups",
|
||||
payload,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.customer_group).toEqual(
|
||||
@@ -66,11 +81,7 @@ describe("/admin/customer-groups", () => {
|
||||
}
|
||||
|
||||
await api
|
||||
.post("/admin/customer-groups", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.post("/admin/customer-groups", payload, adminReqConfig)
|
||||
.catch((err) => {
|
||||
expect(err.response.status).toEqual(422)
|
||||
expect(err.response.data.type).toEqual("duplicate_error")
|
||||
@@ -99,11 +110,10 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const id = "customer-group-1"
|
||||
|
||||
const deleteResponse = await api.delete(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const deleteResponse = await api.delete(
|
||||
`/admin/customer-groups/${id}`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(deleteResponse.data).toEqual({
|
||||
id: id,
|
||||
@@ -112,11 +122,7 @@ describe("/admin/customer-groups", () => {
|
||||
})
|
||||
|
||||
await api
|
||||
.get(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get(`/admin/customer-groups/${id}`, adminReqConfig)
|
||||
.catch((error) => {
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
@@ -134,11 +140,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const customerRes_preDeletion = await api.get(
|
||||
`/admin/customers/test-customer-delete-cg?expand=groups`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(customerRes_preDeletion.data.customer).toEqual(
|
||||
@@ -153,11 +155,7 @@ describe("/admin/customer-groups", () => {
|
||||
)
|
||||
|
||||
const deleteResponse = await api
|
||||
.delete(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.delete(`/admin/customer-groups/${id}`, adminReqConfig)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(deleteResponse.data).toEqual({
|
||||
@@ -168,11 +166,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const customerRes = await api.get(
|
||||
`/admin/customers/test-customer-delete-cg?expand=groups`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(customerRes.data.customer).toEqual(
|
||||
@@ -198,11 +192,7 @@ describe("/admin/customer-groups", () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/customer-groups/test-group-5/customers", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get("/admin/customer-groups/test-group-5/customers", adminReqConfig)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
@@ -246,11 +236,7 @@ describe("/admin/customer-groups", () => {
|
||||
const batchAddResponse = await api.post(
|
||||
"/admin/customer-groups/customer-group-1/customers/batch",
|
||||
payload,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(batchAddResponse.status).toEqual(200)
|
||||
@@ -262,9 +248,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
@@ -304,11 +288,7 @@ describe("/admin/customer-groups", () => {
|
||||
.post(
|
||||
"/admin/customer-groups/non-existing-customer-group-1/customers/batch",
|
||||
payload,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
.catch((err) => {
|
||||
expect(err.response.data.type).toEqual("not_found")
|
||||
@@ -332,11 +312,7 @@ describe("/admin/customer-groups", () => {
|
||||
.post(
|
||||
"/admin/customer-groups/customer-group-1/customers/batch",
|
||||
payload_1,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
@@ -355,11 +331,7 @@ describe("/admin/customer-groups", () => {
|
||||
.post(
|
||||
"/admin/customer-groups/customer-group-1/customers/batch",
|
||||
payload_2,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
.catch((err) => {
|
||||
expect(err.response.data.type).toEqual("not_found")
|
||||
@@ -371,9 +343,7 @@ describe("/admin/customer-groups", () => {
|
||||
// check that customer-1 is only added once and that customer-2 is added correctly
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
@@ -419,11 +389,11 @@ describe("/admin/customer-groups", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const response = await api.post(`/admin/customer-groups/${id}`, body, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const response = await api.post(
|
||||
`/admin/customer-groups/${id}`,
|
||||
body,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.customer_group).toEqual(
|
||||
@@ -454,11 +424,11 @@ describe("/admin/customer-groups", () => {
|
||||
}
|
||||
|
||||
const response = await api
|
||||
.post(`/admin/customer-groups/${id}?expand=customers`, body, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.post(
|
||||
`/admin/customer-groups/${id}?expand=customers`,
|
||||
body,
|
||||
adminReqConfig
|
||||
)
|
||||
.catch(console.log)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -490,11 +460,7 @@ describe("/admin/customer-groups", () => {
|
||||
const response = await api
|
||||
.get(
|
||||
`/admin/customer-groups?limit=5&offset=2&expand=customers&order=created_at`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
.catch(console.log)
|
||||
|
||||
@@ -510,11 +476,10 @@ describe("/admin/customer-groups", () => {
|
||||
it("retreive a list of customer groups filtered by name using `q` param", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/admin/customer-groups?q=vip-customers`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const response = await api.get(
|
||||
`/admin/customer-groups?q=vip-customers`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
@@ -525,6 +490,90 @@ describe("/admin/customer-groups", () => {
|
||||
)
|
||||
expect(response.data.customer_groups[0]).not.toHaveProperty("customers")
|
||||
})
|
||||
|
||||
it("lists customers in group filtered by discount condition id and count", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const resCustomerGroup = await api.get(
|
||||
"/admin/customer-groups",
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
const customerGroup1 = resCustomerGroup.data.customer_groups[0]
|
||||
const customerGroup2 = resCustomerGroup.data.customer_groups[2]
|
||||
|
||||
const buildDiscountData = (code, conditionId, groups) => {
|
||||
return {
|
||||
code,
|
||||
rule: {
|
||||
type: DiscountRuleType.PERCENTAGE,
|
||||
value: 10,
|
||||
allocation: AllocationType.TOTAL,
|
||||
conditions: [
|
||||
{
|
||||
id: conditionId,
|
||||
type: DiscountConditionType.CUSTOMER_GROUPS,
|
||||
operator: DiscountConditionOperator.IN,
|
||||
customer_groups: groups,
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const discountConditionId = IdMap.getId(
|
||||
"discount-condition-customer-group-1"
|
||||
)
|
||||
await simpleDiscountFactory(
|
||||
dbConnection,
|
||||
buildDiscountData("code-1", discountConditionId, [customerGroup1.id])
|
||||
)
|
||||
|
||||
const discountConditionId2 = IdMap.getId(
|
||||
"discount-condition-customer-group-2"
|
||||
)
|
||||
await simpleDiscountFactory(
|
||||
dbConnection,
|
||||
buildDiscountData("code-2", discountConditionId2, [customerGroup2.id])
|
||||
)
|
||||
|
||||
let res = await api.get(
|
||||
`/admin/customer-groups?discount_condition_id=${discountConditionId}`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.customer_groups).toHaveLength(1)
|
||||
expect(res.data.customer_groups).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ id: customerGroup1.id }),
|
||||
])
|
||||
)
|
||||
|
||||
res = await api.get(
|
||||
`/admin/customer-groups?discount_condition_id=${discountConditionId2}`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.customer_groups).toHaveLength(1)
|
||||
expect(res.data.customer_groups).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ id: customerGroup2.id }),
|
||||
])
|
||||
)
|
||||
|
||||
res = await api.get(`/admin/customer-groups`, adminReqConfig)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.customer_groups).toHaveLength(7)
|
||||
expect(res.data.customer_groups).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ id: customerGroup1.id }),
|
||||
expect.objectContaining({ id: customerGroup2.id }),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/customer-groups/:id", () => {
|
||||
@@ -543,11 +592,10 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const id = "customer-group-1"
|
||||
|
||||
const response = await api.get(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const response = await api.get(
|
||||
`/admin/customer-groups/${id}`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.customer_group).toEqual(
|
||||
@@ -566,11 +614,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/customer-groups/${id}?expand=customers`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -589,11 +633,7 @@ describe("/admin/customer-groups", () => {
|
||||
const id = "test-group-000"
|
||||
|
||||
await api
|
||||
.get(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get(`/admin/customer-groups/${id}`, adminReqConfig)
|
||||
.catch((err) => {
|
||||
expect(err.response.status).toEqual(404)
|
||||
expect(err.response.data.type).toEqual("not_found")
|
||||
@@ -624,9 +664,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const batchAddResponse = await api
|
||||
.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
...adminReqConfig,
|
||||
data: payload,
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
@@ -641,9 +679,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
@@ -669,9 +705,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const batchAddResponse = await api
|
||||
.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
...adminReqConfig,
|
||||
data: payload,
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
@@ -686,9 +720,7 @@ describe("/admin/customer-groups", () => {
|
||||
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers/test-customer-7?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customer).toEqual(
|
||||
@@ -714,17 +746,13 @@ describe("/admin/customer-groups", () => {
|
||||
}
|
||||
|
||||
await api.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
...adminReqConfig,
|
||||
data: payload,
|
||||
})
|
||||
|
||||
// check that customer-1 is only added once and that customer-2 is added correctly
|
||||
const getCustomerResponse = await api
|
||||
.get("/admin/customers?expand=groups", {
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
})
|
||||
.get("/admin/customers?expand=groups", adminReqConfig)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
@@ -756,18 +784,14 @@ describe("/admin/customer-groups", () => {
|
||||
}
|
||||
|
||||
await api.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
...adminReqConfig,
|
||||
data: payload,
|
||||
})
|
||||
|
||||
const idempotentRes = await api.delete(
|
||||
"/admin/customer-groups/test-group-5/customers/batch",
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
...adminReqConfig,
|
||||
data: payload,
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user