Skip to content

Commit

Permalink
Merge branch 'develop' into docs/fulfillment-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Sep 30, 2024
2 parents 01a3f6c + a4fc9d6 commit 47b2c52
Show file tree
Hide file tree
Showing 36 changed files with 618 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IEventBusModuleService } from "@medusajs/types"
import { CommonEvents, Modules } from "@medusajs/utils"
import FormData from "form-data"
import fs from "fs/promises"
import { TestEventUtils, medusaIntegrationTestRunner } from "medusa-test-utils"
import { medusaIntegrationTestRunner, TestEventUtils } from "medusa-test-utils"
import path from "path"
import {
adminHeaders,
Expand Down
8 changes: 8 additions & 0 deletions integration-tests/http/__tests__/region/admin/region.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ medusaIntegrationTestRunner({
expect(response.data.regions).toEqual([
expect.objectContaining({
name: "United Kingdom",
id: region1.id,
currency_code: "gbp",
automatic_taxes: region1.automatic_taxes,
countries: [],
}),
expect.objectContaining({
name: "United States",
id: region2.id,
currency_code: "usd",
automatic_taxes: region2.automatic_taxes,
countries: [],
}),
])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ export function generateCreateFulfillmentData(
provider_id: string
shipping_option_id: string
order_id: string
location_id: string
}
) {
const randomString = Math.random().toString(36).substring(7)

return {
location_id: "test-location",
location_id: data.location_id,
packed_at: null,
shipped_at: null,
delivered_at: null,
Expand Down Expand Up @@ -97,8 +98,10 @@ export async function setupFullDataFulfillmentStructure(
service: IFulfillmentModuleService,
{
providerId,
locationId,
}: {
providerId: string
locationId: string
}
) {
const randomString = Math.random().toString(36).substring(7)
Expand Down Expand Up @@ -133,6 +136,8 @@ export async function setupFullDataFulfillmentStructure(

await service.createFulfillment(
generateCreateFulfillmentData({
order_id: "fake-order",
location_id: locationId,
provider_id: providerId,
shipping_option_id: shippingOption.id,
})
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/modules/__tests__/fixtures/tax/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export const setupTaxStructure = async (service: ITaxModuleService) => {
},
{
country_code: "CA",
default_tax_rate: { name: "Canada Default Rate", rate: 5 },
default_tax_rate: {
name: "Canada Default Rate",
rate: 5,
code: "CA_DEF",
},
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import {
updateFulfillmentWorkflow,
updateFulfillmentWorkflowId,
} from "@medusajs/core-flows"
import { IFulfillmentModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import {
IFulfillmentModuleService,
MedusaContainer,
StockLocationDTO,
} from "@medusajs/types"
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
generateCreateFulfillmentData,
Expand All @@ -22,15 +26,110 @@ medusaIntegrationTestRunner({
env: { MEDUSA_FF_MEDUSA_V2: true },
testSuite: ({ getContainer }) => {
describe("Workflows: Fulfillment", () => {
let appContainer
let location: StockLocationDTO
let appContainer: MedusaContainer
let service: IFulfillmentModuleService

beforeAll(async () => {
appContainer = getContainer()
service = appContainer.resolve(Modules.FULFILLMENT)
})

beforeEach(async () => {
const stockLocationService = appContainer.resolve(
Modules.STOCK_LOCATION
)

location = await stockLocationService.createStockLocations({
name: "Test Location",
address: {
address_1: "Test Address",
address_2: "tttest",
city: "Test City",
country_code: "us",
postal_code: "12345",
metadata: { email: "[email protected]" },
},
metadata: { custom_location: "yes" },
})
})

describe("createFulfillmentWorkflow", () => {
describe("invoke", () => {
it("should get stock location", async () => {
const workflow = createFulfillmentWorkflow(appContainer)

const link = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

const shippingProfile = await service.createShippingProfiles({
name: "test",
type: "default",
})

const fulfillmentSet = await service.createFulfillmentSets({
name: "test",
type: "test-type",
})

await link.create({
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
})

const serviceZone = await service.createServiceZones({
name: "test",
fulfillment_set_id: fulfillmentSet.id,
})

const shippingOption = await service.createShippingOptions(
generateCreateShippingOptionsData({
provider_id: providerId,
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
})
)

const data = generateCreateFulfillmentData({
provider_id: providerId,
shipping_option_id: shippingOption.id,
order_id: "fake-order",
location_id: location.id,
})

const { transaction } = await workflow.run({
input: data,
throwOnError: true,
})

expect(
transaction.context.invoke["get-location"].output.output
).toEqual({
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
name: "Test Location",
address: {
id: expect.any(String),
address_1: "Test Address",
address_2: "tttest",
city: "Test City",
country_code: "us",
postal_code: "12345",
metadata: { email: "[email protected]" },
phone: null,
province: null,
},
metadata: { custom_location: "yes" },
})
})
})

describe("compensation", () => {
it("should cancel created fulfillment if step following step throws error", async () => {
const workflow = createFulfillmentWorkflow(appContainer)
Expand Down Expand Up @@ -70,6 +169,7 @@ medusaIntegrationTestRunner({
provider_id: providerId,
shipping_option_id: shippingOption.id,
order_id: "fake-order",
location_id: location.id,
})
const { errors } = await workflow.run({
input: data,
Expand Down Expand Up @@ -130,19 +230,24 @@ medusaIntegrationTestRunner({
)

const data = generateCreateFulfillmentData({
order_id: "fake-order",
provider_id: providerId,
shipping_option_id: shippingOption.id,
location_id: location.id,
})

const fulfillment = await service.createFulfillment(data)
const fulfillment = await service.createFulfillment({
...data,
location,
})

const date = new Date()
const { errors } = await workflow.run({
input: {
id: fulfillment.id,
shipped_at: date,
packed_at: date,
location_id: "new location",
location_id: location.id,
},
throwOnError: false,
})
Expand Down Expand Up @@ -209,12 +314,15 @@ medusaIntegrationTestRunner({
)

const data = generateCreateFulfillmentData({
order_id: "fake-order",
provider_id: providerId,
shipping_option_id: shippingOption.id,
location_id: location.id,
})

const fulfillment = await service.createFulfillment({
...data,
location,
labels: [],
})

Expand Down
40 changes: 34 additions & 6 deletions integration-tests/modules/__tests__/fulfillment/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFulfillmentModuleService } from "@medusajs/types"
import { IFulfillmentModuleService, StockLocationDTO } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../helpers/create-admin-user"
Expand All @@ -21,6 +21,7 @@ medusaIntegrationTestRunner({
testSuite: ({ getContainer, api, dbConnection }) => {
let service: IFulfillmentModuleService
let container
let location: StockLocationDTO

beforeAll(() => {
container = getContainer()
Expand All @@ -29,6 +30,20 @@ medusaIntegrationTestRunner({

beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, container)
const stockLocationService = container.resolve(Modules.STOCK_LOCATION)

location = await stockLocationService.createStockLocations({
name: "Test Location",
address: {
address_1: "Test Address",
address_2: "tttest",
city: "Test City",
country_code: "us",
postal_code: "12345",
metadata: { email: "[email protected]" },
},
metadata: { custom_location: "yes" },
})
})

/**
Expand All @@ -38,7 +53,10 @@ medusaIntegrationTestRunner({
*/
describe("Fulfillment module migrations backward compatibility", () => {
it("should allow to create a full data structure after the backward compatible migration have run on top of the medusa v1 database", async () => {
await setupFullDataFulfillmentStructure(service, { providerId })
await setupFullDataFulfillmentStructure(service, {
providerId,
locationId: location.id,
})

const fulfillmentSets = await service.listFulfillmentSets(
{},
Expand Down Expand Up @@ -92,7 +110,10 @@ medusaIntegrationTestRunner({
})

it("should cancel a fulfillment", async () => {
await setupFullDataFulfillmentStructure(service, { providerId })
await setupFullDataFulfillmentStructure(service, {
providerId,
locationId: location.id,
})

const [fulfillment] = await service.listFulfillments()

Expand Down Expand Up @@ -138,6 +159,7 @@ medusaIntegrationTestRunner({
)

const data = generateCreateFulfillmentData({
location_id: location.id,
provider_id: providerId,
shipping_option_id: shippingOption.id,
order_id: "order_123",
Expand All @@ -151,7 +173,7 @@ medusaIntegrationTestRunner({
expect(response.data.fulfillment).toEqual(
expect.objectContaining({
id: expect.any(String),
location_id: "test-location",
location_id: location.id,
packed_at: null,
shipped_at: null,
delivered_at: null,
Expand Down Expand Up @@ -218,7 +240,10 @@ medusaIntegrationTestRunner({
})

it("should update a fulfillment to be shipped", async () => {
await setupFullDataFulfillmentStructure(service, { providerId })
await setupFullDataFulfillmentStructure(service, {
providerId,
locationId: location.id,
})

const [fulfillment] = await service.listFulfillments()

Expand Down Expand Up @@ -255,7 +280,10 @@ medusaIntegrationTestRunner({
})

it("should throw error when already shipped", async () => {
await setupFullDataFulfillmentStructure(service, { providerId })
await setupFullDataFulfillmentStructure(service, {
providerId,
locationId: location.id,
})

const [fulfillment] = await service.listFulfillments()

Expand Down
Loading

0 comments on commit 47b2c52

Please sign in to comment.