diff --git a/packages/core/core-flows/src/common/steps/create-remote-links.ts b/packages/core/core-flows/src/common/steps/create-remote-links.ts index 6a5bf2c94a..b8f3281d59 100644 --- a/packages/core/core-flows/src/common/steps/create-remote-links.ts +++ b/packages/core/core-flows/src/common/steps/create-remote-links.ts @@ -1,5 +1,5 @@ import { LinkDefinition, RemoteLink } from "@medusajs/modules-sdk" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" +import { createStep, StepResponse } from "@medusajs/workflows-sdk" import { ContainerRegistrationKeys } from "@medusajs/utils" @@ -12,6 +12,11 @@ export const createRemoteLinkStep = createStep( const link = container.resolve( ContainerRegistrationKeys.REMOTE_LINK ) + + if (!data.length) { + return new StepResponse([], []) + } + await link.create(data) return new StepResponse(data, data) diff --git a/packages/core/core-flows/src/common/steps/dismiss-remote-links.ts b/packages/core/core-flows/src/common/steps/dismiss-remote-links.ts index 8f5ccfd587..5efd4de09e 100644 --- a/packages/core/core-flows/src/common/steps/dismiss-remote-links.ts +++ b/packages/core/core-flows/src/common/steps/dismiss-remote-links.ts @@ -1,5 +1,5 @@ import { LinkDefinition, RemoteLink } from "@medusajs/modules-sdk" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" +import { createStep, StepResponse } from "@medusajs/workflows-sdk" import { ContainerRegistrationKeys } from "@medusajs/utils" @@ -10,6 +10,11 @@ export const dismissRemoteLinkStep = createStep( dismissRemoteLinkStepId, async (data: DismissRemoteLinksStepInput, { container }) => { const entries = Array.isArray(data) ? data : [data] + + if (!entries.length) { + return new StepResponse([], []) + } + const link = container.resolve( ContainerRegistrationKeys.REMOTE_LINK ) diff --git a/packages/core/core-flows/src/common/steps/remove-remote-links.ts b/packages/core/core-flows/src/common/steps/remove-remote-links.ts index 0aeb906463..56cf16909e 100644 --- a/packages/core/core-flows/src/common/steps/remove-remote-links.ts +++ b/packages/core/core-flows/src/common/steps/remove-remote-links.ts @@ -1,5 +1,5 @@ import { DeleteEntityInput, RemoteLink } from "@medusajs/modules-sdk" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" +import { createStep, StepResponse } from "@medusajs/workflows-sdk" import { ContainerRegistrationKeys } from "@medusajs/utils" @@ -10,6 +10,11 @@ export const removeRemoteLinkStep = createStep( removeRemoteLinkStepId, async (data: RemoveRemoteLinksStepInput, { container }) => { const entries = Array.isArray(data) ? data : [data] + + if (!entries.length) { + return new StepResponse(void 0) + } + const grouped: DeleteEntityInput = {} for (const entry of entries) { diff --git a/packages/core/core-flows/src/product/steps/get-products.ts b/packages/core/core-flows/src/product/steps/get-products.ts index 615bc1af31..34b6e2bd1c 100644 --- a/packages/core/core-flows/src/product/steps/get-products.ts +++ b/packages/core/core-flows/src/product/steps/get-products.ts @@ -1,9 +1,9 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { IProductModuleService } from "@medusajs/types" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" +import { createStep, StepResponse } from "@medusajs/workflows-sdk" type StepInput = { - ids: string[] + ids?: string[] } export const getProductsStepId = "get-products" @@ -14,6 +14,10 @@ export const getProductsStep = createStep( ModuleRegistrationName.PRODUCT ) + if (!data.ids?.length) { + return new StepResponse([], []) + } + const products = await service.list( { id: data.ids }, { relations: ["variants"], take: null } diff --git a/packages/core/core-flows/src/product/steps/update-products.ts b/packages/core/core-flows/src/product/steps/update-products.ts index c87247ce7f..35e95a97f3 100644 --- a/packages/core/core-flows/src/product/steps/update-products.ts +++ b/packages/core/core-flows/src/product/steps/update-products.ts @@ -1,10 +1,10 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { IProductModuleService, ProductTypes } from "@medusajs/types" import { - MedusaError, getSelectsAndRelationsFromObjectArray, + MedusaError, } from "@medusajs/utils" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" +import { createStep, StepResponse } from "@medusajs/workflows-sdk" export type UpdateProductsStepInput = | { @@ -31,6 +31,10 @@ export const updateProductsStep = createStep( ) } + if (!data.products.length) { + return new StepResponse([], []) + } + const prevData = await service.list({ id: data.products.map((p) => p.id) as string[], }) diff --git a/packages/core/core-flows/src/product/workflows/update-products.ts b/packages/core/core-flows/src/product/workflows/update-products.ts index 1ca1b45fe1..67ba29ebbe 100644 --- a/packages/core/core-flows/src/product/workflows/update-products.ts +++ b/packages/core/core-flows/src/product/workflows/update-products.ts @@ -4,9 +4,9 @@ import { Modules } from "@medusajs/modules-sdk" import { ProductTypes } from "@medusajs/types" import { arrayDifference } from "@medusajs/utils" import { - WorkflowData, createWorkflow, transform, + WorkflowData, } from "@medusajs/workflows-sdk" import { createRemoteLinkStep, @@ -39,6 +39,10 @@ function prepareUpdateProductInput({ input: WorkflowInput }): UpdateProductsStepInput { if ("products" in input) { + if (!input.products.length) { + return { products: [] } + } + return { products: input.products.map((p) => ({ ...p, @@ -83,6 +87,10 @@ function prepareSalesChannelLinks({ input: WorkflowInput }): Record>[] { if ("products" in input) { + if (!input.products.length) { + return [] + } + return input.products .filter((p) => p.sales_channels) .flatMap((p) => @@ -121,6 +129,10 @@ function prepareToDeleteLinks({ sales_channel_id: string }[] }) { + if (!currentLinks.length) { + return [] + } + return currentLinks.map(({ product_id, sales_channel_id }) => ({ [Modules.PRODUCT]: { product_id, diff --git a/packages/core/medusa-test-utils/src/medusa-test-runner.ts b/packages/core/medusa-test-utils/src/medusa-test-runner.ts index ea13527a4a..6dca6e5e96 100644 --- a/packages/core/medusa-test-utils/src/medusa-test-runner.ts +++ b/packages/core/medusa-test-utils/src/medusa-test-runner.ts @@ -104,6 +104,7 @@ export function medusaIntegrationTestRunner({ ) => { const config = originalConfigLoader(rootDirectory) config.projectConfig.databaseUrl = dbConfig.clientUrl + config.projectConfig.databaseLogging = !!dbConfig.debug config.projectConfig.databaseDriverOptions = dbConfig.clientUrl.includes( "localhost" ) diff --git a/packages/core/utils/src/dal/mikro-orm/mikro-orm-repository.ts b/packages/core/utils/src/dal/mikro-orm/mikro-orm-repository.ts index 6a7d0c272a..5274ed4445 100644 --- a/packages/core/utils/src/dal/mikro-orm/mikro-orm-repository.ts +++ b/packages/core/utils/src/dal/mikro-orm/mikro-orm-repository.ts @@ -180,18 +180,7 @@ export class MikroOrmBaseRepository idsOrFilter: string[] | InternalFilterQuery, sharedContext: Context = {} ): Promise<[T[], Record]> { - // TODO handle composite keys - const isArray = Array.isArray(idsOrFilter) - const filter = - isArray || isString(idsOrFilter) - ? { - id: { - $in: isArray ? idsOrFilter : [idsOrFilter], - }, - } - : idsOrFilter - - const query = buildQuery(filter, { + const query = buildQuery(idsOrFilter, { withDeleted: true, }) @@ -761,6 +750,27 @@ export function mikroOrmBaseRepositoryFactory( return orderedEntities } + async restore( + filters: + | string + | string[] + | (FilterQuery & BaseFilterable>) + | (FilterQuery & BaseFilterable>)[], + sharedContext: Context = {} + ): Promise<[T[], Record]> { + if (Array.isArray(filters) && !filters.filter(Boolean).length) { + return [[], {}] + } + + if (!filters) { + return [[], {}] + } + + const normalizedFilters = this.normalizeFilters(filters) + + return await super.restore(normalizedFilters, sharedContext) + } + async softDelete( filters: | string @@ -769,6 +779,26 @@ export function mikroOrmBaseRepositoryFactory( | (FilterQuery & BaseFilterable>)[], sharedContext: Context = {} ): Promise<[T[], Record]> { + if (Array.isArray(filters) && !filters.filter(Boolean).length) { + return [[], {}] + } + + if (!filters) { + return [[], {}] + } + + const normalizedFilters = this.normalizeFilters(filters) + + return await super.softDelete(normalizedFilters, sharedContext) + } + + private normalizeFilters( + filters: + | string + | string[] + | (FilterQuery & BaseFilterable>) + | (FilterQuery & BaseFilterable>)[] + ) { const primaryKeys = MikroOrmAbstractBaseRepository_.retrievePrimaryKeys(entity) @@ -784,7 +814,7 @@ export function mikroOrmBaseRepositoryFactory( }), } - return await super.softDelete(normalizedFilters, sharedContext) + return normalizedFilters } }