Feat: bulk delete customers from customer group (#1097)
* integration testing * customer seeder * initial bulk removal * integraiton testing of deletes * delete fix * not found test * remove unused code * Apply suggestions from code review Co-authored-by: Sebastian Rindom <skrindom@gmail.com> * update integration tests * pr review fixes * update migration * formatting * integration tests for deletion * pr feedback * fix failing integration tests * remove integration tests before merging Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
@@ -290,7 +290,6 @@ describe("/admin/customer-groups", () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("gets customer group", async () => {
|
||||
const api = useApi()
|
||||
|
||||
@@ -358,4 +357,187 @@ describe("/admin/customer-groups", () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/customer-groups/{id}/batch", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
await customerSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("removes multiple customers from a group", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
customer_ids: [{ id: "test-customer-5" }, { id: "test-customer-6" }],
|
||||
}
|
||||
|
||||
const batchAddResponse = await api
|
||||
.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
data: payload,
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(batchAddResponse.status).toEqual(200)
|
||||
expect(batchAddResponse.data).toEqual({
|
||||
customer_group: expect.objectContaining({
|
||||
id: "test-group-5",
|
||||
name: "test-group-5",
|
||||
}),
|
||||
})
|
||||
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "test-customer-5",
|
||||
groups: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-customer-6",
|
||||
groups: [],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("removes customers from only one group", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
customer_ids: [{ id: "test-customer-7" }],
|
||||
}
|
||||
|
||||
const batchAddResponse = await api
|
||||
.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
data: payload,
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(batchAddResponse.status).toEqual(200)
|
||||
expect(batchAddResponse.data).toEqual({
|
||||
customer_group: expect.objectContaining({
|
||||
id: "test-group-5",
|
||||
name: "test-group-5",
|
||||
}),
|
||||
})
|
||||
|
||||
const getCustomerResponse = await api.get(
|
||||
"/admin/customers/test-customer-7?expand=groups",
|
||||
{
|
||||
headers: { Authorization: "Bearer test_token" },
|
||||
}
|
||||
)
|
||||
|
||||
expect(getCustomerResponse.data.customer).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-customer-7",
|
||||
groups: [
|
||||
expect.objectContaining({
|
||||
id: "test-group-6",
|
||||
name: "test-group-6",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("removes only select customers from a group", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// re-adding customer-1 to the customer group along with new addintion:
|
||||
// customer-2 and some non-existing customers should cause the request to fail
|
||||
const payload = {
|
||||
customer_ids: [{ id: "test-customer-5" }],
|
||||
}
|
||||
|
||||
await api.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
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" },
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(getCustomerResponse.data.customers).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "test-customer-5",
|
||||
groups: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-customer-6",
|
||||
groups: [
|
||||
expect.objectContaining({
|
||||
name: "test-group-5",
|
||||
id: "test-group-5",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("removes customers from a group idempotently", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// re-adding customer-1 to the customer group along with new addintion:
|
||||
// customer-2 and some non-existing customers should cause the request to fail
|
||||
const payload = {
|
||||
customer_ids: [{ id: "test-customer-5" }],
|
||||
}
|
||||
|
||||
await api.delete("/admin/customer-groups/test-group-5/customers/batch", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
data: payload,
|
||||
})
|
||||
|
||||
const idempotentRes = await api.delete(
|
||||
"/admin/customer-groups/test-group-5/customers/batch",
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
data: payload,
|
||||
}
|
||||
)
|
||||
|
||||
expect(idempotentRes.status).toEqual(200)
|
||||
expect(idempotentRes.data).toEqual({
|
||||
customer_group: expect.objectContaining({
|
||||
id: "test-group-5",
|
||||
name: "test-group-5",
|
||||
}),
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user