feat(core-flows,link-modules,modules-sdk): add cart <> promotion link as source of truth (#6561)

what:

- adds promotion cart link
- update steps to create and remove links
This commit is contained in:
Riqwan Thamir
2024-03-04 09:50:49 +00:00
committed by GitHub
parent 8dad2b51a2
commit d550be3685
14 changed files with 272 additions and 51 deletions
@@ -1,4 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
LinkModuleUtils,
ModuleRegistrationName,
Modules,
RemoteLink,
} from "@medusajs/modules-sdk"
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types"
import { PromotionType } from "@medusajs/utils"
import path from "path"
@@ -18,6 +23,7 @@ describe("Store Carts API: Add promotions to cart", () => {
let shutdownServer
let cartModuleService: ICartModuleService
let promotionModuleService: IPromotionModuleService
let remoteLinkService: RemoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
@@ -28,6 +34,7 @@ describe("Store Carts API: Add promotions to cart", () => {
promotionModuleService = appContainer.resolve(
ModuleRegistrationName.PROMOTION
)
remoteLinkService = appContainer.resolve(LinkModuleUtils.REMOTE_LINK)
})
afterAll(async () => {
@@ -119,6 +126,11 @@ describe("Store Carts API: Add promotions to cart", () => {
},
])
await remoteLinkService.create({
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
})
const api = useApi() as any
const created = await api.post(`/store/carts/${cart.id}/promotions`, {
@@ -247,6 +259,11 @@ describe("Store Carts API: Add promotions to cart", () => {
]
)
await remoteLinkService.create({
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
})
const [adjustment] = await cartModuleService.addShippingMethodAdjustments(
cart.id,
[
@@ -1,4 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
LinkModuleUtils,
ModuleRegistrationName,
Modules,
RemoteLink,
} from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
@@ -31,7 +36,7 @@ describe("Store Carts API", () => {
let customerModule: ICustomerModuleService
let productModule: IProductModuleService
let pricingModule: IPricingModuleService
let remoteLink
let remoteLink: RemoteLink
let promotionModule: IPromotionModuleService
let defaultRegion
@@ -47,7 +52,7 @@ describe("Store Carts API", () => {
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
productModule = appContainer.resolve(ModuleRegistrationName.PRODUCT)
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
remoteLink = appContainer.resolve("remoteLink")
remoteLink = appContainer.resolve(LinkModuleUtils.REMOTE_LINK)
promotionModule = appContainer.resolve(ModuleRegistrationName.PROMOTION)
})
@@ -351,6 +356,11 @@ describe("Store Carts API", () => {
},
])
await remoteLink.create({
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
})
const api = useApi() as any
// Should remove earlier adjustments from other promocodes
@@ -629,6 +639,11 @@ describe("Store Carts API", () => {
},
])
await remoteLink.create({
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
})
const api = useApi() as any
const response = await api.post(`/store/carts/${cart.id}/line-items`, {
variant_id: product.variants[0].id,
@@ -1,4 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
LinkModuleUtils,
ModuleRegistrationName,
Modules,
RemoteLink,
} from "@medusajs/modules-sdk"
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types"
import { PromotionType } from "@medusajs/utils"
import path from "path"
@@ -18,6 +23,7 @@ describe("Store Carts API: Remove promotions from cart", () => {
let shutdownServer
let cartModuleService: ICartModuleService
let promotionModuleService: IPromotionModuleService
let remoteLinkService: RemoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
@@ -25,6 +31,7 @@ describe("Store Carts API: Remove promotions from cart", () => {
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
remoteLinkService = appContainer.resolve(LinkModuleUtils.REMOTE_LINK)
promotionModuleService = appContainer.resolve(
ModuleRegistrationName.PROMOTION
)
@@ -129,6 +136,17 @@ describe("Store Carts API: Remove promotions from cart", () => {
},
])
await remoteLinkService.create([
{
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
},
{
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotionToRemove.id },
},
])
const api = useApi() as any
const response = await api.delete(`/store/carts/${cart.id}/promotions`, {
@@ -263,6 +281,17 @@ describe("Store Carts API: Remove promotions from cart", () => {
},
])
await remoteLinkService.create([
{
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
},
{
[Modules.CART]: { cart_id: cart.id },
[Modules.PROMOTION]: { promotion_id: appliedPromotionToRemove.id },
},
])
const api = useApi() as any
const response = await api.delete(`/store/carts/${cart.id}/promotions`, {