feat(medusa): Allow to assign a Collection to a Product on import (#2764)
This commit is contained in:
@@ -8,6 +8,7 @@ const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
const userSeeder = require("../../../helpers/user-seeder")
|
||||
const { simpleSalesChannelFactory } = require("../../../factories")
|
||||
const batchJobSeeder = require("../../../helpers/batch-job-seeder")
|
||||
const { simpleProductCollectionFactory } = require("../../../factories/simple-product-collection-factory");
|
||||
|
||||
const startServerWithEnvironment =
|
||||
require("../../../../helpers/start-server-with-environment").default
|
||||
@@ -51,6 +52,8 @@ describe("Product import - Sales Channel", () => {
|
||||
let dbConnection
|
||||
let medusaProcess
|
||||
|
||||
let collectionHandle1 = "test-collection1"
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
|
||||
@@ -86,6 +89,9 @@ describe("Product import - Sales Channel", () => {
|
||||
await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "Import Sales Channel 2",
|
||||
})
|
||||
await simpleProductCollectionFactory(dbConnection, {
|
||||
handle: collectionHandle1
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw e
|
||||
@@ -162,6 +168,9 @@ describe("Product import - Sales Channel", () => {
|
||||
is_disabled: false,
|
||||
}),
|
||||
],
|
||||
collection: expect.objectContaining({
|
||||
handle: collectionHandle1
|
||||
})
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -9,6 +9,7 @@ const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
const batchJobSeeder = require("../../../helpers/batch-job-seeder")
|
||||
const userSeeder = require("../../../helpers/user-seeder")
|
||||
const { simpleProductFactory } = require("../../../factories")
|
||||
const { simpleProductCollectionFactory } = require("../../../factories/simple-product-collection-factory");
|
||||
|
||||
const adminReqConfig = {
|
||||
headers: {
|
||||
@@ -49,6 +50,9 @@ describe("Product import batch job", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
let collectionHandle1 = "test-collection1"
|
||||
let collectionHandle2 = "test-collection2"
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
@@ -72,14 +76,14 @@ describe("Product import batch job", () => {
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await batchJobSeeder(dbConnection)
|
||||
await adminSeeder(dbConnection)
|
||||
await userSeeder(dbConnection)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw e
|
||||
}
|
||||
await batchJobSeeder(dbConnection)
|
||||
await adminSeeder(dbConnection)
|
||||
await userSeeder(dbConnection)
|
||||
await simpleProductCollectionFactory(dbConnection, [{
|
||||
handle: collectionHandle1
|
||||
}, {
|
||||
handle: collectionHandle2
|
||||
}])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -220,6 +224,9 @@ describe("Product import batch job", () => {
|
||||
value: "123_1",
|
||||
}),
|
||||
],
|
||||
collection: expect.objectContaining({
|
||||
handle: collectionHandle1,
|
||||
})
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: "Test product",
|
||||
@@ -279,6 +286,9 @@ describe("Product import batch job", () => {
|
||||
}),
|
||||
],
|
||||
tags: [],
|
||||
collection: expect.objectContaining({
|
||||
handle: collectionHandle1,
|
||||
})
|
||||
}),
|
||||
// UPDATED PRODUCT
|
||||
expect.objectContaining({
|
||||
@@ -373,6 +383,9 @@ describe("Product import batch job", () => {
|
||||
value: "123",
|
||||
}),
|
||||
],
|
||||
collection: expect.objectContaining({
|
||||
handle: collectionHandle2
|
||||
})
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Connection } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { ProductCollection } from "@medusajs/medusa"
|
||||
|
||||
export type Data = {
|
||||
title?: string,
|
||||
handle?: string
|
||||
}
|
||||
|
||||
export const simpleProductCollectionFactory = async <
|
||||
TData extends Data | Data[] = Data | Data[],
|
||||
TResult = TData extends Array<Data> ? ProductCollection[] : ProductCollection
|
||||
>(
|
||||
connection: Connection,
|
||||
data?: TData,
|
||||
seed?: number
|
||||
): Promise<TResult> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = connection.manager
|
||||
|
||||
data = data || [{
|
||||
title: faker.datatype.string(10),
|
||||
}] as TData
|
||||
|
||||
const collectionsData = Array.isArray(data) ? data : [data]
|
||||
|
||||
const collections: ProductCollection[] = []
|
||||
|
||||
for (const collectionData of collectionsData) {
|
||||
const collection_ = manager.create(ProductCollection, {
|
||||
id: `simple-id-${Math.random() * 1000}`,
|
||||
title: collectionData.title ?? faker.datatype.string(10),
|
||||
handle: collectionData.handle
|
||||
})
|
||||
collections.push(collection_)
|
||||
}
|
||||
|
||||
const productCollections = await manager.save(collections)
|
||||
return (Array.isArray(data) ? productCollections : productCollections[0]) as unknown as TResult
|
||||
}
|
||||
Reference in New Issue
Block a user