From 5fc5f2c35ff18a65ee2e96d7706cdb5c158d5b64 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Mon, 29 Jul 2024 10:30:53 +0200 Subject: [PATCH] feat(medusa): add stock_location_id filter to providers api (#8319) what: - adds a filter to fulfillment providers API to scope by stock locations RESOLVES CC-260 --- .../admin/fulfillment-provider.spec.ts | 72 +++++++++++++++++++ .../fulfillment-providers/middlewares.ts | 6 ++ .../admin/fulfillment-providers/validators.ts | 1 + 3 files changed, 79 insertions(+) create mode 100644 integration-tests/http/__tests__/fulfillment/admin/fulfillment-provider.spec.ts diff --git a/integration-tests/http/__tests__/fulfillment/admin/fulfillment-provider.spec.ts b/integration-tests/http/__tests__/fulfillment/admin/fulfillment-provider.spec.ts new file mode 100644 index 0000000000..dd8886d6fb --- /dev/null +++ b/integration-tests/http/__tests__/fulfillment/admin/fulfillment-provider.spec.ts @@ -0,0 +1,72 @@ +import { medusaIntegrationTestRunner } from "medusa-test-utils" +import { + adminHeaders, + createAdminUser, +} from "../../../../helpers/create-admin-user" + +jest.setTimeout(50000) + +medusaIntegrationTestRunner({ + testSuite: ({ dbConnection, getContainer, api }) => { + describe("Admin: Fulfillment Provider API", () => { + let location + + beforeEach(async () => { + await createAdminUser(dbConnection, adminHeaders, getContainer()) + + location = ( + await api.post( + `/admin/stock-locations`, + { name: "Test location" }, + adminHeaders + ) + ).data.stock_location + }) + + describe("GET /admin/fulfillment-providers", () => { + it("should list all fulfillment providers successfully", async () => { + const response = await api.get( + `/admin/fulfillment-providers`, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.fulfillment_providers).toEqual([ + expect.objectContaining({ + id: "manual_test-provider", + is_enabled: true, + }), + ]) + }) + + it("should list all fulfillment providers scoped by stock location", async () => { + let response = await api.get( + `/admin/fulfillment-providers?stock_location_id=${location.id}`, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.fulfillment_providers).toEqual([]) + + await api.post( + `/admin/stock-locations/${location.id}/fulfillment-providers`, + { add: ["manual_test-provider"] }, + adminHeaders + ) + + response = await api.get( + `/admin/fulfillment-providers?stock_location_id=${location.id}`, + adminHeaders + ) + + expect(response.data.fulfillment_providers).toEqual([ + expect.objectContaining({ + id: "manual_test-provider", + is_enabled: true, + }), + ]) + }) + }) + }) + }, +}) diff --git a/packages/medusa/src/api/admin/fulfillment-providers/middlewares.ts b/packages/medusa/src/api/admin/fulfillment-providers/middlewares.ts index 9cbbaf78ff..ab045e401e 100644 --- a/packages/medusa/src/api/admin/fulfillment-providers/middlewares.ts +++ b/packages/medusa/src/api/admin/fulfillment-providers/middlewares.ts @@ -1,4 +1,5 @@ import { MiddlewareRoute } from "../../../types/middlewares" +import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter" import { validateAndTransformQuery } from "../../utils/validate-query" import * as QueryConfig from "./query-config" import { AdminFulfillmentProvidersParams } from "./validators" @@ -12,6 +13,11 @@ export const adminFulfillmentProvidersRoutesMiddlewares: MiddlewareRoute[] = [ AdminFulfillmentProvidersParams, QueryConfig.listTransformQueryConfig ), + maybeApplyLinkFilter({ + entryPoint: "location_fulfillment_provider", + resourceId: "fulfillment_provider_id", + filterableField: "stock_location_id", + }), ], }, ] diff --git a/packages/medusa/src/api/admin/fulfillment-providers/validators.ts b/packages/medusa/src/api/admin/fulfillment-providers/validators.ts index dd49441191..10376df43d 100644 --- a/packages/medusa/src/api/admin/fulfillment-providers/validators.ts +++ b/packages/medusa/src/api/admin/fulfillment-providers/validators.ts @@ -12,6 +12,7 @@ export const AdminFulfillmentProvidersParams = createFindParams({ .merge( z.object({ id: z.union([z.string(), z.array(z.string())]).optional(), + stock_location_id: z.union([z.string(), z.array(z.string())]).optional(), is_enabled: OptionalBooleanValidator, q: z.string().optional(), })