chore: Merge master to develop (#3653)
This commit is contained in:
@@ -4,16 +4,16 @@ import { default as wrap } from "./await-middleware"
|
||||
import { default as normalizeQuery } from "./normalized-query"
|
||||
import { default as requireCustomerAuthentication } from "./require-customer-authentication"
|
||||
|
||||
export { canAccessBatchJob } from "./batch-job/can-access-batch-job"
|
||||
export { getRequestedBatchJob } from "./batch-job/get-requested-batch-job"
|
||||
export { doesConditionBelongToDiscount } from "./discount/does-condition-belong-to-discount"
|
||||
export { transformIncludesOptions } from "./transform-includes-options"
|
||||
export { transformBody } from "./transform-body"
|
||||
export { default as authenticate } from "./authenticate"
|
||||
export { default as authenticateCustomer } from "./authenticate-customer"
|
||||
export { default as wrapHandler } from "./await-middleware"
|
||||
export { canAccessBatchJob } from "./batch-job/can-access-batch-job"
|
||||
export { getRequestedBatchJob } from "./batch-job/get-requested-batch-job"
|
||||
export { doesConditionBelongToDiscount } from "./discount/does-condition-belong-to-discount"
|
||||
export { default as normalizeQuery } from "./normalized-query"
|
||||
export { default as requireCustomerAuthentication } from "./require-customer-authentication"
|
||||
export { transformBody } from "./transform-body"
|
||||
export { transformIncludesOptions } from "./transform-includes-options"
|
||||
export { transformQuery, transformStoreQuery } from "./transform-query"
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,7 @@ import { AddressPayload } from "../../../../types/common"
|
||||
import { DraftOrderCreateProps } from "../../../../types/draft-orders"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders
|
||||
@@ -154,7 +155,7 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
res.status(200).json({ draft_order: cleanResponseData(draftOrder, []) })
|
||||
}
|
||||
|
||||
enum Status {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders/{id}/line-items
|
||||
@@ -138,7 +139,9 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
res.status(200).json({
|
||||
draft_order: cleanResponseData(draftOrder, []),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import { DraftOrder } from "../../../.."
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [delete] /admin/draft-orders/{id}/line-items/{line_id}
|
||||
@@ -93,6 +94,8 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
res.status(200).json({
|
||||
draft_order: cleanResponseData(draftOrder, []),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
} from "."
|
||||
|
||||
import { DraftOrder } from "../../../.."
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [get] /admin/draft-orders/{id}
|
||||
@@ -77,5 +78,7 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.json({ draft_order: draftOrder })
|
||||
res.json({
|
||||
draft_order: cleanResponseData(draftOrder, []),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,13 +6,14 @@ import {
|
||||
ProductVariantInventoryService,
|
||||
} from "../../../../services"
|
||||
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { Order } from "../../../../models"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import {
|
||||
defaultAdminOrdersFields as defaultOrderFields,
|
||||
defaultAdminOrdersRelations as defaultOrderRelations,
|
||||
} from "../../../../types/orders"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders/{id}/pay
|
||||
@@ -119,7 +120,7 @@ export default async (req, res) => {
|
||||
return order
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export const reserveQuantityForDraftOrder = async (
|
||||
|
||||
@@ -19,6 +19,7 @@ import { CartUpdateProps } from "../../../../types/cart"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders/{id}
|
||||
@@ -137,7 +138,7 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
res.status(200).json({ draft_order: cleanResponseData(draftOrder, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import { EntityManager } from "typeorm"
|
||||
import { LineItemUpdate } from "../../../../types/cart"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders/{id}/line-items/{line_id}
|
||||
@@ -139,7 +140,9 @@ export default async (req, res) => {
|
||||
select: defaultAdminDraftOrdersCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
res.status(200).json({
|
||||
draft_order: cleanResponseData(draftOrder, []),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/shipping-methods
|
||||
@@ -97,7 +98,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/archive
|
||||
@@ -70,7 +71,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersOrderArchiveParams extends FindParams {}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { ClaimService, OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/claims/{claim_id}/cancel
|
||||
@@ -85,7 +86,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersClaimCancel extends FindParams {}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/claims/{claim_id}/fulfillments/{fulfillment_id}/cancel
|
||||
@@ -101,7 +102,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersClaimFulfillmentsCancelParams extends FindParams {}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/swaps/{swap_id}/fulfillments/{fulfillment_id}/cancel
|
||||
@@ -101,7 +102,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
|
||||
@@ -9,6 +9,7 @@ import { MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { Fulfillment } from "../../../../models"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/fulfillments/{fulfillment_id}/cancel
|
||||
@@ -108,7 +109,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export const adjustInventoryForCancelledFulfillment = async (
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/cancel
|
||||
@@ -69,7 +70,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersOrderCancel extends FindParams {}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { OrderService, SwapService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/swaps/{swap_id}/cancel
|
||||
@@ -83,7 +84,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersSwapCancelParams extends FindParams {}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/capture
|
||||
@@ -72,7 +73,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersOrderCaptureParams extends FindParams {}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/complete
|
||||
@@ -72,7 +73,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminPostOrdersOrderCompleteParams extends FindParams {}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ClaimService, OrderService } from "../../../../services"
|
||||
import { IsArray, IsNotEmpty, IsOptional, IsString } from "class-validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/claims/{claim_id}/shipments
|
||||
@@ -92,7 +93,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@ import { MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { ClaimTypeValue } from "../../../../types/claim"
|
||||
import { AddressPayload, FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/claims
|
||||
@@ -254,7 +255,9 @@ export default async (req, res) => {
|
||||
|
||||
return {
|
||||
response_code: 200,
|
||||
response_body: { order },
|
||||
response_body: {
|
||||
order,
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -288,6 +291,13 @@ export default async (req, res) => {
|
||||
throw err
|
||||
}
|
||||
|
||||
if (idempotencyKey.response_body.order) {
|
||||
idempotencyKey.response_body.order = cleanResponseData(
|
||||
idempotencyKey.response_body.order,
|
||||
[]
|
||||
)
|
||||
}
|
||||
|
||||
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
|
||||
import { Fulfillment, LineItem } from "../../../../models"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/fulfillment
|
||||
@@ -150,7 +151,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export const updateInventoryAndReservations = async (
|
||||
|
||||
@@ -10,6 +10,7 @@ import { EntityManager } from "typeorm"
|
||||
import { OrderService } from "../../../../services"
|
||||
import { TrackingLink } from "../../../../models"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/shipment
|
||||
@@ -104,7 +105,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@ import { OrderService, SwapService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/swaps/{swap_id}/shipments
|
||||
@@ -102,7 +103,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@ import { EntityManager } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { Type } from "class-transformer"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/swaps
|
||||
@@ -219,7 +220,9 @@ export default async (req, res) => {
|
||||
|
||||
return {
|
||||
response_code: 200,
|
||||
response_body: { order },
|
||||
response_body: {
|
||||
order: cleanResponseData(order, []),
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { IsBoolean, IsObject, IsOptional, IsString } from "class-validator"
|
||||
import {
|
||||
ClaimService,
|
||||
OrderService,
|
||||
ProductVariantInventoryService,
|
||||
} from "../../../../services"
|
||||
import { IsBoolean, IsObject, IsOptional, IsString } from "class-validator"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { updateInventoryAndReservations } from "./create-fulfillment"
|
||||
|
||||
/**
|
||||
@@ -128,7 +129,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,8 +6,9 @@ import {
|
||||
} from "../../../../services"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { updateInventoryAndReservations } from "./create-fulfillment"
|
||||
|
||||
/**
|
||||
@@ -134,7 +135,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Order } from "../../../../models"
|
||||
import { OrderService } from "../../../../services"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { Order } from "../../../../models"
|
||||
|
||||
/**
|
||||
* @oas [get] /admin/orders/{id}
|
||||
@@ -72,7 +72,7 @@ export default async (req, res) => {
|
||||
|
||||
order = cleanResponseData(order, req.allowedProperties)
|
||||
|
||||
res.json({ order: order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
export class AdminGetOrdersOrderParams extends FindParams {}
|
||||
|
||||
@@ -98,7 +98,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
transformIncludesOptions(allowedOrderIncludes),
|
||||
transformQuery(AdminGetOrdersParams, {
|
||||
defaultRelations: relations,
|
||||
defaultFields: defaultAdminOrdersFields,
|
||||
defaultFields: defaultFields,
|
||||
isList: true,
|
||||
}),
|
||||
middlewares.wrap(require("./list-orders").default)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
|
||||
import { AdminListOrdersSelector } from "../../../../types/orders"
|
||||
import { OrderService } from "../../../../services"
|
||||
import { Type } from "class-transformer"
|
||||
import { OrderService } from "../../../../services"
|
||||
import { AdminListOrdersSelector } from "../../../../types/orders"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
@@ -208,7 +208,12 @@ export default async (req, res) => {
|
||||
|
||||
const data = cleanResponseData(orders, req.allowedProperties)
|
||||
|
||||
res.json({ orders: data, count, offset: skip, limit: take })
|
||||
res.json({
|
||||
orders: cleanResponseData(data, []),
|
||||
count,
|
||||
offset: skip,
|
||||
limit: take,
|
||||
})
|
||||
}
|
||||
|
||||
export class AdminGetOrdersParams extends AdminListOrdersSelector {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { OrderService, SwapService } from "../../../../services"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/swaps/{swap_id}/process-payment
|
||||
@@ -73,7 +74,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { OrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/refund
|
||||
@@ -97,7 +98,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,17 +6,18 @@ import {
|
||||
IsInt,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { Order, Return } from "../../../../models"
|
||||
import {
|
||||
EventBusService,
|
||||
OrderService,
|
||||
ReturnService
|
||||
ReturnService,
|
||||
} from "../../../../services"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { OrdersReturnItem } from "../../../../types/orders"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/return
|
||||
@@ -241,7 +242,9 @@ export default async (req, res) => {
|
||||
|
||||
return {
|
||||
response_code: 200,
|
||||
response_body: { order },
|
||||
response_body: {
|
||||
order: cleanResponseData(order, []),
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Type } from "class-transformer"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}/claims/{claim_id}
|
||||
@@ -103,7 +104,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.json({ order: data })
|
||||
res.json({ order: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ import { AddressPayload, FindParams } from "../../../../types/common"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { OrderService } from "../../../../services"
|
||||
import { Type } from "class-transformer"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/orders/{id}
|
||||
@@ -96,7 +97,7 @@ export default async (req, res) => {
|
||||
includes: req.includes,
|
||||
})
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ import { EntityManager } from "typeorm"
|
||||
import { defaultAdminProductFields, defaultAdminProductRelations } from "."
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
import { ProductStatus, ProductVariant } from "../../../../models"
|
||||
import { ProductVariantRepository } from "../../../../repositories/product-variant"
|
||||
import {
|
||||
PricingService,
|
||||
ProductService,
|
||||
@@ -39,7 +40,6 @@ import {
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { DistributedTransaction } from "../../../../utils/transaction"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { ProductVariantRepository } from "../../../../repositories/product-variant"
|
||||
import {
|
||||
createVariantTransaction,
|
||||
revertVariantTransaction,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/shipping-methods
|
||||
@@ -62,10 +62,7 @@ import { validator } from "../../../../utils/validator"
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
StorePostCartsCartShippingMethodReq,
|
||||
req.body
|
||||
)
|
||||
const validated = req.validatedBody
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
@@ -94,7 +91,7 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@ import { CartService, IdempotencyKeyService } from "../../../../services"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import { IdempotencyKey } from "../../../../models/idempotency-key"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/taxes
|
||||
@@ -118,5 +119,12 @@ export default async (req, res) => {
|
||||
throw err
|
||||
}
|
||||
|
||||
if (idempotencyKey.response_body.cart) {
|
||||
idempotencyKey.response_body.cart = cleanResponseData(
|
||||
idempotencyKey.response_body.cart,
|
||||
[]
|
||||
)
|
||||
}
|
||||
|
||||
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { EntityManager } from "typeorm"
|
||||
import { AbstractCartCompletionStrategy } from "../../../../interfaces"
|
||||
import { IdempotencyKey } from "../../../../models"
|
||||
import { IdempotencyKeyService } from "../../../../services"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/complete
|
||||
@@ -90,5 +91,9 @@ export default async (req, res) => {
|
||||
req.request_context
|
||||
)
|
||||
|
||||
if (response_body.data) {
|
||||
response_body.data = cleanResponseData(response_body.data, [])
|
||||
}
|
||||
|
||||
res.status(response_code).json(response_body)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||
import reqIp from "request-ip"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
@@ -10,18 +7,22 @@ import {
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||
import reqIp from "request-ip"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
import { Cart, LineItem } from "../../../../models"
|
||||
import {
|
||||
CartService,
|
||||
LineItemService,
|
||||
RegionService,
|
||||
} from "../../../../services"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { Cart, LineItem } from "../../../../models"
|
||||
import { CartCreateProps } from "../../../../types/cart"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { FlagRouter } from "../../../../utils/flag-router"
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
import { CartCreateProps } from "../../../../types/cart"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts
|
||||
@@ -172,7 +173,7 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart })
|
||||
res.status(200).json({ cart: cleanResponseData(cart, []) })
|
||||
}
|
||||
|
||||
export class Item {
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
runIdempotencyStep,
|
||||
RunIdempotencyStepOptions,
|
||||
} from "../../../../../utils/idempotency"
|
||||
import { cleanResponseData } from "../../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/line-items
|
||||
@@ -130,6 +131,13 @@ export default async (req, res) => {
|
||||
throw err
|
||||
}
|
||||
|
||||
if (idempotencyKey.response_body.cart) {
|
||||
idempotencyKey.response_body.cart = cleanResponseData(
|
||||
idempotencyKey.response_body.cart,
|
||||
[]
|
||||
)
|
||||
}
|
||||
|
||||
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import IdempotencyKeyService from "../../../../services/idempotency-key"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/payment-sessions
|
||||
@@ -133,5 +134,11 @@ export default async (req, res) => {
|
||||
throw err
|
||||
}
|
||||
|
||||
if (idempotencyKey.response_body.cart) {
|
||||
idempotencyKey.response_body.data = cleanResponseData(
|
||||
idempotencyKey.response_body.cart,
|
||||
[]
|
||||
)
|
||||
}
|
||||
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [delete] /store/carts/{id}/discounts/{code}
|
||||
@@ -71,5 +72,5 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [delete] /store/carts/{id}/line-items/{line_id}
|
||||
@@ -72,5 +73,6 @@ export default async (req, res) => {
|
||||
select: defaultStoreCartFields,
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
res.status(200).json({ cart: data })
|
||||
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [delete] /store/carts/{id}/payment-sessions/{provider_id}
|
||||
@@ -63,5 +64,5 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [get] /store/carts/{id}
|
||||
@@ -70,5 +71,5 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
const data = await cartService.retrieveWithTotals(id, req.retrieveConfig)
|
||||
res.json({ cart: data })
|
||||
res.json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ import { StorePostCartReq } from "./create-cart"
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
||||
import { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
||||
import { StorePostCartsCartShippingMethodReq } from "./add-shipping-method"
|
||||
import { StorePostCartsCartPaymentSessionReq } from "./set-payment-session"
|
||||
import { StorePostCartsCartLineItemsItemReq } from "./update-line-item"
|
||||
import { StorePostCartsCartPaymentSessionUpdateReq } from "./update-payment-session"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -43,6 +47,11 @@ export default (app, container) => {
|
||||
|
||||
const createMiddlewares = [
|
||||
middlewareService.usePreCartCreation(),
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartReq),
|
||||
extendRequestParams,
|
||||
validateSalesChannelParam,
|
||||
@@ -56,75 +65,149 @@ export default (app, container) => {
|
||||
|
||||
route.post(
|
||||
"/:id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartsCartReq),
|
||||
middlewares.wrap(require("./update-cart").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/complete",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./complete-cart").default)
|
||||
)
|
||||
|
||||
// DEPRECATION
|
||||
route.post(
|
||||
"/:id/complete-cart",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./complete-cart").default)
|
||||
)
|
||||
|
||||
// Line items
|
||||
route.post(
|
||||
"/:id/line-items",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./create-line-item").default)
|
||||
)
|
||||
route.post(
|
||||
"/:id/line-items/:line_id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartsCartLineItemsItemReq),
|
||||
middlewares.wrap(require("./update-line-item").default)
|
||||
)
|
||||
route.delete(
|
||||
"/:id/line-items/:line_id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./delete-line-item").default)
|
||||
)
|
||||
|
||||
route.delete(
|
||||
"/:id/discounts/:code",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./delete-discount").default)
|
||||
)
|
||||
|
||||
// Payment sessions
|
||||
route.post(
|
||||
"/:id/payment-sessions",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./create-payment-sessions").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/payment-sessions/:provider_id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartsCartPaymentSessionUpdateReq),
|
||||
middlewares.wrap(require("./update-payment-session").default)
|
||||
)
|
||||
|
||||
route.delete(
|
||||
"/:id/payment-sessions/:provider_id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./delete-payment-session").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/payment-sessions/:provider_id/refresh",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./refresh-payment-session").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/payment-session",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartsCartPaymentSessionReq),
|
||||
middlewares.wrap(require("./set-payment-session").default)
|
||||
)
|
||||
|
||||
// Shipping Options
|
||||
route.post(
|
||||
"/:id/shipping-methods",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(StorePostCartsCartShippingMethodReq),
|
||||
middlewares.wrap(require("./add-shipping-method").default)
|
||||
)
|
||||
|
||||
// Taxes
|
||||
route.post(
|
||||
"/:id/taxes",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
defaultFields: defaultStoreCartFields,
|
||||
isList: false,
|
||||
}),
|
||||
middlewares.wrap(require("./calculate-taxes").default)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/payment-sessions/{provider_id}/refresh
|
||||
@@ -67,5 +68,5 @@ export default async (req, res) => {
|
||||
],
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { IsString } from "class-validator"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/payment-session
|
||||
@@ -62,10 +62,7 @@ import { validator } from "../../../../utils/validator"
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
StorePostCartsCartPaymentSessionReq,
|
||||
req.body
|
||||
)
|
||||
const validated = req.validatedBody
|
||||
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
|
||||
@@ -81,7 +78,7 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ import { CartService } from "../../../../services"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}
|
||||
@@ -100,7 +101,7 @@ export default async (req, res) => {
|
||||
select: defaultStoreCartFields,
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
res.json({ cart: data })
|
||||
res.json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
class GiftCard {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/line-items/{line_id}
|
||||
@@ -63,10 +63,7 @@ import { validator } from "../../../../utils/validator"
|
||||
export default async (req, res) => {
|
||||
const { id, line_id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
StorePostCartsCartLineItemsItemReq,
|
||||
req.body
|
||||
)
|
||||
const validated = req.validatedBody
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
@@ -115,7 +112,7 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IsObject } from "class-validator"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [post] /store/carts/{id}/payment-sessions/{provider_id}
|
||||
@@ -64,10 +64,7 @@ import { EntityManager } from "typeorm"
|
||||
export default async (req, res) => {
|
||||
const { id, provider_id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
StorePostCartsCartPaymentSessionUpdateReq,
|
||||
req.body
|
||||
)
|
||||
const validated = req.validatedBody
|
||||
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
|
||||
@@ -86,7 +83,7 @@ export default async (req, res) => {
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: data })
|
||||
res.status(200).json({ cart: cleanResponseData(data, []) })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { defaultStoreOrdersFields, defaultStoreOrdersRelations } from "."
|
||||
|
||||
import { OrderService } from "../../../../services"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
/**
|
||||
* @oas [get] /store/orders/cart/{cart_id}
|
||||
@@ -54,5 +55,5 @@ export default async (req, res) => {
|
||||
relations: defaultStoreOrdersRelations,
|
||||
})
|
||||
|
||||
res.json({ order })
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import { StorePostCustomersCustomerOrderClaimReq } from "./request-order"
|
||||
import { StorePostCustomersCustomerAcceptClaimReq } from "./confirm-order-request"
|
||||
import { StoreGetOrderParams } from "./get-order"
|
||||
import { StoreGetOrdersParams } from "./lookup-order"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -50,6 +51,12 @@ export default (app) => {
|
||||
*/
|
||||
route.get(
|
||||
"/cart/:cart_id",
|
||||
transformStoreQuery(FindParams, {
|
||||
defaultFields: defaultStoreOrdersFields,
|
||||
defaultRelations: defaultStoreOrdersRelations,
|
||||
allowedFields: allowedStoreOrdersFields,
|
||||
allowedRelations: allowedStoreOrdersRelations,
|
||||
}),
|
||||
middlewares.wrap(require("./get-order-by-cart").default)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Router } from "express"
|
||||
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
||||
import { ProductCategory } from "../../../../models"
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
||||
|
||||
import listProductCategories, {
|
||||
StoreGetProductCategoriesParams,
|
||||
|
||||
@@ -2,14 +2,14 @@ import { Router } from "express"
|
||||
import "reflect-metadata"
|
||||
|
||||
import { Product } from "../../../.."
|
||||
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import { FlagRouter } from "../../../../utils/flag-router"
|
||||
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
||||
import { validateProductSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-product-sales-channel-association"
|
||||
import { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
||||
import { StoreGetProductsParams } from "./list-products"
|
||||
import { StoreGetProductsProductParams } from "./get-product"
|
||||
import { FlagRouter } from "../../../../utils/flag-router"
|
||||
import { StoreGetProductsParams } from "./list-products"
|
||||
|
||||
const route = Router()
|
||||
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import {
|
||||
moduleHelper,
|
||||
moduleLoader,
|
||||
registerModules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { asValue, createContainer } from "awilix"
|
||||
import express from "express"
|
||||
import jwt from "jsonwebtoken"
|
||||
@@ -7,16 +12,11 @@ import "reflect-metadata"
|
||||
import supertest from "supertest"
|
||||
import apiLoader from "../loaders/api"
|
||||
import featureFlagLoader, { featureFlagRouter } from "../loaders/feature-flags"
|
||||
import models from "../loaders/models"
|
||||
import passportLoader from "../loaders/passport"
|
||||
import repositories from "../loaders/repositories"
|
||||
import servicesLoader from "../loaders/services"
|
||||
import strategiesLoader from "../loaders/strategies"
|
||||
import {
|
||||
moduleHelper,
|
||||
moduleLoader,
|
||||
registerModules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import repositories from "../loaders/repositories"
|
||||
import models from "../loaders/models"
|
||||
|
||||
const adminSessionOpts = {
|
||||
cookieName: "session",
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { TransactionBaseService } from "@medusajs/utils"
|
||||
import { IdempotencyKey } from "../models"
|
||||
import { RequestContext } from "../types/request"
|
||||
import { TransactionBaseService } from "./transaction-base-service"
|
||||
|
||||
export type CartCompletionResponse = {
|
||||
/** The response code for the completion request */
|
||||
response_code: number
|
||||
|
||||
/** The response body for the completion request */
|
||||
response_body: object
|
||||
response_body: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface ICartCompletionStrategy {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
|
||||
export class lineItemAdjustmentsAmount1678093365812 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE line_item_adjustment ALTER COLUMN amount TYPE NUMERIC;
|
||||
`)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE line_item_adjustment ALTER COLUMN amount TYPE integer;
|
||||
`)
|
||||
}
|
||||
}
|
||||
@@ -170,6 +170,10 @@
|
||||
* type: integer
|
||||
* example: 1000
|
||||
* discount_total:
|
||||
* description: The total of discount rounded
|
||||
* type: integer
|
||||
* example: 800
|
||||
* raw_discount_total:
|
||||
* description: The total of discount
|
||||
* type: integer
|
||||
* example: 800
|
||||
@@ -376,6 +380,7 @@ export class Cart extends SoftDeletableEntity {
|
||||
|
||||
shipping_total?: number
|
||||
discount_total?: number
|
||||
raw_discount_total?: number
|
||||
item_tax_total?: number | null
|
||||
shipping_tax_total?: number | null
|
||||
tax_total?: number | null
|
||||
|
||||
@@ -8,10 +8,9 @@ import {
|
||||
PrimaryColumn,
|
||||
} from "typeorm"
|
||||
|
||||
import { DbAwareColumn } from "../utils/db-aware-column"
|
||||
import { DbAwareColumn, generateEntityId } from "../utils"
|
||||
import { Discount } from "./discount"
|
||||
import { LineItem } from "./line-item"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
|
||||
@Entity()
|
||||
@Index(["discount_id", "item_id"], {
|
||||
@@ -41,7 +40,7 @@ export class LineItemAdjustment {
|
||||
@Column({ nullable: true })
|
||||
discount_id: string
|
||||
|
||||
@Column({ type: "int" })
|
||||
@Column({ type: "numeric", transformer: { to: (value) => value, from: (value) => parseFloat(value) } })
|
||||
amount: number
|
||||
|
||||
@DbAwareColumn({ type: "jsonb", nullable: true })
|
||||
@@ -93,7 +92,7 @@ export class LineItemAdjustment {
|
||||
* $ref: "#/components/schemas/Discount"
|
||||
* amount:
|
||||
* description: The adjustment amount
|
||||
* type: integer
|
||||
* type: number
|
||||
* example: 1000
|
||||
* metadata:
|
||||
* description: An optional key-value map with additional details
|
||||
|
||||
@@ -10,7 +10,8 @@ import {
|
||||
} from "typeorm"
|
||||
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||
import TaxInclusivePricingFeatureFlag
|
||||
from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||
import { generateEntityId } from "../utils"
|
||||
import { DbAwareColumn } from "../utils/db-aware-column"
|
||||
import { FeatureFlagColumn } from "../utils/feature-flag-decorators"
|
||||
@@ -147,6 +148,7 @@ export class LineItem extends BaseEntity {
|
||||
original_total?: number | null
|
||||
original_tax_total?: number | null
|
||||
discount_total?: number | null
|
||||
raw_discount_total?: number | null
|
||||
gift_card_total?: number | null
|
||||
|
||||
@BeforeInsert()
|
||||
@@ -341,6 +343,10 @@ export class LineItem extends BaseEntity {
|
||||
* type: integer
|
||||
* example: 0
|
||||
* discount_total:
|
||||
* description: The total of discount of the line item rounded
|
||||
* type: integer
|
||||
* example: 0
|
||||
* raw_discount_total:
|
||||
* description: The total of discount of the line item
|
||||
* type: integer
|
||||
* example: 0
|
||||
|
||||
@@ -241,6 +241,7 @@ export class Order extends BaseEntity {
|
||||
// Total fields
|
||||
shipping_total: number
|
||||
discount_total: number
|
||||
raw_discount_total: number
|
||||
tax_total: number | null
|
||||
refunded_total: number
|
||||
total: number
|
||||
@@ -506,10 +507,14 @@ export class Order extends BaseEntity {
|
||||
* type: integer
|
||||
* description: The total of shipping
|
||||
* example: 1000
|
||||
* discount_total:
|
||||
* raw_discount_total:
|
||||
* description: The total of discount
|
||||
* type: integer
|
||||
* example: 800
|
||||
* discount_total:
|
||||
* description: The total of discount rounded
|
||||
* type: integer
|
||||
* example: 800
|
||||
* tax_total:
|
||||
* description: The total of tax
|
||||
* type: integer
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
|
||||
|
||||
import { Image } from "../models"
|
||||
import { dataSource } from "../loaders/database"
|
||||
import { In } from "typeorm"
|
||||
import { dataSource } from "../loaders/database"
|
||||
import { Image } from "../models"
|
||||
|
||||
export const ImageRepository = dataSource.getRepository(Image).extend({
|
||||
async insertBulk(data: QueryDeepPartialEntity<Image>[]): Promise<Image[]> {
|
||||
|
||||
@@ -8,12 +8,12 @@ import {
|
||||
WhereExpressionBuilder,
|
||||
} from "typeorm"
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
|
||||
import { dataSource } from "../loaders/database"
|
||||
import { MoneyAmount } from "../models"
|
||||
import {
|
||||
PriceListPriceCreateInput,
|
||||
PriceListPriceUpdateInput,
|
||||
} from "../types/price-list"
|
||||
import { dataSource } from "../loaders/database"
|
||||
import { ProductVariantPrice } from "../types/product-variant"
|
||||
import { isString } from "../utils"
|
||||
|
||||
|
||||
@@ -19,4 +19,5 @@ export const StagedJobRepository = dataSource.getRepository(StagedJob).extend({
|
||||
return rawStagedJobs.generatedMaps.map((d) => this.create(d))
|
||||
},
|
||||
})
|
||||
|
||||
export default StagedJobRepository
|
||||
|
||||
@@ -737,8 +737,9 @@ describe("DiscountService", () => {
|
||||
}
|
||||
)
|
||||
|
||||
expect(adjustment1).toBe(291)
|
||||
expect(adjustment2).toBe(109)
|
||||
// The sum of both is equal to the expected 400
|
||||
expect(adjustment1).toBeCloseTo(291, 0)
|
||||
expect(adjustment2).toBeCloseTo(109, 0)
|
||||
})
|
||||
|
||||
it("returns line item amount if discount exceeds lime item price", async () => {
|
||||
|
||||
@@ -1349,6 +1349,7 @@ describe("OrderService", () => {
|
||||
id: IdMap.getId("order"),
|
||||
items: [],
|
||||
paid_total: 0,
|
||||
raw_discount_total: 0,
|
||||
refundable_amount: 0,
|
||||
refunded_total: 0,
|
||||
shipping_methods: [
|
||||
@@ -1388,6 +1389,7 @@ describe("OrderService", () => {
|
||||
id: IdMap.getId("order"),
|
||||
items: [],
|
||||
paid_total: 0,
|
||||
raw_discount_total: 0,
|
||||
refundable_amount: 0,
|
||||
refunded_total: 0,
|
||||
shipping_methods: [
|
||||
|
||||
@@ -2600,7 +2600,7 @@ class CartService extends TransactionBaseService {
|
||||
const itemWithTotals = Object.assign(item, itemsTotals[item.id] ?? {})
|
||||
|
||||
cart.subtotal! += itemWithTotals.subtotal ?? 0
|
||||
cart.discount_total! += itemWithTotals.discount_total ?? 0
|
||||
cart.discount_total! += itemWithTotals.raw_discount_total ?? 0
|
||||
cart.item_tax_total! += itemWithTotals.tax_total ?? 0
|
||||
|
||||
return itemWithTotals
|
||||
@@ -2627,11 +2627,15 @@ class CartService extends TransactionBaseService {
|
||||
giftCards: cart.gift_cards,
|
||||
}
|
||||
)
|
||||
|
||||
cart.gift_card_total = giftCardTotal.total || 0
|
||||
cart.gift_card_tax_total = giftCardTotal.tax_total || 0
|
||||
|
||||
cart.tax_total = cart.item_tax_total + cart.shipping_tax_total
|
||||
|
||||
cart.raw_discount_total = cart.discount_total
|
||||
cart.discount_total = Math.round(cart.discount_total)
|
||||
|
||||
cart.total =
|
||||
cart.subtotal +
|
||||
cart.shipping_total +
|
||||
|
||||
@@ -619,7 +619,10 @@ class DiscountService extends TransactionBaseService {
|
||||
})
|
||||
|
||||
let fullItemPrice = lineItem.unit_price * lineItem.quantity
|
||||
const includesTax = this.featureFlagRouter_.isFeatureEnabled(TaxInclusivePricingFeatureFlag.key) && lineItem.includes_tax
|
||||
const includesTax =
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
TaxInclusivePricingFeatureFlag.key
|
||||
) && lineItem.includes_tax
|
||||
|
||||
if (includesTax) {
|
||||
const lineItemTotals = await this.newTotalsService_
|
||||
@@ -656,14 +659,13 @@ class DiscountService extends TransactionBaseService {
|
||||
}, 0)
|
||||
const nominator = Math.min(value, subtotal)
|
||||
const totalItemPercentage = fullItemPrice / subtotal
|
||||
adjustment = Math.round(nominator * totalItemPercentage)
|
||||
|
||||
adjustment = nominator * totalItemPercentage
|
||||
} else {
|
||||
adjustment = value * lineItem.quantity
|
||||
}
|
||||
|
||||
// if the amount of the discount exceeds the total price of the item,
|
||||
// we return the total item price, else the fixed amount
|
||||
return adjustment >= fullItemPrice ? fullItemPrice : adjustment
|
||||
return Math.min(adjustment, fullItemPrice)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ type LineItemTotals = {
|
||||
original_tax_total: number
|
||||
tax_lines: LineItemTaxLine[]
|
||||
discount_total: number
|
||||
|
||||
raw_discount_total: number
|
||||
}
|
||||
|
||||
type GiftCardTransaction = {
|
||||
@@ -166,7 +168,8 @@ export default class NewTotalsService extends TransactionBaseService {
|
||||
subtotal = 0 // in that case we need to know the tax rate to compute it later
|
||||
}
|
||||
|
||||
const discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const raw_discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const discount_total = Math.round(raw_discount_total)
|
||||
|
||||
const totals: LineItemTotals = {
|
||||
unit_price: item.unit_price,
|
||||
@@ -178,6 +181,8 @@ export default class NewTotalsService extends TransactionBaseService {
|
||||
original_tax_total: 0,
|
||||
tax_total: 0,
|
||||
tax_lines: item.tax_lines ?? [],
|
||||
|
||||
raw_discount_total: raw_discount_total,
|
||||
}
|
||||
|
||||
if (includeTax) {
|
||||
@@ -268,7 +273,8 @@ export default class NewTotalsService extends TransactionBaseService {
|
||||
subtotal = 0 // in that case we need to know the tax rate to compute it later
|
||||
}
|
||||
|
||||
const discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const raw_discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const discount_total = Math.round(raw_discount_total)
|
||||
|
||||
const totals: LineItemTotals = {
|
||||
unit_price: item.unit_price,
|
||||
@@ -280,6 +286,8 @@ export default class NewTotalsService extends TransactionBaseService {
|
||||
original_tax_total: 0,
|
||||
tax_total: 0,
|
||||
tax_lines: [],
|
||||
|
||||
raw_discount_total,
|
||||
}
|
||||
|
||||
taxRate = taxRate / 100
|
||||
|
||||
@@ -1842,6 +1842,7 @@ class OrderService extends TransactionBaseService {
|
||||
order.paid_total =
|
||||
order.payments?.reduce((acc, next) => (acc += next.amount), 0) || 0
|
||||
order.refundable_amount = order.paid_total - order.refunded_total || 0
|
||||
|
||||
let item_tax_total = 0
|
||||
let shipping_tax_total = 0
|
||||
|
||||
@@ -1855,7 +1856,7 @@ class OrderService extends TransactionBaseService {
|
||||
Object.assign(item, itemsTotals[item.id] ?? {}, { refundable })
|
||||
|
||||
order.subtotal += item.subtotal ?? 0
|
||||
order.discount_total += item.discount_total ?? 0
|
||||
order.discount_total += item.raw_discount_total ?? 0
|
||||
item_tax_total += item.tax_total ?? 0
|
||||
|
||||
if (isReturnableItem(item)) {
|
||||
@@ -1929,6 +1930,9 @@ class OrderService extends TransactionBaseService {
|
||||
})
|
||||
}
|
||||
|
||||
order.raw_discount_total = order.discount_total
|
||||
order.discount_total = Math.round(order.discount_total)
|
||||
|
||||
order.total =
|
||||
order.subtotal +
|
||||
order.shipping_total +
|
||||
|
||||
@@ -47,13 +47,13 @@ import {
|
||||
setMetadata,
|
||||
} from "../utils"
|
||||
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
|
||||
import { CartRepository } from "../repositories/cart"
|
||||
import { MoneyAmountRepository } from "../repositories/money-amount"
|
||||
import { ProductRepository } from "../repositories/product"
|
||||
import { ProductOptionValueRepository } from "../repositories/product-option-value"
|
||||
import EventBusService from "./event-bus"
|
||||
import RegionService from "./region"
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
|
||||
|
||||
class ProductVariantService extends TransactionBaseService {
|
||||
static Events = {
|
||||
|
||||
@@ -473,9 +473,6 @@ class ProductService extends TransactionBaseService {
|
||||
): Promise<Product> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.withRepository(this.productRepository_)
|
||||
const productVariantRepo = manager.withRepository(
|
||||
this.productVariantRepository_
|
||||
)
|
||||
const productTagRepo = manager.withRepository(this.productTagRepository_)
|
||||
const productTypeRepo = manager.withRepository(
|
||||
this.productTypeRepository_
|
||||
|
||||
@@ -58,6 +58,8 @@ type LineItemTotals = {
|
||||
original_tax_total: number
|
||||
tax_lines: LineItemTaxLine[]
|
||||
discount_total: number
|
||||
|
||||
raw_discount_total: number
|
||||
}
|
||||
|
||||
type LineItemTotalsOptions = {
|
||||
@@ -460,7 +462,7 @@ class TotalsService extends TransactionBaseService {
|
||||
/**
|
||||
* Used for the refund computation
|
||||
*/
|
||||
unit_amount: Math.round(adjustmentAmount / ld.item.quantity),
|
||||
unit_amount: adjustmentAmount / ld.item.quantity,
|
||||
}
|
||||
} else {
|
||||
allocationMap[ld.item.id] = {
|
||||
@@ -795,7 +797,8 @@ class TotalsService extends TransactionBaseService {
|
||||
subtotal = 0 // in that case we need to know the tax rate to compute it later
|
||||
}
|
||||
|
||||
const discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const raw_discount_total = lineItemAllocation.discount?.amount ?? 0
|
||||
const discount_total = Math.round(raw_discount_total)
|
||||
|
||||
const lineItemTotals: LineItemTotals = {
|
||||
unit_price: lineItem.unit_price,
|
||||
@@ -807,6 +810,8 @@ class TotalsService extends TransactionBaseService {
|
||||
original_tax_total: 0,
|
||||
tax_total: 0,
|
||||
tax_lines: lineItem.tax_lines || [],
|
||||
|
||||
raw_discount_total,
|
||||
}
|
||||
|
||||
// Tax Information
|
||||
@@ -1002,7 +1007,9 @@ class TotalsService extends TransactionBaseService {
|
||||
excludeNonDiscounts: true,
|
||||
})
|
||||
|
||||
const discountTotal = this.getLineItemAdjustmentsTotal(cartOrOrder)
|
||||
const discountTotal = Math.round(
|
||||
this.getLineItemAdjustmentsTotal(cartOrOrder)
|
||||
)
|
||||
|
||||
if (subtotal < 0) {
|
||||
return this.rounded(Math.max(subtotal, discountTotal))
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { omitDeep } from "../omit-deep"
|
||||
|
||||
describe("omitDeep", () => {
|
||||
it("should omit properties in a nested object", () => {
|
||||
const input = {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
address: {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
},
|
||||
variants: [
|
||||
20,
|
||||
{
|
||||
id: 22,
|
||||
title: "hello world",
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
variantOption: {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
test: null,
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const output = {
|
||||
id: 1,
|
||||
address: {
|
||||
id: 1,
|
||||
},
|
||||
variants: [
|
||||
20,
|
||||
{
|
||||
id: 22,
|
||||
title: "hello world",
|
||||
variantOption: {
|
||||
id: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
test: null,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
expect(
|
||||
omitDeep(input, ["createdAt", "updatedAt", "__typename"])
|
||||
).toEqual(output)
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,11 @@
|
||||
import { pick } from "lodash"
|
||||
import { omitDeep } from "./omit-deep"
|
||||
|
||||
// TODO: once the legacy totals decoration will be removed.
|
||||
// We will be able to only compute the totals if one of the total fields is present
|
||||
// and therefore avoid totals computation if the user don't want them to appear in the response
|
||||
// and therefore the below const will be removed
|
||||
const EXCLUDED_FIELDS = [
|
||||
const INCLUDED_FIELDS = [
|
||||
"shipping_total",
|
||||
"discount_total",
|
||||
"tax_total",
|
||||
@@ -22,6 +23,8 @@ const EXCLUDED_FIELDS = [
|
||||
"original_tax_total",
|
||||
]
|
||||
|
||||
const EXCLUDED_FIELDS = ["raw_discount_total"]
|
||||
|
||||
/**
|
||||
* Filter response data to contain props specified in the `allowedProperties`.
|
||||
* You can read more in the transformQuery middleware utility methods.
|
||||
@@ -33,17 +36,25 @@ function cleanResponseData<T extends unknown | unknown[]>(
|
||||
data: T,
|
||||
fields: string[]
|
||||
): T extends [] ? Partial<T>[] : Partial<T> {
|
||||
if (!fields.length) {
|
||||
return data as T extends [] ? Partial<T>[] : Partial<T>
|
||||
}
|
||||
fields = fields ?? []
|
||||
|
||||
const isDataArray = Array.isArray(data)
|
||||
const fieldsSet = new Set([...fields, ...EXCLUDED_FIELDS])
|
||||
let arrayData: Partial<T>[] = isDataArray ? data : [data]
|
||||
|
||||
if (!fields.length) {
|
||||
arrayData = arrayData.map((record) => omitDeep(record, EXCLUDED_FIELDS))
|
||||
return (isDataArray ? arrayData : arrayData[0]) as T extends []
|
||||
? Partial<T>[]
|
||||
: Partial<T>
|
||||
}
|
||||
|
||||
const fieldsSet = new Set([...fields, ...INCLUDED_FIELDS])
|
||||
|
||||
fields = [...fieldsSet]
|
||||
|
||||
let arrayData: Partial<T>[] = isDataArray ? data : [data]
|
||||
arrayData = arrayData.map((record) => pick(record, fields))
|
||||
arrayData = arrayData.map((record) =>
|
||||
pick(omitDeep(record, EXCLUDED_FIELDS), fields)
|
||||
)
|
||||
|
||||
return (isDataArray ? arrayData : arrayData[0]) as T extends []
|
||||
? Partial<T>[]
|
||||
|
||||
@@ -4,12 +4,12 @@ export * from "./csv-cell-content-formatter"
|
||||
export * from "./db-aware-column"
|
||||
export * from "./exception-formatter"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./has-changes"
|
||||
export * from "./is-date"
|
||||
export * from "./is-object"
|
||||
export * from "./is-string"
|
||||
export * from "./omit-deep"
|
||||
export * from "./product-category"
|
||||
export * from "./remove-undefined-properties"
|
||||
export * from "./set-metadata"
|
||||
export * from "./validate-id"
|
||||
export * from "./is-object"
|
||||
export * from "./has-changes"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export function isObject(obj: unknown): obj is object {
|
||||
return typeof obj === "object" && !!obj
|
||||
export function isObject(obj: any): obj is object {
|
||||
return obj != null && obj?.constructor?.name === "Object"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
export function omitDeep<T extends object = object>(
|
||||
input: object,
|
||||
excludes: Array<number | string>
|
||||
): T {
|
||||
if (!input) {
|
||||
return input
|
||||
}
|
||||
|
||||
return Object.entries(input).reduce((nextInput, [key, value]) => {
|
||||
const shouldExclude = excludes.includes(key)
|
||||
if (shouldExclude) {
|
||||
return nextInput
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
nextInput[key] = value.map((arrItem) => {
|
||||
if (isObject(arrItem)) {
|
||||
return omitDeep(arrItem, excludes)
|
||||
}
|
||||
return arrItem
|
||||
})
|
||||
return nextInput
|
||||
} else if (isObject(value)) {
|
||||
nextInput[key] = omitDeep(value, excludes)
|
||||
return nextInput
|
||||
}
|
||||
|
||||
nextInput[key] = value
|
||||
|
||||
return nextInput
|
||||
}, {} as T)
|
||||
}
|
||||
Reference in New Issue
Block a user