Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-flows,medusa): add endpoint to add/remove fulfillment providers to location #8299

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions integration-tests/http/__tests__/returns/returns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,27 +202,17 @@ medusaIntegrationTestRunner({
adminHeaders
)

await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ContainerRegistrationKeys,
Modules,
RuleOperator,
} from "@medusajs/utils"
import { RuleOperator } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
Expand Down Expand Up @@ -65,6 +61,12 @@ medusaIntegrationTestRunner({
)
).data.stock_location

await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

fulfillmentSet = (
await api.post(
`/admin/fulfillment-sets/${location.fulfillment_sets[0].id}/service-zones`,
Expand Down Expand Up @@ -111,22 +113,6 @@ medusaIntegrationTestRunner({
})

it("should create a shipping option successfully", async () => {
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])

const shippingOptionPayload = {
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
Expand Down Expand Up @@ -271,22 +257,6 @@ medusaIntegrationTestRunner({
],
}

const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])

const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
Expand Down Expand Up @@ -432,22 +402,6 @@ medusaIntegrationTestRunner({
rules: [shippingOptionRule],
}

const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])

const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
Expand Down Expand Up @@ -501,22 +455,6 @@ medusaIntegrationTestRunner({
rules: [shippingOptionRule],
}

const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])

const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
adminHeaders,
createAdminUser,
} from "../../../../helpers/create-admin-user"

import { medusaIntegrationTestRunner } from "medusa-test-utils"

jest.setTimeout(30000)

medusaIntegrationTestRunner({
testSuite: ({ dbConnection, getContainer, api }) => {
let location

beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, getContainer())

location = (
await api.post(
`/admin/stock-locations`,
{
name: "Test Location 1",
address: {
address_1: "Test Address",
country_code: "US",
},
},
adminHeaders
)
).data.stock_location
})

describe("POST /admin/stock-locations/:id/fulfillment-providers/batch", () => {
riqwan marked this conversation as resolved.
Show resolved Hide resolved
it("should add a fulfillment provider to a stock location successfully", async () => {
const response = await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers?fields=id,*fulfillment_providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.stock_location.fulfillment_providers).toEqual([
{ id: "manual_test-provider", is_enabled: true },
])
})

it("should detach a fulfillment provider from a stock location successfully", async () => {
await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

const response = await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers?fields=id,*fulfillment_providers`,
{ remove: ["manual_test-provider"] },
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.stock_location.fulfillment_providers).toHaveLength(
0
)
})
})
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -1499,16 +1499,11 @@ medusaIntegrationTestRunner({
},
])

await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await api.post(
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

const shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
Expand Down
15 changes: 5 additions & 10 deletions integration-tests/modules/__tests__/cart/store/carts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1941,16 +1941,11 @@ medusaIntegrationTestRunner({
},
])

await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await api.post(
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

shippingOption = (
await api.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,12 @@ medusaIntegrationTestRunner({
},
])

await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await api.post(
`/admin/stock-locations/${location.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

const serviceZone = await fulfillmentModule.createServiceZones({
name: "Test",
fulfillment_set_id: fulfillmentSet.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ async function prepareDataFixtures({ container }) {
fulfillment_provider_id: "manual_test-provider",
},
},
])

await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,14 @@ medusaIntegrationTestRunner({
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])

await api.post(
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
{ add: ["manual_test-provider"] },
adminHeaders
)

shippingOption = (
await api.post(
`/admin/shipping-options`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const updateRemoteLinksStepId = "update-remote-links-step"
export const updateRemoteLinksStep = createStep(
updateRemoteLinksStepId,
async (data: LinkDefinition[], { container }) => {
if (!data.length) {
if (!data?.length) {
return new StepResponse([], [])
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { batchLinksWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { Modules } from "@medusajs/utils"

import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { refetchStockLocation } from "../../helpers"

const buildLinks = (id, fulfillmentProviderIds: string[]) => {
return fulfillmentProviderIds.map((fulfillmentProviderId) => ({
[Modules.STOCK_LOCATION]: { stock_location_id: id },
[Modules.FULFILLMENT]: {
fulfillment_provider_id: fulfillmentProviderId,
},
}))
}

export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
res: MedusaResponse
) => {
const { id } = req.params
const { add = [], remove = [] } = req.validatedBody

await batchLinksWorkflow(req.scope).run({
input: {
create: buildLinks(id, add),
delete: buildLinks(id, remove),
},
})

const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)

res.status(200).json({ stock_location: stockLocation })
}
11 changes: 11 additions & 0 deletions packages/medusa/src/api/admin/stock-locations/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["POST"],
matcher: "/admin/stock-locations/:id/fulfillment-providers",
middlewares: [
validateAndTransformBody(createLinkBody()),
validateAndTransformQuery(
AdminGetStockLocationParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
]
Loading