chore(medusa, workflows-sdk): Workflow default to throw the first error (#7442)

**What**
Currently when a workflow fail it will throw an error which contains the messages of all error thrown durring the workflow lifetime. Therefore, in our cases we are always preventing workflow to throw and grab the first error that we then re throw.
This pr eliminate that need by throwing back the first error among the error thrown by a workflow as it is the main case. In case someone need a special handling they can still set the option throwOnError to false and handle the error the way they need
This commit is contained in:
Adrien de Peretti
2024-05-24 14:55:05 +02:00
committed by GitHub
parent 16651f9849
commit fff1b3ef9c
109 changed files with 147 additions and 773 deletions
@@ -1 +0,0 @@
export const emptyHandler: any = () => {}
@@ -1,5 +1,4 @@
export * from "./merge-data"
export * from "./empty-handler"
export * from "./pipe"
export * from "./workflow-export"
export * from "./type"
@@ -4,6 +4,7 @@ import { isObject } from "@medusajs/utils"
/**
* Pipe utils that merges data from an object into a new object.
* The new object will have a target key with the merged data from the keys if specified.
* @deprecated
* @param keys
* @param target
*/
@@ -66,6 +66,11 @@ export type PipelineHandler<T extends any = undefined> = (
: T
>
/**
* @deprecated
* @param input
* @param functions
*/
export function pipe<T>(
input: PipelineInput,
...functions: [...PipelineHandler[], PipelineHandler<T>]
@@ -5,7 +5,7 @@ import {
TransactionState,
} from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types"
import { MedusaContextType, isPresent } from "@medusajs/utils"
import { isPresent, MedusaContextType } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
@@ -87,10 +87,11 @@ function createContextualWorkflowRunner<
failedStatus.includes(transaction.getState()) &&
throwOnError
) {
const errorMessage = errors
/*const errorMessage = errors
?.map((err) => `${err.error?.message}${EOL}${err.error?.stack}`)
?.join(`${EOL}`)
throw new Error(errorMessage)
?.join(`${EOL}`)*/
const firstError = errors?.[0]?.error ?? new Error("Unknown error")
throw firstError
}
let result
@@ -10,7 +10,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminRevokeApiKeyType>,
res: MedusaResponse
) => {
const { errors } = await revokeApiKeysWorkflow(req.scope).run({
await revokeApiKeysWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
revoke: {
@@ -18,13 +18,8 @@ export const POST = async (
revoked_by: req.auth_context.actor_id,
},
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const apiKey = await refetchApiKey(
req.params.id,
req.scope,
@@ -27,18 +27,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateApiKeyType>,
res: MedusaResponse
) => {
const { errors } = await updateApiKeysWorkflow(req.scope).run({
await updateApiKeysWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const apiKey = await refetchApiKey(
req.params.id,
req.scope,
@@ -54,15 +49,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteApiKeysWorkflow(req.scope).run({
await deleteApiKeysWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "api_key",
@@ -21,19 +21,14 @@ export const POST = async (
)
}
const { errors } = await linkSalesChannelsToApiKeyWorkflow(req.scope).run({
await linkSalesChannelsToApiKeyWorkflow(req.scope).run({
input: {
id: req.params.id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const updatedApiKey = await refetchApiKey(
req.params.id,
req.scope,
@@ -1,4 +1,4 @@
import { createApiKeysWorkflow } from "@medusajs/core-flows"
import {createApiKeysWorkflow} from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
@@ -7,7 +7,7 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { AdminCreateApiKeyType } from "./validators"
import {AdminCreateApiKeyType} from "./validators"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -45,15 +45,10 @@ export const POST = async (
},
]
const { result, errors } = await createApiKeysWorkflow(req.scope).run({
const { result } = await createApiKeysWorkflow(req.scope).run({
input: { api_keys: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
// We should not refetch the api key here, as we need to show the secret key in the response (and never again)
// And the only time we get to see the secret, is when we create it
res.status(200).json({ api_key: result[0] })
@@ -13,16 +13,9 @@ export const POST = async (
) => {
const { id } = req.params
const { add, remove } = req.validatedBody
const { errors } = await addOrRemoveCampaignPromotionsWorkflow(req.scope).run(
{
input: { id, add, remove },
throwOnError: false,
}
)
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
await addOrRemoveCampaignPromotionsWorkflow(req.scope).run({
input: { id, add, remove },
})
const campaign = await refetchCampaign(
req.params.id,
@@ -43,15 +43,10 @@ export const POST = async (
},
]
const { errors } = await updateCampaigns.run({
await updateCampaigns.run({
input: { campaignsData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const campaign = await refetchCampaign(
req.params.id,
req.scope,
@@ -67,15 +62,10 @@ export const DELETE = async (
const id = req.params.id
const deleteCampaigns = deleteCampaignsWorkflow(req.scope)
const { errors } = await deleteCampaigns.run({
await deleteCampaigns.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "campaign",
@@ -2,13 +2,13 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { createCampaignsWorkflow } from "@medusajs/core-flows"
import {createCampaignsWorkflow} from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { AdminCreateCampaignType } from "./validators"
import { refetchCampaign } from "./helpers"
import {AdminCreateCampaignType} from "./validators"
import {refetchCampaign} from "./helpers"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -42,18 +42,13 @@ export const POST = async (
const createCampaigns = createCampaignsWorkflow(req.scope)
const campaignsData = [req.validatedBody]
const { result, errors } = await createCampaigns.run({
const { result } = await createCampaigns.run({
input: { campaignsData },
throwOnError: false,
context: {
requestId: req.requestId,
},
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const campaign = await refetchCampaign(
result[0].id,
req.scope,
@@ -14,19 +14,14 @@ export const POST = async (
const { add = [], remove = [] } = req.validatedBody
const workflow = batchLinkProductsToCollectionWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const collection = await refetchCollection(
req.params.id,
req.scope,
@@ -27,18 +27,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>,
res: MedusaResponse
) => {
const { errors } = await updateCollectionsWorkflow(req.scope).run({
await updateCollectionsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const collection = await refetchCollection(
req.params.id,
req.scope,
@@ -54,15 +49,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteCollectionsWorkflow(req.scope).run({
await deleteCollectionsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "collection",
@@ -46,15 +46,10 @@ export const POST = async (
},
]
const { result, errors } = await createCollectionsWorkflow(req.scope).run({
const { result } = await createCollectionsWorkflow(req.scope).run({
input: { collections: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const collection = await refetchCollection(
result[0].id,
req.scope,
@@ -15,19 +15,14 @@ export const POST = async (
const { add, remove } = req.validatedBody
const workflow = linkCustomersToCustomerGroupWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customerGroup = await refetchCustomerGroup(
req.params.id,
req.scope,
@@ -28,18 +28,13 @@ export const POST = async (
res: MedusaResponse
) => {
const updateGroups = updateCustomerGroupsWorkflow(req.scope)
const { errors } = await updateGroups.run({
await updateGroups.run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customerGroup = await refetchCustomerGroup(
req.params.id,
req.scope,
@@ -55,15 +50,10 @@ export const DELETE = async (
const id = req.params.id
const deleteCustomerGroups = deleteCustomerGroupsWorkflow(req.scope)
const { errors } = await deleteCustomerGroups.run({
await deleteCustomerGroups.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "customer_group",
@@ -47,15 +47,10 @@ export const POST = async (
},
]
const { result, errors } = await createGroups.run({
const { result } = await createGroups.run({
input: { customersData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customerGroup = await refetchCustomerGroup(
result[0].id,
req.scope,
@@ -37,18 +37,13 @@ export const POST = async (
res: MedusaResponse
) => {
const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
const { errors } = await updateAddresses.run({
await updateAddresses.run({
input: {
selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -65,15 +60,10 @@ export const DELETE = async (
const id = req.params.address_id
const deleteAddress = deleteCustomerAddressesWorkflow(req.scope)
const { errors } = await deleteAddress.run({
await deleteAddress.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -49,15 +49,10 @@ export const POST = async (
},
]
const { errors } = await createAddresses.run({
await createAddresses.run({
input: { addresses },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
customerId,
req.scope,
@@ -35,18 +35,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>,
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const { errors } = await updateCustomersWorkflow(req.scope).run({
await updateCustomersWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -62,15 +57,10 @@ export const DELETE = async (
const id = req.params.id
const deleteCustomers = deleteCustomersWorkflow(req.scope)
const { errors } = await deleteCustomers.run({
await deleteCustomers.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "customer",
@@ -50,15 +50,10 @@ export const POST = async (
},
]
const { result, errors } = await createCustomers.run({
const { result } = await createCustomers.run({
input: { customersData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
result[0].id,
req.scope,
@@ -76,15 +76,10 @@ export const POST = async (
input.email = customer?.email
}
const { result, errors } = await createOrdersWorkflow(req.scope).run({
const { result } = await createOrdersWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const draftOrder = await refetchOrder(
result.id,
req.scope,
@@ -12,15 +12,10 @@ export const DELETE = async (
) => {
const { id } = req.params
const { errors } = await deleteFulfillmentSetsWorkflow(req.scope).run({
await deleteFulfillmentSetsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "fulfillment_set",
@@ -75,15 +75,10 @@ export const POST = async (
update: req.validatedBody,
}
const { errors } = await updateServiceZonesWorkflow(req.scope).run({
await updateServiceZonesWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const [fulfillment_set] = await remoteQuery(
remoteQueryObjectFromString({
entryPoint: "fulfillment_sets",
@@ -119,15 +114,10 @@ export const DELETE = async (
)
}
const { errors } = await deleteServiceZonesWorkflow(req.scope).run({
await deleteServiceZonesWorkflow(req.scope).run({
input: { ids: [zone_id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id: zone_id,
object: "service-zone",
@@ -17,15 +17,10 @@ export const POST = async (
],
}
const { errors } = await createServiceZonesWorkflow(req.scope).run({
await createServiceZonesWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const fulfillmentSet = await refetchFulfillmentSet(
req.params.id,
req.scope,
@@ -11,15 +11,10 @@ export const POST = async (
res: MedusaResponse
) => {
const { id } = req.params
const { errors } = await cancelFulfillmentWorkflow(req.scope).run({
await cancelFulfillmentWorkflow(req.scope).run({
input: { id },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const fulfillment = await refetchFulfillment(
id,
req.scope,
@@ -12,15 +12,10 @@ export const POST = async (
) => {
const { id } = req.params
const { errors } = await createShipmentWorkflow(req.scope).run({
await createShipmentWorkflow(req.scope).run({
input: { id, ...req.validatedBody },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const fulfillment = await refetchFulfillment(
id,
req.scope,
@@ -10,17 +10,12 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateFulfillmentType>,
res: MedusaResponse
) => {
const { result: fullfillment, errors } = await createFulfillmentWorkflow(
const { result: fullfillment } = await createFulfillmentWorkflow(
req.scope
).run({
input: req.validatedBody,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const fulfillment = await refetchFulfillment(
fullfillment.id,
req.scope,
@@ -64,17 +64,12 @@ export const POST = async (
res: MedusaResponse
) => {
const { id, location_id } = req.params
const { errors } = await updateInventoryLevelsWorkflow(req.scope).run({
await updateInventoryLevelsWorkflow(req.scope).run({
input: {
updates: [{ ...req.validatedBody, inventory_item_id: id, location_id }],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const inventoryItem = await refetchInventoryItem(
id,
req.scope,
@@ -20,7 +20,7 @@ export const POST = async (
// TODO: Normalize workflow and response, and add support for updates
const workflow = bulkCreateDeleteLevelsWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
deletes:
req.validatedBody.delete?.map((location_id) => ({
@@ -33,12 +33,7 @@ export const POST = async (
inventory_item_id: id,
})) ?? [],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ inventory_item: {} })
}
@@ -18,7 +18,7 @@ export const POST = async (
const { id } = req.params
const workflow = createInventoryLevelsWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
inventory_levels: [
{
@@ -27,13 +27,8 @@ export const POST = async (
},
],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const inventoryItem = await refetchInventoryItem(
id,
req.scope,
@@ -60,15 +60,10 @@ export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
const id = req.params.id
const deleteInventoryItems = deleteInventoryItemWorkflow(req.scope)
const { errors } = await deleteInventoryItems.run({
await deleteInventoryItems.run({
input: [id],
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "inventory_item",
@@ -35,15 +35,10 @@ export const DELETE = async (
const { id } = req.params
const workflow = deleteInvitesWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "invite",
@@ -45,14 +45,9 @@ export const POST = async (
input: {
invites: [req.validatedBody],
},
throwOnError: false,
}
const { result, errors } = await workflow.run(input)
if (errors?.length) {
throw errors[0].error
}
const { result } = await workflow.run(input)
const invite = await refetchInvite(
result[0].id,
@@ -13,15 +13,10 @@ export const POST = async (
const remoteQuery = req.scope.resolve("remoteQuery")
const { id } = req.params
const { errors } = await archiveOrderWorkflow(req.scope).run({
await archiveOrderWorkflow(req.scope).run({
input: { orderIds: [req.validatedBody.order_id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const queryObject = remoteQueryObjectFromString({
entryPoint: "order",
variables: { id },
@@ -13,15 +13,10 @@ export const POST = async (
const remoteQuery = req.scope.resolve("remoteQuery")
const { id } = req.params
const { errors } = await completeOrderWorkflow(req.scope).run({
await completeOrderWorkflow(req.scope).run({
input: { orderIds: [req.validatedBody.order_id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const queryObject = remoteQueryObjectFromString({
entryPoint: "order",
variables: { id },
@@ -12,19 +12,14 @@ export const POST = async (
) => {
const { id } = req.params
const { errors } = await capturePaymentWorkflow(req.scope).run({
await capturePaymentWorkflow(req.scope).run({
input: {
payment_id: id,
captured_by: req.auth_context.actor_id,
amount: req.validatedBody.amount,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const payment = await refetchPayment(
id,
req.scope,
@@ -11,19 +11,14 @@ export const POST = async (
res: MedusaResponse
) => {
const { id } = req.params
const { errors } = await refundPaymentWorkflow(req.scope).run({
await refundPaymentWorkflow(req.scope).run({
input: {
payment_id: id,
created_by: req.auth_context.actor_id,
amount: req.validatedBody.amount,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const payment = await refetchPayment(
id,
req.scope,
@@ -29,7 +29,7 @@ export const POST = async (
} = req.validatedBody
const workflow = batchPriceListPricesWorkflow(req.scope)
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input: {
data: {
id,
@@ -38,13 +38,8 @@ export const POST = async (
delete: deletePriceIds,
},
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const [created, updated] = await promiseAll([
listPrices(
result.created.map((c) => c.id),
@@ -34,7 +34,7 @@ export const POST = async (
)
const workflow = batchPriceListPricesWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
data: {
id,
@@ -43,13 +43,8 @@ export const POST = async (
delete: productPriceIds,
},
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const priceList = await fetchPriceList(
id,
req.scope,
@@ -29,15 +29,10 @@ export const POST = async (
const id = req.params.id
const workflow = updatePriceListsWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { price_lists_data: [{ ...req.validatedBody, id }] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const price_list = await fetchPriceList(
id,
req.scope,
@@ -54,15 +49,10 @@ export const DELETE = async (
const id = req.params.id
const workflow = deletePriceListsWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "price_list",
@@ -40,15 +40,10 @@ export const POST = async (
res: MedusaResponse
) => {
const workflow = createPriceListsWorkflow(req.scope)
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input: { price_lists_data: [req.validatedBody] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const price_list = await fetchPriceList(
result[0].id,
req.scope,
@@ -38,17 +38,12 @@ export const POST = async (
res: MedusaResponse
) => {
const workflow = updatePricingRuleTypesWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
data: [{ ...req.validatedBody, id: req.params.id }],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const ruleType = await refetchRuleType(
req.params.id,
req.scope,
@@ -67,15 +62,10 @@ export const DELETE = async (
const id = req.params.id
const workflow = deletePricingRuleTypesWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "rule_type",
@@ -44,15 +44,10 @@ export const POST = async (
const workflow = createPricingRuleTypesWorkflow(req.scope)
const ruleTypesData = [req.validatedBody]
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input: { data: ruleTypesData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const ruleType = await refetchRuleType(
result[0].id,
req.scope,
@@ -15,15 +15,10 @@ export const POST = async (
) => {
const { id } = req.params
const { errors } = await batchLinkProductsToCategoryWorkflow(req.scope).run({
await batchLinkProductsToCategoryWorkflow(req.scope).run({
input: { id, ...req.validatedBody },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const category = await refetchEntity(
"product_category",
id,
@@ -31,15 +31,10 @@ export const POST = async (
const { id } = req.params
// TODO: Should be converted to bulk update
const { errors } = await updateProductCategoryWorkflow(req.scope).run({
await updateProductCategoryWorkflow(req.scope).run({
input: { id, data: req.validatedBody },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const [category] = await refetchEntities(
"product_category",
{ id, ...req.filterableFields },
@@ -37,16 +37,9 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateProductCategoryType>,
res: MedusaResponse<AdminProductCategoryResponse>
) => {
const { result, errors } = await createProductCategoryWorkflow(req.scope).run(
{
input: { product_category: req.validatedBody },
throwOnError: false,
}
)
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const { result } = await createProductCategoryWorkflow(req.scope).run({
input: { product_category: req.validatedBody },
})
const [category] = await refetchEntities(
"product_category",
@@ -30,18 +30,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductTypeType>,
res: MedusaResponse
) => {
const { result, errors } = await updateProductTypesWorkflow(req.scope).run({
const { result } = await updateProductTypesWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const productType = await refetchProductType(
result[0].id,
req.scope,
@@ -57,15 +52,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteProductTypesWorkflow(req.scope).run({
await deleteProductTypesWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "product_type",
@@ -44,15 +44,10 @@ export const POST = async (
) => {
const input = [req.validatedBody]
const { result, errors } = await createProductTypesWorkflow(req.scope).run({
const { result } = await createProductTypesWorkflow(req.scope).run({
input: { product_types: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const productType = await refetchProductType(
result[0].id,
req.scope,
@@ -42,18 +42,13 @@ export const POST = async (
const productId = req.params.id
const optionId = req.params.option_id
const { errors } = await updateProductOptionsWorkflow(req.scope).run({
await updateProductOptionsWorkflow(req.scope).run({
input: {
selector: { id: optionId, product_id: productId },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -70,15 +65,10 @@ export const DELETE = async (
const optionId = req.params.option_id
// TODO: I believe here we cannot even enforce the product ID based on the standard API we provide?
const { errors } = await deleteProductOptionsWorkflow(req.scope).run({
await deleteProductOptionsWorkflow(req.scope).run({
input: { ids: [optionId] /* product_id: productId */ },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -51,15 +51,10 @@ export const POST = async (
},
]
const { errors } = await createProductOptionsWorkflow(req.scope).run({
await createProductOptionsWorkflow(req.scope).run({
input: { product_options: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -43,18 +43,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductType>,
res: MedusaResponse
) => {
const { result, errors } = await updateProductsWorkflow(req.scope).run({
const { result } = await updateProductsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody as UpdateProductDTO,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
result[0].id,
req.scope,
@@ -69,15 +64,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteProductsWorkflow(req.scope).run({
await deleteProductsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "product",
@@ -49,18 +49,13 @@ export const POST = async (
const productId = req.params.id
const variantId = req.params.variant_id
const { errors } = await updateProductVariantsWorkflow(req.scope).run({
await updateProductVariantsWorkflow(req.scope).run({
input: {
selector: { id: variantId, product_id: productId },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -78,15 +73,10 @@ export const DELETE = async (
const variantId = req.params.variant_id
// TODO: I believe here we cannot even enforce the product ID based on the standard API we provide?
const { errors } = await deleteProductVariantsWorkflow(req.scope).run({
await deleteProductVariantsWorkflow(req.scope).run({
input: { ids: [variantId] /* product_id: productId */ },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -34,15 +34,10 @@ export const POST = async (
// TODO: Fix types
} as any
const { result, errors } = await batchProductVariantsWorkflow(req.scope).run({
const { result } = await batchProductVariantsWorkflow(req.scope).run({
input: normalizedInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchVariants(
result,
req.scope,
@@ -56,15 +56,10 @@ export const POST = async (
},
]
const { errors } = await createProductVariantsWorkflow(req.scope).run({
await createProductVariantsWorkflow(req.scope).run({
input: { product_variants: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
productId,
req.scope,
@@ -19,15 +19,10 @@ export const POST = async (
// TODO: Fix types
const input = req.validatedBody as any
const { result, errors } = await batchProductsWorkflow(req.scope).run({
const { result } = await batchProductsWorkflow(req.scope).run({
input,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchProducts(
result,
req.scope,
@@ -50,15 +50,10 @@ export const POST = async (
) => {
const input = [req.validatedBody as CreateProductDTO]
const { result, errors } = await createProductsWorkflow(req.scope).run({
const { result } = await createProductsWorkflow(req.scope).run({
input: { products: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const product = await refetchProduct(
result[0].id,
req.scope,
@@ -21,7 +21,7 @@ export const POST = async (
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
const { result } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.BUY_RULES,
@@ -29,13 +29,8 @@ export const POST = async (
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
@@ -54,15 +54,10 @@ export const POST = async (
} as any,
]
const { errors } = await updatePromotions.run({
await updatePromotions.run({
input: { promotionsData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
req.params.id,
req.scope,
@@ -79,15 +74,10 @@ export const DELETE = async (
const id = req.params.id
const deletePromotions = deletePromotionsWorkflow(req.scope)
const { errors } = await deletePromotions.run({
await deletePromotions.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "promotion",
@@ -21,7 +21,7 @@ export const POST = async (
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
const { result } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.RULES,
@@ -29,13 +29,8 @@ export const POST = async (
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
@@ -21,7 +21,7 @@ export const POST = async (
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
const { result } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.TARGET_RULES,
@@ -29,13 +29,8 @@ export const POST = async (
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
@@ -45,15 +45,10 @@ export const POST = async (
const createPromotions = createPromotionsWorkflow(req.scope)
const promotionsData = [req.validatedBody] as any
const { result, errors } = await createPromotions.run({
const { result } = await createPromotions.run({
input: { promotionsData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
result[0].id,
req.scope,
@@ -25,18 +25,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateRegionType>,
res: MedusaResponse
) => {
const { result, errors } = await updateRegionsWorkflow(req.scope).run({
const { result } = await updateRegionsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const region = await refetchRegion(
result[0].id,
req.scope,
@@ -52,15 +47,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteRegionsWorkflow(req.scope).run({
await deleteRegionsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "region",
@@ -41,15 +41,10 @@ export const POST = async (
) => {
const input = [req.validatedBody]
const { result, errors } = await createRegionsWorkflow(req.scope).run({
const { result } = await createRegionsWorkflow(req.scope).run({
input: { regions: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const region = await refetchRegion(
result[0].id,
req.scope,
@@ -38,17 +38,12 @@ export const POST = async (
res: MedusaResponse
) => {
const { id } = req.params
const { errors } = await updateReservationsWorkflow(req.scope).run({
await updateReservationsWorkflow(req.scope).run({
input: {
updates: [{ ...req.validatedBody, id }],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const reservation = await refetchReservation(
id,
req.scope,
@@ -63,15 +58,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteReservationsWorkflow(req.scope).run({
await deleteReservationsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "reservation",
@@ -45,15 +45,10 @@ export const POST = async (
) => {
const input = [req.validatedBody]
const { result, errors } = await createReservationsWorkflow(req.scope).run({
const { result } = await createReservationsWorkflow(req.scope).run({
input: { reservations: input },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const reservation = await refetchReservation(
result[0].id,
req.scope,
@@ -43,11 +43,7 @@ export const POST = async (
update: req.validatedBody,
}
const { result, errors } = await workflow.run({ input })
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const { result } = await workflow.run({ input })
const variables = { id: result[0].id }
@@ -50,11 +50,7 @@ export const POST = async (
],
}
const { result, errors } = await workflow.run({ input })
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const { result } = await workflow.run({ input })
const variables = { id: result[0].id }
@@ -1,4 +1,4 @@
import { createReturnOrderWorkflow } from "@medusajs/core-flows"
import {createReturnOrderWorkflow} from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
@@ -7,7 +7,7 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { AdminPostReturnsReqSchemaType } from "./validators"
import {AdminPostReturnsReqSchemaType} from "./validators"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -43,14 +43,9 @@ export const POST = async (
const input = req.validatedBody as AdminPostReturnsReqSchemaType
const workflow = createReturnOrderWorkflow(req.scope)
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ return: result })
}
@@ -15,19 +15,14 @@ export const POST = async (
const { add, remove } = req.validatedBody
const workflow = linkProductsToSalesChannelWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
req.params.id,
req.scope,
@@ -29,18 +29,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateSalesChannelType>,
res: MedusaResponse
) => {
const { errors } = await updateSalesChannelsWorkflow(req.scope).run({
await updateSalesChannelsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
req.params.id,
req.scope,
@@ -55,15 +50,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteSalesChannelsWorkflow(req.scope).run({
await deleteSalesChannelsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "sales-channel",
@@ -44,15 +44,10 @@ export const POST = async (
) => {
const salesChannelsData = [req.validatedBody]
const { errors, result } = await createSalesChannelsWorkflow(req.scope).run({
const { result } = await createSalesChannelsWorkflow(req.scope).run({
input: { salesChannelsData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
result[0].id,
req.scope,
@@ -28,15 +28,10 @@ export const POST = async (
...shippingOptionPayload,
}
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input: [workflowInput],
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const shippingOption = await refetchShippingOption(
result[0].id,
req.scope,
@@ -54,15 +49,10 @@ export const DELETE = async (
const workflow = deleteShippingOptionsWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { ids: [shippingOptionId] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res
.status(200)
.json({ id: shippingOptionId, object: "shipping_option", deleted: true })
@@ -20,9 +20,7 @@ export const POST = async (
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchShippingOptionRulesWorkflow(
req.scope
).run({
const { result } = await batchShippingOptionRulesWorkflow(req.scope).run({
input: {
create: req.validatedBody.create?.map((c) => ({
...c,
@@ -31,13 +29,8 @@ export const POST = async (
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
@@ -50,15 +50,10 @@ export const POST = async (
const workflow = createShippingOptionsWorkflow(req.scope)
const { result, errors } = await workflow.run({
const { result } = await workflow.run({
input: [shippingOptionPayload],
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const shippingOption = await refetchShippingOption(
result[0].id,
req.scope,
@@ -38,15 +38,10 @@ export const DELETE = async (
// Test if exists
await fulfillmentModuleService.retrieveShippingProfile(id)
const { errors } = await deleteShippingProfileWorkflow(req.scope).run({
await deleteShippingProfileWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "shipping_profile",
@@ -23,17 +23,10 @@ export const POST = async (
) => {
const shippingProfilePayload = req.validatedBody
const { result, errors } = await createShippingProfilesWorkflow(
req.scope
).run({
const { result } = await createShippingProfilesWorkflow(req.scope).run({
input: { data: [shippingProfilePayload] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const shippingProfile = await refetchShippingProfile(
result?.[0].id,
req.scope,
@@ -10,7 +10,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateStockLocationFulfillmentSetType>,
res: MedusaResponse
) => {
const { errors } = await createLocationFulfillmentSetWorkflow(req.scope).run({
await createLocationFulfillmentSetWorkflow(req.scope).run({
input: {
location_id: req.params.id,
fulfillment_set_data: {
@@ -18,13 +18,8 @@ export const POST = async (
type: req.validatedBody.type,
},
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
@@ -65,15 +65,10 @@ export const DELETE = async (
) => {
const { id } = req.params
const { errors } = await deleteStockLocationsWorkflow(req.scope).run({
await deleteStockLocationsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "stock_location",
@@ -15,19 +15,14 @@ export const POST = async (
const { add, remove } = req.validatedBody
const workflow = linkSalesChannelsToStockLocationWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
@@ -32,18 +32,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateStoreType>,
res: MedusaResponse
) => {
const { result, errors } = await updateStoresWorkflow(req.scope).run({
const { result } = await updateStoresWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody as UpdateStoreDTO,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const store = await refetchStore(
result[0].id,
req.scope,
@@ -20,18 +20,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateTaxRateType>,
res: MedusaResponse
) => {
const { errors } = await updateTaxRatesWorkflow(req.scope).run({
await updateTaxRatesWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: { ...req.validatedBody, updated_by: req.auth_context.actor_id },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const taxRate = await refetchTaxRate(
req.params.id,
req.scope,
@@ -62,15 +57,10 @@ export const DELETE = async (
res: MedusaResponse
) => {
const id = req.params.id
const { errors } = await deleteTaxRatesWorkflow(req.scope).run({
await deleteTaxRatesWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "tax_rate",
@@ -9,15 +9,10 @@ export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { errors } = await deleteTaxRateRulesWorkflow(req.scope).run({
await deleteTaxRateRulesWorkflow(req.scope).run({
input: { ids: [req.params.rule_id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const taxRate = await refetchTaxRate(
req.params.id,
req.scope,
@@ -10,7 +10,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateTaxRateRuleType>,
res: MedusaResponse
) => {
const { errors } = await createTaxRateRulesWorkflow(req.scope).run({
await createTaxRateRulesWorkflow(req.scope).run({
input: {
rules: [
{
@@ -20,13 +20,8 @@ export const POST = async (
},
],
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const taxRate = await refetchTaxRate(
req.params.id,
req.scope,
@@ -17,20 +17,15 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateTaxRateType>,
res: MedusaResponse
) => {
const { result, errors } = await createTaxRatesWorkflow(req.scope).run({
const { result } = await createTaxRatesWorkflow(req.scope).run({
input: [
{
...req.validatedBody,
created_by: req.auth_context.actor_id,
},
],
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const taxRate = await refetchTaxRate(
result[0].id,
req.scope,
@@ -32,15 +32,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteTaxRegionsWorkflow(req.scope).run({
await deleteTaxRegionsWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "tax_region",
@@ -17,20 +17,15 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateTaxRegionType>,
res: MedusaResponse
) => {
const { result, errors } = await createTaxRegionsWorkflow(req.scope).run({
const { result } = await createTaxRegionsWorkflow(req.scope).run({
input: [
{
...req.validatedBody,
created_by: req.auth_context.actor_id,
},
],
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const taxRegion = await refetchTaxRegion(
result[0].id,
req.scope,
@@ -36,15 +36,10 @@ export const DELETE = async (
) => {
const id = req.params.id
const { errors } = await deleteFilesWorkflow(req.scope).run({
await deleteFilesWorkflow(req.scope).run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "file",
@@ -19,7 +19,7 @@ export const POST = async (
)
}
const { result, errors } = await uploadFilesWorkflow(req.scope).run({
const { result } = await uploadFilesWorkflow(req.scope).run({
input: {
files: input?.map((f) => ({
filename: f.originalname,
@@ -27,12 +27,7 @@ export const POST = async (
content: f.buffer.toString("binary"),
})),
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ files: result })
}
@@ -73,15 +73,10 @@ export const DELETE = async (
const { id } = req.params
const workflow = deleteUsersWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "user",
+1 -8
View File
@@ -53,16 +53,9 @@ export const POST = async (
userData: req.validatedBody,
authIdentityId: req.auth_context.auth_identity_id,
},
throwOnError: false,
}
const { result, errors } = await createUserAccountWorkflow(req.scope).run(
input
)
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const { result } = await createUserAccountWorkflow(req.scope).run(input)
const { http } = req.scope.resolve(
ContainerRegistrationKeys.CONFIG_MODULE
@@ -28,7 +28,6 @@ export const POST = async (
context: {
requestId: req.requestId,
},
throwOnError: false,
} as WorkflowOrchestratorTypes.WorkflowOrchestratorRunDTO
const { acknowledgement } = await workflowEngineService.run(
@@ -22,7 +22,7 @@ export const POST = async (
// When an error occurs on the workflow, its potentially got to with cart validations, payments
// or inventory checks. Return the cart here along with errors for the consumer to take more action
// and fix them
if (Array.isArray(errors) && errors[0]) {
if (errors?.[0]) {
const error = errors[0].error
const statusOKErrors: string[] = [
// TODO: add inventory specific errors
@@ -45,15 +45,10 @@ export const POST = async (
update: req.validatedBody,
}
const { errors } = await updateLineItemInCartWorkflow(req.scope).run({
await updateLineItemInCartWorkflow(req.scope).run({
input,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const updatedCart = await refetchCart(
req.params.id,
req.scope,
@@ -69,15 +64,10 @@ export const DELETE = async (
) => {
const id = req.params.line_id
const { errors } = await deleteLineItemsWorkflow(req.scope).run({
await deleteLineItemsWorkflow(req.scope).run({
input: { cart_id: req.params.id, ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -18,15 +18,10 @@ export const POST = async (
cart,
}
const { errors } = await addToCartWorkflow(req.scope).run({
await addToCartWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const updatedCart = await refetchCart(
req.params.id,
req.scope,
@@ -14,19 +14,14 @@ export const POST = async (
const workflow = updateCartPromotionsWorkflow(req.scope)
const payload = req.validatedBody
const { errors } = await workflow.run({
await workflow.run({
input: {
promoCodes: payload.promo_codes,
cartId: req.params.id,
action: PromotionActions.ADD,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -43,19 +38,14 @@ export const DELETE = async (
const workflow = updateCartPromotionsWorkflow(req.scope)
const payload = req.validatedBody
const { errors } = await workflow.run({
await workflow.run({
input: {
promoCodes: payload.promo_codes,
cartId: req.params.id,
action: PromotionActions.REMOVE,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -20,18 +20,13 @@ export const POST = async (
) => {
const workflow = updateCartWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
...(req.validatedBody as UpdateCartDataDTO),
id: req.params.id,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -10,18 +10,13 @@ export const POST = async (
const workflow = addShippingMethodToWorkflow(req.scope)
const payload = req.validatedBody
const { errors } = await workflow.run({
await workflow.run({
input: {
options: [{ id: payload.option_id, data: payload.data }],
cart_id: req.params.id,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -9,18 +9,13 @@ export const POST = async (
) => {
const workflow = updateTaxLinesWorkflow(req.scope)
const { errors } = await workflow.run({
await workflow.run({
input: {
cart_or_cart_id: req.params.id,
force_tax_calculation: true,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,

Some files were not shown because too many files have changed in this diff Show More