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
2025-05-06 14:00:18 +02:00
committed by GitHub
parent ea6faa8c82
commit aba4cba373
4 changed files with 33 additions and 25 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Support additional data on admin/collections

View File

@@ -1,16 +1,16 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import {
deleteCollectionsWorkflow,
updateCollectionsWorkflow,
} from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { AdminUpdateCollectionType } from "../validators"
import { refetchCollection } from "../helpers"
import { HttpTypes } from "@medusajs/framework/types"
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils"
import { refetchCollection } from "../helpers"
import { AdminUpdateCollectionType } from "../validators"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -26,7 +26,7 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType>,
req: AuthenticatedMedusaRequest<AdminUpdateCollectionType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse>
) => {
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({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
update: rest,
additional_data,
},
})

View File

@@ -1,16 +1,15 @@
import { createCollectionsWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { createCollectionsWorkflow } from "@medusajs/core-flows"
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/framework/utils"
import { AdminCreateCollectionType } from "./validators"
import { refetchCollection } from "./helpers"
import { HttpTypes } from "@medusajs/framework/types"
import { AdminCreateCollectionType } from "./validators"
export const GET = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCollectionListParams>,
@@ -38,17 +37,13 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCollectionType>,
req: AuthenticatedMedusaRequest<AdminCreateCollectionType & AdditionalData>,
res: MedusaResponse<HttpTypes.AdminCollectionResponse>
) => {
const input = [
{
...req.validatedBody,
},
]
const { additional_data, ...rest } = req.validatedBody
const { result } = await createCollectionsWorkflow(req.scope).run({
input: { collections: input },
input: { collections: [rest], additional_data },
})
const collection = await refetchCollection(

View File

@@ -1,10 +1,11 @@
import { z } from "zod"
import { applyAndAndOrOperators } from "../../utils/common-validators"
import {
createFindParams,
createOperatorMap,
createSelectParams,
WithAdditionalData,
} from "../../utils/validators"
import { applyAndAndOrOperators } from "../../utils/common-validators"
export const AdminGetCollectionParams = createSelectParams()
@@ -27,16 +28,20 @@ export const AdminGetCollectionsParams = createFindParams({
.merge(AdminGetCollectionsParamsFields)
.merge(applyAndAndOrOperators(AdminGetCollectionsParamsFields))
export type AdminCreateCollectionType = z.infer<typeof AdminCreateCollection>
export const AdminCreateCollection = z.object({
export type AdminCreateCollectionType = z.infer<typeof CreateCollection>
export const CreateCollection = z.object({
title: z.string(),
handle: z.string().optional(),
metadata: z.record(z.unknown()).nullish(),
})
export type AdminUpdateCollectionType = z.infer<typeof AdminUpdateCollection>
export const AdminUpdateCollection = z.object({
export const AdminCreateCollection = WithAdditionalData(CreateCollection)
export type AdminUpdateCollectionType = z.infer<typeof UpdateCollection>
export const UpdateCollection = z.object({
title: z.string().optional(),
handle: z.string().optional(),
metadata: z.record(z.unknown()).nullish(),
})
export const AdminUpdateCollection = WithAdditionalData(UpdateCollection)