Merge pull request #1073 from medusajs/feat/get-customer-group

feat: get customer group
This commit is contained in:
Frane Polić
2022-02-22 09:37:07 +01:00
committed by GitHub
6 changed files with 195 additions and 0 deletions

View File

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

View File

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

View File

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

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
}
@@ -22,4 +23,6 @@ export type AdminCustomerGroupsListRes = PaginatedResponse & {
customer_groups: CustomerGroup[]
}
export const defaultAdminCustomerGroupsRelations = []
export * from "./create-customer-group"

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

@@ -41,6 +41,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