feat(medusa, medusa-js, medusa-react): Start implementing remove batch products on a sales channel (#1842)
What Support sales channel remove product batch in medusa, medusa-js and medusa-react How By implementing a new endpoint and the associated service method as well as the repository methods. Medusa-js new removeProductd method in the resource Medusa-react new hook in the mutations Tests Endpoint test Service test Integration test Hook tests Fixes CORE-292
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
AdminPostSalesChannelsSalesChannelReq,
|
||||
AdminSalesChannelsDeleteRes,
|
||||
AdminSalesChannelsListRes,
|
||||
AdminDeleteSalesChannelsChannelProductsBatchReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { ResponsePromise } from "../../typings"
|
||||
import BaseResource from "../base"
|
||||
@@ -87,6 +88,22 @@ class AdminSalesChannelsResource extends BaseResource {
|
||||
const path = `/admin/sales-channels/${salesChannelId}`
|
||||
return this.client.request("DELETE", path, {}, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove products from 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.
|
||||
* @description Remove products from a sales channel
|
||||
* @returns a medusa sales channel
|
||||
*/
|
||||
removeProducts(
|
||||
salesChannelId: string,
|
||||
payload: AdminDeleteSalesChannelsChannelProductsBatchReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminSalesChannelsRes> {
|
||||
const path = `/admin/sales-channels/${salesChannelId}/products/batch`
|
||||
return this.client.request("DELETE", path, payload, {}, customHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminSalesChannelsResource
|
||||
|
||||
@@ -1726,4 +1726,13 @@ export const adminHandlers = [
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.delete("/admin/sales-channels/:id/products/batch", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
sales_channel: fixtures.get("sales_channel"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
]
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
AdminSalesChannelsRes,
|
||||
AdminPostSalesChannelsSalesChannelReq,
|
||||
AdminSalesChannelsDeleteRes,
|
||||
AdminDeleteSalesChannelsChannelProductsBatchReq
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
@@ -87,3 +88,33 @@ export const useAdminDeleteSalesChannel = (
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove products from 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.
|
||||
* @description remove products from a sales channel
|
||||
* @param id
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminDeleteProductsFromSalesChannel = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminSalesChannelsRes>,
|
||||
Error,
|
||||
AdminDeleteSalesChannelsChannelProductsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: AdminDeleteSalesChannelsChannelProductsBatchReq) => {
|
||||
return client.admin.salesChannels.removeProducts(id, payload)
|
||||
},
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminSalesChannelsKeys.lists(), adminSalesChannelsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
useAdminDeleteSalesChannel,
|
||||
useAdminCreateSalesChannel,
|
||||
useAdminUpdateSalesChannel,
|
||||
useAdminDeleteProductsFromSalesChannel,
|
||||
} from "../../../../src"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
@@ -84,3 +85,25 @@ describe("useAdminDeleteSalesChannel hook", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminDeleteProductsFromSalesChannel hook", () => {
|
||||
test("remove products from a sales channel", async () => {
|
||||
const id = fixtures.get("sales_channel").id
|
||||
const productId = fixtures.get("product").id
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminDeleteProductsFromSalesChannel(id),
|
||||
{ wrapper: createWrapper() }
|
||||
)
|
||||
|
||||
result.current.mutate({ product_ids: [
|
||||
{ id: productId }
|
||||
]})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data).toEqual(expect.objectContaining({
|
||||
sales_channel: fixtures.get("sales_channel"),
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { SalesChannelServiceMock } from "../../../../../services/__mocks__/sales-channel"
|
||||
|
||||
describe("DELETE /admin/sales-channels/:id/products/batch", () => {
|
||||
describe("remove product from a sales channel", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"DELETE",
|
||||
`/admin/sales-channels/${IdMap.getId("sales_channel_1")}/products/batch`,
|
||||
{
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
payload: {
|
||||
product_ids: [{ id: IdMap.getId("sales_channel_1_product_1") }]
|
||||
},
|
||||
flags: ["sales_channels"],
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("calls the retrieve method from the sales channel service", () => {
|
||||
expect(SalesChannelServiceMock.removeProducts).toHaveBeenCalledTimes(1)
|
||||
expect(SalesChannelServiceMock.removeProducts).toHaveBeenCalledWith(
|
||||
IdMap.getId("sales_channel_1"),
|
||||
[IdMap.getId("sales_channel_1_product_1")]
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Type } from "class-transformer"
|
||||
import { IsArray, ValidateNested } from "class-validator"
|
||||
import { SalesChannelService } from "../../../../services"
|
||||
import { Request, Response } from "express"
|
||||
import { ProductBatchSalesChannel } from "../../../../types/sales-channels"
|
||||
|
||||
/**
|
||||
* @oas [delete] /sales-channels/{id}/products/batch
|
||||
* operationId: "DeleteSalesChannelsChannelProductsBatch"
|
||||
* summary: "Remove a list of products from a sales channel"
|
||||
* description: "Remove a list of products from a sales channel."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) id=* {string} The id of the customer group.
|
||||
* - (body) product_ids=* {ProductBatchSalesChannel[]} ids of the product to remove
|
||||
* tags:
|
||||
* - Sales Channel
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* sales_channel:
|
||||
* $ref: "#/components/schemas/sales_channel"
|
||||
*/
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
const { id } = req.params
|
||||
|
||||
const salesChannelService: SalesChannelService = req.scope.resolve(
|
||||
"salesChannelService"
|
||||
)
|
||||
|
||||
const validatedBody =
|
||||
req.validatedBody as AdminDeleteSalesChannelsChannelProductsBatchReq
|
||||
const salesChannel = await salesChannelService.removeProducts(
|
||||
id,
|
||||
validatedBody.product_ids.map((p) => p.id)
|
||||
)
|
||||
res.status(200).json({ sales_channel: salesChannel })
|
||||
}
|
||||
|
||||
export class AdminDeleteSalesChannelsChannelProductsBatchReq {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ProductBatchSalesChannel)
|
||||
product_ids: ProductBatchSalesChannel[]
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import middlewares, {
|
||||
import { AdminPostSalesChannelsSalesChannelReq } from "./update-sales-channel"
|
||||
import { AdminPostSalesChannelsReq } from "./create-sales-channel"
|
||||
import { AdminGetSalesChannelsParams } from "./list-sales-channels"
|
||||
import { AdminDeleteSalesChannelsChannelProductsBatchReq } from "./delete-products-batch"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -40,6 +41,11 @@ export default (app) => {
|
||||
transformBody(AdminPostSalesChannelsSalesChannelReq),
|
||||
middlewares.wrap(require("./update-sales-channel").default)
|
||||
)
|
||||
salesChannelRouter.delete(
|
||||
"/products/batch",
|
||||
transformBody(AdminDeleteSalesChannelsChannelProductsBatchReq),
|
||||
middlewares.wrap(require("./delete-products-batch").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/",
|
||||
@@ -75,3 +81,4 @@ export * from "./create-sales-channel"
|
||||
export * from "./list-sales-channels"
|
||||
export * from "./update-sales-channel"
|
||||
export * from "./delete-sales-channel"
|
||||
export * from "./delete-products-batch"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Brackets, EntityRepository, Repository } from "typeorm"
|
||||
import { Brackets, DeleteResult, EntityRepository, In, Repository } from "typeorm"
|
||||
import { SalesChannel } from "../models"
|
||||
import { ExtendedFindConfig, Selector } from "../types/common";
|
||||
|
||||
@EntityRepository(SalesChannel)
|
||||
export class SalesChannelRepository extends Repository<SalesChannel> {
|
||||
public async getFreeTextSearchResultsAndCount(
|
||||
public async getFreeTextSearchResultsAndCount(
|
||||
q: string,
|
||||
options: ExtendedFindConfig<SalesChannel, Selector<SalesChannel>> = { where: {} },
|
||||
): Promise<[SalesChannel[], number]> {
|
||||
@@ -30,4 +30,18 @@ export class SalesChannelRepository extends Repository<SalesChannel> {
|
||||
|
||||
return await qb.getManyAndCount()
|
||||
}
|
||||
|
||||
async removeProducts(
|
||||
salesChannelId: string,
|
||||
productIds: string[]
|
||||
): Promise<DeleteResult> {
|
||||
return await this.createQueryBuilder()
|
||||
.delete()
|
||||
.from("product_sales_channel")
|
||||
.where({
|
||||
sales_channel_id: salesChannelId,
|
||||
product_id: In(productIds),
|
||||
})
|
||||
.execute()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,11 @@ export const SalesChannelServiceMock = {
|
||||
description: "sales channel 1 description",
|
||||
is_disabled: false,
|
||||
})
|
||||
})
|
||||
}),
|
||||
|
||||
removeProducts: jest.fn().mockImplementation((id, productIds) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
|
||||
@@ -44,13 +44,16 @@ describe("SalesChannelService", () => {
|
||||
}),
|
||||
}),
|
||||
getFreeTextSearchResultsAndCount: jest.fn().mockImplementation(() =>
|
||||
Promise.resolve([
|
||||
{
|
||||
id: IdMap.getId("sales_channel_1"),
|
||||
...salesChannelData
|
||||
},
|
||||
]),
|
||||
)
|
||||
Promise.resolve([
|
||||
{
|
||||
id: IdMap.getId("sales_channel_1"),
|
||||
...salesChannelData
|
||||
},
|
||||
]),
|
||||
),
|
||||
removeProducts: jest.fn().mockImplementation((id: string, productIds: string[]): any => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
}
|
||||
|
||||
describe("create default", async () => {
|
||||
@@ -290,4 +293,35 @@ describe("SalesChannelService", () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("Remove products", () => {
|
||||
const salesChannelService = new SalesChannelService({
|
||||
manager: MockManager,
|
||||
eventBusService: EventBusServiceMock as unknown as EventBusService,
|
||||
salesChannelRepository: salesChannelRepositoryMock,
|
||||
storeService: StoreServiceMock as unknown as StoreService,
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('should remove a list of product to a sales channel', async () => {
|
||||
const salesChannel = await salesChannelService.removeProducts(
|
||||
IdMap.getId("sales_channel_1"),
|
||||
[IdMap.getId("sales_channel_1_product_1")]
|
||||
)
|
||||
|
||||
expect(salesChannelRepositoryMock.removeProducts).toHaveBeenCalledTimes(1)
|
||||
expect(salesChannelRepositoryMock.removeProducts).toHaveBeenCalledWith(
|
||||
IdMap.getId("sales_channel_1"),
|
||||
[IdMap.getId("sales_channel_1_product_1")]
|
||||
)
|
||||
expect(salesChannel).toBeTruthy()
|
||||
expect(salesChannel).toEqual({
|
||||
id: IdMap.getId("sales_channel_1"),
|
||||
...salesChannelData,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -244,6 +244,27 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
||||
return defaultSalesChannel
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a batch of product from a sales channel
|
||||
* @param salesChannelId - The id of the sales channel on which to remove the products
|
||||
* @param productIds - The products ids to remove from the sales channel
|
||||
* @return the sales channel on which the products have been removed
|
||||
*/
|
||||
async removeProducts(
|
||||
salesChannelId: string,
|
||||
productIds: string[]
|
||||
): Promise<SalesChannel | never> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const salesChannelRepo = transactionManager.getCustomRepository(
|
||||
this.salesChannelRepository_
|
||||
)
|
||||
|
||||
await salesChannelRepo.removeProducts(salesChannelId, productIds)
|
||||
|
||||
return await this.retrieve(salesChannelId)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default SalesChannelService
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { SalesChannel } from "../models"
|
||||
import { IsString } from "class-validator"
|
||||
|
||||
export type CreateSalesChannelInput = {
|
||||
name: string
|
||||
@@ -7,3 +7,8 @@ export type CreateSalesChannelInput = {
|
||||
}
|
||||
|
||||
export type UpdateSalesChannelInput = Partial<CreateSalesChannelInput>
|
||||
|
||||
export class ProductBatchSalesChannel {
|
||||
@IsString()
|
||||
id: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user