feat: Add list cart option typings, make region optional on update cart (#7455)
This commit is contained in:
1 parent
3b93bc9f55
commit
28a3f9a3df
9 files changed
+58
-10
No files matched your search
@@ -325,11 +325,9 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(errors).toEqual([
|
expect(errors).toEqual([
|
||||||
{
|
expect.objectContaining({
|
||||||
action: "find-one-or-any-region",
|
|
||||||
handlerType: "invoke",
|
|
||||||
error: expect.objectContaining({ message: "No regions found" }),
|
error: expect.objectContaining({ message: "No regions found" }),
|
||||||
},
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,12 @@ export const findOneOrAnyRegionStep = createStep(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (data.regionId) {
|
if (data.regionId) {
|
||||||
|
try {
|
||||||
const region = await service.retrieve(data.regionId)
|
const region = await service.retrieve(data.regionId)
|
||||||
|
|
||||||
return new StepResponse(region)
|
return new StepResponse(region)
|
||||||
|
} catch (error) {
|
||||||
|
return new StepResponse(null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [store] = await storeModule.list()
|
const [store] = await storeModule.list()
|
||||||
@@ -32,7 +35,7 @@ export const findOneOrAnyRegionStep = createStep(
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!region) {
|
if (!region) {
|
||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, "No regions found")
|
return new StepResponse(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StepResponse(region)
|
return new StepResponse(region)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import { productVariantsFields } from "../utils/fields"
|
|||||||
import { prepareLineItemData } from "../utils/prepare-line-item-data"
|
import { prepareLineItemData } from "../utils/prepare-line-item-data"
|
||||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||||
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
||||||
|
import { MedusaError } from "@medusajs/utils"
|
||||||
|
|
||||||
// TODO: The createCartWorkflow are missing the following steps:
|
// TODO: The createCartWorkflow are missing the following steps:
|
||||||
// - Refresh/delete shipping methods (fulfillment module)
|
// - Refresh/delete shipping methods (fulfillment module)
|
||||||
@@ -49,6 +50,10 @@ export const createCartWorkflow = createWorkflow(
|
|||||||
const pricingContext = transform(
|
const pricingContext = transform(
|
||||||
{ input, region, customerData },
|
{ input, region, customerData },
|
||||||
(data) => {
|
(data) => {
|
||||||
|
if (!data.region) {
|
||||||
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
region_id: data.region.id,
|
region_id: data.region.id,
|
||||||
@@ -87,6 +92,10 @@ export const createCartWorkflow = createWorkflow(
|
|||||||
const cartInput = transform(
|
const cartInput = transform(
|
||||||
{ input, region, customerData, salesChannel },
|
{ input, region, customerData, salesChannel },
|
||||||
(data) => {
|
(data) => {
|
||||||
|
if (!data.region) {
|
||||||
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
|
||||||
|
}
|
||||||
|
|
||||||
const data_ = {
|
const data_ = {
|
||||||
...data.input,
|
...data.input,
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { UpdateCartWorkflowInputDTO } from "@medusajs/types"
|
import { UpdateCartWorkflowInputDTO } from "@medusajs/types"
|
||||||
import { PromotionActions, isPresent } from "@medusajs/utils"
|
import { MedusaError, PromotionActions, isPresent } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
@@ -43,6 +43,13 @@ export const updateCartWorkflow = createWorkflow(
|
|||||||
const data_ = { ...updateCartData }
|
const data_ = { ...updateCartData }
|
||||||
|
|
||||||
if (isPresent(updateCartData.region_id)) {
|
if (isPresent(updateCartData.region_id)) {
|
||||||
|
if (!data.region) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_FOUND,
|
||||||
|
"Region not found"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
data_.currency_code = data.region.currency_code
|
data_.currency_code = data.region.currency_code
|
||||||
data_.region_id = data.region.id
|
data_.region_id = data.region.id
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { CreateOrderDTO, OrderDTO } from "@medusajs/types"
|
import { CreateOrderDTO, OrderDTO } from "@medusajs/types"
|
||||||
import { MathBN } from "@medusajs/utils"
|
import { MathBN, MedusaError } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
@@ -95,6 +95,10 @@ export const createOrdersWorkflow = createWorkflow(
|
|||||||
const pricingContext = transform(
|
const pricingContext = transform(
|
||||||
{ input, region, customerData },
|
{ input, region, customerData },
|
||||||
(data) => {
|
(data) => {
|
||||||
|
if (!data.region) {
|
||||||
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
region_id: data.region.id,
|
region_id: data.region.id,
|
||||||
|
|||||||
@@ -252,12 +252,13 @@ export class Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fulfillment = {
|
public fulfillment = {
|
||||||
// TODO: Finalize typings for list options
|
|
||||||
listCartOptions: async (
|
listCartOptions: async (
|
||||||
query?: FindParams & { cart_id: string },
|
query?: FindParams & { cart_id: string },
|
||||||
headers?: ClientHeaders
|
headers?: ClientHeaders
|
||||||
) => {
|
) => {
|
||||||
return this.client.fetch<any>(`/store/shipping-options`, {
|
return this.client.fetch<{
|
||||||
|
shipping_options: HttpTypes.StoreCartShippingOption[]
|
||||||
|
}>(`/store/shipping-options`, {
|
||||||
headers,
|
headers,
|
||||||
query,
|
query,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
export * from "./admin"
|
export * from "./admin"
|
||||||
|
export * from "./store"
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// TODO: The way the cart shipping options are listed now differs from most other endpoints as it is fetched in a workflow.
|
||||||
|
// We should consider refactoring this to be more consistent with other endpoints.
|
||||||
|
export interface StoreCartShippingOption {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
price_type: string
|
||||||
|
service_zone_id: string
|
||||||
|
shipping_profile_id: string
|
||||||
|
provider_id: string
|
||||||
|
data: Record<string, unknown> | null
|
||||||
|
type: {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
description: string
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
provider: {
|
||||||
|
id: string
|
||||||
|
is_enabled: boolean
|
||||||
|
}
|
||||||
|
amount: number
|
||||||
|
}
|
||||||
@@ -5,6 +5,9 @@ export const defaultStoreOrderFields = [
|
|||||||
"status",
|
"status",
|
||||||
"version",
|
"version",
|
||||||
"summary",
|
"summary",
|
||||||
|
"display_id",
|
||||||
|
"total",
|
||||||
|
"currency_code",
|
||||||
"metadata",
|
"metadata",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
|
|||||||
Reference in new issue
Block a user