Skip to content

Commit

Permalink
Add subscriptions to variations
Browse files Browse the repository at this point in the history
  • Loading branch information
thalytafabrine committed Feb 9, 2024
1 parent 23c878a commit 4b5b0dc
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/convertSearchDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,31 +251,44 @@ const getSkuSellers = (offer: SkuOfferDetails, unitMultiplier: number) => {
return setDefaultSeller([seller1].concat(otherSellers))
}

const getSkuVariations = (SpecificationGroups: SkuDocumentSpecificationGroup[]) => {
const getSkuSubscriptions = (agregatedAttachments: SkuDocumentAttachment[]) =>
agregatedAttachments.filter((attachment) => attachment.Name.includes('vtex.subscription.') && attachment.IsActive)

const getSkuVariations = (
specificationGroups: SkuDocumentSpecificationGroup[],
attachments: SkuDocumentAttachment[]
) => {
const attributes: Array<{
key: string
value: string
}> = []

SpecificationGroups.forEach((group) => {
specificationGroups.forEach((group) => {
const filteredSpecs = group.Specifications.filter(
(specification) =>
specification.SpecificationValues.filter((spec) => spec.Value).length && specification.Field.IsStockKeppingUnit
)
).map((specification) => ({
...specification,
SpecificationValues: specification.SpecificationValues.filter((spec) => spec.Value),
}))

filteredSpecs.forEach((spec) => {
spec.SpecificationValues.forEach((value) => {
attributes.push({
key: spec.Field.Name,
value: value.Value,
})
// build_sku_subscriptions_sku_attributes (?)
// translate (ver text_attributes)
})
})
})

const variations = attributes.map((attr) => ({
const subscriptions = getSkuSubscriptions(attachments).map((subscription) => ({
key: 'activeSubscriptions',
value: subscription.Name.replace('vtex.subscription.', ''),
}))

const variations = attributes.concat(subscriptions).map((attr) => ({
name: attr.key,
values: [attr.value],
}))
Expand All @@ -302,7 +315,7 @@ export const itemsFromSearchDocuments = (documents: SkuDocument[], offers: SkuOf
return
}

const variations = getSkuVariations(skuDocument.SpecificationGroups)
const variations = getSkuVariations(skuDocument.SpecificationGroups, skuDocument.AgregatedAttachments)
const images = convertDocumentImages(skuDocument.Images, account)
const attachments = getAttachments(skuDocument.AgregatedAttachments)

Expand Down

0 comments on commit 4b5b0dc

Please sign in to comment.