diff --git a/packages/medusa/src/api/admin/campaigns/[id]/route.ts b/packages/medusa/src/api/admin/campaigns/[id]/route.ts index 465e122c8e..89a452b787 100644 --- a/packages/medusa/src/api/admin/campaigns/[id]/route.ts +++ b/packages/medusa/src/api/admin/campaigns/[id]/route.ts @@ -36,6 +36,16 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingCampaign = await refetchCampaign(req.params.id, req.scope, [ + "id", + ]) + if (!existingCampaign) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Campaign with id "${req.params.id}" not found` + ) + } + const { additional_data, ...rest } = req.validatedBody const updateCampaigns = updateCampaignsWorkflow(req.scope) const campaignsData = [ diff --git a/packages/medusa/src/api/admin/collections/[id]/route.ts b/packages/medusa/src/api/admin/collections/[id]/route.ts index 2f8fa8bf4c..20eaf01b70 100644 --- a/packages/medusa/src/api/admin/collections/[id]/route.ts +++ b/packages/medusa/src/api/admin/collections/[id]/route.ts @@ -10,6 +10,7 @@ import { import { AdminUpdateCollectionType } from "../validators" import { refetchCollection } from "../helpers" import { HttpTypes } from "@medusajs/types" +import { MedusaError } from "@medusajs/utils" export const GET = async ( req: AuthenticatedMedusaRequest, @@ -28,6 +29,16 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingCollection = await refetchCollection(req.params.id, req.scope, [ + "id", + ]) + if (!existingCollection) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Collection with id "${req.params.id}" not found` + ) + } + await updateCollectionsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/customer-groups/[id]/route.ts b/packages/medusa/src/api/admin/customer-groups/[id]/route.ts index 645c2c4860..663371616b 100644 --- a/packages/medusa/src/api/admin/customer-groups/[id]/route.ts +++ b/packages/medusa/src/api/admin/customer-groups/[id]/route.ts @@ -36,6 +36,18 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingCustomerGroup = await refetchCustomerGroup( + req.params.id, + req.scope, + ["id"] + ) + if (!existingCustomerGroup) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Customer group with id "${req.params.id}" not found` + ) + } + await updateCustomerGroupsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/customers/[id]/route.ts b/packages/medusa/src/api/admin/customers/[id]/route.ts index 061cf2efbc..dddf084a21 100644 --- a/packages/medusa/src/api/admin/customers/[id]/route.ts +++ b/packages/medusa/src/api/admin/customers/[id]/route.ts @@ -35,6 +35,16 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingCustomer = await refetchCustomer(req.params.id, req.scope, [ + "id", + ]) + if (!existingCustomer) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Customer with id "${req.params.id}" not found` + ) + } + const { additional_data, ...rest } = req.validatedBody await updateCustomersWorkflow(req.scope).run({ diff --git a/packages/medusa/src/api/admin/product-tags/[id]/route.ts b/packages/medusa/src/api/admin/product-tags/[id]/route.ts index bb9f013f1d..6cafa71b39 100644 --- a/packages/medusa/src/api/admin/product-tags/[id]/route.ts +++ b/packages/medusa/src/api/admin/product-tags/[id]/route.ts @@ -13,6 +13,7 @@ import { } from "../validators" import { refetchEntity } from "../../../utils/refetch-entity" import { HttpTypes } from "@medusajs/types" +import { MedusaError } from "@medusajs/utils" export const GET = async ( req: AuthenticatedMedusaRequest, @@ -32,6 +33,20 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingProductTag = await refetchEntity( + "product_tag", + req.params.id, + req.scope, + ["id"] + ) + + if (!existingProductTag) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Product tag with id "${req.params.id}" not found` + ) + } + const { result } = await updateProductTagsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/product-types/[id]/route.ts b/packages/medusa/src/api/admin/product-types/[id]/route.ts index 11cc36b88a..312761d41b 100644 --- a/packages/medusa/src/api/admin/product-types/[id]/route.ts +++ b/packages/medusa/src/api/admin/product-types/[id]/route.ts @@ -13,6 +13,7 @@ import { AdminUpdateProductTypeType, } from "../validators" import { HttpTypes } from "@medusajs/types" +import { MedusaError } from "@medusajs/utils" export const GET = async ( req: AuthenticatedMedusaRequest, @@ -31,6 +32,19 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingProductType = await refetchProductType( + req.params.id, + req.scope, + ["id"] + ) + + if (!existingProductType) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Product type with id "${req.params.id}" not found` + ) + } + const { result } = await updateProductTypesWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/products/[id]/route.ts b/packages/medusa/src/api/admin/products/[id]/route.ts index d69c831328..79f07bba94 100644 --- a/packages/medusa/src/api/admin/products/[id]/route.ts +++ b/packages/medusa/src/api/admin/products/[id]/route.ts @@ -38,6 +38,22 @@ export const POST = async ( ) => { const { additional_data, ...update } = req.validatedBody + const existingProduct = await refetchEntity( + "product", + req.params.id, + req.scope, + ["id"] + ) + /** + * Check if the product exists with the id or not before calling the workflow. + */ + if (!existingProduct) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Product with id "${req.params.id}" not found` + ) + } + const { result } = await updateProductsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/regions/[id]/route.ts b/packages/medusa/src/api/admin/regions/[id]/route.ts index 66d9738f32..76bb1b5e77 100644 --- a/packages/medusa/src/api/admin/regions/[id]/route.ts +++ b/packages/medusa/src/api/admin/regions/[id]/route.ts @@ -35,6 +35,14 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingRegion = refetchRegion(req.params.id, req.scope, ["id"]) + if (!existingRegion) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Region with id "${req.params.id}" not found` + ) + } + const { result } = await updateRegionsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/sales-channels/[id]/route.ts b/packages/medusa/src/api/admin/sales-channels/[id]/route.ts index 65f580a49a..e4317623ba 100644 --- a/packages/medusa/src/api/admin/sales-channels/[id]/route.ts +++ b/packages/medusa/src/api/admin/sales-channels/[id]/route.ts @@ -38,6 +38,19 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingSalesChannel = await refetchSalesChannel( + req.params.id, + req.scope, + ["id"] + ) + + if (!existingSalesChannel) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Sales channel with id "${req.params.id}" not found` + ) + } + await updateSalesChannelsWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/stores/[id]/route.ts b/packages/medusa/src/api/admin/stores/[id]/route.ts index 232abc77d3..0bf7453704 100644 --- a/packages/medusa/src/api/admin/stores/[id]/route.ts +++ b/packages/medusa/src/api/admin/stores/[id]/route.ts @@ -2,6 +2,7 @@ import { updateStoresWorkflow } from "@medusajs/core-flows" import { remoteQueryObjectFromString, ContainerRegistrationKeys, + MedusaError, } from "@medusajs/utils" import { AuthenticatedMedusaRequest, @@ -32,6 +33,14 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingStore = await refetchStore(req.params.id, req.scope, ["id"]) + if (!existingStore) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Store with id "${req.params.id}" not found` + ) + } + const { result } = await updateStoresWorkflow(req.scope).run({ input: { selector: { id: req.params.id }, diff --git a/packages/medusa/src/api/admin/tax-rates/[id]/route.ts b/packages/medusa/src/api/admin/tax-rates/[id]/route.ts index effbcb9a8a..f0b36a83cb 100644 --- a/packages/medusa/src/api/admin/tax-rates/[id]/route.ts +++ b/packages/medusa/src/api/admin/tax-rates/[id]/route.ts @@ -4,6 +4,7 @@ import { } from "@medusajs/core-flows" import { ContainerRegistrationKeys, + MedusaError, remoteQueryObjectFromString, } from "@medusajs/utils" import { @@ -21,6 +22,15 @@ export const POST = async ( req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const existingTaxRate = await refetchTaxRate(req.params.id, req.scope, ["id"]) + + if (!existingTaxRate) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Tax rate with id "${req.params.id}" not found` + ) + } + await updateTaxRatesWorkflow(req.scope).run({ input: { selector: { id: req.params.id },