Files
medusa-store/integration-tests/plugins/factories/simple-product-factory.ts
T
Philip Korsholm d184d23c63 Feat/bulk operations for inventory service (#4503)
* initial push

* bulk delete reservations by location ids

* add method to interface (not implemented yet)

* bulk update

* delete reservations by location id bulk

* add create bulk for inventory item

* refactor attach inventory item method

* add changeset

* verbose false

* method override instead of multiple methods

* change up method signature

* redo changes when updating interface

* update createInventoryLevel method

* rename variables

* fix feedback

* return correct string array when emitting event

* refactor inventory service

* redo order changes

* snapshot

* move prep methods
2023-07-18 11:17:57 +02:00

93 lines
2.5 KiB
TypeScript

import { Product, ProductOption, ProductStatus, ProductType, ShippingProfile, ShippingProfileType, } from "@medusajs/medusa"
import { ProductVariantFactoryData, simpleProductVariantFactory, } from "./simple-product-variant-factory"
import { Connection } from "typeorm"
import faker from "faker"
export type ProductFactoryData = {
id?: string
is_giftcard?: boolean
title?: string
type?: string
status?: ProductStatus
options?: { id: string; title: string }[]
variants?: ProductVariantFactoryData[]
}
export const simpleProductFactory = async (
connection: Connection,
data: ProductFactoryData = {},
seed?: number
): Promise<Product> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
const gcProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.GIFT_CARD },
})
let typeId: string
if (typeof data.type !== "undefined") {
const toSave = manager.create(ProductType, {
value: data.type,
})
const res = await manager.save(toSave)
typeId = res.id
}
const prodId = data.id || `simple-product-${Math.random() * 1000}`
const toSave = manager.create(Product, {
id: prodId,
type_id: typeId,
status: data.status || ProductStatus.DRAFT,
title: data.title || faker.commerce.productName(),
is_giftcard: data.is_giftcard || false,
discountable: !data.is_giftcard,
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
})
const product = await manager.save(toSave)
const optionId = `${prodId}-option`
const options = data.options || [{ id: optionId, title: "Size" }]
for (const o of options) {
await manager.insert(ProductOption, {
id: o.id,
title: o.title,
product_id: prodId,
})
}
const variants = data.variants || [
{
id: `simple-test-variant-${Math.random() * 1000}`,
title: "Test",
product_id: prodId,
prices: [{ currency: "usd", amount: 100 }],
options: [{ option_id: optionId, value: "Large" }],
},
]
product.variants = await Promise.all(variants.map(async (pv) => {
const factoryData = {
...pv,
product_id: prodId,
}
if (typeof pv.options === "undefined") {
factoryData.options = [
{ option_id: optionId, value: faker.commerce.productAdjective() },
]
}
return await simpleProductVariantFactory(connection, factoryData)
}))
return product
}