diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index 0561f8e72d..f3cd65de4b 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -85,4 +85,88 @@ describe("/admin/customer-groups", () => { }) }) }) + + describe("GET /admin/customer-groups", () => { + beforeEach(async () => { + try { + await adminSeeder(dbConnection) + await customerSeeder(dbConnection) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("gets customer group", async () => { + const api = useApi() + + const id = "customer-group-1" + + const response = await api.get(`/admin/customer-groups/${id}`, { + headers: { + Authorization: "Bearer test_token", + }, + }) + + expect(response.status).toEqual(200) + expect(response.data.customerGroup).toEqual( + expect.objectContaining({ + id: "customer-group-1", + name: "vip-customers", + }) + ) + expect(response.data.customerGroup).not.toHaveProperty("customers") + }) + + it("gets customer group with `customers` prop", async () => { + const api = useApi() + + const id = "customer-group-1" + + const response = await api.get( + `/admin/customer-groups/${id}?expand=customers`, + { + headers: { + Authorization: "Bearer test_token", + }, + } + ) + + expect(response.status).toEqual(200) + expect(response.data.customerGroup).toEqual( + expect.objectContaining({ + id: "customer-group-1", + name: "vip-customers", + }) + ) + expect(response.data.customerGroup.customers).toEqual([]) + }) + + it("throws error when a customer group doesn't exist", async () => { + expect.assertions(3) + + const api = useApi() + + const id = "test-group-000" + + await api + .get(`/admin/customer-groups/${id}`, { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + expect(err.response.status).toEqual(404) + expect(err.response.data.type).toEqual("not_found") + expect(err.response.data.message).toEqual( + `CustomerGroup with ${id} was not found` + ) + }) + }) + }) }) diff --git a/packages/medusa/src/api/routes/admin/customer-groups/__tests__/get-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/__tests__/get-customer-group.ts new file mode 100644 index 0000000000..9fd1ecd201 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/customer-groups/__tests__/get-customer-group.ts @@ -0,0 +1,33 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { CustomerGroupServiceMock } from "../../../../../services/__mocks__/customer-group" + +describe("GET /customer-groups", () => { + let subject + const id = "123" + + beforeAll(async () => { + subject = await request( + "GET", + `/admin/customer-groups/${id}?expand=customers`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls CustomerGroupService get", () => { + expect(CustomerGroupServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(CustomerGroupServiceMock.retrieve).toHaveBeenCalledWith(id, { + relations: ["customers"], + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts new file mode 100644 index 0000000000..ecbad1af32 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts @@ -0,0 +1,51 @@ +import { CustomerGroupService } from "../../../../services" +import { FindParams } from "../../../../types/common" +import { validator } from "../../../../utils/validator" +import { defaultAdminCustomerGroupsRelations } from "." + +/** + * @oas [get] /customer-group/{id} + * operationId: "GetCustomerGroupsGroup" + * summary: "Retrieve a CustomerGroup" + * description: "Retrieves a Customer Group." + * x-authenticated: true + * parameters: + * - (path) id=* {string} The id of the Customer Group. + * tags: + * - CustomerGroup + * responses: + * 200: + * description: OK + * content: + * application/json: + * schema: + * properties: + * customerGroup: + * $ref: "#/components/schemas/customer-group" + */ +export default async (req, res) => { + const { id } = req.params + + const validated = await validator(AdminGetCustomerGroupsGroupParams, req.query) + + const customerGroupService: CustomerGroupService = req.scope.resolve( + "customerGroupService" + ) + + let expandFields: string[] = [] + if (validated.expand) { + expandFields = validated.expand.split(",") + } + + const findConfig = { + relations: expandFields.length + ? expandFields + : defaultAdminCustomerGroupsRelations, + } + + const customerGroup = await customerGroupService.retrieve(id, findConfig) + + res.json({ customerGroup }) +} + +export class AdminGetCustomerGroupsGroupParams extends FindParams {} diff --git a/packages/medusa/src/api/routes/admin/customer-groups/index.ts b/packages/medusa/src/api/routes/admin/customer-groups/index.ts index 91db467403..983a90a236 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/index.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/index.ts @@ -8,6 +8,7 @@ const route = Router() export default (app) => { app.use("/customer-groups", route) + route.get("/:id", middlewares.wrap(require("./get-customer-group").default)) route.post("/", middlewares.wrap(require("./create-customer-group").default)) return app } @@ -22,4 +23,6 @@ export type AdminCustomerGroupsListRes = PaginatedResponse & { customer_groups: CustomerGroup[] } +export const defaultAdminCustomerGroupsRelations = [] + export * from "./create-customer-group" diff --git a/packages/medusa/src/services/__mocks__/customer-group.js b/packages/medusa/src/services/__mocks__/customer-group.js index db92b9a8e5..0a50f82cb8 100644 --- a/packages/medusa/src/services/__mocks__/customer-group.js +++ b/packages/medusa/src/services/__mocks__/customer-group.js @@ -6,6 +6,10 @@ export const CustomerGroupServiceMock = { create: jest.fn().mockImplementation((f) => { return Promise.resolve(f) }), + + retrieve: jest.fn().mockImplementation((f) => { + return Promise.resolve(f) + }), } const mock = jest.fn().mockImplementation(() => { @@ -13,3 +17,4 @@ const mock = jest.fn().mockImplementation(() => { }) export default mock + diff --git a/packages/medusa/src/services/customer-group.ts b/packages/medusa/src/services/customer-group.ts index cb1d98fa8f..bdac9dab29 100644 --- a/packages/medusa/src/services/customer-group.ts +++ b/packages/medusa/src/services/customer-group.ts @@ -41,6 +41,25 @@ class CustomerGroupService extends BaseService { return cloned } + async retrieve(id: string, config = {}): Promise { + const customerRepo = this.manager_.getCustomRepository( + this.customerGroupRepository_ + ) + + const validatedId = this.validateId_(id) + const query = this.buildQuery_({ id: validatedId }, config) + + const customerGroup = await customerRepo.findOne(query) + if (!customerGroup) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `CustomerGroup with ${id} was not found` + ) + } + + return customerGroup + } + /** * Creates a customer group with the provided data. * @param {DeepPartial} group - the customer group to create