feat(medusa): Decorate OrderEdit LineItems with totals (#3108)

This commit is contained in:
Frane Polić
2023-02-06 17:32:26 +01:00
committed by GitHub
parent e22a383f47
commit 4d6e63d68f
10 changed files with 291 additions and 132 deletions
@@ -2,7 +2,6 @@ import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { orderEditServiceMock } from "../../../../../services/__mocks__/order-edit"
describe("GET /admin/order-edits/:id", () => {
describe("successfully requests an order edit confirmation", () => {
const orderEditId = IdMap.getId("testRequestOrder")
@@ -35,8 +34,6 @@ describe("GET /admin/order-edits/:id", () => {
orderEditId,
{ requestedBy: IdMap.getId("admin_user") }
)
expect(orderEditServiceMock.update).toHaveBeenCalledTimes(1)
})
it("returns updated orderEdit", () => {
@@ -84,7 +84,7 @@ export default async (req, res) => {
requestedBy: loggedInUser,
})
const total = await orderEditServiceTx.getTotals(orderEdit.id)
const total = await orderEditServiceTx.decorateTotals(orderEdit)
if (total.difference_due > 0) {
const order = await orderService
@@ -79,20 +79,25 @@ export default async (req: Request, res: Response) => {
const manager: EntityManager = req.scope.resolve("manager")
await manager.transaction(async (transactionManager) => {
await orderEditService
.withTransaction(transactionManager)
.updateLineItem(id, item_id, validatedBody)
})
const decoratedEdit = await manager.transaction(
async (transactionManager) => {
const orderEditTx = orderEditService.withTransaction(transactionManager)
let orderEdit = await orderEditService.retrieve(id, {
select: defaultOrderEditFields,
relations: defaultOrderEditRelations,
})
orderEdit = await orderEditService.decorateTotals(orderEdit)
await orderEditTx.updateLineItem(id, item_id, validatedBody)
const orderEdit = await orderEditTx.retrieve(id, {
select: defaultOrderEditFields,
relations: defaultOrderEditRelations,
})
await orderEditTx.decorateTotals(orderEdit)
return orderEdit
}
)
res.status(200).send({
order_edit: orderEdit,
order_edit: decoratedEdit,
})
}
@@ -117,18 +117,6 @@ export const orderEditServiceMock = {
declined_at: new Date(),
})
}),
getTotals: jest.fn().mockImplementation((id) => {
return Promise.resolve({
shipping_total: 10,
gift_card_total: 0,
gift_card_tax_total: 0,
discount_total: 0,
tax_total: 1,
subtotal: 2000,
difference_due: 1000,
total: 1000,
})
}),
delete: jest.fn().mockImplementation((_) => {
return Promise.resolve()
}),
@@ -3,11 +3,12 @@ import { OrderEditItemChangeType, OrderEditStatus } from "../../models"
import {
EventBusService,
LineItemService,
NewTotalsService,
OrderEditItemChangeService,
OrderEditService,
OrderService,
TaxProviderService,
TotalsService
TotalsService,
} from "../index"
import LineItemAdjustmentService from "../line-item-adjustment"
import { EventBusServiceMock } from "../__mocks__/event-bus"
@@ -17,6 +18,7 @@ import { OrderServiceMock } from "../__mocks__/order"
import { orderEditItemChangeServiceMock } from "../__mocks__/order-edit-item-change"
import { taxProviderServiceMock } from "../__mocks__/tax-provider"
import { TotalsServiceMock } from "../__mocks__/totals"
import NewTotalsServiceMock from "../__mocks__/new-totals"
const orderEditToUpdate = {
id: IdMap.getId("order-edit-to-update"),
@@ -188,6 +190,7 @@ describe("OrderEditService", () => {
orderService: OrderServiceMock as unknown as OrderService,
eventBusService: EventBusServiceMock as unknown as EventBusService,
totalsService: TotalsServiceMock as unknown as TotalsService,
newTotalsService: NewTotalsServiceMock as unknown as NewTotalsService,
lineItemService: lineItemServiceMock as unknown as LineItemService,
orderEditItemChangeService:
orderEditItemChangeServiceMock as unknown as OrderEditItemChangeService,
@@ -330,7 +333,7 @@ describe("OrderEditService", () => {
let result
beforeEach(async () => {
jest.spyOn(orderEditService, "getTotals").mockResolvedValue({
jest.spyOn(orderEditService, "decorateTotals").mockResolvedValue({
difference_due: 1500,
} as any)
+54 -85
View File
@@ -7,23 +7,24 @@ import {
Order,
OrderEdit,
OrderEditItemChangeType,
OrderEditStatus
OrderEditStatus,
} from "../models"
import { OrderEditRepository } from "../repositories/order-edit"
import { FindConfig, Selector } from "../types/common"
import {
AddOrderEditLineItemInput,
CreateOrderEditInput
CreateOrderEditInput,
} from "../types/order-edit"
import { buildQuery, isString } from "../utils"
import {
EventBusService,
LineItemAdjustmentService,
LineItemService,
NewTotalsService,
OrderEditItemChangeService,
OrderService,
TaxProviderService,
TotalsService
TotalsService,
} from "./index"
type InjectedDependencies = {
@@ -32,6 +33,7 @@ type InjectedDependencies = {
orderService: OrderService
totalsService: TotalsService
newTotalsService: NewTotalsService
lineItemService: LineItemService
eventBusService: EventBusService
taxProviderService: TaxProviderService
@@ -56,6 +58,7 @@ export default class OrderEditService extends TransactionBaseService {
protected readonly orderService_: OrderService
protected readonly totalsService_: TotalsService
protected readonly newTotalsService_: NewTotalsService
protected readonly lineItemService_: LineItemService
protected readonly eventBusService_: EventBusService
protected readonly taxProviderService_: TaxProviderService
@@ -69,6 +72,7 @@ export default class OrderEditService extends TransactionBaseService {
lineItemService,
eventBusService,
totalsService,
newTotalsService,
orderEditItemChangeService,
lineItemAdjustmentService,
taxProviderService,
@@ -82,6 +86,7 @@ export default class OrderEditService extends TransactionBaseService {
this.lineItemService_ = lineItemService
this.eventBusService_ = eventBusService
this.totalsService_ = totalsService
this.newTotalsService_ = newTotalsService
this.orderEditItemChangeService_ = orderEditItemChangeService
this.lineItemAdjustmentService_ = lineItemAdjustmentService
this.taxProviderService_ = taxProviderService
@@ -148,69 +153,6 @@ export default class OrderEditService extends TransactionBaseService {
return orderEdits
}
/**
* Compute and return the different totals from the order edit id
* @param orderEditId
*/
async getTotals(orderEditId: string): Promise<{
shipping_total: number
gift_card_total: number
gift_card_tax_total: number
discount_total: number
tax_total: number | null
subtotal: number
difference_due: number
total: number
}> {
const manager = this.transactionManager_ ?? this.manager_
const { order_id, items } = await this.retrieve(orderEditId, {
select: ["id", "order_id", "items"],
relations: ["items", "items.tax_lines", "items.adjustments"],
})
const order = await this.orderService_
.withTransaction(manager)
.retrieve(order_id, {
relations: [
"discounts",
"discounts.rule",
"gift_cards",
"region",
"items",
"items.tax_lines",
"items.adjustments",
"region.tax_rates",
"shipping_methods",
"shipping_methods.tax_lines",
],
})
const computedOrder = { ...order, items } as Order
const totalsServiceTx = this.totalsService_.withTransaction(manager)
const shipping_total = await totalsServiceTx.getShippingTotal(computedOrder)
const { total: gift_card_total, tax_total: gift_card_tax_total } =
await totalsServiceTx.getGiftCardTotal(computedOrder)
const discount_total = await totalsServiceTx.getDiscountTotal(computedOrder)
const tax_total = await totalsServiceTx.getTaxTotal(computedOrder)
const subtotal = await totalsServiceTx.getSubtotal(computedOrder)
const total = await totalsServiceTx.getTotal(computedOrder)
const orderTotal = await totalsServiceTx.getTotal(order)
const difference_due = total - orderTotal
return {
shipping_total,
gift_card_total,
gift_card_tax_total,
discount_total,
tax_total,
subtotal,
total,
difference_due,
}
}
async create(
data: CreateOrderEditInput,
context: { createdBy: string }
@@ -390,11 +332,11 @@ export default class OrderEditService extends TransactionBaseService {
)
}
const lineItem = await this.lineItemService_
.withTransaction(manager)
.retrieve(itemId, {
select: ["id", "order_edit_id", "original_item_id"],
})
const lineItemServiceTx = this.lineItemService_.withTransaction(manager)
const lineItem = await lineItemServiceTx.retrieve(itemId, {
select: ["id", "order_edit_id", "original_item_id"],
})
if (lineItem.order_edit_id !== orderEditId) {
throw new MedusaError(
@@ -427,11 +369,9 @@ export default class OrderEditService extends TransactionBaseService {
})
}
await this.lineItemService_
.withTransaction(manager)
.update(change.line_item_id!, {
quantity: data.quantity,
})
await lineItemServiceTx.update(change.line_item_id!, {
quantity: data.quantity,
})
await this.refreshAdjustments(orderEditId)
})
@@ -538,15 +478,44 @@ export default class OrderEditService extends TransactionBaseService {
}
async decorateTotals(orderEdit: OrderEdit): Promise<OrderEdit> {
const totals = await this.getTotals(orderEdit.id)
orderEdit.discount_total = totals.discount_total
orderEdit.gift_card_total = totals.gift_card_total
orderEdit.gift_card_tax_total = totals.gift_card_tax_total
orderEdit.shipping_total = totals.shipping_total
orderEdit.subtotal = totals.subtotal
orderEdit.tax_total = totals.tax_total
orderEdit.total = totals.total
orderEdit.difference_due = totals.difference_due
const manager = this.transactionManager_ ?? this.manager_
const { order_id, items } = await this.retrieve(orderEdit.id, {
select: ["id", "order_id", "items"],
relations: ["items", "items.tax_lines", "items.adjustments"],
})
const orderServiceTx = this.orderService_.withTransaction(manager)
const order = await orderServiceTx.retrieve(order_id, {
relations: [
"discounts",
"discounts.rule",
"gift_cards",
"region",
"items",
"items.tax_lines",
"items.adjustments",
"region.tax_rates",
"shipping_methods",
"shipping_methods.tax_lines",
],
})
const computedOrder = { ...order, items } as Order
await Promise.all([
await orderServiceTx.decorateTotals(computedOrder),
await orderServiceTx.decorateTotals(order),
])
orderEdit.items = computedOrder.items
orderEdit.discount_total = computedOrder.discount_total
orderEdit.gift_card_total = computedOrder.gift_card_total
orderEdit.gift_card_tax_total = computedOrder.gift_card_tax_total
orderEdit.shipping_total = computedOrder.shipping_total
orderEdit.subtotal = computedOrder.subtotal
orderEdit.tax_total = computedOrder.tax_total
orderEdit.total = computedOrder.total
orderEdit.difference_due = computedOrder.total - order.total
return orderEdit
}
+1 -1
View File
@@ -129,7 +129,7 @@ class TotalsService extends TransactionBaseService {
}
/**
* Calculates subtotal of a given cart or order.
* Calculates total of a given cart or order.
* @param cartOrOrder - object to calculate total for
* @param options - options to calculate by
* @return the calculated subtotal