feat: Use tag ids instead of values wherever possible (#8394)

This commit is contained in:
Stevche Radevski
2024-08-02 09:22:03 +02:00
committed by GitHub
parent 71f0d24359
commit 3a068c6b27
26 changed files with 259 additions and 229 deletions

View File

@@ -1,10 +1,14 @@
import { ProductTypes } from "@medusajs/types"
import { HttpTypes, RegionTypes } from "@medusajs/types"
import { MedusaError, lowerCaseFirst } from "@medusajs/utils"
// We want to convert the csv data format to a standard DTO format.
export const normalizeForImport = (
rawProducts: object[],
regions: RegionTypes.RegionDTO[]
additional: {
regions: RegionTypes.RegionDTO[]
tags: ProductTypes.ProductTagDTO[]
}
): HttpTypes.AdminCreateProduct[] => {
const productMap = new Map<
string,
@@ -15,13 +19,16 @@ export const normalizeForImport = (
>()
// Currently region names are treated as case-insensitive.
const regionsMap = new Map(regions.map((r) => [r.name.toLowerCase(), r]))
const regionsMap = new Map(
additional.regions.map((r) => [r.name.toLowerCase(), r])
)
const tagsMap = new Map(additional.tags.map((t) => [t.value, t]))
rawProducts.forEach((rawProduct) => {
const productInMap = productMap.get(rawProduct["Product Handle"])
if (!productInMap) {
productMap.set(rawProduct["Product Handle"], {
product: normalizeProductForImport(rawProduct),
product: normalizeProductForImport(rawProduct, tagsMap),
variants: [normalizeVariantForImport(rawProduct, regionsMap)],
})
return
@@ -86,7 +93,8 @@ const booleanFields = [
]
const normalizeProductForImport = (
rawProduct: object
rawProduct: object,
tagsMap: Map<string, ProductTypes.ProductTagDTO>
): HttpTypes.AdminCreateProduct => {
const response = {}
@@ -108,10 +116,15 @@ const normalizeProductForImport = (
}
if (normalizedKey.startsWith("product_tag_")) {
response["tags"] = [
...(response["tags"] || []),
{ value: normalizedValue },
]
const tag = tagsMap.get(normalizedValue)
if (!tag) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Tag with value ${normalizedValue} not found`
)
}
response["tags"] = [...(response["tags"] || []), { id: tag.id }]
return
}

View File

@@ -38,9 +38,13 @@ export const groupProductsForBatchStep = createStep(
return acc
}
// New products will be created with a new ID, even if there is one present in the CSV.
// New products and variants will be created with a new ID, even if there is one present in the CSV.
// To add support for creating with predefined IDs we will need to do changes to the upsert method.
delete product.id
product.variants?.forEach((variant) => {
delete (variant as any).id
})
acc.toCreate.push(product)
return acc
},

View File

@@ -52,12 +52,21 @@ export const parseProductCsvStep = createStep(
}
})
const allRegions = await regionService.listRegions(
{},
{ select: ["id", "name", "currency_code"], take: null }
)
const [allRegions, allTags] = await Promise.all([
regionService.listRegions(
{},
{ select: ["id", "name", "currency_code"], take: null }
),
productService.listProductTags(
{},
{ select: ["id", "value"], take: null }
),
])
const normalizedData = normalizeForImport(v1Normalized, allRegions)
const normalizedData = normalizeForImport(v1Normalized, {
regions: allRegions,
tags: allTags,
})
return new StepResponse(normalizedData)
}
)