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
@@ -12,6 +12,16 @@ Object {
}
`;
exports[`sales channels POST /admin/sales-channels successfully creates a sales channel 1`] = `
Object {
"sales_channel": ObjectContaining {
"description": "sales channel description",
"is_disabled": false,
"name": "sales channel name",
},
}
`;
exports[`sales channels POST /admin/sales-channels/:id updates sales channel properties 1`] = `
Object {
"created_at": Any<String>,
@@ -26,6 +26,7 @@ describe("sales channels", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_SALES_CHANNELS: true },
verbose: false,
})
dbConnection = connection
medusaProcess = process
@@ -38,14 +39,6 @@ describe("sales channels", () => {
medusaProcess.kill()
})
describe("GET /admin/sales-channels", () => {
it("is true", () => {
// dummy test to ensure test suite passes
expect(true).toBeTruthy()
})
})
describe("POST /admin/sales-channels", () => {})
describe("GET /admin/sales-channels/:id", () => {
let salesChannel
@@ -136,5 +129,49 @@ describe("sales channels", () => {
})
})
describe("POST /admin/sales-channels", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
} catch (e) {
console.error(e)
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully creates a sales channel", async () => {
const api = useApi()
const newSalesChannel = {
name: "sales channel name",
description: "sales channel description",
}
const response = await api
.post("/admin/sales-channels", newSalesChannel, adminReqConfig)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.sales_channel).toBeTruthy()
expect(response.data).toMatchSnapshot({
sales_channel: expect.objectContaining({
name: newSalesChannel.name,
description: newSalesChannel.description,
is_disabled: false,
}),
})
})
})
describe("GET /admin/sales-channels/:id", () => {})
describe("POST /admin/sales-channels/:id", () => {})
describe("DELETE /admin/sales-channels/:id", () => {})
})
@@ -1,4 +1,5 @@
import {
AdminPostSalesChannelsReq,
AdminSalesChannelsRes,
AdminPostSalesChannelsSalesChannelReq,
} from "@medusajs/medusa"
@@ -20,10 +21,17 @@ class AdminSalesChannelsResource extends BaseResource {
return this.client.request("GET", path, {}, {}, customHeaders)
}
/* create(
payload: any,
/* *
* Create a medusa sales channel
* @returns the created channel
*/
create(
payload: AdminPostSalesChannelsReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<any> {}*/
): ResponsePromise<AdminSalesChannelsRes> {
const path = `/admin/sales-channels`
return this.client.request("POST", path, payload, {}, customHeaders)
}
/** update a sales channel
* @experimental This feature is under development and may change in the future.
+12 -2
View File
@@ -869,7 +869,7 @@ export const adminHandlers = [
discount_condition: {
...fixtures
.get("discount")
.rule.conditions.find(c => c.id === req.params.conditionId),
.rule.conditions.find((c) => c.id === req.params.conditionId),
},
})
)
@@ -1692,5 +1692,15 @@ export const adminHandlers = [
},
})
)
})
}),
rest.post("/admin/sales-channels", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
sales_channel: fixtures.get("sales_channel"),
...(req.body as Record<string, unknown>),
})
)
}),
]
@@ -1,16 +1,41 @@
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import {
AdminPostSalesChannelsReq,
AdminSalesChannelsRes,
AdminPostSalesChannelsSalesChannelReq,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import { useMedusa } from "../../../contexts"
import { buildOptions } from "../../utils/buildOptions"
import { adminSalesChannelsKeys } from "./queries"
/**
* Hook provides a mutation function for creating sales channel.
*
* @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.
*/
export const useAdminCreateSalesChannel = (
options?: UseMutationOptions<
Response<AdminSalesChannelsRes>,
Error,
AdminPostSalesChannelsReq
>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
(payload: AdminPostSalesChannelsReq) =>
client.admin.salesChannels.create(payload),
buildOptions(queryClient, [adminSalesChannelsKeys.list()], options)
)
}
/** update a sales channel
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `sales_channels` in your medusa backend project.
* To use this feature please enable feature flag `sales_channels` in your medusa backend project.
* @description updates a sales channel
* @returns the updated medusa sales channel
*/
@@ -34,35 +59,3 @@ export const useAdminUpdateSalesChannel = (
)
)
}
/*export const useAdminCreateSalesChannel = (
options?: UseMutationOptions<
Response<AdminSalesChannelsRes>,
Error,
AdminPostSalesChannelsReq
>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
(payload: AdminPostSalesChannelsReq) => client.admin.salesChannels.create(payload),
buildOptions(queryClient, adminSalesChannelsKeys.lists(), options)
)
}*/
/*export const useAdminDeleteSalesChannel = (
id: string,
options?: UseMutationOptions<Response<AdminSalesChannelsDeleteRes>, Error, void>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
() => client.admin.salesChannels.delete(id),
buildOptions(
queryClient,
[adminSalesChannelsKeys.lists(), adminSalesChannelsKeys.detail(id)],
options
)
)
}*/
@@ -15,7 +15,7 @@ type SalesChannelsQueryKeys = typeof adminSalesChannelsKeys
/** retrieve a sales channel
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `sales_channels` in your medusa backend project.
* To use this feature please enable feature flag `sales_channels` in your medusa backend project.
* @description gets a sales channel
* @returns a medusa sales channel
*/
@@ -1,9 +1,35 @@
import { useAdminUpdateSalesChannel } from "../../../../src"
import { renderHook } from "@testing-library/react-hooks"
import { useAdminCreateSalesChannel, useAdminUpdateSalesChannel } from "../../../../src"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminUpdateStore hook", () => {
describe("useAdminCreateSalesChannel hook", () => {
test("returns a sales channel", async () => {
const salesChannel = {
name: "sales channel 1 name",
description: "sales channel 1 description",
}
const { result, waitFor } = renderHook(() => useAdminCreateSalesChannel(), {
wrapper: createWrapper(),
})
result.current.mutate(salesChannel)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.sales_channel).toEqual(
expect.objectContaining({
...fixtures.get("sales_channel"),
...salesChannel,
})
)
})
})
describe("useAdminUpdateSalesChannel hook", () => {
test("updates a store", async () => {
const salesChannel = {
name: "medusa sales channel",
@@ -6,9 +6,12 @@ import { createWrapper } from "../../../utils"
describe("useAdminSalesChannel hook", () => {
test("returns a product", async () => {
const salesChannel = fixtures.get("sales_channel")
const { result, waitFor } = renderHook(() => useAdminSalesChannel(salesChannel.id), {
wrapper: createWrapper(),
})
const { result, waitFor } = renderHook(
() => useAdminSalesChannel(salesChannel.id),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
@@ -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(