feat(medusa, medusa-js, medusa-react): Implement Sales Channel update (#1797)

This commit is contained in:
Philip Korsholm
2022-07-06 15:44:09 +02:00
committed by GitHub
parent 263a661031
commit 9d19cc0818
19 changed files with 395 additions and 89 deletions
@@ -20,8 +20,8 @@ import productTypesRoutes from "./product-types"
import productRoutes from "./products"
import regionRoutes from "./regions"
import returnReasonRoutes from "./return-reasons"
import salesChannelRoutes from "./sales-channels"
import returnRoutes from "./returns"
import salesChannelRoutes from "./sales-channels"
import shippingOptionRoutes from "./shipping-options"
import shippingProfileRoutes from "./shipping-profiles"
import storeRoutes from "./store"
@@ -0,0 +1,48 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SalesChannelServiceMock } from "../../../../../services/__mocks__/sales-channel"
describe("POST /admin/regions/:region_id/countries", () => {
describe("successful creation", () => {
let subject
beforeAll(async () => {
const id = IdMap.getId("test_sales_channel")
subject = await request("POST", `/admin/sales-channels/${id}`, {
payload: {
name: "amazon",
description: "This is our amazon sales channel",
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
flags: ["sales_channels"],
})
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("returns updated sales channel", () => {
expect(subject.body.sales_channel).toEqual({
id: IdMap.getId("test_sales_channel"),
name: "amazon",
description: "This is our amazon sales channel",
})
})
it("calls service update", () => {
expect(SalesChannelServiceMock.update).toHaveBeenCalledTimes(1)
expect(SalesChannelServiceMock.update).toHaveBeenCalledWith(
IdMap.getId("test_sales_channel"),
{
name: "amazon",
description: "This is our amazon sales channel",
}
)
})
})
})
@@ -10,7 +10,7 @@ import SalesChannelService from "../../../../services/sales-channel"
* parameters:
* - (path) id=* {string} The id of the Sales channel.
* tags:
* - Sales channel
* - Sales Channel
* responses:
* 200:
* description: OK
@@ -3,7 +3,8 @@ import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import "reflect-metadata"
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
import { SalesChannel } from "../../../../models"
import middlewares from "../../../middlewares"
import middlewares, { transformBody } from "../../../middlewares"
import { AdminPostSalesChannelsSalesChannelReq } from "./update-sales-channel"
const route = Router()
@@ -22,14 +23,18 @@ export default (app) => {
route.post("/", (req, res) => {})
route.post("/:id", (req, res) => {})
route.post(
"/:id",
transformBody(AdminPostSalesChannelsSalesChannelReq),
middlewares.wrap(require("./update-sales-channel").default)
)
route.delete("/:id", (req, res) => {})
return app
}
export type AdminSalesChannelRes = {
export type AdminSalesChannelsRes = {
sales_channel: SalesChannel
}
@@ -42,5 +47,5 @@ export type AdminSalesChannelListRes = PaginatedResponse & {
export * from "./get-sales-channel"
// export * from './'
// export * from './'
// export * from './'
export * from "./update-sales-channel"
// export * from './'
@@ -0,0 +1,64 @@
import { IsBoolean, IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { SalesChannelService } from "../../../../services"
/**
* @oas [post] /sales-channels/{id}
* operationId: "PostSalesChannelsSalesChannel"
* summary: "Update a Sales Channel"
* description: "Updates a Sales Channel."
* x-authenticated: true
* parameters:
* - (path) id=* {string} The id of the Sales Channel.
* requestBody:
* content:
* application/json:
* schema:
* properties:
* name:
* type: string
* description: Name of the sales channel.
* description:
* type: string
* description: Sales Channel description.
* is_disabled:
* type: boolean
* description: Indication of if the sales channel is active.
* tags:
* - Sales Channel
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* customer:
* $ref: "#/components/schemas/sales-channel"
*/
export default async (req: Request, res: Response) => {
const { id } = req.params
const { validatedBody } = req as {
validatedBody: AdminPostSalesChannelsSalesChannelReq
}
const salesChannelService: SalesChannelService = req.scope.resolve(
"salesChannelService"
)
const sales_channel = await salesChannelService.update(id, validatedBody)
res.status(200).json({ sales_channel })
}
export class AdminPostSalesChannelsSalesChannelReq {
@IsOptional()
@IsString()
name?: string
@IsOptional()
@IsString()
description?: string
@IsBoolean()
@IsOptional()
is_disabled?: boolean
}