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

This commit is contained in:
Harminder Virk
2024-09-02 16:23:31 +05:30
committed by GitHub
parent 25367734c7
commit 0ce584029c
11 changed files with 128 additions and 0 deletions

View File

@@ -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, [
"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 = [

View File

@@ -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<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 },

View File

@@ -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 },

View File

@@ -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({

View File

@@ -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>,
@@ -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 },

View File

@@ -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>,
@@ -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 },

View File

@@ -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 },

View File

@@ -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 },

View File

@@ -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 },

View File

@@ -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<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 },

View File

@@ -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<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 },