feat(medusa): Add product isolation FF to update cart (#5168)
**What** Update cart must use the remote query when being in product isolation mode. This pr is a first iteration that aims to quickly make compatible the end point in isolation. In a future iteration, this end point will be migrated to a workflow Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
27d74cb399
commit
dfa5d041c9
@@ -15,6 +15,7 @@ import { AddressPayload } from "../../../../types/common"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}
|
||||
@@ -75,14 +76,22 @@ export default async (req, res) => {
|
||||
const validated = req.validatedBody as StorePostCartsCartReq
|
||||
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
const featureFlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
if (req.user?.customer_id) {
|
||||
validated.customer_id = req.user.customer_id
|
||||
}
|
||||
|
||||
let cart
|
||||
if (featureFlagRouter.isFeatureEnabled(IsolateProductDomainFeatureFlag.key)) {
|
||||
cart = await retrieveCartWithIsolatedProductModule(req, id)
|
||||
}
|
||||
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await cartService.withTransaction(transactionManager).update(id, validated)
|
||||
await cartService
|
||||
.withTransaction(transactionManager)
|
||||
.update(cart ?? id, validated)
|
||||
|
||||
const updated = await cartService
|
||||
.withTransaction(transactionManager)
|
||||
@@ -101,9 +110,60 @@ export default async (req, res) => {
|
||||
select: defaultStoreCartFields,
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
async function retrieveCartWithIsolatedProductModule(req, id: string) {
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const relations = [
|
||||
"items",
|
||||
"shipping_methods",
|
||||
"shipping_methods.shipping_option",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"gift_cards",
|
||||
"customer",
|
||||
"region",
|
||||
"payment_sessions",
|
||||
"region.countries",
|
||||
"discounts",
|
||||
"discounts.rule",
|
||||
]
|
||||
|
||||
const cart = await cartService.retrieve(id, {
|
||||
relations,
|
||||
})
|
||||
|
||||
const products = await remoteQuery({
|
||||
products: {
|
||||
__args: {
|
||||
id: cart.items.map((i) => i.product_id),
|
||||
},
|
||||
fields: ["id"],
|
||||
variants: {
|
||||
fields: ["id"],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const variantsMap = new Map(
|
||||
products.flatMap((p) => p.variants).map((v) => [v.id, v])
|
||||
)
|
||||
|
||||
cart.items.forEach((item) => {
|
||||
if (!item.variant_id) {
|
||||
return
|
||||
}
|
||||
|
||||
item.variant = variantsMap.get(item.variant_id)
|
||||
})
|
||||
|
||||
return cart
|
||||
}
|
||||
|
||||
class GiftCard {
|
||||
@IsString()
|
||||
code: string
|
||||
|
||||
Reference in New Issue
Block a user