feat(customer): add remove from group, update group (#6158)
Open #6149 again - was merged to wrong base branch.
This commit is contained in:
@@ -409,4 +409,81 @@ describe("Customer Module Service", () => {
|
||||
expect(remainingCustomers.length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeCustomerFromGroup", () => {
|
||||
it("should remove a single customer from a group", async () => {
|
||||
// Creating a customer and a group
|
||||
const [customer] = await service.create([
|
||||
{ first_name: "John", last_name: "Doe", email: "john.doe@example.com" },
|
||||
])
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
|
||||
// Adding the customer to the group
|
||||
await service.addCustomerToGroup({
|
||||
customer_id: customer.id,
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
const [customerInGroup] = await service.list(
|
||||
{ id: customer.id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
expect(customerInGroup.groups).toEqual([
|
||||
expect.objectContaining({ id: group.id }),
|
||||
])
|
||||
|
||||
// Removing the customer from the group
|
||||
await service.removeCustomerFromGroup({
|
||||
customer_id: customer.id,
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
const [updatedCustomer] = await service.list(
|
||||
{ id: customer.id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
expect(updatedCustomer.groups).toEqual([])
|
||||
})
|
||||
|
||||
it("should remove multiple customers from groups", async () => {
|
||||
// Creating multiple customers and groups
|
||||
const customers = await service.create([
|
||||
{ first_name: "John", last_name: "Doe", email: "john.doe@example.com" },
|
||||
{
|
||||
first_name: "Jane",
|
||||
last_name: "Smith",
|
||||
email: "jane.smith@example.com",
|
||||
},
|
||||
])
|
||||
const groups = await service.createCustomerGroup([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
|
||||
// Adding customers to groups
|
||||
const pairsToAdd = [
|
||||
{ customer_id: customers[0].id, customer_group_id: groups[0].id },
|
||||
{ customer_id: customers[1].id, customer_group_id: groups[1].id },
|
||||
]
|
||||
await service.addCustomerToGroup(pairsToAdd)
|
||||
|
||||
// Removing customers from groups
|
||||
const pairsToRemove = [
|
||||
{ customer_id: customers[0].id, customer_group_id: groups[0].id },
|
||||
{ customer_id: customers[1].id, customer_group_id: groups[1].id },
|
||||
]
|
||||
await service.removeCustomerFromGroup(pairsToRemove)
|
||||
|
||||
// Verification for each customer
|
||||
for (const pair of pairsToRemove) {
|
||||
const [updatedCustomer] = await service.list(
|
||||
{ id: pair.customer_id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
expect(updatedCustomer.groups).not.toContainEqual(
|
||||
expect.objectContaining({ id: pair.customer_group_id })
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user