From c9323443ce957b429556e263a47c85091a5131d9 Mon Sep 17 00:00:00 2001 From: tniezg Date: Thu, 19 Aug 2021 11:34:33 +0200 Subject: [PATCH] Type Spree variants and line items and temporarily remove height, width and depth --- framework/spree/types/index.ts | 44 +++++++++++++++++++++++++ framework/spree/utils/normalize-cart.ts | 25 +++++++++----- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/framework/spree/types/index.ts b/framework/spree/types/index.ts index 1f3eb53a2..3ff71eaa1 100644 --- a/framework/spree/types/index.ts +++ b/framework/spree/types/index.ts @@ -44,3 +44,47 @@ export interface OptionTypeAttr extends JsonApiDocument { filterable: boolean } } + +export interface LineItemAttr extends JsonApiDocument { + attributes: { + name: string + quantity: number + slug: string + options_text: string + price: string + currency: string + display_price: string + total: string + display_total: string + adjustment_total: string + display_adjustment_total: string + additional_tax_total: string + display_additional_tax_total: string + discounted_amount: string + display_discounted_amount: string + pre_tax_amount: string + display_pre_tax_amount: string + promo_total: string + display_promo_total: string + included_tax_total: string + display_inluded_tax_total: string + } +} + +export interface VariantAttr extends JsonApiDocument { + attributes: { + sku: string + price: string + currency: string + display_price: string + weight: string + height: string + width: string + depth: string + is_master: boolean + options_text: string + purchasable: boolean + in_stock: boolean + backorderable: boolean + } +} diff --git a/framework/spree/utils/normalize-cart.ts b/framework/spree/utils/normalize-cart.ts index c8e4ea69e..23cd77267 100644 --- a/framework/spree/utils/normalize-cart.ts +++ b/framework/spree/utils/normalize-cart.ts @@ -7,7 +7,6 @@ import type { import MissingLineItemVariantError from '@framework/errors/MissingLineItemVariantError' import { requireConfigValue } from '@framework/isomorphic-config' import type { - JsonApiDocument, JsonApiListResponse, JsonApiSingleResponse, } from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi' @@ -17,7 +16,11 @@ import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces import createGetAbsoluteImageUrl from './create-get-absolute-image-url' import getMediaGallery from './get-media-gallery' import { findIncluded, findIncludedOfType } from './find-json-api-documents' -import type { OptionTypeAttr } from '@framework/types' +import type { + LineItemAttr, + OptionTypeAttr, + VariantAttr, +} from '@framework/types' const isColorProductOption = (productOptionType: OptionTypeAttr) => { return productOptionType.attributes.presentation === 'Color' @@ -25,7 +28,7 @@ const isColorProductOption = (productOptionType: OptionTypeAttr) => { const normalizeVariant = ( spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, - spreeVariant: JsonApiDocument + spreeVariant: VariantAttr ): ProductVariant => { const productIdentifier = spreeVariant.relationships.product .data as RelationType @@ -81,18 +84,22 @@ const normalizeVariant = ( image: lineItemImage, isInStock: spreeVariant.attributes.in_stock, availableForSale: spreeVariant.attributes.purchasable, - weight: spreeVariant.attributes.weight, - height: spreeVariant.attributes.height, - width: spreeVariant.attributes.width, - depth: spreeVariant.attributes.depth, + ...(spreeVariant.attributes.weight === '0.0' + ? {} + : { + weight: { + value: parseFloat(spreeVariant.attributes.weight), + unit: 'KILOGRAMS', + }, + }), + // TODO: Add height, width and depth when Measurement type allows distance measurements. } } const normalizeLineItem = ( spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, - spreeLineItem: JsonApiDocument + spreeLineItem: LineItemAttr ): LineItem => { - //TODO: Replace JsonApiDocument type in spreeLineItem with more specific, new Spree line item item const variantIdentifier = spreeLineItem.relationships.variant .data as RelationType const variant = findIncluded(