Skip to content

Commit

Permalink
chore: shipping subtotal
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Sep 5, 2022
1 parent 17c2d67 commit f0e4fa2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 87 deletions.
47 changes: 32 additions & 15 deletions packages/medusa/src/services/__tests__/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { IdMap } from "medusa-test-utils"
import TotalsService from "../totals"
import { FlagRouter } from "../../utils/flag-router"

import * as CalculatePriceTaxAmount from "../../utils/calculate-price-tax-amount"

import TaxInclusivePricingFeatureFlag from "../../loaders/feature-flags/tax-inclusive-pricing"
import { calculatePriceTaxAmount } from "../../utils"

Expand Down Expand Up @@ -711,7 +709,26 @@ describe("TotalsService", () => {

describe("getShippingTotal", () => {
let res
const totalsService = new TotalsService(container)

const getTaxLinesMock = jest.fn(() =>
Promise.resolve([
{ shipping_method_id: IdMap.getId("expensiveShipping") },
])
)
const calculateMock = jest.fn(() => Promise.resolve(20))

const totalsService = new TotalsService({
...container,
taxProviderService: {
withTransaction: function() {
return this
},
getTaxLines: getTaxLinesMock,
},
taxCalculationStrategy: {
calculate: calculateMock,
},
})

beforeEach(() => {
jest.clearAllMocks()
Expand All @@ -721,7 +738,7 @@ describe("TotalsService", () => {
const order = {
shipping_methods: [
{
_id: IdMap.getId("expensiveShipping"),
id: IdMap.getId("expensiveShipping"),
name: "Expensive Shipping",
price: 100,
provider_id: "default_provider",
Expand All @@ -732,7 +749,7 @@ describe("TotalsService", () => {
},
],
}
res = totalsService.getShippingTotal(order)
res = await totalsService.getShippingTotal(order)

expect(res).toEqual(100)
})
Expand Down Expand Up @@ -994,8 +1011,12 @@ describe("TotalsService", () => {
})

describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] getShippingTotal ", () => {
const calculateMock = jest.fn(() => Promise.resolve(20))
const totalsService = new TotalsService({
...container,
taxCalculationStrategy: {
calculate: calculateMock,
},
featureFlagRouter: new FlagRouter({
[TaxInclusivePricingFeatureFlag.key]: true,
}),
Expand All @@ -1007,9 +1028,7 @@ describe("TotalsService", () => {

it("calculates total", async () => {
const order = {
region: {
tax_rate: 25,
},
tax_rate: null,
items: [
{
unit_price: 20,
Expand All @@ -1023,10 +1042,13 @@ describe("TotalsService", () => {
],
shipping_methods: [
{
_id: IdMap.getId("expensiveShipping"),
id: IdMap.getId("expensiveShipping"),
name: "Expensive Shipping",
price: 120,
includes_tax: true,
tax_lines: [
{ shipping_method_id: IdMap.getId("expensiveShipping") },
],
provider_id: "default_provider",
profile_id: IdMap.getId("default"),
data: {
Expand All @@ -1036,14 +1058,9 @@ describe("TotalsService", () => {
],
}

const calculatePriceTaxAmountMock = jest
.spyOn(CalculatePriceTaxAmount, "calculatePriceTaxAmount")
.mockReturnValue(20)

const res = totalsService.getShippingTotal(order)
const res = await totalsService.getShippingTotal(order)

expect(res).toEqual(100)
expect(calculatePriceTaxAmountMock).toHaveBeenCalledTimes(1)
})
})
})
14 changes: 9 additions & 5 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ class CartService extends TransactionBaseService {
break
}
case "shipping_total": {
totals.shipping_total = this.totalsService_.getShippingTotal(cart)
totals.shipping_total = await this.totalsService_.getShippingTotal(
cart
)
break
}
case "discount_total":
Expand Down Expand Up @@ -1749,8 +1751,9 @@ class CartService extends TransactionBaseService {

const methods = [newShippingMethod]
if (shipping_methods?.length) {
const shippingOptionServiceTx =
this.shippingOptionService_.withTransaction(transactionManager)
const shippingOptionServiceTx = this.shippingOptionService_.withTransaction(
transactionManager
)

for (const shippingMethod of shipping_methods) {
if (
Expand All @@ -1767,8 +1770,9 @@ class CartService extends TransactionBaseService {
}

if (cart.items?.length) {
const lineItemServiceTx =
this.lineItemService_.withTransaction(transactionManager)
const lineItemServiceTx = this.lineItemService_.withTransaction(
transactionManager
)

await Promise.all(
cart.items.map(async (item) => {
Expand Down
50 changes: 31 additions & 19 deletions packages/medusa/src/services/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ class OrderService extends TransactionBaseService {
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)
const query = buildQuery(selector, config)

const { select, relations, totalsToSelect } =
this.transformQueryForTotals(config)
const { select, relations, totalsToSelect } = this.transformQueryForTotals(
config
)

if (select && select.length) {
query.select = select
Expand Down Expand Up @@ -233,8 +234,9 @@ class OrderService extends TransactionBaseService {
}
}

const { select, relations, totalsToSelect } =
this.transformQueryForTotals(config)
const { select, relations, totalsToSelect } = this.transformQueryForTotals(
config
)

if (select && select.length) {
query.select = select
Expand All @@ -252,7 +254,9 @@ class OrderService extends TransactionBaseService {
return [orders, count]
}

protected transformQueryForTotals(config: FindConfig<Order>): {
protected transformQueryForTotals(
config: FindConfig<Order>
): {
relations: string[] | undefined
select: FindConfig<Order>["select"]
totalsToSelect: FindConfig<Order>["select"]
Expand Down Expand Up @@ -333,8 +337,9 @@ class OrderService extends TransactionBaseService {
): Promise<Order> {
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)

const { select, relations, totalsToSelect } =
this.transformQueryForTotals(config)
const { select, relations, totalsToSelect } = this.transformQueryForTotals(
config
)

const query = {
where: { id: orderId },
Expand Down Expand Up @@ -373,8 +378,9 @@ class OrderService extends TransactionBaseService {
): Promise<Order> {
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)

const { select, relations, totalsToSelect } =
this.transformQueryForTotals(config)
const { select, relations, totalsToSelect } = this.transformQueryForTotals(
config
)

const query = {
where: { cart_id: cartId },
Expand Down Expand Up @@ -412,8 +418,9 @@ class OrderService extends TransactionBaseService {
): Promise<Order> {
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)

const { select, relations, totalsToSelect } =
this.transformQueryForTotals(config)
const { select, relations, totalsToSelect } = this.transformQueryForTotals(
config
)

const query = {
where: { external_id: externalId },
Expand Down Expand Up @@ -851,8 +858,9 @@ class OrderService extends TransactionBaseService {
.withTransaction(manager)
.createShippingMethod(optionId, data ?? {}, { order, ...config })

const shippingOptionServiceTx =
this.shippingOptionService_.withTransaction(manager)
const shippingOptionServiceTx = this.shippingOptionService_.withTransaction(
manager
)

const methods = [newMethod]
if (shipping_methods.length) {
Expand Down Expand Up @@ -1023,8 +1031,9 @@ class OrderService extends TransactionBaseService {
await inventoryServiceTx.adjustInventory(item.variant_id, item.quantity)
}

const paymentProviderServiceTx =
this.paymentProviderService_.withTransaction(manager)
const paymentProviderServiceTx = this.paymentProviderService_.withTransaction(
manager
)
for (const p of order.payments) {
await paymentProviderServiceTx.cancelPayment(p)
}
Expand Down Expand Up @@ -1064,8 +1073,9 @@ class OrderService extends TransactionBaseService {
)
}

const paymentProviderServiceTx =
this.paymentProviderService_.withTransaction(manager)
const paymentProviderServiceTx = this.paymentProviderService_.withTransaction(
manager
)

const payments: Payment[] = []
for (const p of order.payments) {
Expand Down Expand Up @@ -1218,7 +1228,7 @@ class OrderService extends TransactionBaseService {
const fulfillments = await this.fulfillmentService_
.withTransaction(manager)
.createFulfillment(
order as unknown as CreateFulfillmentOrder,
(order as unknown) as CreateFulfillmentOrder,
itemsToFulfill,
{
metadata,
Expand Down Expand Up @@ -1429,7 +1439,9 @@ class OrderService extends TransactionBaseService {
for (const totalField of totalsFields) {
switch (totalField) {
case "shipping_total": {
order.shipping_total = this.totalsService_.getShippingTotal(order)
order.shipping_total = await this.totalsService_.getShippingTotal(
order
)
break
}
case "gift_card_total": {
Expand Down
Loading

0 comments on commit f0e4fa2

Please sign in to comment.