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

This commit is contained in:
Frane Polić authored and GitHub committed 2022-07-06 22:18:05 +02:00
1 parent 428a801293
commit 0d1624cf6a
14 files changed
+274 -60

No files matched your search

@@ -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"
@@ -17,9 +17,14 @@ export const SalesChannelServiceMock = {
listAndCount: jest.fn().mockImplementation(() => {}),
create: jest.fn().mockImplementation(() => {}),
delete: jest.fn().mockImplementation(() => {}),
create: jest.fn().mockImplementation((data) => {
return Promise.resolve({
id: id,
...data,
})
}),
}
const mock = jest.fn().mockImplementation(() => {
+33 -3
View File
@@ -1,5 +1,6 @@
import { EntityManager } from "typeorm"
import { MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { TransactionBaseService } from "../interfaces"
import { SalesChannel } from "../models"
import { SalesChannelRepository } from "../repositories/sales-channel"
@@ -10,6 +11,7 @@ import {
} from "../types/sales-channels"
import EventBusService from "./event-bus"
import { buildQuery } from "../utils"
import { PostgresError } from "../utils/exception-formatter"
type InjectedDependencies = {
salesChannelRepository: typeof SalesChannelRepository
@@ -20,6 +22,7 @@ type InjectedDependencies = {
class SalesChannelService extends TransactionBaseService<SalesChannelService> {
static Events = {
UPDATED: "sales_channel.updated",
CREATED: "sales_channel.created",
}
protected manager_: EntityManager
@@ -41,6 +44,13 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
this.eventBusService_ = eventBusService
}
/**
* Retrieve a SalesChannel by id
*
* @experimental This feature is under development and may change in the future.
* To use this feature please enable the corresponding feature flag in your medusa backend project.
* @returns a sales channel
*/
async retrieve(
salesChannelId: string,
config: FindConfig<SalesChannel> = {}
@@ -77,8 +87,28 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
throw new Error("Method not implemented.")
}
async create(data: CreateSalesChannelInput): Promise<SalesChannel> {
throw new Error("Method not implemented.")
/**
* Creates a SalesChannel
*
* @experimental This feature is under development and may change in the future.
* To use this feature please enable the corresponding feature flag in your medusa backend project.
* @returns the created channel
*/
async create(data: CreateSalesChannelInput): Promise<SalesChannel | never> {
return await this.atomicPhase_(async (manager) => {
const salesChannelRepo: SalesChannelRepository =
manager.getCustomRepository(this.salesChannelRepository_)
const salesChannel = salesChannelRepo.create(data)
await this.eventBusService_
.withTransaction(manager)
.emit(SalesChannelService.Events.CREATED, {
id: salesChannel.id,
})
return await salesChannelRepo.save(salesChannel)
})
}
async update(