Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(customer): add customer group customer management #6276

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("POST /admin/customer-groups/:id/customers/batch", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should batch add customers to a group", async () => {
const api = useApi() as any

const group = await customerModuleService.createCustomerGroup({
name: "VIP",
})
const customers = await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
},
{
first_name: "Test2",
last_name: "Test2",
},
{
first_name: "Test3",
last_name: "Test3",
},
])

const response = await api.post(
`/admin/customer-groups/${group.id}/customers/batch`,
{
customer_ids: customers.map((c) => ({ id: c.id })),
},
adminHeaders
)

expect(response.status).toEqual(200)

const updatedGroup = await customerModuleService.retrieveCustomerGroup(
group.id,
{
relations: ["customers"],
}
)
expect(updatedGroup.customers?.length).toEqual(3)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("DELETE /admin/customer-groups/:id/customers/remove", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should batch delete customers from a group", async () => {
const api = useApi() as any

const group = await customerModuleService.createCustomerGroup({
name: "VIP",
})
const customers = await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
},
{
first_name: "Test2",
last_name: "Test2",
},
{
first_name: "Test3",
last_name: "Test3",
},
])

await customerModuleService.addCustomerToGroup(
customers.map((c) => ({ customer_id: c.id, customer_group_id: group.id }))
)

const response = await api.post(
`/admin/customer-groups/${group.id}/customers/remove`,
{
customer_ids: customers.map((c) => ({ id: c.id })),
},
adminHeaders
)

expect(response.status).toEqual(200)

const updatedGroup = await customerModuleService.retrieveCustomerGroup(
group.id,
{
relations: ["customers"],
}
)
expect(updatedGroup.customers?.length).toEqual(0)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("GET /admin/customer-groups/:id/customers", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should get all customer groups and its count", async () => {
const group = await customerModuleService.createCustomerGroup({
name: "Test",
})

const customers = await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
},
{
first_name: "Test2",
last_name: "Test2",
},
])

// add to group

await customerModuleService.addCustomerToGroup(
customers.map((c) => ({ customer_id: c.id, customer_group_id: group.id }))
)

const api = useApi() as any

const response = await api.get(
`/admin/customer-groups/${group.id}/customers`,
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(2)
expect(response.data.customers).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: customers[0].id,
}),
expect.objectContaining({
id: customers[1].id,
}),
])
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GroupCustomerPair, ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export const createCustomerGroupCustomersStepId =
"create-customer-group-customers"
export const createCustomerGroupCustomersStep = createStep(
createCustomerGroupCustomersStepId,
async (data: GroupCustomerPair[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

const groupPairs = await service.addCustomerToGroup(data)

return new StepResponse(groupPairs, data)
},
async (groupPairs, { container }) => {
if (!groupPairs?.length) {
return
}

const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

await service.removeCustomerFromGroup(groupPairs)
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { GroupCustomerPair, ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export const deleteCustomerGroupCustomersStepId =
"delete-customer-group-customers"
export const deleteCustomerGroupCustomersStep = createStep(
deleteCustomerGroupCustomersStepId,
async (data: GroupCustomerPair[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

await service.removeCustomerFromGroup(data)

return new StepResponse(void 0, data)
},
async (groupPairs, { container }) => {
if (!groupPairs?.length) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

await service.addCustomerToGroup(groupPairs)
}
)
2 changes: 2 additions & 0 deletions packages/core-flows/src/customer-group/steps/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
export * from "./create-customer-group-customers"
export * from "./delete-customer-group-customers"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GroupCustomerPair } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createCustomerGroupCustomersStep } from "../steps"

type WorkflowInput = { groupCustomers: GroupCustomerPair[] }

export const createCustomerGroupCustomersWorkflowId =
"create-customer-group-customers"
export const createCustomerGroupCustomersWorkflow = createWorkflow(
createCustomerGroupCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<{ id: string }[]> => {
return createCustomerGroupCustomersStep(input.groupCustomers)
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GroupCustomerPair } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteCustomerGroupCustomersStep } from "../steps"

type WorkflowInput = { groupCustomers: GroupCustomerPair[] }

export const deleteCustomerGroupCustomersWorkflowId =
"delete-customer-group-customers"
export const deleteCustomerGroupCustomersWorkflow = createWorkflow(
deleteCustomerGroupCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteCustomerGroupCustomersStep(input.groupCustomers)
}
)
2 changes: 2 additions & 0 deletions packages/core-flows/src/customer-group/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
export * from "./create-customer-group-customers"
export * from "./delete-customer-group-customers"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"

export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const { id } = req.params
const { customer_ids } =
req.validatedBody as AdminPostCustomerGroupsGroupCustomersBatchReq

const createCustomers = createCustomerGroupCustomersWorkflow(req.scope)

const { result, errors } = await createCustomers.run({
input: {
groupCustomers: customer_ids.map((c) => ({
customer_id: c.id,
customer_group_id: id,
})),
},
throwOnError: false,
})

if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}

res.status(200).json({ customer_group_customers: result })
}
Loading
Loading