From 21d99a44a9fb2bd0f52c8bc8b5c75d200ef6d539 Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 16 Feb 2022 11:55:21 +0100 Subject: [PATCH 1/9] feat: GET customer group endpoint --- .../api/__tests__/admin/customer-groups.js | 37 +++++++++++++++++++ .../__tests__/get-customer-group.ts | 27 ++++++++++++++ .../customer-groups/get-customer-group.ts | 32 ++++++++++++++++ .../api/routes/admin/customer-groups/index.ts | 1 + .../src/services/__mocks__/customer-group.js | 5 +++ .../medusa/src/services/customer-group.ts | 19 ++++++++++ 6 files changed, 121 insertions(+) create mode 100644 packages/medusa/src/api/routes/admin/customer-groups/__tests__/get-customer-group.ts create mode 100644 packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index 0561f8e72d..8087234ac6 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -85,4 +85,41 @@ 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 = "test-group-4" + + 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: "test-group-4", + name: "test-group", + }) + ) + }) + }) }) 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..2e5e13e5ff --- /dev/null +++ b/packages/medusa/src/api/routes/admin/customer-groups/__tests__/get-customer-group.ts @@ -0,0 +1,27 @@ +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}`, { + 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) + }) +}) 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..c0e86e3d50 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts @@ -0,0 +1,32 @@ +import { CustomerGroupService } from "../../../../services" + +/** + * @oas [get] /customer-group/{id} + * operationId: "GetCustomerGroups" + * 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 customerGroupService: CustomerGroupService = req.scope.resolve( + "customerGroupService" + ) + + const customerGroup = await customerGroupService.retrieve(id) + + res.json({ customerGroup }) +} 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..2facc6ff0f 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 } 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 80cc6b9b82..f081bada10 100644 --- a/packages/medusa/src/services/customer-group.ts +++ b/packages/medusa/src/services/customer-group.ts @@ -39,6 +39,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 From 963b594a8a9efe317f7130ebbec33e3cdcca5d4e Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 16 Feb 2022 12:06:16 +0100 Subject: [PATCH 2/9] test for the 404 case --- .../api/__tests__/admin/customer-groups.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index 8087234ac6..9cac01f8ec 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -121,5 +121,25 @@ describe("/admin/customer-groups", () => { }) ) }) + + 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`) + }) + + }) + }) }) From 8313680a7123b0686247dcd4f78fb23e17568789 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 21 Feb 2022 14:33:20 +0100 Subject: [PATCH 3/9] fix: integration test case --- .../api/__tests__/admin/customer-groups.js | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index 9cac01f8ec..768018d29c 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -105,7 +105,7 @@ describe("/admin/customer-groups", () => { it("gets customer group", async () => { const api = useApi() - const id = "test-group-4" + const id = "customer-group-1" const response = await api.get(`/admin/customer-groups/${id}`, { headers: { @@ -116,30 +116,32 @@ describe("/admin/customer-groups", () => { expect(response.status).toEqual(200) expect(response.data.customerGroup).toEqual( expect.objectContaining({ - id: "test-group-4", - name: "test-group", + id: "customer-group-1", + name: "vip-customers", }) ) }) 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`) - }) + 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` + ) + }) }) - }) }) From ecd6ed820e77904fe59ff6fcc9195533968dd9f3 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 21 Feb 2022 15:24:52 +0100 Subject: [PATCH 4/9] feat: add `extend` param for customer groups --- .../api/__tests__/admin/customer-groups.js | 25 +++++++++++++++++++ .../customer-groups/get-customer-group.ts | 19 +++++++++++++- .../api/routes/admin/customer-groups/index.ts | 2 ++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index 768018d29c..e1b5af2777 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -120,6 +120,31 @@ describe("/admin/customer-groups", () => { 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 () => { 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 index c0e86e3d50..d97442c2a7 100644 --- 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 @@ -1,4 +1,7 @@ import { CustomerGroupService } from "../../../../services" +import { FindParams } from "../../../../types/common" +import { validator } from "../../../../utils/validator" +import { defaultAdminCustomerGroupsRelations } from "." /** * @oas [get] /customer-group/{id} @@ -22,11 +25,25 @@ import { CustomerGroupService } from "../../../../services" */ export default async (req, res) => { const { id } = req.params + + const validated = await validator(FindParams, req.query) + const customerGroupService: CustomerGroupService = req.scope.resolve( "customerGroupService" ) - const customerGroup = await customerGroupService.retrieve(id) + 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 }) } 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 2facc6ff0f..983a90a236 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/index.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/index.ts @@ -23,4 +23,6 @@ export type AdminCustomerGroupsListRes = PaginatedResponse & { customer_groups: CustomerGroup[] } +export const defaultAdminCustomerGroupsRelations = [] + export * from "./create-customer-group" From f21454970c156c628cfe9a2096b4d3e14b65ad1b Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 21 Feb 2022 15:26:28 +0100 Subject: [PATCH 5/9] fix: typo in an assertion --- integration-tests/api/__tests__/admin/customer-groups.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/api/__tests__/admin/customer-groups.js b/integration-tests/api/__tests__/admin/customer-groups.js index e1b5af2777..f3cd65de4b 100644 --- a/integration-tests/api/__tests__/admin/customer-groups.js +++ b/integration-tests/api/__tests__/admin/customer-groups.js @@ -120,7 +120,7 @@ describe("/admin/customer-groups", () => { name: "vip-customers", }) ) - expect(response.data.customerGroup).not.toHaveProperty("customers:") + expect(response.data.customerGroup).not.toHaveProperty("customers") }) it("gets customer group with `customers` prop", async () => { From def8763ee203c60728815ef3d36e57b5533b97c8 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 21 Feb 2022 15:30:44 +0100 Subject: [PATCH 6/9] fix: unit test case for `CustomerGroupServiceMock.retrieve` --- .../__tests__/get-customer-group.ts | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 index 2e5e13e5ff..9fd1ecd201 100644 --- 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 @@ -7,13 +7,17 @@ describe("GET /customer-groups", () => { const id = "123" beforeAll(async () => { - subject = await request("GET", `/admin/customer-groups/${id}`, { - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), + subject = await request( + "GET", + `/admin/customer-groups/${id}?expand=customers`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, }, - }, - }) + } + ) }) it("returns 200", () => { @@ -22,6 +26,8 @@ describe("GET /customer-groups", () => { it("calls CustomerGroupService get", () => { expect(CustomerGroupServiceMock.retrieve).toHaveBeenCalledTimes(1) - expect(CustomerGroupServiceMock.retrieve).toHaveBeenCalledWith(id) + expect(CustomerGroupServiceMock.retrieve).toHaveBeenCalledWith(id, { + relations: ["customers"], + }) }) }) From 7e1c372c711e7a04ea80b48c93cf52291f712668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Mon, 21 Feb 2022 20:41:40 +0100 Subject: [PATCH 7/9] Update packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts Co-authored-by: Sebastian Rindom --- .../src/api/routes/admin/customer-groups/get-customer-group.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index d97442c2a7..cd95a68488 100644 --- 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 @@ -5,7 +5,7 @@ import { defaultAdminCustomerGroupsRelations } from "." /** * @oas [get] /customer-group/{id} - * operationId: "GetCustomerGroups" + * operationId: "GetCustomerGroupsGroup" * summary: "Retrieve a CustomerGroup" * description: "Retrieves a Customer Group." * x-authenticated: true From 4ee2af2582d6c6420e7faf238447b77499e5d32d Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 21 Feb 2022 20:50:07 +0100 Subject: [PATCH 8/9] fix: export params type --- .../src/api/routes/admin/customer-groups/get-customer-group.ts | 2 ++ 1 file changed, 2 insertions(+) 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 index cd95a68488..de753b6bdd 100644 --- 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 @@ -47,3 +47,5 @@ export default async (req, res) => { res.json({ customerGroup }) } + +export class AdminGetCustomerGroupsGroupParams extends FindParams {} From 0409a1ded3d536ff1bf136a00bd59246689d2171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Tue, 22 Feb 2022 09:18:26 +0100 Subject: [PATCH 9/9] Update packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts Co-authored-by: Sebastian Rindom --- .../src/api/routes/admin/customer-groups/get-customer-group.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index de753b6bdd..ecbad1af32 100644 --- 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 @@ -26,7 +26,7 @@ import { defaultAdminCustomerGroupsRelations } from "." export default async (req, res) => { const { id } = req.params - const validated = await validator(FindParams, req.query) + const validated = await validator(AdminGetCustomerGroupsGroupParams, req.query) const customerGroupService: CustomerGroupService = req.scope.resolve( "customerGroupService"