Skip to content

Commit

Permalink
feat(medusa): seed command can create product categories (#3528)
Browse files Browse the repository at this point in the history
* chore: seed command can create product categories

* chore: lint fixes

* chore: add a default value for categories
  • Loading branch information
riqwan authored Mar 20, 2023
1 parent 55c5fba commit 98cad6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-berries-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): seed command can create product categories
35 changes: 35 additions & 0 deletions packages/medusa/src/commands/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import featureFlagLoader from "../loaders/feature-flags"

import {
ProductService,
ProductCategoryService,
ProductVariantService,
RegionService,
ShippingOptionService,
ShippingProfileService,
StoreService,
UserService,
} from "../services"
import { ProductCategory } from "../models"
import { ConfigModule } from "../types/global"
import { CreateProductInput } from "../types/product"
import { CreateProductCategoryInput } from "../types/product-category"
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"

type SeedOptions = {
Expand Down Expand Up @@ -97,6 +100,10 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
const userService: UserService = container.resolve("userService")
const regionService: RegionService = container.resolve("regionService")
const productService: ProductService = container.resolve("productService")
const productCategoryService: ProductCategoryService = container.resolve(
"productCategoryService"
)

/* eslint-disable */
const productVariantService: ProductVariantService = container.resolve(
"productVariantService"
Expand All @@ -114,6 +121,7 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
store: seededStore,
regions,
products,
categories = [],
shipping_options,
users,
} = JSON.parse(fs.readFileSync(resolvedPath, `utf-8`))
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 98cad6d

Please sign in to comment.