From dc218c14cae8d05bb0af6dde866481de6f0cef3c Mon Sep 17 00:00:00 2001 From: Kasper Date: Tue, 14 Sep 2021 19:41:33 +0200 Subject: [PATCH] resolved issues caused by using unreleased medusa-js version --- .../medusa/api/operations/get-product.ts | 4 +- framework/medusa/types.ts | 2 +- framework/medusa/utils/call-medusa.ts | 106 ++++++++++++++++++ .../utils/normalizers/normalize-products.ts | 10 +- 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/framework/medusa/api/operations/get-product.ts b/framework/medusa/api/operations/get-product.ts index 365437527..ed65fd411 100644 --- a/framework/medusa/api/operations/get-product.ts +++ b/framework/medusa/api/operations/get-product.ts @@ -21,8 +21,8 @@ export default function getProductOperation({ const response = await config.fetch('products', 'list', {}) - if (response.data?.products) { - const products: MedusaProduct[] = response.data.products + if (response.products) { + const products: MedusaProduct[] = response.products const product = products ? products.find(({ handle }) => handle === variables!.slug) : null diff --git a/framework/medusa/types.ts b/framework/medusa/types.ts index bb5beea41..7215e85f9 100644 --- a/framework/medusa/types.ts +++ b/framework/medusa/types.ts @@ -90,7 +90,7 @@ export interface MedusaProductVariant { title: string product_id: string product: MedusaProduct - prices: MedusaMoneyAmount + prices: MedusaMoneyAmount[] sku?: string barcase?: string ean?: string diff --git a/framework/medusa/utils/call-medusa.ts b/framework/medusa/utils/call-medusa.ts index ccbcec772..267312d01 100644 --- a/framework/medusa/utils/call-medusa.ts +++ b/framework/medusa/utils/call-medusa.ts @@ -265,6 +265,37 @@ export const callMedusa = async ( return await medusa.customers.update(customer_id, payload) } + case 'orders': + if (method === 'lookupOrder') { + const { payload } = variables + + if (!payload) { + throw new CommerceError({ + message: 'An argument for payload is required', + }) + } + + return await medusa.orders.lookupOrder(payload) + } else if (method === 'retrieve') { + const { order_id } = variables + + if (!order_id) { + throw new CommerceError({ + message: 'An argument for order_id is required', + }) + } + + return await medusa.orders.retrieve(order_id) + } else if (method === 'retrieveByCartId') { + const { cart_id } = variables + + if (!cart_id) { + throw new CommerceError({ + message: 'An argument for cart_id is required', + }) + } + return await medusa.orders.retrieveByCartId(cart_id) + } case 'products': if (method === 'variantsList') { const { params } = variables @@ -319,6 +350,81 @@ export const callMedusa = async ( message: 'No valid method argument was provided', }) } + case 'returnReasons': + if (method === 'list') { + return await medusa.returnReasons.list() + } else { + throw new CommerceError({ + message: 'No valid method argument was provided', + }) + } + case 'returns': + if (method === 'create') { + const { payload } = variables + + if (!payload) { + throw new CommerceError({ + message: 'An argument for payload is required', + }) + } + return await medusa.returns.create(payload) + } else { + throw new CommerceError({ + message: 'No valid method argument was provided', + }) + } + case 'shippingOptions': + if (method === 'list') { + const { cart_id } = variables + + if (!cart_id) { + throw new CommerceError({ + message: 'An argument for cart_id is required', + }) + } + + return await medusa.shippingOptions.list(cart_id) + } else if (method === 'create') { + const { cart_id } = variables + + if (!cart_id) { + throw new CommerceError({ + message: 'An argument for cart_id is required', + }) + } + + return await medusa.shippingOptions.listCartOptions(cart_id) + } else { + throw new CommerceError({ + message: 'No valid method argument was provided', + }) + } + case 'swaps': + if (method === 'create') { + const { cart_id } = variables + + if (!cart_id) { + throw new CommerceError({ + message: 'An argument for cart_id is required', + }) + } + + return await medusa.swaps.create({ cart_id }) + } else if (method === 'retrieve') { + const { cart_id } = variables + + if (!cart_id) { + throw new CommerceError({ + message: 'An argument for cart_id is required', + }) + } + + return await medusa.swaps.retrieve(cart_id) + } else { + throw new CommerceError({ + message: 'No valid method argument was provided', + }) + } default: throw new CommerceError({ message: 'No valid query argument was provided', diff --git a/framework/medusa/utils/normalizers/normalize-products.ts b/framework/medusa/utils/normalizers/normalize-products.ts index 62f89a441..75f6b9fd1 100644 --- a/framework/medusa/utils/normalizers/normalize-products.ts +++ b/framework/medusa/utils/normalizers/normalize-products.ts @@ -78,11 +78,11 @@ export function normalizeProduct({ handle: slug, thumbnail, }: MedusaProduct): Product { - const tmpVariant = medusaVariants.reduce((prev, curr) => - prev.prices.amount < curr.prices.amount ? prev : curr - ) + // const tmpVariant = medusaVariants.reduce((prev, curr) => + // prev.prices.amount < curr.prices.amount ? prev : curr + // ) - const minPrice = normalizePrice(tmpVariant.prices[0]) //need to fix typing in medusa types + // const minPrice = normalizePrice(tmpVariant.prices) //need to fix typing in medusa types return { id, @@ -91,7 +91,7 @@ export function normalizeProduct({ variants: normalizeProductVariants(medusaVariants), images: thumbnail && !images.length ? [{ url: thumbnail }] : images, options: normalizeOptions(medusaOptions), - price: minPrice, + price: normalizePrice(medusaVariants[0].prices[0]), path: `/${slug}`, slug, }