feat: Add payment collection creation for cart (#6527)

This commit is contained in:
Oli Juhl
2024-03-04 09:02:01 +01:00
committed by GitHub
parent 71ed21de4a
commit 883cb0dca7
11 changed files with 371 additions and 8 deletions
@@ -0,0 +1,50 @@
import { createPaymentCollectionForCartWorkflow } from "@medusajs/core-flows"
import { UpdateCartDataDTO } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { defaultStoreCartFields } from "../../query-config"
export const POST = async (
req: MedusaRequest<UpdateCartDataDTO>,
res: MedusaResponse
) => {
const workflow = createPaymentCollectionForCartWorkflow(req.scope)
const remoteQuery = req.scope.resolve("remoteQuery")
let [cart] = await remoteQuery(
{
cart: {
fields: ["id", "currency_code", "region_id", "total"],
},
},
{
cart: { id: req.params.id },
}
)
const { errors } = await workflow.run({
input: {
cart_id: req.params.id,
region_id: cart.region_id,
currency_code: cart.currency_code,
amount: cart.total ?? 0, // TODO: This should be calculated from the cart when totals decoration is introduced
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const query = remoteQueryObjectFromString({
entryPoint: "cart",
variables: { id: req.params.id },
fields: defaultStoreCartFields,
})
;[cart] = await remoteQuery(query)
res.status(200).json({ cart })
}
@@ -42,6 +42,11 @@ export const defaultStoreCartFields = [
"region.name",
"region.currency_code",
"sales_channel_id",
// TODO: To be updated when payment sessions are introduces in the Rest API
"payment_collection.id",
"payment_collection.amount",
"payment_collection.payment_sessions",
]
export const defaultStoreCartRelations = [