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