feat: GET customer group endpoint

This commit is contained in:
fPolic
2022-02-16 11:55:21 +01:00
parent c2241d1101
commit 21d99a44a9
6 changed files with 121 additions and 0 deletions

View File

@@ -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",
})
)
})
})
})

View File

@@ -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)
})
})

View File

@@ -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 })
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -39,6 +39,25 @@ class CustomerGroupService extends BaseService {
return cloned
}
async retrieve(id: string, config = {}): Promise<CustomerGroup> {
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<CustomerGroup>} group - the customer group to create