Skip to content

Commit

Permalink
chore: fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidweterings authored and mvantellingen committed Apr 30, 2021
1 parent d5d1251 commit 5e1e03d
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/repositories/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
ProductDraft,
ProductPublishAction,
ProductSetAttributeAction,
ProductVariant, ProductVariantDraft,
ProductVariant,
ProductVariantDraft,
ReferenceTypeId,
} from '@commercetools/platform-sdk'
import { getBaseResourceProperties } from '../helpers'
import AbstractRepository from './abstract'
import { Writable } from '../types'



export class ProductRepository extends AbstractRepository {
getTypeId(): ReferenceTypeId {
return 'product'
Expand All @@ -23,10 +22,13 @@ export class ProductRepository extends AbstractRepository {
name: draft.name,
slug: draft.slug,
categories: [],
masterVariant: draft.masterVariant && variantFromDraft(0, draft.masterVariant!),
variants: draft.variants && draft.variants.map((variant, index) => {
return variantFromDraft(index + 1, variant)
}),
masterVariant:
draft.masterVariant && variantFromDraft(0, draft.masterVariant!),
variants:
draft.variants &&
draft.variants.map((variant, index) => {
return variantFromDraft(index + 1, variant)
}),

// @ts-ignore
searchKeywords: draft.searchKeywords,
Expand Down Expand Up @@ -70,7 +72,11 @@ export class ProductRepository extends AbstractRepository {
) => {
const isStaged = staged !== undefined ? staged : false
const productData = getProductData(resource, isStaged)
const [variant, isMasterVariant, variantIndex] = getVariant(productData, variantId, sku)
const { variant, isMasterVariant, variantIndex } = getVariant(
productData,
variantId,
sku
)
if (!variant) {
throw new Error(
`Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
Expand Down Expand Up @@ -160,11 +166,17 @@ const getProductData = (product: Product, staged: boolean) => {
return product.masterData.staged
}

interface VariantResult {
variant: Writable<ProductVariant> | undefined
isMasterVariant: boolean
variantIndex: number
}

const getVariant = (
productData: ProductData,
variantId?: number,
sku?: string
): [Writable<ProductVariant | undefined>, boolean, number] => {
): VariantResult => {
const variants = [productData.masterVariant, ...productData.variants]
const foundVariant = variants.find((variant: ProductVariant) => {
if (variantId) {
Expand All @@ -177,13 +189,23 @@ const getVariant = (
})

const isMasterVariant = foundVariant === productData.masterVariant
return [foundVariant, isMasterVariant, !isMasterVariant && foundVariant ? productData.variants.indexOf(foundVariant) : -1]
return {
variant: foundVariant,
isMasterVariant,
variantIndex:
!isMasterVariant && foundVariant
? productData.variants.indexOf(foundVariant)
: -1,
}
}

const variantFromDraft = (variantId: number, variant: ProductVariantDraft): ProductVariant => {
const variantFromDraft = (
variantId: number,
variant: ProductVariantDraft
): ProductVariant => {
return {
id: variantId,
sku: variant?.sku,
attributes: variant?.attributes,
}
}
}

0 comments on commit 5e1e03d

Please sign in to comment.