initial create-swap with sales channel (#3998)

This commit is contained in:
Philip Korsholm
2023-05-03 17:03:33 +02:00
committed by GitHub
parent 8b93bae8f8
commit d2443d83e6
5 changed files with 102 additions and 16 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
Feat(medusa): Add sales_channel_id to create-swap

View File

@@ -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", () => {

View File

@@ -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 })

View File

@@ -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"
/**

View File

@@ -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<Swap | never> {
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()