feat: GET customer group endpoint
This commit is contained in:
@@ -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",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -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 })
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ const route = Router()
|
|||||||
export default (app) => {
|
export default (app) => {
|
||||||
app.use("/customer-groups", route)
|
app.use("/customer-groups", route)
|
||||||
|
|
||||||
|
route.get("/:id", middlewares.wrap(require("./get-customer-group").default))
|
||||||
route.post("/", middlewares.wrap(require("./create-customer-group").default))
|
route.post("/", middlewares.wrap(require("./create-customer-group").default))
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ export const CustomerGroupServiceMock = {
|
|||||||
create: jest.fn().mockImplementation((f) => {
|
create: jest.fn().mockImplementation((f) => {
|
||||||
return Promise.resolve(f)
|
return Promise.resolve(f)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
retrieve: jest.fn().mockImplementation((f) => {
|
||||||
|
return Promise.resolve(f)
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
const mock = jest.fn().mockImplementation(() => {
|
const mock = jest.fn().mockImplementation(() => {
|
||||||
@@ -13,3 +17,4 @@ const mock = jest.fn().mockImplementation(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
export default mock
|
export default mock
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,25 @@ class CustomerGroupService extends BaseService {
|
|||||||
return cloned
|
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.
|
* Creates a customer group with the provided data.
|
||||||
* @param {DeepPartial<CustomerGroup>} group - the customer group to create
|
* @param {DeepPartial<CustomerGroup>} group - the customer group to create
|
||||||
|
|||||||
Reference in New Issue
Block a user