feat: create CSV normalizer to normalize a CSV file (#12396)

This commit is contained in:
Harminder Virk
2025-05-13 18:04:59 +05:30
committed by GitHub
parent da270cd3e2
commit 4602163b56
13 changed files with 3305 additions and 2 deletions
@@ -25,4 +25,5 @@ export * from "./generate-product-csv"
export * from "./parse-product-csv"
export * from "./group-products-for-batch"
export * from "./wait-confirmation-product-import"
export * from "./get-variant-availability"
export * from "./get-variant-availability"
export * from "./normalize-products-v1"
@@ -0,0 +1,48 @@
import { CSVNormalizer } from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
import { convertCsvToJson } from "../utlils"
import { GroupProductsForBatchStepOutput } from "./group-products-for-batch"
/**
* The CSV file content to parse.
*/
export type NormalizeProductCsvStepInput = string
export const normalizeCsvStepId = "normalize-product-csv"
/**
* This step parses a CSV file holding products to import, returning the products as
* objects that can be imported.
*
* @example
* const data = parseProductCsvStep("products.csv")
*/
export const normalizeCsvStep = createStep(
normalizeCsvStepId,
async (fileContent: NormalizeProductCsvStepInput, { container }) => {
const csvProducts =
convertCsvToJson<ConstructorParameters<typeof CSVNormalizer>[0][0]>(
fileContent
)
const normalizer = new CSVNormalizer(csvProducts)
const products = normalizer.proccess()
const create = Object.keys(products.toCreate).reduce<
(typeof products)["toCreate"][keyof (typeof products)["toCreate"]][]
>((result, toCreateHandle) => {
result.push(products.toCreate[toCreateHandle])
return result
}, [])
const update = Object.keys(products.toUpdate).reduce<
(typeof products)["toUpdate"][keyof (typeof products)["toUpdate"]][]
>((result, toCreateId) => {
result.push(products.toUpdate[toCreateId])
return result
}, [])
return new StepResponse({
create,
update,
} as GroupProductsForBatchStepOutput)
}
)