fix(medusa, workflows): Create product workflow with Isolated modules + module registration (#5081)

* fix(medusa, workflows): Isolated modules

* Create polite-kangaroos-rhyme.md

* finalise

* naming
This commit is contained in:
Adrien de Peretti
2023-09-21 17:10:14 +02:00
committed by GitHub
parent 89735aa425
commit 202049f8aa
6 changed files with 141 additions and 7 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/workflows": patch
---
fix(medusa, workflows): Create product workflow with Isolated modules + module registration

View File

@@ -8,7 +8,11 @@ import {
IsString,
ValidateNested,
} from "class-validator"
import { defaultAdminProductFields, defaultAdminProductRelations } from "."
import {
defaultAdminProductFields,
defaultAdminProductRelations,
defaultAdminProductRemoteQueryObject,
} from "."
import {
PricingService,
ProductService,
@@ -43,6 +47,7 @@ import { ProductStatus } from "../../../../models"
import { Logger } from "../../../../types/global"
import { validator } from "../../../../utils"
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
/**
* @oas [post] /admin/products
@@ -259,16 +264,41 @@ export default async (req, res) => {
})
}
const rawProduct = await productService.retrieve(product.id, {
let rawProduct
if (featureFlagRouter.isFeatureEnabled(IsolateProductDomainFeatureFlag.key)) {
rawProduct = await getProductWithIsolatedProductModule(req, product.id)
} else {
rawProduct = await productService.retrieve(product.id, {
select: defaultAdminProductFields,
relations: defaultAdminProductRelations,
})
}
const [pricedProduct] = await pricingService.setProductPrices([rawProduct])
res.json({ product: pricedProduct })
}
async function getProductWithIsolatedProductModule(req, id) {
// TODO: Add support for fields/expands
const remoteQuery = req.scope.resolve("remoteQuery")
const variables = { id }
const query = {
product: {
__args: variables,
...defaultAdminProductRemoteQueryObject,
},
}
const [product] = await remoteQuery(query)
product.profile_id = product.profile?.id
return product
}
class ProductVariantOptionReq {
@IsString()
value: string

View File

@@ -138,6 +138,95 @@ export const defaultAdminProductFields: (keyof Product)[] = [
export const defaultAdminGetProductsVariantsFields = ["id", "product_id"]
/**
* This is temporary.
*/
export const defaultAdminProductRemoteQueryObject = {
fields: defaultAdminProductFields,
images: {
fields: ["id", "created_at", "updated_at", "deleted_at", "url", "metadata"],
},
tags: {
fields: ["id", "created_at", "updated_at", "deleted_at", "value"],
},
type: {
fields: ["id", "created_at", "updated_at", "deleted_at", "value"],
},
collection: {
fields: ["title", "handle", "id", "created_at", "updated_at", "deleted_at"],
},
options: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"product_id",
"metadata",
],
values: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"value",
"option_id",
"variant_id",
"metadata",
],
},
},
variants: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"product_id",
"sku",
"barcode",
"ean",
"upc",
"variant_rank",
"inventory_quantity",
"allow_backorder",
"manage_inventory",
"hs_code",
"origin_country",
"mid_code",
"material",
"weight",
"length",
"height",
"width",
"metadata",
],
options: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"value",
"option_id",
"variant_id",
"metadata",
],
},
},
profile: {
fields: ["id", "created_at", "updated_at", "deleted_at", "name", "type"],
},
}
/**
* @schema AdminProductsDeleteOptionRes
* type: object

View File

@@ -230,7 +230,7 @@ export default async ({
if (featureFlagRouter.isFeatureEnabled(IsolateProductDomainFeatureFlag.key)) {
mergeModulesConfig(configModule.modules ?? {}, modulesConfig)
const { query } = await MedusaApp({
const { query, modules } = await MedusaApp({
modulesConfig,
servicesConfig: joinerConfig,
remoteFetchData: remoteQueryFetchData(container),
@@ -241,6 +241,14 @@ export default async ({
},
})
// Medusa app load all non legacy modules, so we need to register them in the container since they are into their own container
// We might decide to do it elsewhere but for now I think it is fine
for (const [serviceKey, moduleService] of Object.entries(modules)) {
container.register(
ModulesDefinition[serviceKey].registrationName,
asValue(moduleService)
)
}
container.register("remoteQuery", asValue(query))
}

View File

@@ -16,6 +16,7 @@ import { MODULE_DEFINITIONS, ModulesDefinition } from "../definitions"
* @param modules
* @param isolatedModules Will be removed once the isolated flag is being removed
*/
// TODO: Remove once we have all modules migrated + rename to something like getResolutions
export const registerModules = (
modules?: Record<
string,

View File

@@ -17,7 +17,7 @@ export async function attachInventoryItems({
.resolve("productVariantInventoryService")
.withTransaction(manager)
if (!data?.inventoryItems.length) {
if (!data?.inventoryItems?.length) {
return
}