Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa, link-modules): sales channel <> cart link #5459

Merged
merged 150 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
f4f6bb3
feat: sales channel joiner config
fPolic Oct 19, 2023
4c6ebaf
feat: product sales channel link config, SC list method
fPolic Oct 23, 2023
268bf96
feat: migration
fPolic Oct 23, 2023
3898708
fix: refactor list SC
fPolic Oct 23, 2023
56c366c
refactor: SC repo api
fPolic Oct 23, 2023
a475a66
chore: changeset
fPolic Oct 24, 2023
a4c410f
feat: add dedicated FF
fPolic Oct 24, 2023
a160e4a
wip: cart<>sc link and migration
fPolic Oct 24, 2023
eb3b837
chore: changeset
fPolic Oct 24, 2023
cf19194
fix: update migration with the cart table constraints
fPolic Oct 25, 2023
27ec026
feat: populate the pivot table
fPolic Oct 25, 2023
ecf18d6
chore: remove relation from joiner config
fPolic Oct 25, 2023
339d1a4
fix: constraint name
fPolic Oct 25, 2023
6e90c09
fix: filter out link relations when calling internal services
fPolic Oct 26, 2023
0a1ef33
feat: product<> sc join entity
fPolic Oct 30, 2023
130b5b6
fix: update case
fPolic Oct 30, 2023
6fa9879
fix: add FF on in the repository, fix tests
fPolic Oct 30, 2023
886d735
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 30, 2023
6e821d5
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 30, 2023
87c6b37
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 31, 2023
67b3b3a
fix: assign id when FF is on
fPolic Oct 31, 2023
5e70379
fix: target table
fPolic Oct 31, 2023
b3f3cd1
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 31, 2023
5bb1b3d
feat: product service - fetch SC with RQ
fPolic Nov 2, 2023
4c7eb86
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 2, 2023
1e35ae3
feat: admin list products & SC with isolated product domain
fPolic Nov 2, 2023
98b3410
feat: get admin product
fPolic Nov 2, 2023
5c873e2
feat: store endpoints
fPolic Nov 2, 2023
8e3b450
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 2, 2023
1fd544c
fix: remove duplicate import
fPolic Nov 2, 2023
8fa7fb5
fix: remove "name" prop
fPolic Nov 3, 2023
546d81c
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 3, 2023
c228d0f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 3, 2023
abc19bf
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 3, 2023
11eadb8
feat: typeorm entity changes
fPolic Nov 3, 2023
dc4d60e
feat: pivot table, entity, on cart create changes
fPolic Nov 3, 2023
0048f26
feat: update carts' SC
fPolic Nov 3, 2023
d2d3b8c
feat: cart - getValidatedSalesChannel with RQ
fPolic Nov 3, 2023
c35b211
feat: refactor
fPolic Nov 3, 2023
056d932
wip: changes to create cart workflow
fPolic Nov 3, 2023
87480fd
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 6, 2023
12b0ea7
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 6, 2023
694d910
fix: remove join table entity due to migrations failing
fPolic Nov 6, 2023
cbb2954
fix: product seeder if FF is on
fPolic Nov 6, 2023
a22f7fc
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 6, 2023
938db99
feat: attach SC handler and test
fPolic Nov 6, 2023
02e81cd
fix: env
fPolic Nov 6, 2023
e88d4a8
feat: workflow compensation, cart service retrieve with RQ
fPolic Nov 6, 2023
5e21190
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 8, 2023
f646c0e
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 8, 2023
5c312f4
fix: remote joiner implode map
carlos-r-l-rodrigues Nov 9, 2023
18f44ab
chore: update changesets
fPolic Nov 9, 2023
276a7ed
fix: remove methods from SC service/repo
fPolic Nov 10, 2023
8c1b2a6
feat: use remote link in handlers
fPolic Nov 10, 2023
091629e
fix: remove SC service calls
fPolic Nov 10, 2023
7cc52d1
fix: link params
fPolic Nov 10, 2023
a5304e1
fix: migration add constraint to make link upsert pass
fPolic Nov 10, 2023
4852a00
refactor: workflow product handlers to handle remote links
fPolic Nov 10, 2023
e219e10
fix: condition
fPolic Nov 10, 2023
1986dc0
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 13, 2023
62c97fc
fix: use correct method
fPolic Nov 13, 2023
cbdc122
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 13, 2023
364f41d
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 13, 2023
5e94fa1
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 13, 2023
916fd00
fix: build
fPolic Nov 13, 2023
fd4c0dd
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 13, 2023
38ba66f
wip: update FF
fPolic Nov 13, 2023
6740977
fix: update FF in the handlers
fPolic Nov 13, 2023
976dbff
chore: migrate to medusav2 FF
fPolic Nov 14, 2023
5f3f23e
chore: uncomment test
fPolic Nov 14, 2023
b2a808a
fix: product factory
fPolic Nov 14, 2023
759d331
fix: unlinking SC and product
fPolic Nov 14, 2023
3634fba
fix: use module name variable
fPolic Nov 14, 2023
c151e7c
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 14, 2023
e6eb846
refactor: cleanup query definitions
fPolic Nov 14, 2023
02adccf
fix: add constraint
fPolic Nov 14, 2023
8b22964
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 15, 2023
bd6054a
wip: migrate FF
fPolic Nov 15, 2023
07cc814
fix: comments
fPolic Nov 15, 2023
4d65cd1
feat: cart entity callbacks, fix tests
fPolic Nov 15, 2023
8fe9e13
fix: only create SC in test
fPolic Nov 15, 2023
7f521f5
wip: services updates, changes to models
fPolic Nov 15, 2023
87fe8dc
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
9840608
chore: rename prop
fPolic Nov 15, 2023
2457d88
fix: add hook
fPolic Nov 15, 2023
b57b47f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
2f2da2f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
0e0d07c
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 15, 2023
54a7367
fix: address comments
fPolic Nov 16, 2023
0a8029e
Merge branch 'develop' into feat/sales-channel-module--joiner-config
riqwan Nov 16, 2023
db4f52c
fix: temp sc filtering
fPolic Nov 16, 2023
2f225fd
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 16, 2023
abcb96c
fix: use RQ to filter by SC
fPolic Nov 16, 2023
b12d77b
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 16, 2023
c3859bc
fix: relations on retrieve
fPolic Nov 16, 2023
9b3f2e8
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 17, 2023
fd3fd64
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 17, 2023
127605d
feat: migration sync data, remove FF
fPolic Nov 17, 2023
35e2d2c
fix: revert order of queries
fPolic Nov 17, 2023
34caf21
fix: alter migration, relations in service
fPolic Nov 17, 2023
8dd4a23
fix: revert id
fPolic Nov 17, 2023
28b2eef
fix: migrations
fPolic Nov 17, 2023
f49cfe7
fix: make expand work
fPolic Nov 17, 2023
941b2fe
fix: remote link method call
fPolic Nov 20, 2023
ac8abe3
fix: try making tests work without id in the pivot table
fPolic Nov 20, 2023
b4e63f0
test: use remote link
fPolic Nov 20, 2023
7676936
test: relations changes
fPolic Nov 20, 2023
b858d8d
fix: preserve channel id column
fPolic Nov 20, 2023
498b2d7
fix: seeder and factory
fPolic Nov 20, 2023
8ce62d5
fix: remove sales_channels from response
fPolic Nov 20, 2023
1ace6da
feat: support feature flag arrays
fPolic Nov 20, 2023
16cf123
fix: cover everything with correct FF
fPolic Nov 20, 2023
f934e93
fix: remove verbose
fPolic Nov 20, 2023
bbf6fee
fix: unit and plugin tests
fPolic Nov 20, 2023
c1046e3
chore: comments
fPolic Nov 20, 2023
0158ace
fix: reenable workflow handler, add comments, split cart create workf…
fPolic Nov 21, 2023
2da8847
chore: reenable link in the create mehod, update changesets
fPolic Nov 21, 2023
ccd28f9
fix: address feedback
fPolic Nov 22, 2023
7117d8f
fix: revert migration
fPolic Nov 22, 2023
614f4ec
fix: change the migration to follow link module
fPolic Nov 22, 2023
4b87417
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 23, 2023
1dee5ac
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 23, 2023
aa88e13
fix: migration syntax
fPolic Nov 23, 2023
4e7b6a7
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 23, 2023
6048fb2
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 28, 2023
1401879
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 28, 2023
1a53463
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Nov 28, 2023
2a86830
fix: merge conflicts
fPolic Nov 28, 2023
a6a0da0
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 4, 2023
891b12a
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Dec 4, 2023
f5b5408
fix: typo
fPolic Dec 5, 2023
9b9c841
feat: remove store sales channel foreign key
fPolic Dec 6, 2023
7701814
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 11, 2023
9203645
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 12, 2023
d078900
Merge branch 'feat/sales-channel-module--joiner-config' into feat/sal…
fPolic Dec 12, 2023
b2bbb66
Merge branch 'develop' into feat/sales-channel-module-cart--joiner-co…
fPolic Dec 18, 2023
01bcaa3
fix: merge migrations
fPolic Dec 18, 2023
3511158
fix: FF keys
fPolic Dec 18, 2023
d86c2d4
refactor: cart service
fPolic Dec 18, 2023
bda905a
refactor: FF missing key
fPolic Dec 18, 2023
59f6059
Merge branch 'develop' into feat/sales-channel-module-cart--joiner-co…
fPolic Dec 18, 2023
940595d
fix: comments
fPolic Dec 18, 2023
a1a4b92
Merge branch 'develop' into feat/sales-channel-module-cart--joiner-co…
fPolic Dec 18, 2023
34989ea
fix: address PR comments
fPolic Dec 20, 2023
d6e5e07
fix: new changesets
fPolic Dec 20, 2023
44fddc3
Merge branch 'develop' into feat/sales-channel-module-cart--joiner-co…
fPolic Dec 20, 2023
a1cd81b
fix: revert flag router changes
fPolic Dec 21, 2023
fdfaa42
Merge remote-tracking branch 'origin/feat/sales-channel-module-cart--…
fPolic Dec 21, 2023
23719ac
Merge branch 'develop' into feat/sales-channel-module-cart--joiner-co…
fPolic Dec 21, 2023
a302b61
chore: refactor `isFeatureEnabled`
fPolic Dec 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/two-chefs-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@medusajs/orchestration": patch
"@medusajs/link-modules": patch
"@medusajs/core-flows": patch
"@medusajs/medusa": patch
"@medusajs/utils": patch
---

feat: SalesChannel <> Cart joiner config
18 changes: 16 additions & 2 deletions integration-tests/factories/simple-cart-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ShippingMethodFactoryData,
simpleShippingMethodFactory,
} from "./simple-shipping-method-factory"
import { generateEntityId } from "@medusajs/utils"

export type CartFactoryData = {
id?: string
Expand All @@ -32,6 +33,8 @@ export type CartFactoryData = {
sales_channel_id?: string
}

const isMedusaV2Enabled = process.env.MEDUSA_FF_MEDUSA_V2 == "true"

export const simpleCartFactory = async (
dataSource: DataSource,
data: CartFactoryData = {},
Expand Down Expand Up @@ -77,15 +80,26 @@ export const simpleCartFactory = async (
}

const id = data.id || `simple-cart-${Math.random() * 1000}`
const toSave = manager.create(Cart, {
let toSave = {
id,
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
region_id: regionId,
customer_id: customerId,
shipping_address_id: address.id,
sales_channel_id: sales_channel?.id ?? data.sales_channel_id ?? null,
})
}

if (isMedusaV2Enabled) {
await manager.query(
`INSERT INTO "cart_sales_channel" (id, cart_id, sales_channel_id)
VALUES ('${generateEntityId(undefined, "cartsc")}', '${toSave.id}', '${
sales_channel?.id ?? data.sales_channel_id
}');`
)
}

toSave = manager.create(Cart, toSave)

const cart = await manager.save(toSave)

Expand Down
96 changes: 96 additions & 0 deletions integration-tests/plugins/__tests__/cart/store/ff-medusa-v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Region } from "@medusajs/medusa"
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import {
simpleProductFactory,
simpleSalesChannelFactory,
} from "../../../../factories"

jest.setTimeout(30000)

const env = {
MEDUSA_FF_MEDUSA_V2: true,
}

describe("/store/carts", () => {
let dbConnection
let shutdownServer

const doAfterEach = async () => {
const db = useDb()
return await db.teardown()
}

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

describe("POST /store/carts", () => {
let prod1
let prodSale

beforeEach(async () => {
const manager = dbConnection.manager
await manager.insert(Region, {
id: "region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})

await manager.query(
`UPDATE "country"
SET region_id='region'
WHERE iso_2 = 'us'`
)

prod1 = await simpleProductFactory(dbConnection, {
id: "test-product",
variants: [{ id: "test-variant_1" }],
})

prodSale = await simpleProductFactory(dbConnection, {
id: "test-product-sale",
variants: [
{
id: "test-variant-sale",
prices: [{ amount: 1000, currency: "usd" }],
},
],
})

await simpleSalesChannelFactory(dbConnection, {
id: "amazon-sc",
name: "Amazon store",
})
})

afterEach(async () => {
await doAfterEach()
})

it("should create a cart in a sales channel", async () => {
const api = useApi()

const response = await api.post("/store/carts", {
sales_channel_id: "amazon-sc",
})

expect(response.status).toEqual(200)

const getRes = await api.get(`/store/carts/${response.data.cart.id}`)
expect(getRes.status).toEqual(200)
expect(getRes.data.cart.sales_channel.id).toEqual("amazon-sc")
})
})
})
2 changes: 1 addition & 1 deletion integration-tests/plugins/__tests__/cart/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("/store/carts", () => {

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
dbConnection = await initDb({ cwd } as any)
shutdownServer = await startBootstrapApp({ cwd })
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ const {
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const {
setPort,
useApi,
useExpressServer,
} = require("../../../../environment-helpers/use-api")
const { useApi } = require("../../../../environment-helpers/use-api")

const adminSeeder = require("../../../../helpers/admin-seeder")
const {
Expand Down
48 changes: 44 additions & 4 deletions packages/core-flows/src/definition/cart/create-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { exportWorkflow, pipe } from "@medusajs/workflows-sdk"
enum CreateCartActions {
setContext = "setContext",
attachLineItems = "attachLineItems",
attachToSalesChannel = "attachToSalesChannel",
findRegion = "findRegion",
findSalesChannel = "findSalesChannel",
createCart = "createCart",
Expand Down Expand Up @@ -58,10 +59,13 @@ const workflowSteps: TransactionStepsDefinition = {
noCompensation: true,
next: {
action: CreateCartActions.createCart,
next: {
action: CreateCartActions.attachLineItems,
noCompensation: true,
},
next: [
{
action: CreateCartActions.attachLineItems,
noCompensation: true,
},
{ action: CreateCartActions.attachToSalesChannel },
],
},
},
},
Expand Down Expand Up @@ -134,6 +138,10 @@ const handlers = new Map([
invoke: pipe(
{
invoke: [
{
from: CreateCartActions.findSalesChannel,
alias: CartHandlers.createCart.aliases.SalesChannel,
},
{
from: CreateCartActions.findRegion,
alias: CartHandlers.createCart.aliases.Region,
Expand Down Expand Up @@ -186,6 +194,38 @@ const handlers = new Map([
),
},
],
[
CreateCartActions.attachToSalesChannel,
{
invoke: pipe(
{
invoke: [
{
from: CreateCartActions.createCart,
alias: CartHandlers.attachCartToSalesChannel.aliases.Cart,
},
{
from: CreateCartActions.findSalesChannel,
alias: CartHandlers.attachCartToSalesChannel.aliases.SalesChannel,
},
],
},
CartHandlers.attachCartToSalesChannel
),
compensate: pipe(
{
invoke: [
{
from: CreateCartActions.findSalesChannel,
alias:
CartHandlers.detachCartFromSalesChannel.aliases.SalesChannel,
},
],
},
CartHandlers.detachCartFromSalesChannel
),
},
],
])

WorkflowManager.register(Workflows.CreateCart, workflowSteps, handlers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MedusaV2Flag } from "@medusajs/utils"
import { WorkflowArguments } from "@medusajs/workflows-sdk"

type HandlerInputData = {
cart: {
id: string
}
sales_channel: {
sales_channel_id: string
}
}

enum Aliases {
Cart = "cart",
SalesChannel = "sales_channel",
}

export async function attachCartToSalesChannel({
container,
data,
}: WorkflowArguments<HandlerInputData>): Promise<void> {
const featureFlagRouter = container.resolve("featureFlagRouter")
const remoteLink = container.resolve("remoteLink")

if (!featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
return
}

const cart = data[Aliases.Cart]
const salesChannel = data[Aliases.SalesChannel]

await remoteLink.create({
cartService: {
cart_id: cart.id,
},
salesChannelService: {
sales_channel_id: salesChannel.sales_channel_id,
},
})
}

attachCartToSalesChannel.aliases = Aliases
4 changes: 1 addition & 3 deletions packages/core-flows/src/handlers/cart/create-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ export async function createCart({
const cartService = container.resolve("cartService")
const cartServiceTx = cartService.withTransaction(manager)

const cart = await cartServiceTx.create({
return await cartServiceTx.create({
...data[Aliases.SalesChannel],
...data[Aliases.Addresses],
...data[Aliases.Customer],
...data[Aliases.Region],
...data[Aliases.Context],
})

return cart
}

createCart.aliases = Aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MedusaV2Flag } from "@medusajs/utils"
import { WorkflowArguments } from "@medusajs/workflows-sdk"

type HandlerInputData = {
cart: {
id: string
}
sales_channel: {
sales_channel_id: string
}
}

enum Aliases {
Cart = "cart",
SalesChannel = "sales_channel",
}

export async function detachCartFromSalesChannel({
container,
data,
}: WorkflowArguments<HandlerInputData>): Promise<void> {
const featureFlagRouter = container.resolve("featureFlagRouter")
const remoteLink = container.resolve("remoteLink")

if (!featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
return
}

const cart = data[Aliases.Cart]
const salesChannel = data[Aliases.SalesChannel]

await remoteLink.dismiss({
cartService: {
cart_id: cart.id,
},
salesChannelService: {
sales_channel_id: salesChannel.sales_channel_id,
},
})
}

detachCartFromSalesChannel.aliases = Aliases
2 changes: 2 additions & 0 deletions packages/core-flows/src/handlers/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from "./attach-line-items-to-cart"
export * from "./create-cart"
export * from "./remove-cart"
export * from "./retrieve-cart"
export * from "./attach-cart-to-sales-channel"
export * from "./detach-cart-from-sales-channel"
Loading