Skip to content

Commit

Permalink
feat(customer): add remove from group, update group (#6158)
Browse files Browse the repository at this point in the history
Open #6149 again - was merged to wrong base branch.
  • Loading branch information
srindom authored Jan 22, 2024
1 parent 9904584 commit f72340a
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]" },
])
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: "[email protected]" },
{
first_name: "Jane",
last_name: "Smith",
email: "[email protected]",
},
])
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 })
)
}
})
})
})
93 changes: 92 additions & 1 deletion packages/customer/src/services/customer-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {},
Expand Down
25 changes: 25 additions & 0 deletions packages/types/src/customer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>,
Expand Down

0 comments on commit f72340a

Please sign in to comment.