feat(customer): add remove from group, update group (#6158)
Open #6149 again - was merged to wrong base branch.
This commit is contained in:
1 parent
99045848fd
commit
f72340ad87
3 files changed
+194
-1
No files matched your search
@@ -409,4 +409,81 @@ describe("Customer Module Service", () => {
|
|||||||
expect(remainingCustomers.length).toBe(0)
|
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 = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
) {
|
) {
|
||||||
let updateData: CustomerTypes.UpdateCustomerDTO[] = []
|
let updateData: CustomerTypes.UpdateCustomerDTO[] = []
|
||||||
if (typeof idsOrSelector === "string") {
|
if (isString(idsOrSelector)) {
|
||||||
updateData = [
|
updateData = [
|
||||||
{
|
{
|
||||||
id: idsOrSelector,
|
id: idsOrSelector,
|
||||||
@@ -264,6 +264,73 @@ export default class CustomerModuleService implements ICustomerModuleService {
|
|||||||
return Array.isArray(dataOrArrayOfData) ? serialized : serialized[0]
|
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(
|
async addCustomerToGroup(
|
||||||
groupCustomerPair: CustomerTypes.GroupCustomerPair,
|
groupCustomerPair: CustomerTypes.GroupCustomerPair,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
@@ -291,6 +358,30 @@ export default class CustomerModuleService implements ICustomerModuleService {
|
|||||||
return { id: groupCustomers[0].id }
|
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_")
|
@InjectManager("baseRepository_")
|
||||||
async listCustomerGroups(
|
async listCustomerGroups(
|
||||||
filters: CustomerTypes.FilterableCustomerGroupProps = {},
|
filters: CustomerTypes.FilterableCustomerGroupProps = {},
|
||||||
|
|||||||
@@ -57,6 +57,22 @@ export interface ICustomerModuleService extends IModuleService {
|
|||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<CustomerGroupDTO>
|
): 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(
|
addCustomerToGroup(
|
||||||
groupCustomerPair: GroupCustomerPair,
|
groupCustomerPair: GroupCustomerPair,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
@@ -67,6 +83,15 @@ export interface ICustomerModuleService extends IModuleService {
|
|||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<{ id: string }[]>
|
): 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(
|
list(
|
||||||
filters?: FilterableCustomerProps,
|
filters?: FilterableCustomerProps,
|
||||||
config?: FindConfig<CustomerDTO>,
|
config?: FindConfig<CustomerDTO>,
|
||||||
|
|||||||
Reference in new issue
Block a user