feat(customer): add remove from group, update group (#6158)

Open #6149 again - was merged to wrong base branch.
This commit is contained in:
Sebastian Rindom
2024-01-22 13:49:13 +00:00
committed by GitHub
parent 99045848fd
commit f72340ad87
3 changed files with 194 additions and 1 deletions
@@ -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 })
)
}
})
})
})
@@ -124,7 +124,7 @@ export default class CustomerModuleService implements ICustomerModuleService {
@MedusaContext() sharedContext: Context = {}
) {
let updateData: CustomerTypes.UpdateCustomerDTO[] = []
if (typeof idsOrSelector === "string") {
if (isString(idsOrSelector)) {
updateData = [
{
id: idsOrSelector,
@@ -264,6 +264,73 @@ export default class CustomerModuleService implements ICustomerModuleService {
return Array.isArray(dataOrArrayOfData) ? serialized : serialized[0]
}
async updateCustomerGroup(
groupId: string,
data: Partial<CustomerTypes.CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerTypes.CustomerGroupDTO>
async updateCustomerGroup(
groupIds: string[],
data: Partial<CustomerTypes.CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerTypes.CustomerGroupDTO[]>
async updateCustomerGroup(
selector: CustomerTypes.FilterableCustomerGroupProps,
data: Partial<CustomerTypes.CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerTypes.CustomerGroupDTO[]>
@InjectTransactionManager("baseRepository_")
async updateCustomerGroup(
groupIdOrSelector:
| string
| string[]
| CustomerTypes.FilterableCustomerGroupProps,
data: Partial<CustomerTypes.CreateCustomerGroupDTO>,
@MedusaContext() sharedContext: Context = {}
) {
let updateData: CustomerTypes.UpdateCustomerGroupDTO[] = []
if (isString(groupIdOrSelector)) {
updateData = [
{
id: groupIdOrSelector,
...data,
},
]
} else if (Array.isArray(groupIdOrSelector)) {
updateData = groupIdOrSelector.map((id) => ({
id,
...data,
}))
} else {
const ids = await this.customerGroupService_.list(
groupIdOrSelector,
{ select: ["id"] },
sharedContext
)
updateData = ids.map(({ id }) => ({
id,
...data,
}))
}
const groups = await this.customerGroupService_.update(
updateData,
sharedContext
)
if (isString(groupIdOrSelector)) {
return await this.baseRepository_.serialize<CustomerTypes.CustomerGroupDTO>(
groups[0],
{ populate: true }
)
}
return await this.baseRepository_.serialize<
CustomerTypes.CustomerGroupDTO[]
>(groups, { populate: true })
}
async addCustomerToGroup(
groupCustomerPair: CustomerTypes.GroupCustomerPair,
sharedContext?: Context
@@ -291,6 +358,30 @@ export default class CustomerModuleService implements ICustomerModuleService {
return { id: groupCustomers[0].id }
}
async removeCustomerFromGroup(
groupCustomerPair: CustomerTypes.GroupCustomerPair,
sharedContext?: Context
): Promise<void>
async removeCustomerFromGroup(
groupCustomerPairs: CustomerTypes.GroupCustomerPair[],
sharedContext?: Context
): Promise<void>
@InjectTransactionManager("baseRepository_")
async removeCustomerFromGroup(
data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
const pairs = Array.isArray(data) ? data : [data]
const groupCustomers = await this.customerGroupCustomerService_.list({
$or: pairs,
})
await this.customerGroupCustomerService_.delete(
groupCustomers.map((gc) => gc.id),
sharedContext
)
}
@InjectManager("baseRepository_")
async listCustomerGroups(
filters: CustomerTypes.FilterableCustomerGroupProps = {},
+25
View File
@@ -57,6 +57,22 @@ export interface ICustomerModuleService extends IModuleService {
sharedContext?: Context
): Promise<CustomerGroupDTO>
updateCustomerGroup(
groupId: string,
data: Partial<CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerGroupDTO>
updateCustomerGroup(
groupIds: string[],
data: Partial<CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerGroupDTO[]>
updateCustomerGroup(
selector: FilterableCustomerGroupProps,
data: Partial<CreateCustomerGroupDTO>,
sharedContext?: Context
): Promise<CustomerGroupDTO[]>
addCustomerToGroup(
groupCustomerPair: GroupCustomerPair,
sharedContext?: Context
@@ -67,6 +83,15 @@ export interface ICustomerModuleService extends IModuleService {
sharedContext?: Context
): Promise<{ id: string }[]>
removeCustomerFromGroup(
groupCustomerPair: { customer_id: string; customer_group_id: string },
sharedContext?: Context
): Promise<void>
removeCustomerFromGroup(
groupCustomerPairs: { customer_id: string; customer_group_id: string }[],
sharedContext?: Context
): Promise<void>
list(
filters?: FilterableCustomerProps,
config?: FindConfig<CustomerDTO>,