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
@@ -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 })
@@ -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"
/**
+14 -12
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()