mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 12:11:22 +00:00
Save primary variant's SKU when normalizing a product from Spree
This commit is contained in:
parent
c3bd932d69
commit
d98a75503d
@ -47,7 +47,8 @@ export default function getAllProductsOperation({
|
|||||||
arguments: [
|
arguments: [
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
include: 'variants,images,option_types,variants.option_values',
|
include:
|
||||||
|
'primary_variant,variants,images,option_types,variants.option_values',
|
||||||
per_page: first,
|
per_page: first,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -54,7 +54,8 @@ export default function getProductOperation({
|
|||||||
getProductVariables.slug,
|
getProductVariables.slug,
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
include: 'variants,images,option_types,variants.option_values',
|
include:
|
||||||
|
'primary_variant,variants,images,option_types,variants.option_values',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,8 @@ export const handler: SWRHook<SearchProductsHook> = {
|
|||||||
arguments: [
|
arguments: [
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
include: 'variants,images,option_types,variants.option_values',
|
include:
|
||||||
|
'primary_variant,variants,images,option_types,variants.option_values',
|
||||||
per_page: 50,
|
per_page: 50,
|
||||||
...filter,
|
...filter,
|
||||||
...sort,
|
...sort,
|
||||||
|
@ -14,13 +14,30 @@ import { requireConfigValue } from '@framework/isomorphic-config'
|
|||||||
import createGetAbsoluteImageUrl from './create-get-absolute-image-url'
|
import createGetAbsoluteImageUrl from './create-get-absolute-image-url'
|
||||||
import expandOptions from './expand-options'
|
import expandOptions from './expand-options'
|
||||||
import getMediaGallery from './get-media-gallery'
|
import getMediaGallery from './get-media-gallery'
|
||||||
import { findIncludedOfType } from './find-json-api-documents'
|
import { findIncluded, findIncludedOfType } from './find-json-api-documents'
|
||||||
import getProductPath from './get-product-path'
|
import getProductPath from './get-product-path'
|
||||||
|
import MissingPrimaryVariantError from '@framework/errors/MissingPrimaryVariantError'
|
||||||
|
|
||||||
const normalizeProduct = (
|
const normalizeProduct = (
|
||||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||||
spreeProduct: ProductAttr
|
spreeProduct: ProductAttr
|
||||||
): Product => {
|
): Product => {
|
||||||
|
const primaryVariantIdentifier = spreeProduct.relationships.primary_variant
|
||||||
|
.data as RelationType
|
||||||
|
const primaryVariant = findIncluded(
|
||||||
|
spreeSuccessResponse,
|
||||||
|
primaryVariantIdentifier.type,
|
||||||
|
primaryVariantIdentifier.id
|
||||||
|
)
|
||||||
|
|
||||||
|
if (primaryVariant === null) {
|
||||||
|
throw new MissingPrimaryVariantError(
|
||||||
|
`Couldn't find primary variant with id ${primaryVariantIdentifier.id}.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const sku = primaryVariant.attributes.sku
|
||||||
|
|
||||||
const spreeImageRecords = findIncludedOfType(
|
const spreeImageRecords = findIncludedOfType(
|
||||||
spreeSuccessResponse,
|
spreeSuccessResponse,
|
||||||
spreeProduct,
|
spreeProduct,
|
||||||
@ -95,6 +112,7 @@ const normalizeProduct = (
|
|||||||
price,
|
price,
|
||||||
slug,
|
slug,
|
||||||
path,
|
path,
|
||||||
|
sku,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user