Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle case where product to be updated does not exist #8897

Merged
merged 7 commits into from
Sep 2, 2024
10 changes: 10 additions & 0 deletions packages/medusa/src/api/admin/campaigns/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCampaignType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCampaignResponse>
) => {
const existingCampaign = await refetchCampaign(req.params.id, req.scope, [
thetutlage marked this conversation as resolved.
Show resolved Hide resolved
"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 = [
Expand Down
11 changes: 11 additions & 0 deletions packages/medusa/src/api/admin/collections/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +29,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse>
) => {
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 },
Expand Down
12 changes: 12 additions & 0 deletions packages/medusa/src/api/admin/customer-groups/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerGroupType>,
res: MedusaResponse<HttpTypes.AdminCustomerGroupResponse>
) => {
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 },
Expand Down
10 changes: 10 additions & 0 deletions packages/medusa/src/api/admin/customers/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCustomerResponse>
) => {
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({
Expand Down
15 changes: 15 additions & 0 deletions packages/medusa/src/api/admin/product-tags/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AdminGetProductTagParamsType>,
Expand All @@ -32,6 +33,20 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductTagType>,
res: MedusaResponse<HttpTypes.AdminProductTagResponse>
) => {
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 },
Expand Down
14 changes: 14 additions & 0 deletions packages/medusa/src/api/admin/product-types/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AdminUpdateProductTypeType,
} from "../validators"
import { HttpTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"

export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetProductTypeParamsType>,
Expand All @@ -31,6 +32,19 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductTypeType>,
res: MedusaResponse<HttpTypes.AdminProductTypeResponse>
) => {
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 },
Expand Down
16 changes: 16 additions & 0 deletions packages/medusa/src/api/admin/products/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
8 changes: 8 additions & 0 deletions packages/medusa/src/api/admin/regions/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateRegionType>,
res: MedusaResponse<HttpTypes.AdminRegionResponse>
) => {
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 },
Expand Down
13 changes: 13 additions & 0 deletions packages/medusa/src/api/admin/sales-channels/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateSalesChannelType>,
res: MedusaResponse<HttpTypes.AdminSalesChannelResponse>
) => {
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 },
Expand Down
9 changes: 9 additions & 0 deletions packages/medusa/src/api/admin/stores/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { updateStoresWorkflow } from "@medusajs/core-flows"
import {
remoteQueryObjectFromString,
ContainerRegistrationKeys,
MedusaError,
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
Expand Down Expand Up @@ -32,6 +33,14 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateStoreType>,
res: MedusaResponse<HttpTypes.AdminStoreResponse>
) => {
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 },
Expand Down
10 changes: 10 additions & 0 deletions packages/medusa/src/api/admin/tax-rates/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
MedusaError,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {
Expand All @@ -21,6 +22,15 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateTaxRateType>,
res: MedusaResponse<HttpTypes.AdminTaxRateResponse>
) => {
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 },
Expand Down
Loading