chore: Move factories and helpers to a better place (#4551)
* chore: Move factories and helpers to a better place * align factory product variant * fix factory cart * add simple store fac * fix tests * fix tests * fix * fix cart seeder
This commit is contained in:
1 parent
26e2f81c24
commit
4d326fbbdf
197 files changed
+1073
-4332
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { DataSource } 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
|
||||
>(
|
||||
dataSource: DataSource,
|
||||
data?: TData,
|
||||
seed?: number
|
||||
): Promise<TResult> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.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