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

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

No files matched your search

@@ -36,6 +36,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCampaignType & AdditionalData>, req: AuthenticatedMedusaRequest<AdminUpdateCampaignType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCampaignResponse> 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 { additional_data, ...rest } = req.validatedBody
const updateCampaigns = updateCampaignsWorkflow(req.scope) const updateCampaigns = updateCampaignsWorkflow(req.scope)
const campaignsData = [ const campaignsData = [
@@ -10,6 +10,7 @@ import {
import { AdminUpdateCollectionType } from "../validators" import { AdminUpdateCollectionType } from "../validators"
import { refetchCollection } from "../helpers" import { refetchCollection } from "../helpers"
import { HttpTypes } from "@medusajs/types" import { HttpTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -28,6 +29,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>, req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse> 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({ await updateCollectionsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -36,6 +36,18 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerGroupType>, req: AuthenticatedMedusaRequest<AdminUpdateCustomerGroupType>,
res: MedusaResponse<HttpTypes.AdminCustomerGroupResponse> 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({ await updateCustomerGroupsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -35,6 +35,16 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType & AdditionalData>, req: AuthenticatedMedusaRequest<AdminUpdateCustomerType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCustomerResponse> 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 const { additional_data, ...rest } = req.validatedBody
await updateCustomersWorkflow(req.scope).run({ await updateCustomersWorkflow(req.scope).run({
@@ -13,6 +13,7 @@ import {
} from "../validators" } from "../validators"
import { refetchEntity } from "../../../utils/refetch-entity" import { refetchEntity } from "../../../utils/refetch-entity"
import { HttpTypes } from "@medusajs/types" import { HttpTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetProductTagParamsType>, req: AuthenticatedMedusaRequest<AdminGetProductTagParamsType>,
@@ -32,6 +33,20 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductTagType>, req: AuthenticatedMedusaRequest<AdminUpdateProductTagType>,
res: MedusaResponse<HttpTypes.AdminProductTagResponse> 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({ const { result } = await updateProductTagsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -13,6 +13,7 @@ import {
AdminUpdateProductTypeType, AdminUpdateProductTypeType,
} from "../validators" } from "../validators"
import { HttpTypes } from "@medusajs/types" import { HttpTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetProductTypeParamsType>, req: AuthenticatedMedusaRequest<AdminGetProductTypeParamsType>,
@@ -31,6 +32,19 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateProductTypeType>, req: AuthenticatedMedusaRequest<AdminUpdateProductTypeType>,
res: MedusaResponse<HttpTypes.AdminProductTypeResponse> 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({ const { result } = await updateProductTypesWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -38,6 +38,22 @@ export const POST = async (
) => { ) => {
const { additional_data, ...update } = req.validatedBody 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({ const { result } = await updateProductsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -35,6 +35,14 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateRegionType>, req: AuthenticatedMedusaRequest<AdminUpdateRegionType>,
res: MedusaResponse<HttpTypes.AdminRegionResponse> 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({ const { result } = await updateRegionsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -38,6 +38,19 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateSalesChannelType>, req: AuthenticatedMedusaRequest<AdminUpdateSalesChannelType>,
res: MedusaResponse<HttpTypes.AdminSalesChannelResponse> 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({ await updateSalesChannelsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -2,6 +2,7 @@ import { updateStoresWorkflow } from "@medusajs/core-flows"
import { import {
remoteQueryObjectFromString, remoteQueryObjectFromString,
ContainerRegistrationKeys, ContainerRegistrationKeys,
MedusaError,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
@@ -32,6 +33,14 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateStoreType>, req: AuthenticatedMedusaRequest<AdminUpdateStoreType>,
res: MedusaResponse<HttpTypes.AdminStoreResponse> 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({ const { result } = await updateStoresWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
@@ -4,6 +4,7 @@ import {
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
MedusaError,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
@@ -21,6 +22,15 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateTaxRateType>, req: AuthenticatedMedusaRequest<AdminUpdateTaxRateType>,
res: MedusaResponse<HttpTypes.AdminTaxRateResponse> 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({ await updateTaxRatesWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },