feat: Update service zone (#6990)
This commit is contained in:
1 parent
b6879f017a
commit
a24d6e6c97
16 files changed
+576
-194
No files matched your search
+78
-4
@@ -1,14 +1,78 @@
|
||||
import {
|
||||
deleteServiceZonesWorkflow,
|
||||
updateServiceZonesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { deleteServiceZonesWorkflow } from "../../../../../../../../core-flows/dist"
|
||||
import {
|
||||
AdminFulfillmentSetResponse,
|
||||
AdminServiceZoneDeleteResponse,
|
||||
IFulfillmentModuleService,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import { AdminUpdateFulfillmentSetServiceZonesType } from "../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<AdminUpdateFulfillmentSetServiceZonesType>,
|
||||
res: MedusaResponse<AdminFulfillmentSetResponse>
|
||||
) => {
|
||||
const fulfillmentModuleService = req.scope.resolve<IFulfillmentModuleService>(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
// ensure fulfillment set exists and that the service zone is part of it
|
||||
const fulfillmentSet = await fulfillmentModuleService.retrieve(
|
||||
req.params.id,
|
||||
{ relations: ["service_zones"] }
|
||||
)
|
||||
|
||||
if (!fulfillmentSet.service_zones.find((s) => s.id === req.params.zone_id)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Service zone with id: ${req.params.zone_id} not found on fulfillment set`
|
||||
)
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const workflowInput = {
|
||||
selector: { id: req.params.zone_id },
|
||||
update: req.validatedBody,
|
||||
}
|
||||
|
||||
const { errors } = 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",
|
||||
variables: {
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
)
|
||||
|
||||
res.status(200).json({ fulfillment_set })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
res: MedusaResponse<AdminServiceZoneDeleteResponse>
|
||||
) => {
|
||||
const { id, zone_id } = req.params
|
||||
|
||||
@@ -16,7 +80,17 @@ export const DELETE = async (
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const fulfillmentSet = await fulfillmentModuleService.retrieve(id)
|
||||
// ensure fulfillment set exists and that the service zone is part of it
|
||||
const fulfillmentSet = await fulfillmentModuleService.retrieve(id, {
|
||||
relations: ["service_zones"],
|
||||
})
|
||||
|
||||
if (!fulfillmentSet.service_zones.find((s) => s.id === zone_id)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Service zone with id: ${zone_id} not found on fulfillment set`
|
||||
)
|
||||
}
|
||||
|
||||
const { errors } = await deleteServiceZonesWorkflow(req.scope).run({
|
||||
input: { ids: [zone_id] },
|
||||
|
||||
@@ -6,6 +6,7 @@ import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminCreateFulfillmentSetServiceZonesSchema,
|
||||
AdminFulfillmentSetParams,
|
||||
AdminUpdateFulfillmentSetServiceZonesSchema,
|
||||
} from "./validators"
|
||||
|
||||
export const adminFulfillmentSetsRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -30,4 +31,15 @@ export const adminFulfillmentSetsRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/admin/fulfillment-sets/:id/service-zones/:zone_id",
|
||||
middlewares: [],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/fulfillment-sets/:id/service-zones/:zone_id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdateFulfillmentSetServiceZonesSchema),
|
||||
validateAndTransformQuery(
|
||||
AdminFulfillmentSetParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -1,62 +1,44 @@
|
||||
import { z } from "zod"
|
||||
import { createFindParams, createOperatorMap } from "../../utils/validators"
|
||||
|
||||
const geoZoneBaseSchema = z.object({
|
||||
country_code: z.string(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
|
||||
const geoZoneCountrySchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("country"),
|
||||
})
|
||||
)
|
||||
|
||||
const geoZoneProvinceSchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("province"),
|
||||
province_code: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
const geoZoneCitySchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("city"),
|
||||
province_code: z.string(),
|
||||
city: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
const geoZoneZipSchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("zip"),
|
||||
province_code: z.string(),
|
||||
city: z.string(),
|
||||
postal_expression: z.record(z.unknown()),
|
||||
})
|
||||
)
|
||||
import {
|
||||
geoZoneCitySchema,
|
||||
geoZoneCountrySchema,
|
||||
geoZoneProvinceSchema,
|
||||
geoZoneZipSchema,
|
||||
} from "./validators/geo-zone"
|
||||
|
||||
export const AdminCreateFulfillmentSetServiceZonesSchema = z
|
||||
.object({
|
||||
name: z.string(),
|
||||
geo_zones: z.array(
|
||||
z.union([
|
||||
geoZoneCountrySchema,
|
||||
geoZoneProvinceSchema,
|
||||
geoZoneCitySchema,
|
||||
geoZoneZipSchema,
|
||||
])
|
||||
),
|
||||
geo_zones: z
|
||||
.array(
|
||||
z.union([
|
||||
geoZoneCountrySchema,
|
||||
geoZoneProvinceSchema,
|
||||
geoZoneCitySchema,
|
||||
geoZoneZipSchema,
|
||||
])
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminCreateFulfillmentSetServiceZonesType = z.infer<
|
||||
typeof AdminCreateFulfillmentSetServiceZonesSchema
|
||||
>
|
||||
export const AdminUpdateFulfillmentSetServiceZonesSchema = z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
geo_zones: z
|
||||
.array(
|
||||
z.union([
|
||||
geoZoneCountrySchema.merge(z.object({ id: z.string().optional() })),
|
||||
geoZoneProvinceSchema.merge(z.object({ id: z.string().optional() })),
|
||||
geoZoneCitySchema.merge(z.object({ id: z.string().optional() })),
|
||||
geoZoneZipSchema.merge(z.object({ id: z.string().optional() })),
|
||||
])
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminFulfillmentSetParamsType = z.infer<
|
||||
typeof AdminFulfillmentSetParams
|
||||
>
|
||||
export const AdminFulfillmentSetParams = createFindParams({
|
||||
limit: 20,
|
||||
offset: 0,
|
||||
@@ -71,3 +53,13 @@ export const AdminFulfillmentSetParams = createFindParams({
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
})
|
||||
)
|
||||
|
||||
export type AdminCreateFulfillmentSetServiceZonesType = z.infer<
|
||||
typeof AdminCreateFulfillmentSetServiceZonesSchema
|
||||
>
|
||||
export type AdminUpdateFulfillmentSetServiceZonesType = z.infer<
|
||||
typeof AdminUpdateFulfillmentSetServiceZonesSchema
|
||||
>
|
||||
export type AdminFulfillmentSetParamsType = z.infer<
|
||||
typeof AdminFulfillmentSetParams
|
||||
>
|
||||
@@ -0,0 +1,36 @@
|
||||
import { z } from "zod"
|
||||
|
||||
const geoZoneBaseSchema = z.object({
|
||||
country_code: z.string(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
|
||||
export const geoZoneCountrySchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("country"),
|
||||
})
|
||||
)
|
||||
|
||||
export const geoZoneProvinceSchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("province"),
|
||||
province_code: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
export const geoZoneCitySchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("city"),
|
||||
province_code: z.string(),
|
||||
city: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
export const geoZoneZipSchema = geoZoneBaseSchema.merge(
|
||||
z.object({
|
||||
type: z.literal("zip"),
|
||||
province_code: z.string(),
|
||||
city: z.string(),
|
||||
postal_expression: z.record(z.unknown()),
|
||||
})
|
||||
)
|
||||
Reference in new issue
Block a user