fix(medusa): Support additional data on admin/collections (#12345)

* fix(medusa): Support additional data on admin/collections

* Create five-experts-wink.md

* add to validators
This commit is contained in:
Oli Juhl authored and GitHub committed 2025-05-06 14:00:18 +02:00
1 parent ea6faa8c82
commit aba4cba373
4 files changed
+33 -25

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Support additional data on admin/collections
@@ -1,16 +1,16 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { import {
deleteCollectionsWorkflow, deleteCollectionsWorkflow,
updateCollectionsWorkflow, updateCollectionsWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { AdminUpdateCollectionType } from "../validators" import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import { refetchCollection } from "../helpers"
import { HttpTypes } from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils" import { MedusaError } from "@medusajs/framework/utils"
import { refetchCollection } from "../helpers"
import { AdminUpdateCollectionType } from "../validators"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -26,7 +26,7 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>, req: AuthenticatedMedusaRequest<AdminUpdateCollectionType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse> res: MedusaResponse<HttpTypes.AdminCollectionResponse>
) => { ) => {
const existingCollection = await refetchCollection(req.params.id, req.scope, [ const existingCollection = await refetchCollection(req.params.id, req.scope, [
@@ -39,10 +39,13 @@ export const POST = async (
) )
} }
const { additional_data, ...rest } = req.validatedBody
await updateCollectionsWorkflow(req.scope).run({ await updateCollectionsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody, update: rest,
additional_data,
}, },
}) })
@@ -1,16 +1,15 @@
import { createCollectionsWorkflow } from "@medusajs/core-flows"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "@medusajs/framework/http" } from "@medusajs/framework/http"
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import { createCollectionsWorkflow } from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import { AdminCreateCollectionType } from "./validators"
import { refetchCollection } from "./helpers" import { refetchCollection } from "./helpers"
import { HttpTypes } from "@medusajs/framework/types" import { AdminCreateCollectionType } from "./validators"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCollectionListParams>, req: AuthenticatedMedusaRequest<HttpTypes.AdminCollectionListParams>,
@@ -38,17 +37,13 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCollectionType>, req: AuthenticatedMedusaRequest<AdminCreateCollectionType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse> res: MedusaResponse<HttpTypes.AdminCollectionResponse>
) => { ) => {
const input = [ const { additional_data, ...rest } = req.validatedBody
{
...req.validatedBody,
},
]
const { result } = await createCollectionsWorkflow(req.scope).run({ const { result } = await createCollectionsWorkflow(req.scope).run({
input: { collections: input }, input: { collections: [rest], additional_data },
}) })
const collection = await refetchCollection( const collection = await refetchCollection(
@@ -1,10 +1,11 @@
import { z } from "zod" import { z } from "zod"
import { applyAndAndOrOperators } from "../../utils/common-validators"
import { import {
createFindParams, createFindParams,
createOperatorMap, createOperatorMap,
createSelectParams, createSelectParams,
WithAdditionalData,
} from "../../utils/validators" } from "../../utils/validators"
import { applyAndAndOrOperators } from "../../utils/common-validators"
export const AdminGetCollectionParams = createSelectParams() export const AdminGetCollectionParams = createSelectParams()
@@ -27,16 +28,20 @@ export const AdminGetCollectionsParams = createFindParams({
.merge(AdminGetCollectionsParamsFields) .merge(AdminGetCollectionsParamsFields)
.merge(applyAndAndOrOperators(AdminGetCollectionsParamsFields)) .merge(applyAndAndOrOperators(AdminGetCollectionsParamsFields))
export type AdminCreateCollectionType = z.infer<typeof AdminCreateCollection> export type AdminCreateCollectionType = z.infer<typeof CreateCollection>
export const AdminCreateCollection = z.object({ export const CreateCollection = z.object({
title: z.string(), title: z.string(),
handle: z.string().optional(), handle: z.string().optional(),
metadata: z.record(z.unknown()).nullish(), metadata: z.record(z.unknown()).nullish(),
}) })
export type AdminUpdateCollectionType = z.infer<typeof AdminUpdateCollection> export const AdminCreateCollection = WithAdditionalData(CreateCollection)
export const AdminUpdateCollection = z.object({
export type AdminUpdateCollectionType = z.infer<typeof UpdateCollection>
export const UpdateCollection = z.object({
title: z.string().optional(), title: z.string().optional(),
handle: z.string().optional(), handle: z.string().optional(),
metadata: z.record(z.unknown()).nullish(), metadata: z.record(z.unknown()).nullish(),
}) })
export const AdminUpdateCollection = WithAdditionalData(UpdateCollection)