From d2443d83e60fee3c3f28b762d66378c3f07f1b5f Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Wed, 3 May 2023 17:03:33 +0200 Subject: [PATCH] initial create-swap with sales channel (#3998) --- .changeset/rotten-steaks-jam.md | 5 ++ .../api/__tests__/admin/sales-channels.js | 73 +++++++++++++++++++ .../api/routes/admin/orders/create-swap.ts | 10 ++- .../src/api/routes/store/swaps/create-swap.ts | 4 +- packages/medusa/src/services/swap.ts | 26 ++++--- 5 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 .changeset/rotten-steaks-jam.md diff --git a/.changeset/rotten-steaks-jam.md b/.changeset/rotten-steaks-jam.md new file mode 100644 index 0000000000..9619442a24 --- /dev/null +++ b/.changeset/rotten-steaks-jam.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Feat(medusa): Add sales_channel_id to create-swap diff --git a/integration-tests/api/__tests__/admin/sales-channels.js b/integration-tests/api/__tests__/admin/sales-channels.js index 5bd1890829..4a14d77f65 100644 --- a/integration-tests/api/__tests__/admin/sales-channels.js +++ b/integration-tests/api/__tests__/admin/sales-channels.js @@ -432,6 +432,14 @@ describe("sales channels", () => { name: "test name", description: "test description", }, + payment_status: "captured", + fulfillment_status: "fulfilled", + line_items: [ + { + id: "line-item", + quantity: 2, + }, + ], }) }) @@ -460,6 +468,71 @@ describe("sales channels", () => { }) ) }) + + it("creates swap with order sales channel", async () => { + const api = useApi() + + const product = await simpleProductFactory(dbConnection, { + variants: [{ id: "test-variant", inventory_quantity: 100 }], + }) + + const swap = await api.post( + `/admin/orders/${order.id}/swaps`, + { + return_items: [ + { + item_id: "line-item", + quantity: 1, + }, + ], + additional_items: [{ variant_id: "test-variant", quantity: 1 }], + }, + adminReqConfig + ) + + expect(swap.status).toEqual(200) + + const cartId = swap.data.order.swaps[0].cart_id + + const swapCart = await api.get(`/store/carts/${cartId}`) + + expect(swapCart.data.cart.sales_channel_id).toEqual( + order.sales_channel_id + ) + }) + + it("creates swap with provided sales channel", async () => { + const api = useApi() + + const sc = await simpleSalesChannelFactory(dbConnection, {}) + + const product = await simpleProductFactory(dbConnection, { + variants: [{ id: "test-variant", inventory_quantity: 100 }], + }) + + const swap = await api.post( + `/admin/orders/${order.id}/swaps`, + { + return_items: [ + { + item_id: "line-item", + quantity: 1, + }, + ], + sales_channel_id: sc.id, + additional_items: [{ variant_id: "test-variant", quantity: 1 }], + }, + adminReqConfig + ) + + expect(swap.status).toEqual(200) + + const cartId = swap.data.order.swaps[0].cart_id + + const swapCart = await api.get(`/store/carts/${cartId}`) + + expect(swapCart.data.cart.sales_channel_id).toEqual(sc.id) + }) }) describe("GET /admin/orders?expand=sales_channels", () => { diff --git a/packages/medusa/src/api/routes/admin/orders/create-swap.ts b/packages/medusa/src/api/routes/admin/orders/create-swap.ts index c706ca76bf..79c3b556c0 100644 --- a/packages/medusa/src/api/routes/admin/orders/create-swap.ts +++ b/packages/medusa/src/api/routes/admin/orders/create-swap.ts @@ -18,9 +18,9 @@ import { } from "class-validator" import { EntityManager } from "typeorm" +import { FindParams } from "../../../../types/common" import { MedusaError } from "medusa-core-utils" import { Type } from "class-transformer" -import { FindParams } from "../../../../types/common" import { cleanResponseData } from "../../../../utils/clean-response-data" /** @@ -171,7 +171,9 @@ export default async (req, res) => { await swapService .withTransaction(manager) - .createCart(swap.id, validated.custom_shipping_options) + .createCart(swap.id, validated.custom_shipping_options, { + sales_channel_id: validated.sales_channel_id, + }) const returnOrder = await returnService .withTransaction(manager) @@ -349,6 +351,10 @@ export class AdminPostOrdersOrderSwapsReq { @Type(() => ReturnShipping) return_shipping?: ReturnShipping + @IsOptional() + @IsString() + sales_channel_id?: string + @IsArray() @IsOptional() @ValidateNested({ each: true }) diff --git a/packages/medusa/src/api/routes/store/swaps/create-swap.ts b/packages/medusa/src/api/routes/store/swaps/create-swap.ts index 02febe8bd6..ad902b5977 100644 --- a/packages/medusa/src/api/routes/store/swaps/create-swap.ts +++ b/packages/medusa/src/api/routes/store/swaps/create-swap.ts @@ -9,13 +9,13 @@ import { } from "class-validator" import { defaultStoreSwapFields, defaultStoreSwapRelations } from "." -import { Type } from "class-transformer" -import { MedusaError } from "medusa-core-utils" import { EntityManager } from "typeorm" import IdempotencyKeyService from "../../../../services/idempotency-key" +import { MedusaError } from "medusa-core-utils" import OrderService from "../../../../services/order" import ReturnService from "../../../../services/return" import SwapService from "../../../../services/swap" +import { Type } from "class-transformer" import { validator } from "../../../../utils/validator" /** diff --git a/packages/medusa/src/services/swap.ts b/packages/medusa/src/services/swap.ts index 070bb367b3..f083e16eed 100644 --- a/packages/medusa/src/services/swap.ts +++ b/packages/medusa/src/services/swap.ts @@ -1,9 +1,3 @@ -import { isDefined, MedusaError } from "medusa-core-utils" -import { EntityManager, In } from "typeorm" - -import { TransactionBaseService } from "../interfaces" -import { buildQuery, setMetadata, validateId } from "../utils" - import { Cart, CartType, @@ -19,10 +13,6 @@ import { SwapFulfillmentStatus, SwapPaymentStatus, } from "../models" -import { SwapRepository } from "../repositories/swap" -import { FindConfig, Selector, WithRequiredProperty } from "../types/common" -import { CreateShipmentConfig } from "../types/fulfillment" -import { OrdersReturnItem } from "../types/orders" import { CartService, CustomShippingOptionService, @@ -37,6 +27,15 @@ import { ShippingOptionService, TotalsService, } from "./index" +import { EntityManager, In } from "typeorm" +import { FindConfig, Selector, WithRequiredProperty } from "../types/common" +import { MedusaError, isDefined } from "medusa-core-utils" +import { buildQuery, setMetadata, validateId } from "../utils" + +import { CreateShipmentConfig } from "../types/fulfillment" +import { OrdersReturnItem } from "../types/orders" +import { SwapRepository } from "../repositories/swap" +import { TransactionBaseService } from "../interfaces" type InjectedProps = { manager: EntityManager @@ -557,7 +556,8 @@ class SwapService extends TransactionBaseService { */ async createCart( swapId: string, - customShippingOptions: { option_id: string; price: number }[] = [] + customShippingOptions: { option_id: string; price: number }[] = [], + context: { sales_channel_id?: string } = {} ): Promise { return await this.atomicPhase_(async (manager) => { const swapRepo = manager.withRepository(this.swapRepository_) @@ -611,6 +611,8 @@ class SwapService extends TransactionBaseService { shipping_address_id: order.shipping_address_id, region_id: order.region_id, customer_id: order.customer_id, + sales_channel_id: + context.sales_channel_id ?? order.sales_channel_id ?? undefined, type: CartType.SWAP, metadata: { swap_id: swap.id, @@ -792,7 +794,7 @@ class SwapService extends TransactionBaseService { // Is the cascade insert really used? Also, is it really necessary to pass the entire entities when creating or updating? // We normally should only pass what is needed? swap.shipping_methods = cart.shipping_methods.map((method) => { - (method.tax_lines as any) = undefined + ;(method.tax_lines as any) = undefined return method }) swap.confirmed_at = new Date()