The 2 bigger remaining tasks are: 1. handling prices for variants 2. Handling options (breaking change) After that all tests should pass on both v1 and v2
58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import {
|
|
AuthenticatedMedusaRequest,
|
|
MedusaResponse,
|
|
} from "../../../types/routing"
|
|
|
|
import { CreateProductCollectionDTO } from "@medusajs/types"
|
|
import { createCollectionsWorkflow } from "@medusajs/core-flows"
|
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
|
|
|
export const GET = async (
|
|
req: AuthenticatedMedusaRequest,
|
|
res: MedusaResponse
|
|
) => {
|
|
const remoteQuery = req.scope.resolve("remoteQuery")
|
|
|
|
const queryObject = remoteQueryObjectFromString({
|
|
entryPoint: "product_collection",
|
|
variables: {
|
|
filters: req.filterableFields,
|
|
order: req.listConfig.order,
|
|
skip: req.listConfig.skip,
|
|
take: req.listConfig.take,
|
|
},
|
|
fields: req.listConfig.select as string[],
|
|
})
|
|
|
|
const { rows: collections, metadata } = await remoteQuery(queryObject)
|
|
|
|
res.json({
|
|
collections,
|
|
count: metadata.count,
|
|
offset: metadata.skip,
|
|
limit: metadata.take,
|
|
})
|
|
}
|
|
|
|
export const POST = async (
|
|
req: AuthenticatedMedusaRequest<CreateProductCollectionDTO>,
|
|
res: MedusaResponse
|
|
) => {
|
|
const input = [
|
|
{
|
|
...req.validatedBody,
|
|
},
|
|
]
|
|
|
|
const { result, errors } = await createCollectionsWorkflow(req.scope).run({
|
|
input: { collections: input },
|
|
throwOnError: false,
|
|
})
|
|
|
|
if (Array.isArray(errors) && errors[0]) {
|
|
throw errors[0].error
|
|
}
|
|
|
|
res.status(200).json({ collection: result[0] })
|
|
}
|