From 1ba35b02dd52eeca9f3e1bee073c5e7a17edbc33 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Fri, 16 Feb 2024 20:48:56 +0100 Subject: [PATCH] feat: Cart SalesChannel link + clean-up (#6418) --- .changeset/gentle-hornets-hope.md | 7 + .../{create-cart.spec.ts => carts.spec.ts} | 79 ++++-- .../plugins/__tests__/cart/store/get-cart.ts | 73 ------ .../__tests__/cart/store/update-cart.ts | 63 ----- .../__tests__/link-modules/cart-links.spec.ts | 131 ++++++++++ integration-tests/plugins/medusa-config.js | 3 - .../src/definition/cart/create-cart.ts | 238 ------------------ .../core-flows/src/definition/cart/index.ts | 1 - .../definition/cart/workflows/create-carts.ts | 6 +- .../cart/attach-cart-to-sales-channel.ts | 43 ---- .../cart/attach-line-items-to-cart.ts | 57 ----- .../src/handlers/cart/create-cart.ts | 59 ----- .../cart/detach-cart-from-sales-channel.ts | 43 ---- .../core-flows/src/handlers/cart/index.ts | 6 - .../src/handlers/cart/remove-cart.ts | 28 --- .../src/handlers/cart/retrieve-cart.ts | 40 --- packages/core-flows/src/handlers/index.ts | 1 - .../src/definitions/cart-sales-channel.ts | 48 +--- packages/link-modules/src/links.ts | 6 - .../src/api-v2/store/carts/[id]/route.ts | 12 +- .../src/api-v2/store/carts/query-config.ts | 87 +++---- .../medusa/src/api-v2/store/carts/route.ts | 12 +- .../src/api/routes/store/carts/create-cart.ts | 3 +- .../1698160215000-cart-sales-channels-link.ts | 47 ---- .../medusa/src/models/cart-sales-channel.ts | 29 --- packages/medusa/src/services/cart.ts | 57 +---- packages/region/src/models/region.ts | 3 +- .../sales-channel/src/models/sales-channel.ts | 5 +- .../src/services/sales-channel-module.ts | 8 +- 29 files changed, 276 insertions(+), 919 deletions(-) create mode 100644 .changeset/gentle-hornets-hope.md rename integration-tests/plugins/__tests__/cart/store/{create-cart.spec.ts => carts.spec.ts} (67%) delete mode 100644 integration-tests/plugins/__tests__/cart/store/get-cart.ts delete mode 100644 integration-tests/plugins/__tests__/cart/store/update-cart.ts create mode 100644 integration-tests/plugins/__tests__/link-modules/cart-links.spec.ts delete mode 100644 packages/core-flows/src/definition/cart/create-cart.ts delete mode 100644 packages/core-flows/src/handlers/cart/attach-cart-to-sales-channel.ts delete mode 100644 packages/core-flows/src/handlers/cart/attach-line-items-to-cart.ts delete mode 100644 packages/core-flows/src/handlers/cart/create-cart.ts delete mode 100644 packages/core-flows/src/handlers/cart/detach-cart-from-sales-channel.ts delete mode 100644 packages/core-flows/src/handlers/cart/index.ts delete mode 100644 packages/core-flows/src/handlers/cart/remove-cart.ts delete mode 100644 packages/core-flows/src/handlers/cart/retrieve-cart.ts delete mode 100644 packages/medusa/src/migrations/1698160215000-cart-sales-channels-link.ts delete mode 100644 packages/medusa/src/models/cart-sales-channel.ts diff --git a/.changeset/gentle-hornets-hope.md b/.changeset/gentle-hornets-hope.md new file mode 100644 index 0000000000..1610bb58d9 --- /dev/null +++ b/.changeset/gentle-hornets-hope.md @@ -0,0 +1,7 @@ +--- +"@medusajs/medusa": patch +"@medusajs/core-flows": patch +"@medusajs/link-modules": patch +--- + +feat: Cart SalesChannel link diff --git a/integration-tests/plugins/__tests__/cart/store/create-cart.spec.ts b/integration-tests/plugins/__tests__/cart/store/carts.spec.ts similarity index 67% rename from integration-tests/plugins/__tests__/cart/store/create-cart.spec.ts rename to integration-tests/plugins/__tests__/cart/store/carts.spec.ts index 4a1ef297a2..0b7995127e 100644 --- a/integration-tests/plugins/__tests__/cart/store/create-cart.spec.ts +++ b/integration-tests/plugins/__tests__/cart/store/carts.spec.ts @@ -1,5 +1,9 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { ICartModuleService, IRegionModuleService } from "@medusajs/types" +import { + ICartModuleService, + IRegionModuleService, + ISalesChannelModuleService, +} from "@medusajs/types" import path from "path" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { useApi } from "../../../../environment-helpers/use-api" @@ -17,6 +21,7 @@ describe("POST /store/carts", () => { let shutdownServer let cartModuleService: ICartModuleService let regionModuleService: IRegionModuleService + let scModuleService: ISalesChannelModuleService beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) @@ -25,6 +30,7 @@ describe("POST /store/carts", () => { appContainer = getContainer() cartModuleService = appContainer.resolve(ModuleRegistrationName.CART) regionModuleService = appContainer.resolve(ModuleRegistrationName.REGION) + scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL) }) afterAll(async () => { @@ -45,34 +51,54 @@ describe("POST /store/carts", () => { await db.teardown() }) - it("should create a cart", async () => { + it("should create and update a cart", async () => { const region = await regionModuleService.create({ name: "US", currency_code: "usd", }) + const salesChannel = await scModuleService.create({ + name: "Webshop", + }) + const api = useApi() as any - const response = await api.post(`/store/carts`, { + + const created = await api.post(`/store/carts`, { email: "tony@stark.com", currency_code: "usd", region_id: region.id, + sales_channel_id: salesChannel.id, }) - expect(response.status).toEqual(200) - expect(response.data.cart).toEqual( + expect(created.status).toEqual(200) + expect(created.data.cart).toEqual( expect.objectContaining({ - id: response.data.cart.id, + id: created.data.cart.id, currency_code: "usd", email: "tony@stark.com", region: expect.objectContaining({ id: region.id, currency_code: "usd", }), + sales_channel_id: salesChannel.id, + }) + ) + + const updated = await api.post(`/store/carts/${created.data.cart.id}`, { + email: "tony@stark-industries.com", + }) + + expect(updated.status).toEqual(200) + expect(updated.data.cart).toEqual( + expect.objectContaining({ + id: updated.data.cart.id, + currency_code: "usd", + email: "tony@stark-industries.com", }) ) }) - it("should use any region", async () => { + it("should create cart with any region", async () => { await regionModuleService.create({ name: "US", currency_code: "usd", @@ -97,7 +123,7 @@ describe("POST /store/carts", () => { ) }) - it("should use region currency code", async () => { + it("should create cart with region currency code", async () => { await regionModuleService.create({ name: "US", currency_code: "usd", @@ -132,34 +158,49 @@ describe("POST /store/carts", () => { ).rejects.toThrow() }) - it("should create a cart", async () => { + it("should get cart", async () => { const region = await regionModuleService.create({ name: "US", currency_code: "usd", }) - await regionModuleService.create({ - name: "Europe", - currency_code: "eur", + const salesChannel = await scModuleService.create({ + name: "Webshop", + }) + + const cart = await cartModuleService.create({ + currency_code: "usd", + items: [ + { + unit_price: 1000, + quantity: 1, + title: "Test item", + }, + ], + region_id: region.id, + sales_channel_id: salesChannel.id, }) const api = useApi() as any - const response = await api.post(`/store/carts`, { - email: "tony@stark.com", - currency_code: "usd", - region_id: region.id, - }) + const response = await api.get(`/store/carts/${cart.id}`) expect(response.status).toEqual(200) expect(response.data.cart).toEqual( expect.objectContaining({ - id: response.data.cart.id, + id: cart.id, currency_code: "usd", - email: "tony@stark.com", + items: expect.arrayContaining([ + expect.objectContaining({ + unit_price: 1000, + quantity: 1, + title: "Test item", + }), + ]), region: expect.objectContaining({ id: region.id, currency_code: "usd", }), + sales_channel_id: salesChannel.id, }) ) }) diff --git a/integration-tests/plugins/__tests__/cart/store/get-cart.ts b/integration-tests/plugins/__tests__/cart/store/get-cart.ts deleted file mode 100644 index 8d2b64409c..0000000000 --- a/integration-tests/plugins/__tests__/cart/store/get-cart.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { ICartModuleService } from "@medusajs/types" -import path from "path" -import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" -import { useApi } from "../../../../environment-helpers/use-api" -import { getContainer } from "../../../../environment-helpers/use-container" -import { initDb, useDb } from "../../../../environment-helpers/use-db" -import adminSeeder from "../../../../helpers/admin-seeder" - -jest.setTimeout(50000) - -const env = { MEDUSA_FF_MEDUSA_V2: true } - -describe("GET /store/:id", () => { - let dbConnection - let appContainer - let shutdownServer - let cartModuleService: ICartModuleService - - beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - dbConnection = await initDb({ cwd, env } as any) - shutdownServer = await startBootstrapApp({ cwd, env }) - appContainer = getContainer() - cartModuleService = appContainer.resolve(ModuleRegistrationName.CART) - }) - - afterAll(async () => { - const db = useDb() - await db.shutdown() - await shutdownServer() - }) - - beforeEach(async () => { - await adminSeeder(dbConnection) - }) - - afterEach(async () => { - const db = useDb() - await db.teardown() - }) - - it("should get cart", async () => { - const cart = await cartModuleService.create({ - currency_code: "usd", - items: [ - { - unit_price: 1000, - quantity: 1, - title: "Test item", - }, - ], - }) - - const api = useApi() as any - const response = await api.get(`/store/carts/${cart.id}`) - - expect(response.status).toEqual(200) - expect(response.data.cart).toEqual( - expect.objectContaining({ - id: cart.id, - currency_code: "usd", - items: expect.arrayContaining([ - expect.objectContaining({ - unit_price: 1000, - quantity: 1, - title: "Test item", - }), - ]), - }) - ) - }) -}) diff --git a/integration-tests/plugins/__tests__/cart/store/update-cart.ts b/integration-tests/plugins/__tests__/cart/store/update-cart.ts deleted file mode 100644 index dbbdc2b42a..0000000000 --- a/integration-tests/plugins/__tests__/cart/store/update-cart.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { ICartModuleService } from "@medusajs/types" -import path from "path" -import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" -import { useApi } from "../../../../environment-helpers/use-api" -import { getContainer } from "../../../../environment-helpers/use-container" -import { initDb, useDb } from "../../../../environment-helpers/use-db" -import adminSeeder from "../../../../helpers/admin-seeder" - -jest.setTimeout(50000) - -const env = { MEDUSA_FF_MEDUSA_V2: true } - -describe("POST /store/carts/:id", () => { - let dbConnection - let appContainer - let shutdownServer - let cartModuleService: ICartModuleService - - beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - dbConnection = await initDb({ cwd, env } as any) - shutdownServer = await startBootstrapApp({ cwd, env }) - appContainer = getContainer() - cartModuleService = appContainer.resolve(ModuleRegistrationName.CART) - }) - - afterAll(async () => { - const db = useDb() - await db.shutdown() - await shutdownServer() - }) - - beforeEach(async () => { - await adminSeeder(dbConnection) - }) - - afterEach(async () => { - const db = useDb() - await db.teardown() - }) - - it("should create a cart", async () => { - const api = useApi() as any - - const cart = await cartModuleService.create({ - currency_code: "usd", - }) - - const response = await api.post(`/store/carts/${cart.id}`, { - email: "tony@stark.com", - }) - - expect(response.status).toEqual(200) - expect(response.data.cart).toEqual( - expect.objectContaining({ - id: cart.id, - currency_code: "usd", - email: "tony@stark.com", - }) - ) - }) -}) diff --git a/integration-tests/plugins/__tests__/link-modules/cart-links.spec.ts b/integration-tests/plugins/__tests__/link-modules/cart-links.spec.ts new file mode 100644 index 0000000000..d7082805e6 --- /dev/null +++ b/integration-tests/plugins/__tests__/link-modules/cart-links.spec.ts @@ -0,0 +1,131 @@ +import { ModuleRegistrationName } from "@medusajs/modules-sdk" +import { + ICartModuleService, + IRegionModuleService, + ISalesChannelModuleService, +} from "@medusajs/types" +import path from "path" +import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../environment-helpers/use-container" +import { initDb, useDb } from "../../../environment-helpers/use-db" + +jest.setTimeout(50000) + +const env = { MEDUSA_FF_MEDUSA_V2: true } + +describe("Cart links", () => { + let dbConnection + let appContainer + let shutdownServer + let cartModuleService: ICartModuleService + let scModuleService: ISalesChannelModuleService + let remoteQuery + let regionModule: IRegionModuleService + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd, env } as any) + shutdownServer = await startBootstrapApp({ cwd, env }) + appContainer = getContainer() + cartModuleService = appContainer.resolve(ModuleRegistrationName.CART) + scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL) + regionModule = appContainer.resolve(ModuleRegistrationName.REGION) + remoteQuery = appContainer.resolve("remoteQuery") + }) + + afterAll(async () => { + const db = useDb() + await db.shutdown() + await shutdownServer() + }) + + beforeEach(async () => { + // @ts-ignore + await regionModule.createDefaultCountriesAndCurrencies() + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("should query carts, sales channels, regions with remote query", async () => { + const region = await regionModule.create({ + name: "Region", + currency_code: "usd", + }) + + const salesChannel = await scModuleService.create({ + name: "Webshop", + }) + + const cart = await cartModuleService.create({ + email: "tony@stark.com", + currency_code: "usd", + region_id: region.id, + sales_channel_id: salesChannel.id, + }) + + const carts = await remoteQuery({ + cart: { + fields: ["id"], + sales_channel: { + fields: ["id"], + }, + region: { + fields: ["id"], + }, + }, + }) + + const salesChannels = await remoteQuery({ + sales_channel: { + fields: ["id"], + carts: { + fields: ["id"], + }, + }, + }) + + const regions = await remoteQuery({ + region: { + fields: ["id"], + carts: { + fields: ["id"], + }, + }, + }) + + expect(carts).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: cart.id, + sales_channel: expect.objectContaining({ id: salesChannel.id }), + region: expect.objectContaining({ id: region.id }), + }), + ]) + ) + + expect(salesChannels).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: salesChannel.id, + carts: expect.arrayContaining([ + expect.objectContaining({ id: cart.id }), + ]), + }), + ]) + ) + + expect(regions).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: region.id, + carts: expect.arrayContaining([ + expect.objectContaining({ id: cart.id }), + ]), + }), + ]) + ) + }) +}) diff --git a/integration-tests/plugins/medusa-config.js b/integration-tests/plugins/medusa-config.js index cece0e38f9..4c3b7ab81e 100644 --- a/integration-tests/plugins/medusa-config.js +++ b/integration-tests/plugins/medusa-config.js @@ -37,9 +37,6 @@ module.exports = { }, featureFlags: { medusa_v2: enableMedusaV2, - workflows: { - [Workflows.CreateCart]: true, - }, }, modules: { [Modules.AUTH]: { diff --git a/packages/core-flows/src/definition/cart/create-cart.ts b/packages/core-flows/src/definition/cart/create-cart.ts deleted file mode 100644 index 40b5b8b1e1..0000000000 --- a/packages/core-flows/src/definition/cart/create-cart.ts +++ /dev/null @@ -1,238 +0,0 @@ -import { - TransactionStepsDefinition, - WorkflowManager, -} from "@medusajs/orchestration" -import { CartWorkflow } from "@medusajs/types" - -import { Workflows } from "../../definitions" -import { - AddressHandlers, - CartHandlers, - CommonHandlers, - CustomerHandlers, - RegionHandlers, - SalesChannelHandlers, -} from "../../handlers" -import { exportWorkflow, pipe } from "@medusajs/workflows-sdk" - -enum CreateCartActions { - setContext = "setContext", - attachLineItems = "attachLineItems", - attachToSalesChannel = "attachToSalesChannel", - findRegion = "findRegion", - findSalesChannel = "findSalesChannel", - createCart = "createCart", - findOrCreateAddresses = "findOrCreateAddresses", - findOrCreateCustomer = "findOrCreateCustomer", - removeCart = "removeCart", - removeAddresses = "removeAddresses", -} - -const workflowAlias = "cart" -const getWorkflowInput = (alias = workflowAlias) => ({ - inputAlias: workflowAlias, - invoke: { - from: workflowAlias, - alias, - }, -}) - -const workflowSteps: TransactionStepsDefinition = { - next: [ - { - action: CreateCartActions.findOrCreateCustomer, - noCompensation: true, - }, - { - action: CreateCartActions.findSalesChannel, - noCompensation: true, - }, - { - action: CreateCartActions.setContext, - noCompensation: true, - }, - { - action: CreateCartActions.findRegion, - noCompensation: true, - next: { - action: CreateCartActions.findOrCreateAddresses, - noCompensation: true, - next: { - action: CreateCartActions.createCart, - next: [ - { - action: CreateCartActions.attachLineItems, - noCompensation: true, - }, - { action: CreateCartActions.attachToSalesChannel }, - ], - }, - }, - }, - ], -} - -const handlers = new Map([ - [ - CreateCartActions.findOrCreateCustomer, - { - invoke: pipe( - getWorkflowInput( - CustomerHandlers.findOrCreateCustomer.aliases.Customer - ), - CustomerHandlers.findOrCreateCustomer - ), - }, - ], - [ - CreateCartActions.findSalesChannel, - { - invoke: pipe( - getWorkflowInput( - SalesChannelHandlers.findSalesChannel.aliases.SalesChannel - ), - SalesChannelHandlers.findSalesChannel - ), - }, - ], - [ - CreateCartActions.setContext, - { - invoke: pipe( - getWorkflowInput(CommonHandlers.setContext.aliases.Context), - CommonHandlers.setContext - ), - }, - ], - [ - CreateCartActions.findRegion, - { - invoke: pipe( - getWorkflowInput(RegionHandlers.findRegion.aliases.Region), - RegionHandlers.findRegion - ), - }, - ], - [ - CreateCartActions.findOrCreateAddresses, - { - invoke: pipe( - { - invoke: [ - getWorkflowInput( - AddressHandlers.findOrCreateAddresses.aliases.Addresses - ).invoke, - { - from: CreateCartActions.findRegion, - alias: AddressHandlers.findOrCreateAddresses.aliases.Region, - }, - ], - }, - AddressHandlers.findOrCreateAddresses - ), - }, - ], - [ - CreateCartActions.createCart, - { - invoke: pipe( - { - invoke: [ - { - from: CreateCartActions.findSalesChannel, - alias: CartHandlers.createCart.aliases.SalesChannel, - }, - { - from: CreateCartActions.findRegion, - alias: CartHandlers.createCart.aliases.Region, - }, - { - from: CreateCartActions.setContext, - alias: CartHandlers.createCart.aliases.Context, - }, - { - from: CreateCartActions.findOrCreateCustomer, - alias: CartHandlers.createCart.aliases.Customer, - }, - { - from: CreateCartActions.findOrCreateAddresses, - alias: CartHandlers.createCart.aliases.Addresses, - }, - ], - }, - CartHandlers.createCart - ), - compensate: pipe( - { - invoke: [ - { - from: CreateCartActions.createCart, - alias: CartHandlers.removeCart.aliases.Cart, - }, - ], - }, - CartHandlers.removeCart - ), - }, - ], - [ - CreateCartActions.attachLineItems, - { - invoke: pipe( - { - invoke: [ - getWorkflowInput( - CartHandlers.attachLineItemsToCart.aliases.LineItems - ).invoke, - { - from: CreateCartActions.createCart, - alias: CartHandlers.attachLineItemsToCart.aliases.Cart, - }, - ], - }, - CartHandlers.attachLineItemsToCart - ), - }, - ], - [ - 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) - -type CreateCartWorkflowOutput = Record - -export const createCart = exportWorkflow< - CartWorkflow.CreateCartWorkflowInputDTO, - CreateCartWorkflowOutput ->(Workflows.CreateCart, CreateCartActions.createCart) diff --git a/packages/core-flows/src/definition/cart/index.ts b/packages/core-flows/src/definition/cart/index.ts index c63ebfd8c0..e58562ad24 100644 --- a/packages/core-flows/src/definition/cart/index.ts +++ b/packages/core-flows/src/definition/cart/index.ts @@ -1,4 +1,3 @@ -export * from "./create-cart" export * from "./steps" export * from "./workflows" diff --git a/packages/core-flows/src/definition/cart/workflows/create-carts.ts b/packages/core-flows/src/definition/cart/workflows/create-carts.ts index 8c6c3dfd01..95605c4f1d 100644 --- a/packages/core-flows/src/definition/cart/workflows/create-carts.ts +++ b/packages/core-flows/src/definition/cart/workflows/create-carts.ts @@ -6,12 +6,12 @@ import { } from "@medusajs/workflows-sdk" import { createCartsStep, findOneOrAnyRegionStep } from "../steps" -type WorkflowInput = CreateCartWorkflowInputDTO - export const createCartWorkflowId = "create-cart" export const createCartWorkflow = createWorkflow( createCartWorkflowId, - (input: WorkflowData): WorkflowData => { + ( + input: WorkflowData + ): WorkflowData => { const region = findOneOrAnyRegionStep({ regionId: input.region_id, }) diff --git a/packages/core-flows/src/handlers/cart/attach-cart-to-sales-channel.ts b/packages/core-flows/src/handlers/cart/attach-cart-to-sales-channel.ts deleted file mode 100644 index bd85cbc123..0000000000 --- a/packages/core-flows/src/handlers/cart/attach-cart-to-sales-channel.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { MedusaV2Flag } from "@medusajs/utils" -import { WorkflowArguments } from "@medusajs/workflows-sdk" -import { Modules } from "@medusajs/modules-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): Promise { - 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({ - [Modules.CART]: { - cart_id: cart.id, - }, - [Modules.SALES_CHANNEL]: { - sales_channel_id: salesChannel.sales_channel_id, - }, - }) -} - -attachCartToSalesChannel.aliases = Aliases diff --git a/packages/core-flows/src/handlers/cart/attach-line-items-to-cart.ts b/packages/core-flows/src/handlers/cart/attach-line-items-to-cart.ts deleted file mode 100644 index 1bb2520bd8..0000000000 --- a/packages/core-flows/src/handlers/cart/attach-line-items-to-cart.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { CartWorkflow } from "@medusajs/types" -import { SalesChannelFeatureFlag } from "@medusajs/utils" - -import { WorkflowArguments } from "@medusajs/workflows-sdk" - -type HandlerInputData = { - line_items: { - items?: CartWorkflow.CreateLineItemInputDTO[] - } - cart: { - id: string - customer_id: string - region_id: string - } -} - -enum Aliases { - LineItems = "line_items", - Cart = "cart", -} - -export async function attachLineItemsToCart({ - container, - context, - data, -}: WorkflowArguments): Promise { - const { manager } = context - - const featureFlagRouter = container.resolve("featureFlagRouter") - const lineItemService = container.resolve("lineItemService") - const cartService = container.resolve("cartService") - - const lineItemServiceTx = lineItemService.withTransaction(manager) - const cartServiceTx = cartService.withTransaction(manager) - let lineItems = data[Aliases.LineItems].items - const cart = data[Aliases.Cart] - - if (lineItems?.length) { - const generateInputData = lineItems.map((item) => ({ - variantId: item.variant_id, - quantity: item.quantity, - })) - - lineItems = await lineItemServiceTx.generate(generateInputData, { - region_id: cart.region_id, - customer_id: cart.customer_id, - }) - - await cartServiceTx.addOrUpdateLineItems(cart.id, lineItems, { - validateSalesChannels: featureFlagRouter.isFeatureEnabled( - SalesChannelFeatureFlag.key - ), - }) - } -} - -attachLineItemsToCart.aliases = Aliases diff --git a/packages/core-flows/src/handlers/cart/create-cart.ts b/packages/core-flows/src/handlers/cart/create-cart.ts deleted file mode 100644 index bb926d211b..0000000000 --- a/packages/core-flows/src/handlers/cart/create-cart.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { AddressDTO, CustomerDTO, RegionDTO, legacy_CartDTO } from "@medusajs/types" -import { WorkflowArguments } from "@medusajs/workflows-sdk" - -enum Aliases { - SalesChannel = "SalesChannel", - Addresses = "addresses", - Customer = "customer", - Region = "region", - Context = "context", -} - -type HandlerInputData = { - sales_channel: { - sales_channel_id?: string - } - addresses: { - shipping_address?: AddressDTO - shipping_address_id: string - billing_address?: AddressDTO - billing_address_id: string - } - customer: { - customer?: CustomerDTO - customer_id?: string - email?: string - } - region: { - region?: RegionDTO - region_id: string - } - context: { - context: Record - } -} - -type HandlerOutputData = { - cart: legacy_CartDTO -} - -export async function createCart({ - container, - context, - data, -}: WorkflowArguments): Promise { - const { manager } = context - - const cartService = container.resolve("cartService") - const cartServiceTx = cartService.withTransaction(manager) - - return await cartServiceTx.create({ - ...data[Aliases.SalesChannel], - ...data[Aliases.Addresses], - ...data[Aliases.Customer], - ...data[Aliases.Region], - ...data[Aliases.Context], - }) -} - -createCart.aliases = Aliases diff --git a/packages/core-flows/src/handlers/cart/detach-cart-from-sales-channel.ts b/packages/core-flows/src/handlers/cart/detach-cart-from-sales-channel.ts deleted file mode 100644 index 92fa59d9b7..0000000000 --- a/packages/core-flows/src/handlers/cart/detach-cart-from-sales-channel.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { MedusaV2Flag } from "@medusajs/utils" -import { WorkflowArguments } from "@medusajs/workflows-sdk" -import { Modules } from "@medusajs/modules-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): Promise { - 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({ - [Modules.CART]: { - cart_id: cart.id, - }, - [Modules.SALES_CHANNEL]: { - sales_channel_id: salesChannel.sales_channel_id, - }, - }) -} - -detachCartFromSalesChannel.aliases = Aliases diff --git a/packages/core-flows/src/handlers/cart/index.ts b/packages/core-flows/src/handlers/cart/index.ts deleted file mode 100644 index a65006df57..0000000000 --- a/packages/core-flows/src/handlers/cart/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -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" diff --git a/packages/core-flows/src/handlers/cart/remove-cart.ts b/packages/core-flows/src/handlers/cart/remove-cart.ts deleted file mode 100644 index 57493c1382..0000000000 --- a/packages/core-flows/src/handlers/cart/remove-cart.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { WorkflowArguments } from "@medusajs/workflows-sdk" - -enum Aliases { - Cart = "cart", -} - -type HandlerInputData = { - cart: { - id: string - } -} - -export async function removeCart({ - container, - context, - data, -}: WorkflowArguments): Promise { - const { manager } = context - - const cartService = container.resolve("cartService") - - const cartServiceTx = cartService.withTransaction(manager) - const cart = data[Aliases.Cart] - - await cartServiceTx.delete(cart.id) -} - -removeCart.aliases = Aliases diff --git a/packages/core-flows/src/handlers/cart/retrieve-cart.ts b/packages/core-flows/src/handlers/cart/retrieve-cart.ts deleted file mode 100644 index 8a5e3a2d01..0000000000 --- a/packages/core-flows/src/handlers/cart/retrieve-cart.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { legacy_CartDTO } from "@medusajs/types" -import { WorkflowArguments } from "@medusajs/workflows-sdk" - -type HandlerInputData = { - cart: { - id: string - } - config: { - retrieveConfig: { - select: string[] - relations: string[] - } - } -} - -enum Aliases { - Cart = "cart", - Config = "config", -} - -export async function retrieveCart({ - container, - context, - data, -}: WorkflowArguments): Promise { - const { manager } = context - - const cartService = container.resolve("cartService") - - const cartServiceTx = cartService.withTransaction(manager) - - const retrieved = await cartServiceTx.retrieve( - data[Aliases.Cart].id, - data[Aliases.Config].retrieveConfig - ) - - return retrieved -} - -retrieveCart.aliases = Aliases diff --git a/packages/core-flows/src/handlers/index.ts b/packages/core-flows/src/handlers/index.ts index 2c3607b667..1a47ae2563 100644 --- a/packages/core-flows/src/handlers/index.ts +++ b/packages/core-flows/src/handlers/index.ts @@ -1,5 +1,4 @@ export * as AddressHandlers from "./address" -export * as CartHandlers from "./cart" export * as CommonHandlers from "./common" export * as CustomerHandlers from "./customer" export * as InventoryHandlers from "./inventory" diff --git a/packages/link-modules/src/definitions/cart-sales-channel.ts b/packages/link-modules/src/definitions/cart-sales-channel.ts index 3615958780..b3c949973c 100644 --- a/packages/link-modules/src/definitions/cart-sales-channel.ts +++ b/packages/link-modules/src/definitions/cart-sales-channel.ts @@ -1,60 +1,26 @@ import { Modules } from "@medusajs/modules-sdk" import { ModuleJoinerConfig } from "@medusajs/types" -import { LINKS } from "../links" export const CartSalesChannel: ModuleJoinerConfig = { - serviceName: LINKS.CartSalesChannel, isLink: true, - databaseConfig: { - tableName: "cart_sales_channel", - idPrefix: "cartsc", - }, - alias: [ - { - name: "cart_sales_channel", - }, - { - name: "cart_sales_channels", - }, - ], - primaryKeys: ["id", "cart_id", "sales_channel_id"], - relationships: [ - { - serviceName: Modules.CART, - primaryKey: "id", - foreignKey: "cart_id", - alias: "cart", - }, - { - serviceName: Modules.SALES_CHANNEL, - primaryKey: "id", - foreignKey: "sales_channel_id", - alias: "sales_channel", - }, - ], + isReadOnlyLink: true, extends: [ { serviceName: Modules.CART, - fieldAlias: { - sales_channel: "sales_channel_link.sales_channel", - }, relationship: { - serviceName: LINKS.CartSalesChannel, - primaryKey: "cart_id", - foreignKey: "id", - alias: "sales_channel_link", + serviceName: Modules.SALES_CHANNEL, + primaryKey: "id", + foreignKey: "sales_channel_id", + alias: "sales_channel", }, }, { serviceName: Modules.SALES_CHANNEL, - fieldAlias: { - carts: "cart_link.cart", - }, relationship: { - serviceName: LINKS.CartSalesChannel, + serviceName: Modules.CART, primaryKey: "sales_channel_id", foreignKey: "id", - alias: "cart_link", + alias: "carts", isList: true, }, }, diff --git a/packages/link-modules/src/links.ts b/packages/link-modules/src/links.ts index 9d2a479ef2..4652a9290a 100644 --- a/packages/link-modules/src/links.ts +++ b/packages/link-modules/src/links.ts @@ -28,12 +28,6 @@ export const LINKS = { Modules.SALES_CHANNEL, "sales_channel_id" ), - CartSalesChannel: composeLinkName( - Modules.CART, - "cart_id", - Modules.SALES_CHANNEL, - "sales_channel_id" - ), OrderSalesChannel: composeLinkName( "orderService", "order_id", diff --git a/packages/medusa/src/api-v2/store/carts/[id]/route.ts b/packages/medusa/src/api-v2/store/carts/[id]/route.ts index be3965c8b3..532a4e56a0 100644 --- a/packages/medusa/src/api-v2/store/carts/[id]/route.ts +++ b/packages/medusa/src/api-v2/store/carts/[id]/route.ts @@ -2,18 +2,18 @@ import { updateCartsWorkflow } from "@medusajs/core-flows" import { UpdateCartDataDTO } from "@medusajs/types" import { MedusaRequest, MedusaResponse } from "../../../../types/routing" -import { defaultStoreCartRemoteQueryObject } from "../query-config" +import { remoteQueryObjectFromString } from "@medusajs/utils" +import { defaultStoreCartFields } from "../query-config" export const GET = async (req: MedusaRequest, res: MedusaResponse) => { const remoteQuery = req.scope.resolve("remoteQuery") const variables = { id: req.params.id } - const query = { - cart: { - ...defaultStoreCartRemoteQueryObject, - }, - } + const query = remoteQueryObjectFromString({ + entryPoint: "cart", + fields: defaultStoreCartFields, + }) const [cart] = await remoteQuery(query, { cart: variables }) diff --git a/packages/medusa/src/api-v2/store/carts/query-config.ts b/packages/medusa/src/api-v2/store/carts/query-config.ts index ffc8fb5f8b..1490036c55 100644 --- a/packages/medusa/src/api-v2/store/carts/query-config.ts +++ b/packages/medusa/src/api-v2/store/carts/query-config.ts @@ -4,7 +4,36 @@ export const defaultStoreCartFields = [ "email", "created_at", "updated_at", - "deleted_at", + "items.id", + "items.created_at", + "items.updated_at", + "items.title", + "items.quantity", + "items.unit_price", + "shipping_address.id", + "shipping_address.first_name", + "shipping_address.last_name", + "shipping_address.address_1", + "shipping_address.address_2", + "shipping_address.city", + "shipping_address.postal_code", + "shipping_address.country_code", + "shipping_address.region_code", + "shipping_address.phone", + "billing_address.id", + "billing_address.first_name", + "billing_address.last_name", + "billing_address.address_1", + "billing_address.address_2", + "billing_address.city", + "billing_address.postal_code", + "billing_address.country_code", + "billing_address.region_code", + "billing_address.phone", + "region.id", + "region.name", + "region.currency_code", + "sales_channel_id", ] export const defaultStoreCartRelations = [ @@ -15,54 +44,18 @@ export const defaultStoreCartRelations = [ "shipping_methods", ] +export const allowedRelations = [ + "items", + "region", + "shipping_address", + "billing_address", + "shipping_methods", + "sales_channel", +] + export const retrieveTransformQueryConfig = { defaultFields: defaultStoreCartFields, defaultRelations: defaultStoreCartRelations, + allowedRelations: defaultStoreCartRelations, isList: false, } - -export const defaultStoreCartRemoteQueryObject = { - fields: defaultStoreCartFields, - items: { - fields: [ - "id", - "created_at", - "updated_at", - "deleted_at", - "title", - "quantity", - "unit_price", - ], - }, - shipping_address: { - fields: [ - "id", - "first_name", - "last_name", - "address_1", - "address_2", - "city", - "postal_code", - "country_code", - "region_code", - "phone", - ], - }, - billing_address: { - fields: [ - "id", - "first_name", - "last_name", - "address_1", - "address_2", - "city", - "postal_code", - "country_code", - "region_code", - "phone", - ], - }, - region: { - fields: ["id", "name", "currency_code"], - }, -} diff --git a/packages/medusa/src/api-v2/store/carts/route.ts b/packages/medusa/src/api-v2/store/carts/route.ts index b32824702e..d530a06487 100644 --- a/packages/medusa/src/api-v2/store/carts/route.ts +++ b/packages/medusa/src/api-v2/store/carts/route.ts @@ -1,7 +1,8 @@ import { createCartWorkflow } from "@medusajs/core-flows" import { CreateCartDTO } from "@medusajs/types" +import { remoteQueryObjectFromString } from "@medusajs/utils" import { MedusaRequest, MedusaResponse } from "../../../types/routing" -import { defaultStoreCartRemoteQueryObject } from "../carts/query-config" +import { defaultStoreCartFields } from "../carts/query-config" export const POST = async (req: MedusaRequest, res: MedusaResponse) => { const workflow = createCartWorkflow(req.scope) @@ -19,11 +20,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => { const variables = { id: result[0].id } - const query = { - cart: { - ...defaultStoreCartRemoteQueryObject, - }, - } + const query = remoteQueryObjectFromString({ + entryPoint: "cart", + fields: defaultStoreCartFields, + }) const [cart] = await remoteQuery(query, { cart: variables }) diff --git a/packages/medusa/src/api/routes/store/carts/create-cart.ts b/packages/medusa/src/api/routes/store/carts/create-cart.ts index 8b97b3eac1..18b12233bb 100644 --- a/packages/medusa/src/api/routes/store/carts/create-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/create-cart.ts @@ -205,9 +205,8 @@ export default async (req, res) => { return createdCart }) - // } - cart = await cartService.retrieveWithTotals(cart!.id, { + cart = await cartService.retrieveWithTotals(cart.id, { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) diff --git a/packages/medusa/src/migrations/1698160215000-cart-sales-channels-link.ts b/packages/medusa/src/migrations/1698160215000-cart-sales-channels-link.ts deleted file mode 100644 index 3d017f5965..0000000000 --- a/packages/medusa/src/migrations/1698160215000-cart-sales-channels-link.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm" -import { MedusaV2Flag } from "@medusajs/utils" - -import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels" - -export const featureFlag = [SalesChannelFeatureFlag.key, MedusaV2Flag.key] - -export class CartSalesChannelsLink1698160215000 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - CREATE TABLE IF NOT EXISTS "cart_sales_channel" - ( - "id" character varying NOT NULL, - "cart_id" character varying NOT NULL, - "sales_channel_id" character varying NOT NULL, - "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), - "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), - "deleted_at" TIMESTAMP WITH TIME ZONE, - CONSTRAINT "cart_sales_channel_pk" PRIMARY KEY ("cart_id", "sales_channel_id"), - CONSTRAINT "cart_sales_channel_cart_id_unique" UNIQUE ("cart_id") - ); - - CREATE INDEX IF NOT EXISTS "IDX_id_cart_sales_channel" ON "cart_sales_channel" ("id"); - - insert into "cart_sales_channel" (id, cart_id, sales_channel_id) - (select 'cartsc_' || substr(md5(random()::text), 0, 27), id, sales_channel_id from "cart" WHERE sales_channel_id IS NOT NULL); - - ALTER TABLE IF EXISTS "cart" DROP CONSTRAINT IF EXISTS "FK_a2bd3c26f42e754b9249ba78fd6"; - - ALTER TABLE IF EXISTS "store" DROP CONSTRAINT IF EXISTS "FK_61b0f48cccbb5f41c750bac7286"; - `) - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - UPDATE "cart" SET "sales_channel_id" = "cart_sales_channel"."sales_channel_id" - FROM "cart_sales_channel" - WHERE "cart"."id" = "cart_sales_channel"."cart_id"; - - DROP TABLE IF EXISTS "cart_sales_channel"; - - ALTER TABLE IF EXISTS "cart" ADD CONSTRAINT "FK_a2bd3c26f42e754b9249ba78fd6" FOREIGN KEY ("sales_channel_id") REFERENCES "sales_channel"("id") ON DELETE NO ACTION ON UPDATE NO ACTION; - - ALTER TABLE IF EXISTS "store" ADD CONSTRAINT "FK_61b0f48cccbb5f41c750bac7286" FOREIGN KEY ("default_sales_channel_id") REFERENCES "sales_channel"("id") ON DELETE NO ACTION ON UPDATE NO ACTION; - `) - } -} diff --git a/packages/medusa/src/models/cart-sales-channel.ts b/packages/medusa/src/models/cart-sales-channel.ts deleted file mode 100644 index 30b6d261a2..0000000000 --- a/packages/medusa/src/models/cart-sales-channel.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { BeforeInsert, Column, Index, PrimaryColumn } from "typeorm" -import { MedusaV2Flag, SalesChannelFeatureFlag } from "@medusajs/utils" - -import { generateEntityId } from "../utils" -import { SoftDeletableEntity } from "../interfaces" -import { FeatureFlagEntity } from "../utils/feature-flag-decorators" - -@FeatureFlagEntity([MedusaV2Flag.key, SalesChannelFeatureFlag.key]) -export class CartSalesChannel extends SoftDeletableEntity { - @Column() - id: string - - @Index("cart_sales_channel_cart_id_unique", { - unique: true, - }) - @PrimaryColumn() - cart_id: string - - @PrimaryColumn() - sales_channel_id: string - - /** - * @apiIgnore - */ - @BeforeInsert() - private beforeInsert(): void { - this.id = generateEntityId(this.id, "cartsc") - } -} diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 1879c87421..0bc0aac145 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -1,3 +1,4 @@ +import { RemoteQueryFunction } from "@medusajs/types" import { FlagRouter, isDefined, @@ -45,6 +46,11 @@ import { SalesChannel, ShippingMethod, } from "../models" +import { AddressRepository } from "../repositories/address" +import { CartRepository } from "../repositories/cart" +import { LineItemRepository } from "../repositories/line-item" +import { PaymentSessionRepository } from "../repositories/payment-session" +import { ShippingMethodRepository } from "../repositories/shipping-method" import { CartCreateProps, CartUpdateProps, @@ -59,16 +65,8 @@ import { TotalField, WithRequiredProperty, } from "../types/common" -import { buildQuery, isString, setMetadata } from "../utils" - -import { Modules, RemoteLink } from "@medusajs/modules-sdk" -import { RemoteQueryFunction } from "@medusajs/types" -import { AddressRepository } from "../repositories/address" -import { CartRepository } from "../repositories/cart" -import { LineItemRepository } from "../repositories/line-item" -import { PaymentSessionRepository } from "../repositories/payment-session" -import { ShippingMethodRepository } from "../repositories/shipping-method" import { PaymentSessionInput } from "../types/payment" +import { buildQuery, isString, setMetadata } from "../utils" import { validateEmail } from "../utils/is-email" type InjectedDependencies = { @@ -101,7 +99,6 @@ type InjectedDependencies = { productVariantInventoryService: ProductVariantInventoryService pricingService: PricingService remoteQuery: RemoteQueryFunction - remoteLink: RemoteLink } type TotalsConfig = { @@ -144,7 +141,6 @@ class CartService extends TransactionBaseService { protected readonly lineItemAdjustmentService_: LineItemAdjustmentService protected readonly featureFlagRouter_: FlagRouter protected remoteQuery_: RemoteQueryFunction - protected remoteLink_: RemoteLink // eslint-disable-next-line max-len protected readonly productVariantInventoryService_: ProductVariantInventoryService protected readonly pricingService_: PricingService @@ -176,7 +172,6 @@ class CartService extends TransactionBaseService { featureFlagRouter, storeService, remoteQuery, - remoteLink, productVariantInventoryService, pricingService, }: InjectedDependencies) { @@ -211,7 +206,6 @@ class CartService extends TransactionBaseService { this.productVariantInventoryService_ = productVariantInventoryService this.pricingService_ = pricingService this.remoteQuery_ = remoteQuery - this.remoteLink_ = remoteLink } /** @@ -500,14 +494,7 @@ class CartService extends TransactionBaseService { data.sales_channel_id ) - await this.remoteLink_.create({ - [Modules.CART]: { - cart_id: cart.id, - }, - [Modules.SALES_CHANNEL]: { - sales_channel_id: salesChannel.id, - }, - }) + cart.sales_channel_id = salesChannel.id } await this.eventBus_ @@ -1287,33 +1274,7 @@ class CartService extends TransactionBaseService { await this.onSalesChannelChange(cart, data.sales_channel_id) - /** - * TODO: remove this once update cart workflow is build - * since this will be handled in a handler by the workflow - */ - if (this.featureFlagRouter_.isFeatureEnabled(MedusaV2Flag.key)) { - if (cart.sales_channel_id) { - await this.remoteLink_.dismiss({ - [Modules.CART]: { - cart_id: cart.id, - }, - [Modules.SALES_CHANNEL]: { - sales_channel_id: cart.sales_channel_id, - }, - }) - } - - await this.remoteLink_.create({ - [Modules.CART]: { - cart_id: cart.id, - }, - [Modules.SALES_CHANNEL]: { - sales_channel_id: salesChannel.id, - }, - }) - } else { - cart.sales_channel_id = salesChannel.id - } + cart.sales_channel_id = salesChannel.id } if (isDefined(data.discounts) && data.discounts.length) { diff --git a/packages/region/src/models/region.ts b/packages/region/src/models/region.ts index 79fb01a020..899a77b462 100644 --- a/packages/region/src/models/region.ts +++ b/packages/region/src/models/region.ts @@ -7,6 +7,7 @@ import { Filter, Index, ManyToOne, + OnInit, OneToMany, OptionalProps, PrimaryKey, @@ -71,7 +72,7 @@ export default class Region { this.id = generateEntityId(this.id, "reg") } - @BeforeCreate() + @OnInit() onInit() { this.id = generateEntityId(this.id, "reg") } diff --git a/packages/sales-channel/src/models/sales-channel.ts b/packages/sales-channel/src/models/sales-channel.ts index 9ecbb82631..ab9ef280f9 100644 --- a/packages/sales-channel/src/models/sales-channel.ts +++ b/packages/sales-channel/src/models/sales-channel.ts @@ -1,15 +1,16 @@ import { DALUtils, generateEntityId } from "@medusajs/utils" +import { DAL } from "@medusajs/types" import { BeforeCreate, Entity, Filter, Index, + OnInit, OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" -import { DAL } from "@medusajs/types" type SalesChannelOptionalProps = "is_disabled" | DAL.EntityDateColumns @@ -54,7 +55,7 @@ export default class SalesChannel { this.id = generateEntityId(this.id, "sc") } - @BeforeCreate() + @OnInit() onInit() { this.id = generateEntityId(this.id, "sc") } diff --git a/packages/sales-channel/src/services/sales-channel-module.ts b/packages/sales-channel/src/services/sales-channel-module.ts index b9d02de4ea..ded9511f50 100644 --- a/packages/sales-channel/src/services/sales-channel-module.ts +++ b/packages/sales-channel/src/services/sales-channel-module.ts @@ -9,11 +9,7 @@ import { SalesChannelDTO, UpdateSalesChannelDTO, } from "@medusajs/types" -import { - InjectTransactionManager, - MedusaContext, - ModulesSdkUtils, -} from "@medusajs/utils" +import { MedusaContext, ModulesSdkUtils } from "@medusajs/utils" import { SalesChannel } from "@models" @@ -61,7 +57,6 @@ export default class SalesChannelModuleService< sharedContext?: Context ): Promise - @InjectTransactionManager("baseRepository_") async create( data: CreateSalesChannelDTO | CreateSalesChannelDTO[], @MedusaContext() sharedContext: Context = {} @@ -88,7 +83,6 @@ export default class SalesChannelModuleService< sharedContext?: Context ): Promise - @InjectTransactionManager("baseRepository_") async update( data: UpdateSalesChannelDTO | UpdateSalesChannelDTO[], @MedusaContext() sharedContext: Context = {}