feat(medusa): seed command can create product categories (#3528)
* chore: seed command can create product categories * chore: lint fixes * chore: add a default value for categories
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): seed command can create product categories
|
||||||
@@ -14,6 +14,7 @@ import featureFlagLoader from "../loaders/feature-flags"
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
ProductService,
|
ProductService,
|
||||||
|
ProductCategoryService,
|
||||||
ProductVariantService,
|
ProductVariantService,
|
||||||
RegionService,
|
RegionService,
|
||||||
ShippingOptionService,
|
ShippingOptionService,
|
||||||
@@ -21,8 +22,10 @@ import {
|
|||||||
StoreService,
|
StoreService,
|
||||||
UserService,
|
UserService,
|
||||||
} from "../services"
|
} from "../services"
|
||||||
|
import { ProductCategory } from "../models"
|
||||||
import { ConfigModule } from "../types/global"
|
import { ConfigModule } from "../types/global"
|
||||||
import { CreateProductInput } from "../types/product"
|
import { CreateProductInput } from "../types/product"
|
||||||
|
import { CreateProductCategoryInput } from "../types/product-category"
|
||||||
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"
|
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"
|
||||||
|
|
||||||
type SeedOptions = {
|
type SeedOptions = {
|
||||||
@@ -97,6 +100,10 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
const userService: UserService = container.resolve("userService")
|
const userService: UserService = container.resolve("userService")
|
||||||
const regionService: RegionService = container.resolve("regionService")
|
const regionService: RegionService = container.resolve("regionService")
|
||||||
const productService: ProductService = container.resolve("productService")
|
const productService: ProductService = container.resolve("productService")
|
||||||
|
const productCategoryService: ProductCategoryService = container.resolve(
|
||||||
|
"productCategoryService"
|
||||||
|
)
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
const productVariantService: ProductVariantService = container.resolve(
|
const productVariantService: ProductVariantService = container.resolve(
|
||||||
"productVariantService"
|
"productVariantService"
|
||||||
@@ -114,6 +121,7 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
store: seededStore,
|
store: seededStore,
|
||||||
regions,
|
regions,
|
||||||
products,
|
products,
|
||||||
|
categories = [],
|
||||||
shipping_options,
|
shipping_options,
|
||||||
users,
|
users,
|
||||||
} = JSON.parse(fs.readFileSync(resolvedPath, `utf-8`))
|
} = JSON.parse(fs.readFileSync(resolvedPath, `utf-8`))
|
||||||
@@ -202,6 +210,33 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createProductCategory = async (
|
||||||
|
parameters,
|
||||||
|
parentCategory: ProductCategory | null = null
|
||||||
|
) => {
|
||||||
|
// default to the categories being visible and public
|
||||||
|
parameters.is_active = parameters.is_active || true
|
||||||
|
parameters.is_internal = parameters.is_internal || false
|
||||||
|
parameters.parent_category = parentCategory || null
|
||||||
|
|
||||||
|
const categoryChildren = parameters.category_children || []
|
||||||
|
delete parameters.category_children
|
||||||
|
|
||||||
|
const category = await productCategoryService
|
||||||
|
.withTransaction(tx)
|
||||||
|
.create(parameters as CreateProductCategoryInput)
|
||||||
|
|
||||||
|
if (categoryChildren.length) {
|
||||||
|
for (const categoryChild of categoryChildren) {
|
||||||
|
await createProductCategory(categoryChild, category)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const c of categories) {
|
||||||
|
await createProductCategory(c, null)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
track("CLI_SEED_COMPLETED")
|
track("CLI_SEED_COMPLETED")
|
||||||
|
|||||||
Reference in New Issue
Block a user