chore(orchestration): validate PK when throwIfKeyNotFound (#11190)

CLOSES: FRMW-2895
This commit is contained in:
Carlos R. L. Rodrigues
2025-01-29 19:17:36 +00:00
committed by GitHub
parent e0bd2a79b0
commit e98d3c615e
14 changed files with 209 additions and 118 deletions
+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/orchestration": patch
"@medusajs/core-flows": patch
"@medusajs/framework": patch
---
chore(orchestration): validate missing PK filters when throwIfKeyNotFound
@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { IRegionModuleService, RemoteQueryFunction } from "@medusajs/types" import { IRegionModuleService, RemoteQueryFunction } from "@medusajs/types"
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils" import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { createAdminUser } from "../../..//helpers/create-admin-user" import { createAdminUser } from "../../..//helpers/create-admin-user"
import { adminHeaders } from "../../../helpers/create-admin-user" import { adminHeaders } from "../../../helpers/create-admin-user"
@@ -70,6 +70,77 @@ medusaIntegrationTestRunner({
) )
}) })
it("should fail to retrieve not passing primary key in filters", async () => {
const noPk = remoteQuery(
{
region: {
fields: ["id", "currency_code"],
},
},
{ throwIfKeyNotFound: true }
)
await expect(noPk).rejects.toThrow(
"Region: Primary key(s) [id, iso_2] not found in filters"
)
const noPk2 = remoteQuery(
{
country: {
fields: ["*"],
},
},
{ throwIfKeyNotFound: true }
)
await expect(noPk2).rejects.toThrow(
"Country: Primary key(s) [id, iso_2] not found in filters"
)
const noPk3 = remoteQuery(
{
country: {
fields: ["*"],
__args: {
iso_2: undefined,
},
},
},
{ throwIfKeyNotFound: true }
)
await expect(noPk3).rejects.toThrow(
"Country: Value for primary key iso_2 not found in filters"
)
const noPk4 = remoteQuery(
{
region: {
fields: ["id", "currency_code"],
__args: {
id: null,
},
},
},
{ throwIfKeyNotFound: true }
)
await expect(noPk4).rejects.toThrow(
"Region: Value for primary key id not found in filters"
)
const noPk5 = remoteQuery(
{
region: {
fields: ["id", "currency_code"],
__args: {
currency_code: "EUR",
},
},
},
{ throwIfKeyNotFound: true }
)
await expect(noPk5).rejects.toThrow(
"Region: Primary key(s) [id, iso_2] not found in filters"
)
})
it("should fail if a expected relation is not found", async () => { it("should fail if a expected relation is not found", async () => {
const region = await regionModule.createRegions({ const region = await regionModule.createRegions({
name: "Test Region", name: "Test Region",
@@ -75,14 +75,14 @@ export type ConfirmClaimRequestValidationStepInput = {
/** /**
* This step validates that a requested claim can be confirmed. If the order or claim is canceled, * This step validates that a requested claim can be confirmed. If the order or claim is canceled,
* or the order change is not active, the step will throw an error. * or the order change is not active, the step will throw an error.
* *
* :::note * :::note
* *
* You can retrieve an order, order claim, and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order, order claim, and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = confirmClaimRequestValidationStep({ * const data = confirmClaimRequestValidationStep({
* order: { * order: {
@@ -270,12 +270,12 @@ export type ConfirmClaimRequestWorkflowInput = {
export const confirmClaimRequestWorkflowId = "confirm-claim-request" export const confirmClaimRequestWorkflowId = "confirm-claim-request"
/** /**
* This workflow confirms a requested claim. It's used by the * This workflow confirms a requested claim. It's used by the
* [Confirm Claim Request API Route](https://docs.medusajs.com/api/admin#claims_postclaimsidrequest). * [Confirm Claim Request API Route](https://docs.medusajs.com/api/admin#claims_postclaimsidrequest).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to confirm a claim * You can use this workflow within your customizations or your own custom workflows, allowing you to confirm a claim
* for an order in your custom flows. * for an order in your custom flows.
* *
* @example * @example
* const { result } = await confirmClaimRequestWorkflow(container) * const { result } = await confirmClaimRequestWorkflow(container)
* .run({ * .run({
@@ -283,9 +283,9 @@ export const confirmClaimRequestWorkflowId = "confirm-claim-request"
* claim_id: "claim_123", * claim_id: "claim_123",
* } * }
* }) * })
* *
* @summary * @summary
* *
* Confirm a requested claim. * Confirm a requested claim.
*/ */
export const confirmClaimRequestWorkflow = createWorkflow( export const confirmClaimRequestWorkflow = createWorkflow(
@@ -387,13 +387,6 @@ export const confirmClaimRequestWorkflow = createWorkflow(
updateReturnsStep(updateReturnDate) updateReturnsStep(updateReturnDate)
}) })
const claimId = transform(
{ createdClaimItems },
({ createdClaimItems }) => {
return createdClaimItems?.[0]?.claim_id
}
)
const { returnShippingMethod, claimShippingMethod } = transform( const { returnShippingMethod, claimShippingMethod } = transform(
{ orderPreview, orderClaim, returnId }, { orderPreview, orderClaim, returnId },
extractShippingOption extractShippingOption
@@ -421,7 +414,7 @@ export const confirmClaimRequestWorkflow = createWorkflow(
"additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id", "additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
"additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name", "additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
], ],
variables: { id: claimId }, variables: { id: input.claim_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
}).config({ name: "claim-query" }) }).config({ name: "claim-query" })
@@ -477,7 +470,6 @@ export const confirmClaimRequestWorkflow = createWorkflow(
id: returnShippingMethod.shipping_option_id, id: returnShippingMethod.shipping_option_id,
}, },
list: false, list: false,
throw_if_key_not_found: true,
}).config({ name: "claim-return-shipping-option" }) }).config({ name: "claim-return-shipping-option" })
const fulfillmentData = transform( const fulfillmentData = transform(
@@ -384,7 +384,6 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
id: shippingOptionId, id: shippingOptionId,
}, },
list: false, list: false,
throw_if_key_not_found: true,
}).config({ name: "get-shipping-option" }) }).config({ name: "get-shipping-option" })
const lineItemIds = transform( const lineItemIds = transform(
@@ -63,14 +63,14 @@ export type ConfirmExchangeRequestValidationStepInput = {
/** /**
* This step validates that a requested exchange can be confirmed. * This step validates that a requested exchange can be confirmed.
* If the order or exchange is canceled, or the order change is not active, the step will throw an error. * If the order or exchange is canceled, or the order change is not active, the step will throw an error.
* *
* :::note * :::note
* *
* You can retrieve an order, order exchange, and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order, order exchange, and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = confirmExchangeRequestValidationStep({ * const data = confirmExchangeRequestValidationStep({
* order: { * order: {
@@ -266,10 +266,10 @@ export const confirmExchangeRequestWorkflowId = "confirm-exchange-request"
/** /**
* This workflow confirms an exchange request. It's used by the * This workflow confirms an exchange request. It's used by the
* [Confirm Exchange Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidrequest). * [Confirm Exchange Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidrequest).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to confirm an exchange * You can use this workflow within your customizations or your own custom workflows, allowing you to confirm an exchange
* for an order in your custom flow. * for an order in your custom flow.
* *
* @example * @example
* const { result } = await confirmExchangeRequestWorkflow(container) * const { result } = await confirmExchangeRequestWorkflow(container)
* .run({ * .run({
@@ -277,9 +277,9 @@ export const confirmExchangeRequestWorkflowId = "confirm-exchange-request"
* exchange_id: "exchange_123", * exchange_id: "exchange_123",
* } * }
* }) * })
* *
* @summary * @summary
* *
* Confirm an exchange request. * Confirm an exchange request.
*/ */
export const confirmExchangeRequestWorkflow = createWorkflow( export const confirmExchangeRequestWorkflow = createWorkflow(
@@ -373,13 +373,6 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
updateReturnsStep(updateReturnData) updateReturnsStep(updateReturnData)
}) })
const exchangeId = transform(
{ createdExchangeItems },
({ createdExchangeItems }) => {
return createdExchangeItems?.[0]?.exchange_id
}
)
const { returnShippingMethod, exchangeShippingMethod } = transform( const { returnShippingMethod, exchangeShippingMethod } = transform(
{ orderPreview, orderExchange, returnId }, { orderPreview, orderExchange, returnId },
extractShippingOption extractShippingOption
@@ -407,7 +400,7 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
"additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id", "additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
"additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name", "additional_items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
], ],
variables: { id: exchangeId }, variables: { id: input.exchange_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
}).config({ name: "exchange-query" }) }).config({ name: "exchange-query" })
@@ -463,7 +456,6 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
id: returnShippingMethod.shipping_option_id, id: returnShippingMethod.shipping_option_id,
}, },
list: false, list: false,
throw_if_key_not_found: true,
}).config({ name: "exchange-return-shipping-option" }) }).config({ name: "exchange-return-shipping-option" })
const fulfillmentData = transform( const fulfillmentData = transform(
@@ -97,7 +97,6 @@ export const createOrderRefundCreditLinesWorkflow = createWorkflow(
order_id: input.order_id, order_id: input.order_id,
status: [OrderChangeStatus.PENDING], status: [OrderChangeStatus.PENDING],
}, },
options: { throwIfKeyNotFound: true },
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const orderChange = transform( const orderChange = transform(
@@ -58,14 +58,14 @@ export type ConfirmReturnRequestValidationStepInput = {
/** /**
* This step validates that a return request can be confirmed. * This step validates that a return request can be confirmed.
* If the order or return is canceled or the order change is not active, the step will throw an error. * If the order or return is canceled or the order change is not active, the step will throw an error.
* *
* :::note * :::note
* *
* You can retrieve an order, order change, and return details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order, order change, and return details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = confirmReturnRequestValidationStep({ * const data = confirmReturnRequestValidationStep({
* order: { * order: {
@@ -219,12 +219,12 @@ export type ConfirmReturnRequestWorkflowInput = {
export const confirmReturnRequestWorkflowId = "confirm-return-request" export const confirmReturnRequestWorkflowId = "confirm-return-request"
/** /**
* This workflow confirms a return request. It's used by the * This workflow confirms a return request. It's used by the
* [Confirm Return Request Admin API Route](https://docs.medusajs.com/api/admin#returns_postreturnsidrequest). * [Confirm Return Request Admin API Route](https://docs.medusajs.com/api/admin#returns_postreturnsidrequest).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to confirm a return request * You can use this workflow within your customizations or your own custom workflows, allowing you to confirm a return request
* in your custom flow. * in your custom flow.
* *
* @example * @example
* const { result } = await confirmReturnRequestWorkflow(container) * const { result } = await confirmReturnRequestWorkflow(container)
* .run({ * .run({
@@ -232,9 +232,9 @@ export const confirmReturnRequestWorkflowId = "confirm-return-request"
* return_id: "return_123", * return_id: "return_123",
* } * }
* }) * })
* *
* @summary * @summary
* *
* Confirm a return request. * Confirm a return request.
*/ */
export const confirmReturnRequestWorkflow = createWorkflow( export const confirmReturnRequestWorkflow = createWorkflow(
@@ -326,7 +326,6 @@ export const confirmReturnRequestWorkflow = createWorkflow(
id: returnShippingOptionId, id: returnShippingOptionId,
}, },
list: false, list: false,
throw_if_key_not_found: true,
}).config({ name: "return-shipping-option" }) }).config({ name: "return-shipping-option" })
const fulfillmentData = transform( const fulfillmentData = transform(
@@ -225,17 +225,17 @@ export type CreateCompleteReturnValidationStepInput = {
/** /**
* This step validates that a return can be created and completed for an order. * This step validates that a return can be created and completed for an order.
* If the order is canceled, the items do not exist in the order, * If the order is canceled, the items do not exist in the order,
* the return reasons are invalid, or the refund amount is greater than the order total, * the return reasons are invalid, or the refund amount is greater than the order total,
* the step will throw an error. * the step will throw an error.
* *
* :::note * :::note
* *
* You can retrieve an order details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = createCompleteReturnValidationStep({ * const data = createCompleteReturnValidationStep({
* order: { * order: {
@@ -256,10 +256,7 @@ export type CreateCompleteReturnValidationStepInput = {
export const createCompleteReturnValidationStep = createStep( export const createCompleteReturnValidationStep = createStep(
"create-return-order-validation", "create-return-order-validation",
async function ( async function (
{ { order, input }: CreateCompleteReturnValidationStepInput,
order,
input,
}: CreateCompleteReturnValidationStepInput,
context context
) { ) {
if (!input.items) { if (!input.items) {
@@ -282,13 +279,13 @@ export const createCompleteReturnValidationStep = createStep(
export const createAndCompleteReturnOrderWorkflowId = export const createAndCompleteReturnOrderWorkflowId =
"create-complete-return-order" "create-complete-return-order"
/** /**
* This workflow creates and completes a return from the storefront. The admin would receive the return and * This workflow creates and completes a return from the storefront. The admin would receive the return and
* process it from the dashboard. This workflow is used by the * process it from the dashboard. This workflow is used by the
* [Create Return Store API Route](https://docs.medusajs.com/api/store#return_postreturn). * [Create Return Store API Route](https://docs.medusajs.com/api/store#return_postreturn).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to create a return * You can use this workflow within your customizations or your own custom workflows, allowing you to create a return
* for an order in your custom flow. * for an order in your custom flow.
* *
* @example * @example
* const { result } = await createAndCompleteReturnOrderWorkflow(container) * const { result } = await createAndCompleteReturnOrderWorkflow(container)
* .run({ * .run({
@@ -302,9 +299,9 @@ export const createAndCompleteReturnOrderWorkflowId =
* ] * ]
* } * }
* }) * })
* *
* @summary * @summary
* *
* Create and complete a return for an order. * Create and complete a return for an order.
*/ */
export const createAndCompleteReturnOrderWorkflow = createWorkflow( export const createAndCompleteReturnOrderWorkflow = createWorkflow(
@@ -348,7 +345,6 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
], ],
variables: returnShippingOptionsVariables, variables: returnShippingOptionsVariables,
list: false, list: false,
throw_if_key_not_found: true,
}).config({ name: "return-shipping-option" }) }).config({ name: "return-shipping-option" })
const shippingMethodData = transform( const shippingMethodData = transform(
@@ -18,9 +18,9 @@ import {
} from "@medusajs/utils" } from "@medusajs/utils"
import { useQueryGraphStep } from "../../../common" import { useQueryGraphStep } from "../../../common"
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
import { previewOrderChangeStep } from "../../steps" import { previewOrderChangeStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes" import { confirmOrderChanges } from "../../steps/confirm-order-changes"
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
/** /**
* The details of the order transfer acceptance to validate. * The details of the order transfer acceptance to validate.
@@ -43,14 +43,14 @@ export type AcceptOrderTransferValidationStepInput = {
/** /**
* This step validates that an order transfer can be accepted. If the * This step validates that an order transfer can be accepted. If the
* order doesn't have an existing transfer request, the step throws an error. * order doesn't have an existing transfer request, the step throws an error.
* *
* :::note * :::note
* *
* You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = acceptOrderTransferValidationStep({ * const data = acceptOrderTransferValidationStep({
* token: "sk_123456", * token: "sk_123456",
@@ -95,12 +95,12 @@ export const acceptOrderTransferValidationStep = createStep(
export const acceptOrderTransferWorkflowId = "accept-order-transfer-workflow" export const acceptOrderTransferWorkflowId = "accept-order-transfer-workflow"
/** /**
* This workflow accepts an order transfer, requested previously by the {@link requestOrderTransferWorkflow}. This workflow is used by the * This workflow accepts an order transfer, requested previously by the {@link requestOrderTransferWorkflow}. This workflow is used by the
* [Accept Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransferaccept). * [Accept Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransferaccept).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to build a custom flow * You can use this workflow within your customizations or your own custom workflows, allowing you to build a custom flow
* around accepting an order transfer. * around accepting an order transfer.
* *
* @example * @example
* const { result } = await acceptOrderTransferWorkflow(container) * const { result } = await acceptOrderTransferWorkflow(container)
* .run({ * .run({
@@ -109,9 +109,9 @@ export const acceptOrderTransferWorkflowId = "accept-order-transfer-workflow"
* order_id: "order_123", * order_id: "order_123",
* } * }
* }) * })
* *
* @summary * @summary
* *
* Accept an order transfer request. * Accept an order transfer request.
*/ */
export const acceptOrderTransferWorkflow = createWorkflow( export const acceptOrderTransferWorkflow = createWorkflow(
@@ -149,7 +149,6 @@ export const acceptOrderTransferWorkflow = createWorkflow(
order_id: input.order_id, order_id: input.order_id,
status: [OrderChangeStatus.REQUESTED], status: [OrderChangeStatus.REQUESTED],
}, },
options: { throwIfKeyNotFound: true },
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const orderChange = transform( const orderChange = transform(
@@ -43,14 +43,14 @@ export type CancelTransferOrderRequestValidationStep = {
* This step validates that a requested order transfer can be canceled. * This step validates that a requested order transfer can be canceled.
* If the customer canceling the order transfer isn't the one that requested the transfer, * If the customer canceling the order transfer isn't the one that requested the transfer,
* the step throws an error. Admin users can cancel any order transfer. * the step throws an error. Admin users can cancel any order transfer.
* *
* :::note * :::note
* *
* You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = cancelTransferOrderRequestValidationStep({ * const data = cancelTransferOrderRequestValidationStep({
* order: { * order: {
@@ -101,10 +101,10 @@ export const cancelTransferOrderRequestWorkflowId =
* This workflow cancels a requested order transfer. This operation is allowed only by admin users and the customer that requested the transfer. * This workflow cancels a requested order transfer. This operation is allowed only by admin users and the customer that requested the transfer.
* This workflow is used by the [Cancel Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransfercancel), * This workflow is used by the [Cancel Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransfercancel),
* and the [Cancel Transfer Request Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersidtransfercancel). * and the [Cancel Transfer Request Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersidtransfercancel).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to build a custom flow * You can use this workflow within your customizations or your own custom workflows, allowing you to build a custom flow
* around canceling an order transfer. * around canceling an order transfer.
* *
* @example * @example
* const { result } = await cancelOrderTransferRequestWorkflow(container) * const { result } = await cancelOrderTransferRequestWorkflow(container)
* .run({ * .run({
@@ -114,9 +114,9 @@ export const cancelTransferOrderRequestWorkflowId =
* actor_type: "customer" * actor_type: "customer"
* } * }
* }) * })
* *
* @summary * @summary
* *
* Cancel an order transfer request. * Cancel an order transfer request.
*/ */
export const cancelOrderTransferRequestWorkflow = createWorkflow( export const cancelOrderTransferRequestWorkflow = createWorkflow(
@@ -143,7 +143,6 @@ export const cancelOrderTransferRequestWorkflow = createWorkflow(
order_id: input.order_id, order_id: input.order_id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED], status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
}, },
options: { throwIfKeyNotFound: true },
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const orderChange = transform( const orderChange = transform(
@@ -42,16 +42,16 @@ export type DeclineTransferOrderRequestValidationStepInput = {
/** /**
* This step validates that a requested order transfer can be declineed. * This step validates that a requested order transfer can be declineed.
* If the provided token doesn't match the token of the transfer request, * If the provided token doesn't match the token of the transfer request,
* the step throws an error. * the step throws an error.
* *
* :::note * :::note
* *
* You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
* *
* ::: * :::
* *
* @example * @example
* const data = declineTransferOrderRequestValidationStep({ * const data = declineTransferOrderRequestValidationStep({
* order: { * order: {
@@ -93,9 +93,9 @@ export const declineTransferOrderRequestWorkflowId =
/** /**
* This workflow declines a requested order transfer by its token. It's used by the * This workflow declines a requested order transfer by its token. It's used by the
* [Decline Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransferdecline). * [Decline Order Transfer Store API Route](https://docs.medusajs.com/api/store#orders_postordersidtransferdecline).
* *
* You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around declining an order transfer request. * You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around declining an order transfer request.
* *
* @example * @example
* const { result } = await declineOrderTransferRequestWorkflow(container) * const { result } = await declineOrderTransferRequestWorkflow(container)
* .run({ * .run({
@@ -104,9 +104,9 @@ export const declineTransferOrderRequestWorkflowId =
* order_id: "order_123", * order_id: "order_123",
* } * }
* }) * })
* *
* @summary * @summary
* *
* Decline a requested order transfer. * Decline a requested order transfer.
*/ */
export const declineOrderTransferRequestWorkflow = createWorkflow( export const declineOrderTransferRequestWorkflow = createWorkflow(
@@ -133,7 +133,6 @@ export const declineOrderTransferRequestWorkflow = createWorkflow(
order_id: input.order_id, order_id: input.order_id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED], status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
}, },
options: { throwIfKeyNotFound: true },
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const orderChange = transform( const orderChange = transform(
@@ -33,21 +33,18 @@ export async function ensurePublishableApiKeyMiddleware(
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY) const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
try { try {
const { data } = await query.graph( const { data } = await query.graph({
{ entity: "api_key",
entity: "api_key", fields: ["id", "token", "sales_channels_link.sales_channel_id"],
fields: ["id", "token", "sales_channels_link.sales_channel_id"], filters: {
filters: { token: publishableApiKey,
token: publishableApiKey, type: ApiKeyType.PUBLISHABLE,
type: ApiKeyType.PUBLISHABLE, $or: [
$or: [ { revoked_at: { $eq: null } },
{ revoked_at: { $eq: null } }, { revoked_at: { $gt: new Date() } },
{ revoked_at: { $gt: new Date() } }, ],
],
},
}, },
{ throwIfKeyNotFound: true } })
)
apiKey = data[0] apiKey = data[0]
} catch (e) { } catch (e) {
@@ -832,5 +832,21 @@ describe("RemoteJoiner", () => {
await expect(dataNotFound).rejects.toThrowError( await expect(dataNotFound).rejects.toThrowError(
"order id not found: ord_1234556" "order id not found: ord_1234556"
) )
const queryNotFoundNoParam = RemoteJoiner.parseQuery(`
query {
order {
id
number
}
}
`)
const dataNotFoundNoPK = joiner.query(queryNotFoundNoParam, {
throwIfKeyNotFound: true,
})
await expect(dataNotFoundNoPK).rejects.toThrowError(
"order: Primary key(s) [id] not found in filters"
)
}) })
}) })
@@ -37,6 +37,15 @@ type InternalImplodeMapping = {
isList?: boolean isList?: boolean
} }
type InternalParseExpandsParams = {
initialService: RemoteExpandProperty
query: RemoteJoinerQuery
serviceConfig: InternalJoinerServiceConfig
expands: RemoteJoinerQuery["expands"]
implodeMapping: InternalImplodeMapping[]
options?: RemoteJoinerOptions
}
export class RemoteJoiner { export class RemoteJoiner {
private serviceConfigCache: Map<string, InternalJoinerServiceConfig> = private serviceConfigCache: Map<string, InternalJoinerServiceConfig> =
new Map() new Map()
@@ -831,14 +840,9 @@ export class RemoteJoiner {
}) })
} }
private parseExpands(params: { private parseExpands(
initialService: RemoteExpandProperty params: InternalParseExpandsParams
query: RemoteJoinerQuery ): Map<string, RemoteExpandProperty> {
serviceConfig: InternalJoinerServiceConfig
expands: RemoteJoinerQuery["expands"]
implodeMapping: InternalImplodeMapping[]
options?: RemoteJoinerOptions
}): Map<string, RemoteExpandProperty> {
const { initialService, query, serviceConfig, expands, implodeMapping } = const { initialService, query, serviceConfig, expands, implodeMapping } =
params params
@@ -1203,8 +1207,30 @@ export class RemoteJoiner {
queryObj, queryObj,
}) })
if (options?.throwIfKeyNotFound) {
if (primaryKeyArg?.value == undefined) {
if (!primaryKeyArg) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`${
serviceConfig.entity ?? serviceConfig.serviceName
}: Primary key(s) [${serviceConfig.primaryKeys.join(
", "
)}] not found in filters`
)
}
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`${
serviceConfig.entity ?? serviceConfig.serviceName
}: Value for primary key ${primaryKeyArg.name} not found in filters`
)
}
}
const implodeMapping: InternalImplodeMapping[] = [] const implodeMapping: InternalImplodeMapping[] = []
const parseExpandsConfig: Parameters<typeof this.parseExpands>[0] = { const parseExpandsConfig: InternalParseExpandsParams = {
initialService: { initialService: {
property: "", property: "",
parent: "", parent: "",