commerce/lib/helpers/skus.tsx
2023-11-23 11:26:26 +00:00

59 lines
2.0 KiB
TypeScript

import {
collectionsSKUs,
colorSKUs,
customisationSKUs,
garmentHandleKeys,
garmentSKUs,
garmentSizes,
printSizeSKUs,
sizeSKUs,
} from "constants/sku";
type TitleInfo = Awaited<ReturnType<typeof extractInfoFromTitle>>;
const garmentHandleKeyMapper = (garmentKeys: string[]) => {
const garmentTitle = garmentKeys.join(' ');
const garmentKey = garmentHandleKeys[garmentTitle as keyof typeof garmentHandleKeys]
return garmentSKUs[garmentKey as keyof typeof garmentSKUs]
}
const extractInfoFromTitle = (productTitle: string) => {
const title = productTitle.split(' ');
const collection = title[0]?.toLowerCase();
const garmentKeys = title.slice(2)
return {
title,
collection,
garmentTitle: garmentKeys.join(' '),
artworkNumber: title[1]?.replace('No.', ''),
garmentKeys,
collectionKey: collection!.replace('scape', ''),
}
}
const collectionSKUMapper = (titleInfo: TitleInfo, size: keyof typeof sizeSKUs) => {
const collectionSKU = collectionsSKUs[titleInfo.collectionKey as keyof typeof collectionsSKUs];
const artworkSKU = titleInfo.artworkNumber!.padStart(4, "0");
const garmentSKU = garmentHandleKeyMapper(titleInfo.garmentKeys);
const printSizeSKU = printSizeSKUs[size];
return `SCSQ${collectionSKU}${artworkSKU}${printSizeSKU}_${garmentSKU}`;
}
const customisationSKUMapper = () =>
`${colorSKUs.black}_${customisationSKUs.printArea}_${customisationSKUs.tags}`
export const createProductSKUs = (productTitle: string) => {
const titleInfo = extractInfoFromTitle(productTitle);
const garmentKey = garmentHandleKeys[titleInfo.garmentTitle as keyof typeof garmentHandleKeys]
const getGarmentSizes = garmentSizes[garmentKey]
const customisationSKUs = customisationSKUMapper();
return getGarmentSizes?.map(size => {
const currentSizeSKU = sizeSKUs[size as keyof typeof sizeSKUs]
const collectionSKU = collectionSKUMapper(titleInfo, currentSizeSKU as keyof typeof sizeSKUs)
return `${collectionSKU}_${currentSizeSKU}_${customisationSKUs}`
})
}