feat(*): shipping options update API (#6987)

**What**
- Update the `updateShippingOptions` module service API to follow the newest convention
- Add upsert support for shipping options
- update shipping options workflow
- update shipping options api end point and validation
- update shipping options all integration tests

FIXES CORE-1926
This commit is contained in:
Adrien de Peretti
2024-04-07 16:28:59 +00:00
committed by GitHub
parent f65fbff535
commit f132929c7e
20 changed files with 916 additions and 74 deletions
@@ -1,197 +0,0 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types"
import { RuleOperator } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Admin: Shipping Option Rules API", () => {
let appContainer
let shippingOption
let fulfillmentModule: IFulfillmentModuleService
const shippingOptionRule = {
operator: RuleOperator.EQ,
attribute: "old_attr",
value: "old value",
}
beforeAll(async () => {
appContainer = getContainer()
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
const shippingProfile = await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})
const fulfillmentSet = await fulfillmentModule.create({
name: "Test",
type: "test-type",
service_zones: [
{
name: "Test",
geo_zones: [{ type: "country", country_code: "us" }],
},
],
})
shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: "manual_test-provider",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
rules: [shippingOptionRule],
})
})
describe("POST /admin/shipping-options/:id/rules/batch/add", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
{
rules: [{ operator: RuleOperator.EQ, value: "new_value" }],
},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message:
"attribute must be a string, attribute should not be empty",
})
})
it.only("should throw error when shipping option does not exist", async () => {
const { response } = await api
.post(
`/admin/shipping-options/does-not-exist/rules/batch/add`,
{
rules: [
{ attribute: "new_attr", operator: "eq", value: "new value" },
],
},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(404)
expect(response.data).toEqual({
type: "not_found",
message:
"You tried to set relationship shipping_option_id: does-not-exist, but such entity does not exist",
})
})
it("should add rules to a shipping option successfully", async () => {
const response = await api.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
{
rules: [
{ operator: "eq", attribute: "new_attr", value: "new value" },
],
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.shipping_option).toEqual(
expect.objectContaining({
id: shippingOption.id,
rules: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
operator: "eq",
attribute: "old_attr",
value: "old value",
shipping_option_id: shippingOption.id,
}),
expect.objectContaining({
id: expect.any(String),
operator: "eq",
attribute: "new_attr",
value: "new value",
shipping_option_id: shippingOption.id,
}),
]),
})
)
})
})
describe("POST /admin/shipping-options/:id/rules/batch/remove", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
{},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message:
"each value in rule_ids must be a string, rule_ids should not be empty",
})
})
it("should throw error when shipping option does not exist", async () => {
const { response } = await api
.post(
`/admin/shipping-options/does-not-exist/rules/batch/remove`,
{ rule_ids: ["test"] },
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(404)
expect(response.data).toEqual({
type: "not_found",
message: "ShippingOption with id: does-not-exist was not found",
})
})
it("should add rules to a shipping option successfully", async () => {
const response = await api.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
{
rule_ids: [shippingOption.rules[0].id],
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.shipping_option).toEqual(
expect.objectContaining({
id: shippingOption.id,
rules: [],
})
)
})
})
})
},
})
@@ -1,207 +0,0 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
IFulfillmentModuleService,
IRegionModuleService,
} from "@medusajs/types"
import { RuleOperator } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Admin: Shipping Option API", () => {
let appContainer
let fulfillmentModule: IFulfillmentModuleService
let regionService: IRegionModuleService
let shippingProfile
let fulfillmentSet
let region
const shippingOptionRule = {
operator: RuleOperator.EQ,
attribute: "old_attr",
value: "old value",
}
beforeAll(async () => {
appContainer = getContainer()
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
shippingProfile = await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})
fulfillmentSet = await fulfillmentModule.create({
name: "Test",
type: "test-type",
service_zones: [
{
name: "Test",
geo_zones: [{ type: "country", country_code: "us" }],
},
],
})
region = await regionService.create({
name: "Test region",
countries: ["FR"],
currency_code: "eur",
})
})
describe("POST /admin/shipping-options", () => {
it("should throw error when required params are missing", async () => {
const shippingOptionPayload = {
name: "Test shipping option",
}
let err = await api
.post(
`/admin/shipping-options`,
shippingOptionPayload,
adminHeaders
)
.catch((e) => e.response)
const errorsFields = [
{
code: "invalid_type",
expected: "string",
received: "undefined",
path: ["service_zone_id"],
message: "Required",
},
{
code: "invalid_type",
expected: "string",
received: "undefined",
path: ["shipping_profile_id"],
message: "Required",
},
{
expected: "'calculated' | 'flat'",
received: "undefined",
code: "invalid_type",
path: ["price_type"],
message: "Required",
},
{
code: "invalid_type",
expected: "string",
received: "undefined",
path: ["provider_id"],
message: "Required",
},
{
code: "invalid_type",
expected: "object",
received: "undefined",
path: ["type"],
message: "Required",
},
{
code: "invalid_type",
expected: "array",
received: "undefined",
path: ["prices"],
message: "Required",
},
]
expect(err.status).toEqual(400)
expect(err.data).toEqual({
type: "invalid_data",
message: `Invalid request body: ${JSON.stringify(errorsFields)}`,
})
})
it("should create a shipping option successfully", async () => {
const shippingOptionPayload = {
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: "manual_test-provider",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
prices: [
{
currency_code: "usd",
amount: 1000,
},
{
region_id: region.id,
amount: 1000,
},
],
rules: [shippingOptionRule],
}
const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.shipping_option).toEqual(
expect.objectContaining({
id: expect.any(String),
name: shippingOptionPayload.name,
provider: expect.objectContaining({
id: shippingOptionPayload.provider_id,
}),
price_type: shippingOptionPayload.price_type,
type: expect.objectContaining({
id: expect.any(String),
label: shippingOptionPayload.type.label,
description: shippingOptionPayload.type.description,
code: shippingOptionPayload.type.code,
}),
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
prices: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
amount: 1000,
}),
expect.objectContaining({
id: expect.any(String),
currency_code: "eur",
amount: 1000,
}),
]),
rules: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
operator: "eq",
attribute: "old_attr",
value: "old value",
}),
]),
})
)
})
})
})
},
})
@@ -1,270 +0,0 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
FulfillmentSetDTO,
FulfillmentWorkflow,
IFulfillmentModuleService,
IRegionModuleService,
ServiceZoneDTO,
ShippingProfileDTO,
} from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import { createShippingOptionsWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
RuleOperator,
} from "@medusajs/utils"
jest.setTimeout(100000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const provider_id = "manual_test-provider"
medusaIntegrationTestRunner({
env,
testSuite: ({ getContainer }) => {
let service: IFulfillmentModuleService
let container
beforeAll(() => {
container = getContainer()
service = container.resolve(ModuleRegistrationName.FULFILLMENT)
})
describe("Fulfillment workflows", () => {
let fulfillmentSet: FulfillmentSetDTO
let serviceZone: ServiceZoneDTO
let shippingProfile: ShippingProfileDTO
beforeEach(async () => {
shippingProfile = await service.createShippingProfiles({
name: "test",
type: "default",
})
fulfillmentSet = await service.create({
name: "Test fulfillment set",
type: "manual_test",
})
serviceZone = await service.createServiceZones({
name: "Test service zone",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: "country",
country_code: "US",
},
],
})
})
it("should create shipping options and prices", async () => {
const regionService = container.resolve(
ModuleRegistrationName.REGION
) as IRegionModuleService
const [region] = await regionService.create([
{
name: "Test region",
currency_code: "eur",
countries: ["fr"],
},
])
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
{
name: "Test shipping option",
price_type: "flat",
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
provider_id,
type: {
code: "manual-type",
label: "Manual Type",
description: "Manual Type Description",
},
prices: [
{
currency_code: "usd",
amount: 10,
},
{
region_id: region.id,
amount: 100,
},
],
rules: [
{
attribute: "total",
operator: RuleOperator.EQ,
value: "100",
},
],
}
const { result } = await createShippingOptionsWorkflow(container).run({
input: [shippingOptionData],
})
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const remoteQueryObject = remoteQueryObjectFromString({
entryPoint: "shipping_option",
variables: {
id: result[0].id,
},
fields: [
"id",
"name",
"price_type",
"service_zone_id",
"shipping_profile_id",
"provider_id",
"data",
"metadata",
"type.*",
"created_at",
"updated_at",
"deleted_at",
"shipping_option_type_id",
"prices.*",
],
})
const [createdShippingOption] = await remoteQuery(remoteQueryObject)
const prices = createdShippingOption.prices
delete createdShippingOption.prices
expect(createdShippingOption).toEqual(
expect.objectContaining({
id: result[0].id,
name: shippingOptionData.name,
price_type: shippingOptionData.price_type,
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
provider_id: provider_id,
data: null,
metadata: null,
type: expect.objectContaining({
id: expect.any(String),
code: shippingOptionData.type.code,
label: shippingOptionData.type.label,
description: shippingOptionData.type.description,
}),
shipping_option_type_id: expect.any(String),
})
)
expect(prices).toHaveLength(2)
expect(prices).toContainEqual(
expect.objectContaining({
currency_code: "usd",
amount: 10,
})
)
expect(prices).toContainEqual(
expect.objectContaining({
currency_code: "eur",
amount: 100,
rules_count: 1,
})
)
})
it("should revert the shipping options and prices", async () => {
const regionService = container.resolve(
ModuleRegistrationName.REGION
) as IRegionModuleService
const [region] = await regionService.create([
{
name: "Test region",
currency_code: "eur",
countries: ["fr"],
},
])
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
{
name: "Test shipping option",
price_type: "flat",
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
provider_id,
type: {
code: "manual-type",
label: "Manual Type",
description: "Manual Type Description",
},
prices: [
{
currency_code: "usd",
amount: 10,
},
{
region_id: region.id,
amount: 100,
},
],
rules: [
{
attribute: "total",
operator: RuleOperator.EQ,
value: "100",
},
],
}
const workflow = createShippingOptionsWorkflow(container)
workflow.addAction(
"throw",
{
invoke: async function failStep() {
throw new Error(`Failed to create shipping options`)
},
},
{
noCompensation: true,
}
)
const { errors } = await workflow.run({
input: [shippingOptionData],
throwOnError: false,
})
expect(errors).toHaveLength(1)
expect(errors[0].error.message).toEqual(
`Failed to create shipping options`
)
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const remoteQueryObject = remoteQueryObjectFromString({
entryPoint: "shipping_option",
fields: ["id"],
})
const createdShippingOptions = await remoteQuery(remoteQueryObject)
expect(createdShippingOptions).toHaveLength(0)
const priceSetsRemoteQueryObject = remoteQueryObjectFromString({
entryPoint: "price_sets",
fields: ["id"],
})
const createdPriceSets = await remoteQuery(priceSetsRemoteQueryObject)
expect(createdPriceSets).toHaveLength(0)
})
})
},
})