chore(order): preview order change (#8025)

What:
 - new method `previewOrderChange`
   - Calculate all the actions related to an order change.
   - Return the preview of the final Order, with all the calculated values.
   - Associate actions with items and shipping_methods they modified.

FIXES: CORE-2509
This commit is contained in:
Carlos R. L. Rodrigues
2024-07-09 11:45:55 -03:00
committed by GitHub
parent 4736d9e2dd
commit 2b2e2fbb3d
25 changed files with 373 additions and 57 deletions

View File

@@ -92,6 +92,9 @@ describe("Order Exchange - Actions", function () {
const changes = calculateOrderChange({
order: originalOrder,
actions: actions,
options: {
addActionReferenceToObject: true,
},
})
const sumToJSON = JSON.parse(JSON.stringify(changes.summary))
@@ -151,11 +154,65 @@ describe("Order Exchange - Actions", function () {
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
actions: [
{
action: "RETURN_ITEM",
reference_id: "return_123",
details: {
reference_id: "3",
quantity: 1,
},
amount: "20",
},
],
},
{
id: "item_555",
unit_price: 50,
quantity: 1,
detail: {},
actions: [
{
action: "ITEM_ADD",
details: {
reference_id: "item_555",
unit_price: 50,
quantity: 1,
},
amount: "50",
},
],
},
])
expect(changes.order.shipping_methods).toEqual([
{
id: "ship_123",
price: 0,
},
{
id: "shipping_345",
price: 5,
detail: {},
actions: [
{
action: "SHIPPING_ADD",
reference_id: "shipping_345",
amount: 5,
},
],
},
{
id: "return_shipping_345",
price: 7.5,
detail: {},
actions: [
{
action: "SHIPPING_ADD",
reference_id: "return_shipping_345",
amount: 7.5,
},
],
},
])
})

View File

@@ -174,6 +174,7 @@ async function processAdditionalItems(
reference_id: item.id,
claim_id: claimReference.id,
quantity: addedItem.quantity,
unit_price: item.unit_price,
metadata: addedItem.metadata,
},
})

View File

@@ -129,7 +129,9 @@ async function processAdditionalItems(
createItems.forEach((item, index) => {
const addedItem = itemsToAdd[index]
additionalNewItems[index].item_id = item.id
actions.push({
action: ChangeActionType.ITEM_ADD,
exchange_id: exchangeReference.id,
@@ -140,6 +142,7 @@ async function processAdditionalItems(
reference_id: item.id,
exchange_id: exchangeReference.id,
quantity: addedItem.quantity,
unit_price: item.unit_price,
metadata: addedItem.metadata,
},
})

View File

@@ -16,6 +16,7 @@ import {
import {
BigNumber,
createRawPropertiesFromBigNumber,
DecorateCartLikeInputDTO,
decorateCartTotals,
deduplicate,
InjectManager,
@@ -307,6 +308,14 @@ export default class OrderModuleService<
const order = await super.retrieveOrder(id, config, sharedContext)
const orderChange = await this.getActiveOrderChange_(
order.id,
false,
sharedContext
)
order.order_change = orderChange
return formatOrder(order, {
entity: Order,
includeTotals,
@@ -1795,6 +1804,58 @@ export default class OrderModuleService<
return await this.orderChangeService_.create(input, sharedContext)
}
async previewOrderChange(orderChangeId: string, sharedContext?: Context) {
const orderChange = await super.retrieveOrderChange(
orderChangeId,
{ relations: ["actions"] },
sharedContext
)
orderChange.actions = orderChange.actions.map((action) => {
return {
...action,
version: orderChange.version,
order_id: orderChange.order_id,
return_id: orderChange.return_id,
claim_id: orderChange.claim_id,
exchange_id: orderChange.exchange_id,
}
})
const order = await this.retrieveOrder(
orderChange.order_id,
{
select: ["id", "version", "items.detail", "summary", "total"],
relations: [
"transactions",
"items",
"items.detail",
"shipping_methods",
],
},
sharedContext
)
const calculated = calculateOrderChange({
order: order as any,
actions: orderChange.actions,
transactions: order.transactions ?? [],
options: {
addActionReferenceToObject: true,
},
})
createRawPropertiesFromBigNumber(calculated)
const calcOrder = calculated.order as any
decorateCartTotals(calcOrder as DecorateCartLikeInputDTO)
calcOrder.summary = calculated.summary
createRawPropertiesFromBigNumber(calcOrder)
return calcOrder
}
async cancelOrderChange(
orderId: string,
sharedContext?: Context
@@ -2123,6 +2184,50 @@ export default class OrderModuleService<
)
}
private async getActiveOrderChange_(
orderId: string,
includeActions: boolean,
sharedContext?: Context
): Promise<any> {
const options = {
select: [
"id",
"change_type",
"order_id",
"return_id",
"claim_id",
"exchange_id",
"version",
"requested_at",
"requested_by",
"status",
],
relations: [] as string[],
order: {},
}
if (includeActions) {
options.select.push("actions")
options.relations.push("actions")
options.order = {
actions: {
ordering: "ASC",
},
}
}
const [orderChange] = await this.listOrderChanges(
{
order_id: orderId,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
options,
sharedContext
)
return orderChange
}
private async getAndValidateOrderChange_(
orderChangeIds: string[],
includeActions: boolean,