fix(medusa) : Improperly referenced product id in product import strategy (#2351)

* What - fixing PR #2350 

* Why - In the ProductImportStrategy class, the getImportInstructions improperly checks the product.id for each row. With it setup this way, products will always be considered new because the product.id column is not read here. So products will never be updated. [Link to the code line](https://github.com/medusajs/medusa/blob/bd941309161bcee4fbfc7918d310c426ba8d8f15/packages/medusa/src/strategies/batch-jobs/product/import.ts#L166)

* How - Replacing `row["product.product.id"]` to  `row["product.id"]`

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Ayush Sharma
2022-10-07 09:36:36 +00:00
committed by GitHub
co-authored by Oliver Windall Juhl
parent 3d255302b0
commit d8a5942d3d
9 changed files with 110 additions and 67 deletions
@@ -1,12 +1,10 @@
import { Readable, PassThrough } from "stream"
import { PassThrough, Readable } from "stream"
import { EntityManager } from "typeorm"
import { FileService } from "medusa-interfaces"
import { IdMap, MockManager } from "medusa-test-utils"
import { User } from "../../../../models"
import { BatchJobStatus } from "../../../../types/batch-job"
import ProductImportStrategy from "../../../batch-jobs/product/import"
import {
BatchJobService,
ProductService,
@@ -14,8 +12,10 @@ import {
RegionService,
ShippingProfileService,
} from "../../../../services"
import { InjectedProps } from "../../../batch-jobs/product/types"
import { BatchJobStatus } from "../../../../types/batch-job"
import { FlagRouter } from "../../../../utils/flag-router"
import ProductImportStrategy from "../../../batch-jobs/product/import"
import { InjectedProps } from "../../../batch-jobs/product/types"
let fakeJob = {
id: IdMap.getId("product-import-job"),
@@ -32,10 +32,10 @@ let fakeJob = {
}
async function* generateCSVDataForStream() {
yield "Product id,Product Handle,Product Title,Product Subtitle,Product Description,Product Status,Product Thumbnail,Product Weight,Product Length,Product Width,Product Height,Product HS Code,Product Origin Country,Product MID Code,Product Material,Product Collection Title,Product Collection Handle,Product Type,Product Tags,Product Discountable,Product External ID,Product Profile Name,Product Profile Type,Variant id,Variant Title,Variant SKU,Variant Barcode,Variant Inventory Quantity,Variant Allow backorder,Variant Manage inventory,Variant Weight,Variant Length,Variant Width,Variant Height,Variant HS Code,Variant Origin Country,Variant MID Code,Variant Material,Price france [USD],Price USD,Price denmark [DKK],Price Denmark [DKK],Option 1 Name,Option 1 Value,Option 2 Name,Option 2 Value,Image 1 Url\n"
yield "O6S1YQ6mKm,test-product-product-1,Test product,,test-product-description-1,draft,,,,,,,,,,Test collection 1,test-collection1,test-type-1,123_1,TRUE,,profile_1,profile_type_1,SebniWTDeC,Test variant,test-sku-1,test-barcode-1,10,FALSE,TRUE,,,,,,,,,100,110,130,,test-option-1,option 1 value red,test-option-2,option 2 value 1,test-image.png\n"
yield "5VxiEkmnPV,test-product-product-2,Test product,,test-product-description,draft,,,,,,,,,,Test collection,test-collection2,test-type,123,TRUE,,profile_2,profile_type_2,CaBp7amx3r,Test variant,test-sku-2,test-barcode-2,10,FALSE,TRUE,,,,,,,,,,,,110,test-option,Option 1 value 1,,,test-image.png\n"
yield "5VxiEkmnPV,test-product-product-2,Test product,,test-product-description,draft,,,,,,,,,,Test collection,test-collection2,test-type,123,TRUE,,profile_2,profile_type_2,3SS1MHGDEJ,Test variant,test-sku-3,test-barcode-3,10,FALSE,TRUE,,,,,,,,,,120,,,test-option,Option 1 Value blue,,,test-image.png\n"
yield "Product id,Product Handle,Product Title,Product Subtitle,Product Description,Product Status,Product Thumbnail,Product Weight,Product Length,Product Width,Product Height,Product HS Code,Product Origin Country,Product MID Code,Product Material,Product Collection Title,Product Collection Handle,Product Type,Product Tags,Product Discountable,Product External ID,Variant id,Variant Title,Variant SKU,Variant Barcode,Variant Inventory Quantity,Variant Allow backorder,Variant Manage inventory,Variant Weight,Variant Length,Variant Width,Variant Height,Variant HS Code,Variant Origin Country,Variant MID Code,Variant Material,Price france [USD],Price USD,Price denmark [DKK],Price Denmark [DKK],Option 1 Name,Option 1 Value,Option 2 Name,Option 2 Value,Image 1 Url\n"
yield ",test-product-product-1,Test product,,test-product-description-1,draft,,,,,,,,,,Test collection 1,test-collection1,test-type-1,123_1,TRUE,,SebniWTDeC,Test variant,test-sku-1,test-barcode-1,10,FALSE,TRUE,,,,,,,,,100,110,130,,test-option-1,option 1 value red,test-option-2,option 2 value 1,test-image.png\n"
yield "5VxiEkmnPV,test-product-product-2,Test product,,test-product-description,draft,,,,,,,,,,Test collection,test-collection2,test-type,123,TRUE,,,Test variant,test-sku-2,test-barcode-2,10,FALSE,TRUE,,,,,,,,,,,,110,test-option,Option 1 value 1,,,test-image.png\n"
yield "5VxiEkmnPV,test-product-product-2,Test product,,test-product-description,draft,,,,,,,,,,Test collection,test-collection2,test-type,123,TRUE,,3SS1MHGDEJ,Test variant,test-sku-3,test-barcode-3,10,FALSE,TRUE,,,,,,,,,,120,,,test-option,Option 1 Value blue,,,test-image.png\n"
}
/* ******************** SERVICES MOCK ******************** */
@@ -159,7 +159,7 @@ describe("Product import strategy", () => {
expect(getImportInstructionsSpy).toBeCalledTimes(1)
expect(fileServiceMock.getUploadStreamDescriptor).toBeCalledTimes(2)
expect(fileServiceMock.getUploadStreamDescriptor).toBeCalledTimes(4)
expect(fileServiceMock.getUploadStreamDescriptor).toHaveBeenNthCalledWith(
1,
@@ -170,6 +170,20 @@ describe("Product import strategy", () => {
)
expect(fileServiceMock.getUploadStreamDescriptor).toHaveBeenNthCalledWith(
2,
{
ext: "json",
name: `imports/products/ops/${fakeJob.id}-VARIANT_CREATE`,
}
)
expect(fileServiceMock.getUploadStreamDescriptor).toHaveBeenNthCalledWith(
3,
{
ext: "json",
name: `imports/products/ops/${fakeJob.id}-PRODUCT_UPDATE`,
}
)
expect(fileServiceMock.getUploadStreamDescriptor).toHaveBeenNthCalledWith(
4,
{
ext: "json",
name: `imports/products/ops/${fakeJob.id}-VARIANT_UPDATE`, // because row data has variant.id
@@ -26,7 +26,7 @@ import {
TBuiltProductImportLine,
TParsedProductImportRowData,
} from "./types"
import { BatchJob, SalesChannel } from "../../../models"
import { BatchJob, Product, SalesChannel } from "../../../models"
import { FlagRouter } from "../../../utils/flag-router"
import { transformProductData, transformVariantData } from "./utils"
import SalesChannelFeatureFlag from "../../../loaders/feature-flags/sales-channels"
@@ -163,7 +163,7 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
// save only first occurrence
if (!seenProducts[row["product.handle"] as string]) {
row["product.profile_id"] = shippingProfile!.id
if (row["product.product.id"]) {
if (row["product.id"]) {
productsUpdate.push(row)
} else {
productsCreate.push(row)
@@ -306,7 +306,6 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
await this.updateProducts(batchJob)
await this.createVariants(batchJob)
await this.updateVariants(batchJob)
await this.finalize(batchJob)
})
}
@@ -443,6 +442,8 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
)
}
delete productData.options // for now not supported in the update method
await productServiceTx.update(
productOp["product.id"] as string,
productData
@@ -489,7 +490,7 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
product!.options.find(
(createdProductOption) =>
createdProductOption.title === variantOption.title
)!.id
)?.id
) || []
variant.options =
@@ -498,6 +499,9 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
option_id: optionIds[index],
})) || []
delete variant.id
delete variant.product
await this.productVariantService_
.withTransaction(transactionManager)
.create(product!, variant as unknown as CreateProductVariantInput)
@@ -700,7 +704,6 @@ class ProductImportStrategy extends AbstractBatchJobStrategy {
private async updateProgress(batchJobId: string): Promise<void> {
const newCount = (this.processedCounter[batchJobId] || 0) + 1
this.processedCounter[batchJobId] = newCount
if (newCount % BATCH_SIZE !== 0) {
return
}
@@ -769,9 +772,7 @@ const CSVSchema: ProductImportCsvSchema = {
//
{ name: "Product Discountable", mapTo: "product.discountable" },
{ name: "Product External ID", mapTo: "product.external_id" },
// PRODUCT-SHIPPING_PROFILE
{ name: "Product Profile Name", mapTo: "product.profile.name" },
{ name: "Product Profile Type", mapTo: "product.profile.type" },
// VARIANTS
{
name: "Variant id",
@@ -42,7 +42,7 @@ export type InjectedProps = {
*/
export type TParsedProductImportRowData = Record<
string,
string | number | (string | number | object)[]
string | number | object | (string | number | object)[]
>
/**
@@ -1,3 +1,5 @@
import set from "lodash/set"
import { TParsedProductImportRowData } from "./types"
import { csvRevertCellContentFormatter } from "../../../utils"
@@ -37,7 +39,7 @@ export function transformProductData(
Object.keys(productData).forEach((k) => {
const key = k.split("product.")[1]
ret[key] = productData[k]
set(ret, key, productData[k])
})
return ret
@@ -54,12 +56,12 @@ export function transformVariantData(
Object.keys(productData).forEach((k) => {
const key = k.split("variant.")[1]
ret[key] = productData[k]
set(ret, key, productData[k])
})
// include product handle to keep track of associated product
ret["product.handle"] = data["product.handle"]
ret["product.options"] = data["product.options"]
set(ret, "product.options", data["product.options"])
return ret
}