feat: Add support for sorting export headers (#8386)
* feat: Add support for sorting export headers * fix: Minor fixes to import flow
This commit is contained in:
@@ -236,20 +236,24 @@ const normalizeVariantForImport = (
|
||||
}
|
||||
|
||||
const getNormalizedValue = (key: string, value: any): any => {
|
||||
let res = stringFields.some((field) => key.startsWith(field))
|
||||
? value?.toString()
|
||||
: value
|
||||
if (value === "\r") {
|
||||
return ""
|
||||
}
|
||||
|
||||
if (stringFields.some((field) => key.startsWith(field))) {
|
||||
return value?.toString()
|
||||
}
|
||||
|
||||
if (booleanFields.some((field) => key.startsWith(field))) {
|
||||
if (value === "TRUE") {
|
||||
res = true
|
||||
return true
|
||||
}
|
||||
if (value === "FALSE") {
|
||||
res = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
return value
|
||||
}
|
||||
|
||||
const snakecaseKey = (key: string): string => {
|
||||
|
||||
@@ -106,22 +106,28 @@ export const normalizeV1Products = (
|
||||
Object.entries(finalRes).forEach(([key, value]) => {
|
||||
if (key.startsWith("Price")) {
|
||||
delete finalRes[key]
|
||||
finalRes[`Variant ${key}`] = value
|
||||
if (value) {
|
||||
finalRes[`Variant ${key}`] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (key.startsWith("Option")) {
|
||||
delete finalRes[key]
|
||||
finalRes[`Variant ${key}`] = value
|
||||
if (value) {
|
||||
finalRes[`Variant ${key}`] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (key.startsWith("Image")) {
|
||||
delete finalRes[key]
|
||||
finalRes[`Product Image ${key.split(" ")[1]}`] = value
|
||||
if (value) {
|
||||
finalRes[`Product Image ${key.split(" ")[1]}`] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (key.startsWith("Sales Channel")) {
|
||||
delete finalRes[key]
|
||||
if (key.endsWith("Id")) {
|
||||
if (key.endsWith("Id") && value) {
|
||||
if (!salesChannelsMap.has(value)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
@@ -134,6 +140,15 @@ export const normalizeV1Products = (
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
key.startsWith("Product Category") &&
|
||||
(key.endsWith("Handle") ||
|
||||
key.endsWith("Name") ||
|
||||
key.endsWith("Description"))
|
||||
) {
|
||||
delete finalRes[key]
|
||||
}
|
||||
|
||||
// Note: Product categories from v1 are not imported to v2
|
||||
})
|
||||
|
||||
|
||||
@@ -7,6 +7,56 @@ import { ModuleRegistrationName, convertJsonToCsv } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { normalizeForExport } from "../helpers/normalize-for-export"
|
||||
|
||||
const prodColumnPositions = new Map([
|
||||
["Product Id", 0],
|
||||
["Product Handle", 1],
|
||||
["Product Title", 2],
|
||||
["Product Status", 3],
|
||||
["Product Description", 4],
|
||||
["Product Subtitle", 5],
|
||||
["Product External Id", 6],
|
||||
["Product Thumbnail", 7],
|
||||
["Product Collection Id", 8],
|
||||
["Product Type Id", 9],
|
||||
])
|
||||
|
||||
const variantColumnPositions = new Map([
|
||||
["Variant Id", 0],
|
||||
["Variant Title", 1],
|
||||
["Variant Sku", 3],
|
||||
["Variant Upc", 4],
|
||||
["Variant Ean", 5],
|
||||
["Variant Hs Code", 6],
|
||||
["Variant Mid Code", 7],
|
||||
["Variant Manage Inventory", 8],
|
||||
["Variant Allow Backorder", 9],
|
||||
])
|
||||
|
||||
const comparator = (a: string, b: string, columnMap: Map<string, number>) => {
|
||||
if (columnMap.has(a) && columnMap.has(b)) {
|
||||
return columnMap.get(a)! - columnMap.get(b)!
|
||||
}
|
||||
if (columnMap.has(a)) {
|
||||
return -1
|
||||
}
|
||||
if (columnMap.has(b)) {
|
||||
return 1
|
||||
}
|
||||
return a.localeCompare(b)
|
||||
}
|
||||
|
||||
const csvSortFunction = (a: string, b: string) => {
|
||||
if (a.startsWith("Product") && b.startsWith("Product")) {
|
||||
return comparator(a, b, prodColumnPositions)
|
||||
}
|
||||
|
||||
if (a.startsWith("Variant") && b.startsWith("Variant")) {
|
||||
return comparator(a, b, variantColumnPositions)
|
||||
}
|
||||
|
||||
return a.localeCompare(b)
|
||||
}
|
||||
|
||||
export const generateProductCsvStepId = "generate-product-csv"
|
||||
export const generateProductCsvStep = createStep(
|
||||
generateProductCsvStepId,
|
||||
@@ -21,7 +71,9 @@ export const generateProductCsvStep = createStep(
|
||||
)
|
||||
|
||||
const normalizedData = normalizeForExport(products, { regions })
|
||||
const csvContent = convertJsonToCsv(normalizedData)
|
||||
const csvContent = convertJsonToCsv(normalizedData, {
|
||||
sortHeader: csvSortFunction,
|
||||
})
|
||||
|
||||
const fileModule: IFileModuleService = container.resolve(
|
||||
ModuleRegistrationName.FILE
|
||||
|
||||
Reference in New Issue
Block a user