fix(medusa): Add default sales channel to product in seeding (#3013)
This commit is contained in:
committed by
GitHub
parent
5d769ccad2
commit
d2c692aa96
5
.changeset/twelve-worms-serve.md
Normal file
5
.changeset/twelve-worms-serve.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa): Add default sales channel to product in seeding
|
||||
@@ -1,20 +1,37 @@
|
||||
import path from "path"
|
||||
import fs from "fs"
|
||||
import express from "express"
|
||||
import { createConnection } from "typeorm"
|
||||
import fs from "fs"
|
||||
import { sync as existsSync } from "fs-exists-cached"
|
||||
import { getConfigFile } from "medusa-core-utils"
|
||||
import { track } from "medusa-telemetry"
|
||||
import path from "path"
|
||||
import { ConnectionOptions, createConnection } from "typeorm"
|
||||
|
||||
import loaders from "../loaders"
|
||||
import { handleConfigError } from "../loaders/config"
|
||||
import Logger from "../loaders/logger"
|
||||
import loaders from "../loaders"
|
||||
|
||||
import featureFlagLoader from "../loaders/feature-flags"
|
||||
|
||||
import {
|
||||
ProductService,
|
||||
ProductVariantService,
|
||||
RegionService,
|
||||
ShippingOptionService,
|
||||
ShippingProfileService,
|
||||
StoreService,
|
||||
UserService,
|
||||
} from "../services"
|
||||
import { ConfigModule } from "../types/global"
|
||||
import { CreateProductInput } from "../types/product"
|
||||
import getMigrations from "./utils/get-migrations"
|
||||
|
||||
const t = async function ({ directory, migrate, seedFile }) {
|
||||
type SeedOptions = {
|
||||
directory: string
|
||||
migrate: boolean
|
||||
seedFile: string
|
||||
}
|
||||
|
||||
const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
||||
track("CLI_SEED")
|
||||
let resolvedPath = seedFile
|
||||
|
||||
@@ -30,7 +47,8 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
}
|
||||
}
|
||||
|
||||
const { configModule, error } = getConfigFile(directory, `medusa-config`)
|
||||
const { configModule, error }: { configModule: ConfigModule; error?: any } =
|
||||
getConfigFile(directory, `medusa-config`)
|
||||
|
||||
if (error) {
|
||||
handleConfigError(error)
|
||||
@@ -41,7 +59,8 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
const dbType = configModule.projectConfig.database_type
|
||||
if (migrate && dbType !== "sqlite") {
|
||||
const migrationDirs = await getMigrations(directory, featureFlagRouter)
|
||||
const connection = await createConnection({
|
||||
|
||||
const connectionOptions = {
|
||||
type: configModule.projectConfig.database_type,
|
||||
database: configModule.projectConfig.database_database,
|
||||
schema: configModule.projectConfig.database_schema,
|
||||
@@ -49,7 +68,9 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
extra: configModule.projectConfig.database_extra || {},
|
||||
migrations: migrationDirs,
|
||||
logging: true,
|
||||
})
|
||||
} as ConnectionOptions
|
||||
|
||||
const connection = await createConnection(connectionOptions)
|
||||
|
||||
await connection.runMigrations()
|
||||
await connection.close()
|
||||
@@ -60,30 +81,39 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
const { container } = await loaders({
|
||||
directory,
|
||||
expressApp: app,
|
||||
isTest: false,
|
||||
})
|
||||
|
||||
const manager = container.resolve("manager")
|
||||
|
||||
const storeService = container.resolve("storeService")
|
||||
const userService = container.resolve("userService")
|
||||
const regionService = container.resolve("regionService")
|
||||
const productService = container.resolve("productService")
|
||||
const productVariantService = container.resolve("productVariantService")
|
||||
const shippingOptionService = container.resolve("shippingOptionService")
|
||||
const shippingProfileService = container.resolve("shippingProfileService")
|
||||
const storeService: StoreService = container.resolve("storeService")
|
||||
const userService: UserService = container.resolve("userService")
|
||||
const regionService: RegionService = container.resolve("regionService")
|
||||
const productService: ProductService = container.resolve("productService")
|
||||
/* eslint-disable */
|
||||
const productVariantService: ProductVariantService = container.resolve("productVariantService")
|
||||
const shippingOptionService: ShippingOptionService = container.resolve("shippingOptionService")
|
||||
const shippingProfileService: ShippingProfileService = container.resolve("shippingProfileService")
|
||||
/* eslint-enable */
|
||||
|
||||
await manager.transaction(async (tx) => {
|
||||
const { store, regions, products, shipping_options, users } = JSON.parse(
|
||||
fs.readFileSync(resolvedPath, `utf-8`)
|
||||
)
|
||||
const {
|
||||
store: seededStore,
|
||||
regions,
|
||||
products,
|
||||
shipping_options,
|
||||
users,
|
||||
} = JSON.parse(fs.readFileSync(resolvedPath, `utf-8`))
|
||||
|
||||
const gcProfile = await shippingProfileService.retrieveGiftCardDefault()
|
||||
const defaultProfile = await shippingProfileService.retrieveDefault()
|
||||
|
||||
if (store) {
|
||||
await storeService.withTransaction(tx).update(store)
|
||||
if (seededStore) {
|
||||
await storeService.withTransaction(tx).update(seededStore)
|
||||
}
|
||||
|
||||
const store = await storeService.retrieve()
|
||||
|
||||
for (const u of users) {
|
||||
const pass = u.password
|
||||
if (pass) {
|
||||
@@ -112,9 +142,9 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
so.region_id = regionIds[so.region_id]
|
||||
}
|
||||
|
||||
so.profile_id = defaultProfile.id
|
||||
so.profile_id = defaultProfile!.id
|
||||
if (so.is_giftcard) {
|
||||
so.profile_id = gcProfile.id
|
||||
so.profile_id = gcProfile!.id
|
||||
delete so.is_giftcard
|
||||
}
|
||||
|
||||
@@ -128,16 +158,20 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
// default to the products being visible
|
||||
p.status = p.status || "published"
|
||||
|
||||
p.profile_id = defaultProfile.id
|
||||
p.sales_channels = [{ id: store.default_sales_channel_id }]
|
||||
|
||||
p.profile_id = defaultProfile!.id
|
||||
if (p.is_giftcard) {
|
||||
p.profile_id = gcProfile.id
|
||||
p.profile_id = gcProfile!.id
|
||||
}
|
||||
|
||||
const newProd = await productService.withTransaction(tx).create(p)
|
||||
const newProd = await productService
|
||||
.withTransaction(tx)
|
||||
.create(p as CreateProductInput)
|
||||
|
||||
if (variants && variants.length) {
|
||||
const optionIds = p.options.map(
|
||||
(o) => newProd.options.find((newO) => newO.title === o.title).id
|
||||
(o) => newProd.options.find((newO) => newO.title === o.title)?.id
|
||||
)
|
||||
|
||||
for (const v of variants) {
|
||||
@@ -160,4 +194,4 @@ const t = async function ({ directory, migrate, seedFile }) {
|
||||
track("CLI_SEED_COMPLETED")
|
||||
}
|
||||
|
||||
export default t
|
||||
export default seed
|
||||
Reference in New Issue
Block a user