fix(medusa): Shipping profile CRUD (#3154)
**What** - Fixes wrong payload class for `POST /admin/shipping-profiles` - Fixes wrong payload class for `POST /admin/shipping-profiles/:id` - Fixes an issue where updating a shipping profile with products and/or shipping options would fail. - Fixes an issue where passing `profile_id` to `ShippingOptionService.update()` would not update the shipping profile of the option. **Testing** - Adds new `simpleshippingProfileFactory` - Adds new integration test suite for shipping profiles operations. Resolves CORE-1065
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
export * from "./simple-gift-card-factory"
|
||||
export * from "./simple-payment-factory"
|
||||
export * from "./simple-batch-job-factory"
|
||||
export * from "./simple-discount-factory"
|
||||
export * from "./simple-order-factory"
|
||||
export * from "./simple-cart-factory"
|
||||
export * from "./simple-region-factory"
|
||||
export * from "./simple-custom-shipping-option-factory"
|
||||
export * from "./simple-customer-factory"
|
||||
export * from "./simple-discount-factory"
|
||||
export * from "./simple-gift-card-factory"
|
||||
export * from "./simple-line-item-factory"
|
||||
export * from "./simple-order-edit-factory"
|
||||
export * from "./simple-order-factory"
|
||||
export * from "./simple-order-item-change-factory"
|
||||
export * from "./simple-payment-collection-factory"
|
||||
export * from "./simple-payment-factory"
|
||||
export * from "./simple-price-list-factory"
|
||||
export * from "./simple-product-category-factory"
|
||||
export * from "./simple-product-factory"
|
||||
export * from "./simple-product-variant-factory"
|
||||
export * from "./simple-product-tax-rate-factory"
|
||||
export * from "./simple-product-type-tax-rate-factory"
|
||||
export * from "./simple-product-variant-factory"
|
||||
export * from "./simple-region-factory"
|
||||
export * from "./simple-sales-channel-factory"
|
||||
export * from "./simple-shipping-method-factory"
|
||||
export * from "./simple-shipping-option-factory"
|
||||
export * from "./simple-shipping-profile-factory"
|
||||
export * from "./simple-shipping-tax-rate-factory"
|
||||
export * from "./simple-tax-rate-factory"
|
||||
export * from "./simple-shipping-option-factory"
|
||||
export * from "./simple-shipping-method-factory"
|
||||
export * from "./simple-product-type-tax-rate-factory"
|
||||
export * from "./simple-price-list-factory"
|
||||
export * from "./simple-batch-job-factory"
|
||||
export * from "./simple-sales-channel-factory"
|
||||
export * from "./simple-custom-shipping-option-factory"
|
||||
export * from "./simple-payment-collection-factory"
|
||||
export * from "./simple-order-edit-factory"
|
||||
export * from "./simple-order-item-change-factory"
|
||||
export * from "./simple-customer-factory"
|
||||
export * from "./simple-product-category-factory"
|
||||
|
||||
@@ -7,28 +7,29 @@ import {
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { Connection } from "typeorm"
|
||||
import { simpleRegionFactory } from "./simple-region-factory"
|
||||
|
||||
export type ShippingOptionFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
region_id: string
|
||||
region_id?: string
|
||||
is_return?: boolean
|
||||
is_giftcard?: boolean
|
||||
price?: number
|
||||
price_type?: ShippingOptionPriceType
|
||||
includes_tax?: boolean
|
||||
data?: object
|
||||
requirements: ShippingOptionRequirementData[]
|
||||
requirements?: ShippingOptionRequirementData[]
|
||||
}
|
||||
|
||||
type ShippingOptionRequirementData = {
|
||||
type: 'min_subtotal' | 'max_subtotal'
|
||||
type: "min_subtotal" | "max_subtotal"
|
||||
amount: number
|
||||
}
|
||||
|
||||
export const simpleShippingOptionFactory = async (
|
||||
connection: Connection,
|
||||
data: ShippingOptionFactoryData,
|
||||
data: ShippingOptionFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<ShippingOption> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
@@ -44,11 +45,18 @@ export const simpleShippingOptionFactory = async (
|
||||
type: ShippingProfileType.GIFT_CARD,
|
||||
})
|
||||
|
||||
let region_id = data.region_id
|
||||
|
||||
if (!region_id) {
|
||||
const { id } = await simpleRegionFactory(connection)
|
||||
region_id = id
|
||||
}
|
||||
|
||||
const shippingOptionData = {
|
||||
id: data.id ?? `simple-so-${Math.random() * 1000}`,
|
||||
name: data.name || "Test Method",
|
||||
is_return: data.is_return ?? false,
|
||||
region_id: data.region_id,
|
||||
region_id: region_id,
|
||||
provider_id: "test-ful",
|
||||
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
|
||||
price_type: data.price_type ?? ShippingOptionPriceType.FLAT_RATE,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ShippingProfile, ShippingProfileType } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { Connection } from "typeorm"
|
||||
|
||||
export type ShippingProfileFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
type?: ShippingProfileType
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const simpleShippingProfileFactory = async (
|
||||
connection: Connection,
|
||||
data: ShippingOptionFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<ShippingProfile> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = connection.manager
|
||||
|
||||
const shippingProfileData = {
|
||||
id: data.id ?? `simple-sp-${Math.random() * 1000}`,
|
||||
name: data.name || `sp-${Math.random() * 1000}`,
|
||||
type: data.type || ShippingProfileType.DEFAULT,
|
||||
metadata: data.metadata,
|
||||
products: [],
|
||||
shipping_options: [],
|
||||
}
|
||||
|
||||
const created = manager.create(ShippingProfile, shippingProfileData)
|
||||
|
||||
return await manager.save(created)
|
||||
}
|
||||
Reference in New Issue
Block a user