initial create-swap with sales channel (#3998)
This commit is contained in:
1 parent
8b93bae8f8
commit
d2443d83e6
5 files changed
+102
-16
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Feat(medusa): Add sales_channel_id to create-swap
|
||||||
@@ -432,6 +432,14 @@ describe("sales channels", () => {
|
|||||||
name: "test name",
|
name: "test name",
|
||||||
description: "test description",
|
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", () => {
|
describe("GET /admin/orders?expand=sales_channels", () => {
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ import {
|
|||||||
} from "class-validator"
|
} from "class-validator"
|
||||||
|
|
||||||
import { EntityManager } from "typeorm"
|
import { EntityManager } from "typeorm"
|
||||||
|
import { FindParams } from "../../../../types/common"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
import { FindParams } from "../../../../types/common"
|
|
||||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,7 +171,9 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
await swapService
|
await swapService
|
||||||
.withTransaction(manager)
|
.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
|
const returnOrder = await returnService
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
@@ -349,6 +351,10 @@ export class AdminPostOrdersOrderSwapsReq {
|
|||||||
@Type(() => ReturnShipping)
|
@Type(() => ReturnShipping)
|
||||||
return_shipping?: ReturnShipping
|
return_shipping?: ReturnShipping
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
sales_channel_id?: string
|
||||||
|
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ import {
|
|||||||
} from "class-validator"
|
} from "class-validator"
|
||||||
import { defaultStoreSwapFields, defaultStoreSwapRelations } from "."
|
import { defaultStoreSwapFields, defaultStoreSwapRelations } from "."
|
||||||
|
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import { MedusaError } from "medusa-core-utils"
|
|
||||||
import { EntityManager } from "typeorm"
|
import { EntityManager } from "typeorm"
|
||||||
import IdempotencyKeyService from "../../../../services/idempotency-key"
|
import IdempotencyKeyService from "../../../../services/idempotency-key"
|
||||||
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import OrderService from "../../../../services/order"
|
import OrderService from "../../../../services/order"
|
||||||
import ReturnService from "../../../../services/return"
|
import ReturnService from "../../../../services/return"
|
||||||
import SwapService from "../../../../services/swap"
|
import SwapService from "../../../../services/swap"
|
||||||
|
import { Type } from "class-transformer"
|
||||||
import { validator } from "../../../../utils/validator"
|
import { validator } from "../../../../utils/validator"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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 {
|
import {
|
||||||
Cart,
|
Cart,
|
||||||
CartType,
|
CartType,
|
||||||
@@ -19,10 +13,6 @@ import {
|
|||||||
SwapFulfillmentStatus,
|
SwapFulfillmentStatus,
|
||||||
SwapPaymentStatus,
|
SwapPaymentStatus,
|
||||||
} from "../models"
|
} 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 {
|
import {
|
||||||
CartService,
|
CartService,
|
||||||
CustomShippingOptionService,
|
CustomShippingOptionService,
|
||||||
@@ -37,6 +27,15 @@ import {
|
|||||||
ShippingOptionService,
|
ShippingOptionService,
|
||||||
TotalsService,
|
TotalsService,
|
||||||
} from "./index"
|
} 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 = {
|
type InjectedProps = {
|
||||||
manager: EntityManager
|
manager: EntityManager
|
||||||
@@ -557,7 +556,8 @@ class SwapService extends TransactionBaseService {
|
|||||||
*/
|
*/
|
||||||
async createCart(
|
async createCart(
|
||||||
swapId: string,
|
swapId: string,
|
||||||
customShippingOptions: { option_id: string; price: number }[] = []
|
customShippingOptions: { option_id: string; price: number }[] = [],
|
||||||
|
context: { sales_channel_id?: string } = {}
|
||||||
): Promise<Swap | never> {
|
): Promise<Swap | never> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
return await this.atomicPhase_(async (manager) => {
|
||||||
const swapRepo = manager.withRepository(this.swapRepository_)
|
const swapRepo = manager.withRepository(this.swapRepository_)
|
||||||
@@ -611,6 +611,8 @@ class SwapService extends TransactionBaseService {
|
|||||||
shipping_address_id: order.shipping_address_id,
|
shipping_address_id: order.shipping_address_id,
|
||||||
region_id: order.region_id,
|
region_id: order.region_id,
|
||||||
customer_id: order.customer_id,
|
customer_id: order.customer_id,
|
||||||
|
sales_channel_id:
|
||||||
|
context.sales_channel_id ?? order.sales_channel_id ?? undefined,
|
||||||
type: CartType.SWAP,
|
type: CartType.SWAP,
|
||||||
metadata: {
|
metadata: {
|
||||||
swap_id: swap.id,
|
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?
|
// 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?
|
// We normally should only pass what is needed?
|
||||||
swap.shipping_methods = cart.shipping_methods.map((method) => {
|
swap.shipping_methods = cart.shipping_methods.map((method) => {
|
||||||
(method.tax_lines as any) = undefined
|
;(method.tax_lines as any) = undefined
|
||||||
return method
|
return method
|
||||||
})
|
})
|
||||||
swap.confirmed_at = new Date()
|
swap.confirmed_at = new Date()
|
||||||
|
|||||||
Reference in new issue
Block a user