feat: Add list cart option typings, make region optional on update cart (#7455)

This commit is contained in:
Stevche Radevski
2024-05-27 09:37:10 +02:00
committed by GitHub
parent 3b93bc9f55
commit 28a3f9a3df
9 changed files with 60 additions and 12 deletions
@@ -325,11 +325,9 @@ medusaIntegrationTestRunner({
})
expect(errors).toEqual([
{
action: "find-one-or-any-region",
handlerType: "invoke",
expect.objectContaining({
error: expect.objectContaining({ message: "No regions found" }),
},
}),
])
})
@@ -16,9 +16,12 @@ export const findOneOrAnyRegionStep = createStep(
)
if (data.regionId) {
const region = await service.retrieve(data.regionId)
return new StepResponse(region)
try {
const region = await service.retrieve(data.regionId)
return new StepResponse(region)
} catch (error) {
return new StepResponse(null)
}
}
const [store] = await storeModule.list()
@@ -32,7 +35,7 @@ export const findOneOrAnyRegionStep = createStep(
})
if (!region) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, "No regions found")
return new StepResponse(null)
}
return new StepResponse(region)
@@ -20,6 +20,7 @@ import { productVariantsFields } from "../utils/fields"
import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
import { MedusaError } from "@medusajs/utils"
// TODO: The createCartWorkflow are missing the following steps:
// - Refresh/delete shipping methods (fulfillment module)
@@ -49,6 +50,10 @@ export const createCartWorkflow = createWorkflow(
const pricingContext = transform(
{ input, region, customerData },
(data) => {
if (!data.region) {
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
}
return {
currency_code: data.input.currency_code ?? data.region.currency_code,
region_id: data.region.id,
@@ -87,6 +92,10 @@ export const createCartWorkflow = createWorkflow(
const cartInput = transform(
{ input, region, customerData, salesChannel },
(data) => {
if (!data.region) {
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
}
const data_ = {
...data.input,
currency_code: data.input.currency_code ?? data.region.currency_code,
@@ -1,5 +1,5 @@
import { UpdateCartWorkflowInputDTO } from "@medusajs/types"
import { PromotionActions, isPresent } from "@medusajs/utils"
import { MedusaError, PromotionActions, isPresent } from "@medusajs/utils"
import {
WorkflowData,
createWorkflow,
@@ -43,6 +43,13 @@ export const updateCartWorkflow = createWorkflow(
const data_ = { ...updateCartData }
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_.region_id = data.region.id
}
@@ -1,5 +1,5 @@
import { CreateOrderDTO, OrderDTO } from "@medusajs/types"
import { MathBN } from "@medusajs/utils"
import { MathBN, MedusaError } from "@medusajs/utils"
import {
WorkflowData,
createWorkflow,
@@ -95,6 +95,10 @@ export const createOrdersWorkflow = createWorkflow(
const pricingContext = transform(
{ input, region, customerData },
(data) => {
if (!data.region) {
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
}
return {
currency_code: data.input.currency_code ?? data.region.currency_code,
region_id: data.region.id,
+3 -2
View File
@@ -252,12 +252,13 @@ export class Store {
}
public fulfillment = {
// TODO: Finalize typings for list options
listCartOptions: async (
query?: FindParams & { cart_id: string },
headers?: ClientHeaders
) => {
return this.client.fetch<any>(`/store/shipping-options`, {
return this.client.fetch<{
shipping_options: HttpTypes.StoreCartShippingOption[]
}>(`/store/shipping-options`, {
headers,
query,
})
@@ -1 +1,2 @@
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",
"version",
"summary",
"display_id",
"total",
"currency_code",
"metadata",
"created_at",
"updated_at",