Feat: Create customer group (#1074)
* fix babel transform-runtime regenerator required for migrations * add customer group model * add migration for customer group * add customer group model export * add customer group repository * add customer group service * add CustomerGroupRepository to "withTransaction" in CustomerGroupService * remove unnecessary argument to runtime plugin * service export ordering * add create customer group endpoint * add customergroup to route index in admin * add customer group service * add customer groups test * cleanup * duplicate error handling * ducplicate name integration test * add jsdoc * customergroup not customer * pr feedback * pipeline test * fix weird merge Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
co-authored by
Sebastian Rindom
parent
22d387dcce
commit
b16976a6f4
+65
@@ -0,0 +1,65 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { CustomerGroupServiceMock } from "../../../../../services/__mocks__/customer-group"
|
||||
|
||||
describe("POST /customer-groups", () => {
|
||||
describe("successfully calls create customer group", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request("POST", `/admin/customer-groups`, {
|
||||
payload: {
|
||||
name: "test group",
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("calls CustomerGroupService create", () => {
|
||||
expect(CustomerGroupServiceMock.create).toHaveBeenCalledTimes(1)
|
||||
expect(CustomerGroupServiceMock.create).toHaveBeenCalledWith({
|
||||
name: "test group",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("fails if no name is provided for the group ", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request("POST", `/admin/customer-groups`, {
|
||||
payload: {},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("returns 400", () => {
|
||||
expect(subject.status).toEqual(400)
|
||||
})
|
||||
|
||||
it("returns descriptive error that name is missing", () => {
|
||||
expect(subject.body.type).toEqual("invalid_data")
|
||||
expect(subject.body.message).toEqual("name must be a string")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import { IsObject, IsOptional, IsString } from "class-validator"
|
||||
import { CustomerGroupService } from "../../../../services"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
|
||||
/**
|
||||
* @oas [post] /customer-groups
|
||||
* operationId: "PostCustomerGroups"
|
||||
* summary: "Create a CustomerGroup"
|
||||
* description: "Creates a CustomerGroup."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (body) name=* {string} Name of the customer group
|
||||
* - (body) metadata {object} Metadata for the customer.
|
||||
* tags:
|
||||
* - CustomerGroup
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* customerGroup:
|
||||
* $ref: "#/components/schemas/customergroup"
|
||||
*/
|
||||
|
||||
export default async (req, res) => {
|
||||
const validated = await validator(AdminPostCustomerGroupsReq, req.body)
|
||||
|
||||
const customerGroupService: CustomerGroupService = req.scope.resolve(
|
||||
"customerGroupService"
|
||||
)
|
||||
|
||||
const customerGroup = await customerGroupService.create(validated)
|
||||
res.status(200).json({ customerGroup })
|
||||
}
|
||||
|
||||
export class AdminPostCustomerGroupsReq {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: object
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Router } from "express"
|
||||
import { CustomerGroup } from "../../../.."
|
||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares from "../../../middlewares"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app) => {
|
||||
app.use("/customer-groups", route)
|
||||
|
||||
route.post("/", middlewares.wrap(require("./create-customer-group").default))
|
||||
return app
|
||||
}
|
||||
|
||||
export type AdminCustomerGroupsRes = {
|
||||
customer_group: CustomerGroup
|
||||
}
|
||||
|
||||
export type AdminCustomerGroupsDeleteRes = DeleteResponse
|
||||
|
||||
export type AdminCustomerGroupsListRes = PaginatedResponse & {
|
||||
customer_groups: CustomerGroup[]
|
||||
}
|
||||
|
||||
export * from "./create-customer-group"
|
||||
@@ -25,6 +25,7 @@ import collectionRoutes from "./collections"
|
||||
import productTagRoutes from "./product-tags"
|
||||
import notificationRoutes from "./notifications"
|
||||
import noteRoutes from "./notes"
|
||||
import customerGroupRoutes from "./customer-groups"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -80,6 +81,7 @@ export default (app, container, config) => {
|
||||
productTagRoutes(route)
|
||||
noteRoutes(route)
|
||||
inviteRoutes(route)
|
||||
customerGroupRoutes(route)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user