feat: update customer groups (#1075)

* update customer service prep

* expand on the update customer groups test case

* add the test case for customer group update

* docs for `groups` prop on update customer endpoint

* refactor, update comments

* expend on integration tests, add customer mock with a group

* refactor to use `customerGroupService.list` method

* update units

* remove `updateCustomerGroups`

* fix rebase conflict

* fix customer seed data, add JoinTable on groups

* group retrieval using the expand param

* fix: use `buildQuery_`

* fix: validation for `groups`, enable `expand`param on update customer endpoint

* fix: remove fileds form the `FilterableCustomerGroupProps`

* fix: spearate body/query validation

Co-authored-by: fPolic <frane@medusajs.com>
This commit is contained in:
Frane Polić
2022-02-21 16:24:38 +01:00
committed by GitHub
parent c2241d1101
commit 75fb2ce9c3
9 changed files with 286 additions and 19 deletions

View File

@@ -62,6 +62,7 @@ Object {
"deleted_at": null,
"email": "test1@email.com",
"first_name": null,
"groups": Array [],
"has_account": false,
"id": "test-customer-1",
"last_name": null,

View File

@@ -56,7 +56,7 @@ describe("/admin/customers", () => {
})
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(4)
expect(response.data.count).toEqual(5)
expect(response.data.customers).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -228,6 +228,106 @@ describe("/admin/customers", () => {
})
)
})
it("Correctly updates customer groups", async () => {
const api = useApi()
let response = await api
.post(
"/admin/customers/test-customer-3?expand=groups",
{
groups: [{ id: "test-group-4" }],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer.groups).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: "test-group-4", name: "test-group-4" }),
])
)
// Try adding a non existing group
response = await api
.post(
"/admin/customers/test-customer-3?expand=groups",
{
groups: [{ id: "test-group-4" }, { id: "fake-group-0" }],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer.groups.length).toEqual(1)
expect(response.data.customer.groups).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: "test-group-4", name: "test-group-4" }),
])
)
// Delete all groups
response = await api
.post(
"/admin/customers/test-customer-3?expand=groups",
{
groups: [],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer.groups.length).toEqual(0)
// Adding a group to a customer with already existing groups.
response = await api
.post(
"/admin/customers/test-customer-5?expand=groups",
{
groups: [{ id: "test-group-5" }, { id: "test-group-4" }],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer.groups.length).toEqual(2)
expect(response.data.customer.groups).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: "test-group-4", name: "test-group-4" }),
expect.objectContaining({ id: "test-group-5", name: "test-group-5" }),
])
)
})
})
describe("GET /admin/customers/:id", () => {
@@ -278,7 +378,7 @@ describe("/admin/customers", () => {
const api = useApi()
const response = await api
.get("/admin/customers/test-customer-1?expand=billing_address", {
.get("/admin/customers/test-customer-1?expand=billing_address,groups", {
headers: {
Authorization: "Bearer test_token",
},
@@ -295,6 +395,7 @@ describe("/admin/customers", () => {
created_at: expect.any(String),
updated_at: expect.any(String),
},
groups: [],
created_at: expect.any(String),
updated_at: expect.any(String),
})