fix: make packages/medusa/src/services/totals.js pass eslint

This commit is contained in:
Ayush Jain committed 2021-10-14 17:57:54 +05:30
1 parent 62fda1043e
commit 23e83aff1c
2 files changed
+18 -18

No files matched your search

-1
View File
@@ -25,7 +25,6 @@
/packages/medusa/src/services/store.js /packages/medusa/src/services/store.js
/packages/medusa/src/services/swap.js /packages/medusa/src/services/swap.js
/packages/medusa/src/services/system-payment-provider.js /packages/medusa/src/services/system-payment-provider.js
/packages/medusa/src/services/totals.js
/packages/medusa/src/subscribers/notification.js /packages/medusa/src/subscribers/notification.js
/packages/medusa/src/subscribers/order.js /packages/medusa/src/subscribers/order.js
+18 -17
View File
@@ -4,7 +4,7 @@ import { MedusaError } from "medusa-core-utils"
/** /**
* A service that calculates total and subtotals for orders, carts etc.. * A service that calculates total and subtotals for orders, carts etc..
* @implements BaseService * @implements {BaseService}
*/ */
class TotalsService extends BaseService { class TotalsService extends BaseService {
constructor() { constructor() {
@@ -48,7 +48,8 @@ class TotalsService extends BaseService {
/** /**
* Calculates subtotal of a given cart or order. * Calculates subtotal of a given cart or order.
* @param {Cart || Order} object - cart or order to calculate subtotal for * @param {(Cart|Order)} object - cart or order to calculate subtotal for
* @param {Object} opts - options
* @return {int} the calculated subtotal * @return {int} the calculated subtotal
*/ */
getSubtotal(object, opts = {}) { getSubtotal(object, opts = {}) {
@@ -57,7 +58,7 @@ class TotalsService extends BaseService {
return subtotal return subtotal
} }
object.items.map(item => { object.items.map((item) => {
if (opts.excludeNonDiscounts) { if (opts.excludeNonDiscounts) {
if (item.allow_discounts) { if (item.allow_discounts) {
subtotal += item.unit_price * item.quantity subtotal += item.unit_price * item.quantity
@@ -128,7 +129,7 @@ class TotalsService extends BaseService {
const lineDiscounts = this.getLineDiscounts(object, discount) const lineDiscounts = this.getLineDiscounts(object, discount)
const discountedLine = lineDiscounts.find( const discountedLine = lineDiscounts.find(
line => line.item.id === lineItem.id (line) => line.item.id === lineItem.id
) )
const discountAmount = const discountAmount =
@@ -148,17 +149,17 @@ class TotalsService extends BaseService {
* @return {int} the calculated subtotal * @return {int} the calculated subtotal
*/ */
getRefundTotal(order, lineItems) { getRefundTotal(order, lineItems) {
let itemIds = order.items.map(i => i.id) let itemIds = order.items.map((i) => i.id)
// in case we swap a swap, we need to include swap items // in case we swap a swap, we need to include swap items
if (order.swaps && order.swaps.length) { if (order.swaps && order.swaps.length) {
for (const s of order.swaps) { for (const s of order.swaps) {
const swapItemIds = s.additional_items.map(el => el.id) const swapItemIds = s.additional_items.map((el) => el.id)
itemIds = [...itemIds, ...swapItemIds] itemIds = [...itemIds, ...swapItemIds]
} }
} }
const refunds = lineItems.map(i => { const refunds = lineItems.map((i) => {
if (!itemIds.includes(i.id)) { if (!itemIds.includes(i.id)) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
@@ -177,7 +178,7 @@ class TotalsService extends BaseService {
* @param {string} lineItem - id of line item * @param {string} lineItem - id of line item
* @param {string} variant - id of variant in line item * @param {string} variant - id of variant in line item
* @param {int} variantPrice - price of the variant based on region * @param {int} variantPrice - price of the variant based on region
* @param {int} valye - discount value * @param {int} value - discount value
* @param {string} discountType - the type of discount (fixed or percentage) * @param {string} discountType - the type of discount (fixed or percentage)
* @return {{ string, string, int }} triples of lineitem, variant and * @return {{ string, string, int }} triples of lineitem, variant and
* applied discount * applied discount
@@ -265,7 +266,7 @@ class TotalsService extends BaseService {
percentage = nominator / subtotal percentage = nominator / subtotal
} }
return merged.map(item => { return merged.map((item) => {
const lineTotal = item.unit_price * item.quantity const lineTotal = item.unit_price * item.quantity
return { return {
@@ -279,18 +280,18 @@ class TotalsService extends BaseService {
cart, cart,
type type
) )
return merged.map(item => { return merged.map((item) => {
const discounted = allocationDiscounts.find( const discounted = allocationDiscounts.find(
a => a.lineItem.id === item.id (a) => a.lineItem.id === item.id
) )
return { return {
item, item,
amount: !!discounted ? discounted.amount : 0, amount: discounted ? discounted.amount : 0,
} }
}) })
} }
return merged.map(i => ({ item: i, amount: 0 })) return merged.map((i) => ({ item: i, amount: 0 }))
} }
getGiftCardTotal(cart) { getGiftCardTotal(cart) {
@@ -317,11 +318,11 @@ class TotalsService extends BaseService {
/** /**
* Calculates the total discount amount for each of the different supported * Calculates the total discount amount for each of the different supported
* discount types. If discounts aren't present or invalid returns 0. * discount types. If discounts aren't present or invalid returns 0.
* @param {Cart} Cart - the cart to calculate discounts for * @param {Cart} cart - the cart to calculate discounts for
* @return {int} the total discounts amount * @return {int} the total discounts amount
*/ */
getDiscountTotal(cart) { getDiscountTotal(cart) {
let subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true }) const subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true })
if (!cart.discounts || !cart.discounts.length) { if (!cart.discounts || !cart.discounts.length) {
return 0 return 0
@@ -348,7 +349,7 @@ class TotalsService extends BaseService {
cart, cart,
"percentage" "percentage"
) )
toReturn = _.sumBy(itemPercentageDiscounts, d => d.amount) toReturn = _.sumBy(itemPercentageDiscounts, (d) => d.amount)
} else if (type === "fixed" && allocation === "total") { } else if (type === "fixed" && allocation === "total") {
toReturn = value toReturn = value
} else if (type === "fixed" && allocation === "item") { } else if (type === "fixed" && allocation === "item") {
@@ -357,7 +358,7 @@ class TotalsService extends BaseService {
cart, cart,
"fixed" "fixed"
) )
toReturn = _.sumBy(itemFixedDiscounts, d => d.amount) toReturn = _.sumBy(itemFixedDiscounts, (d) => d.amount)
} }
if (subtotal < 0) { if (subtotal < 0) {