feat(medusa, medusa-js, medusa-react): Implement Sales Channel creation (#1795)

This commit is contained in:
Frane Polić
2022-07-06 22:18:05 +02:00
committed by GitHub
parent 428a801293
commit 0d1624cf6a
14 changed files with 274 additions and 60 deletions
@@ -0,0 +1,39 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SalesChannelServiceMock } from "../../../../../services/__mocks__/sales-channel"
describe("POST /admin/sales-channels", () => {
describe("successfully get a sales channel", () => {
let subject
beforeAll(async () => {
subject = await request("POST", `/admin/sales-channels`, {
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
payload: {
name: "sales channel 1 name",
description: "sales channel 1 description",
},
flags: ["sales_channels"],
})
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls the create method from the sales channel service", () => {
expect(SalesChannelServiceMock.create).toHaveBeenCalledTimes(1)
expect(SalesChannelServiceMock.create).toHaveBeenCalledWith(
expect.objectContaining({
name: "sales channel 1 name",
description: "sales channel 1 description",
})
)
})
})
})
@@ -0,0 +1,47 @@
import { Request, Response } from "express"
import { IsObject, IsOptional, IsString } from "class-validator"
import SalesChannelService from "../../../../services/sales-channel"
import { CreateSalesChannelInput } from "../../../../types/sales-channels"
/**
* @oas [post] /sales-channels
* operationId: "PostSalesChannels"
* summary: "Create a sales channel"
* description: "Creates a sales channel."
* x-authenticated: true
* parameters:
* - (body) name=* {string} Name of the sales channel
* - (body) description=* {string} Description of the sales channel
* tags:
* - Sales Channels
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* sales_channel:
* $ref: "#/components/schemas/sales_channel"
*/
export default async (req: Request, res: Response) => {
const salesChannelService: SalesChannelService = req.scope.resolve(
"salesChannelService"
)
const salesChannel = await salesChannelService.create(
req.validatedBody as CreateSalesChannelInput
)
res.status(200).json({ sales_channel: salesChannel })
}
export class AdminPostSalesChannelsReq {
@IsString()
name: string
@IsString()
@IsOptional()
description: string
}
@@ -19,7 +19,7 @@ import SalesChannelService from "../../../../services/sales-channel"
* schema:
* properties:
* sales_channel:
* $ref: "#/components/schemas/sales-channel"
* $ref: "#/components/schemas/sales_channel"
*/
export default async (req: Request, res: Response): Promise<void> => {
const { id } = req.params
@@ -5,6 +5,7 @@ import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
import { SalesChannel } from "../../../../models"
import middlewares, { transformBody } from "../../../middlewares"
import { AdminPostSalesChannelsSalesChannelReq } from "./update-sales-channel"
import { AdminPostSalesChannelsReq } from "./create-sales-channel"
const route = Router()
@@ -21,7 +22,11 @@ export default (app) => {
route.get("/", (req, res) => {})
route.post("/", (req, res) => {})
route.post(
"/",
transformBody(AdminPostSalesChannelsReq),
middlewares.wrap(require("./create-sales-channel").default)
)
route.post(
"/:id",
@@ -45,6 +50,7 @@ export type AdminSalesChannelListRes = PaginatedResponse & {
}
export * from "./get-sales-channel"
export * from "./create-sales-channel"
// export * from './'
// export * from './'
export * from "./update-sales-channel"