Merge pull request #561 from Ayush0730/develop

fix: make packages/medusa/src/services/totals.js pass eslint
This commit is contained in:
Sebastian Rindom authored and GitHub committed 2021-10-20 08:58:55 +02:00
commit 4f35041885
2 files changed
+17 -18

No files matched your search

-2
View File
@@ -15,8 +15,6 @@
/packages/medusa/src/services/shipping-profile.js /packages/medusa/src/services/shipping-profile.js
/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/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
/packages/medusa/src/subscribers/product.js /packages/medusa/src/subscribers/product.js
+17 -16
View File
@@ -5,7 +5,7 @@ import carts from "../api/routes/store/carts"
/** /**
* 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() {
@@ -49,7 +49,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 = {}) {
@@ -58,7 +59,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
@@ -129,7 +130,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 =
@@ -149,12 +150,12 @@ 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]
} }
} }
@@ -185,7 +186,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
@@ -279,7 +280,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 {
@@ -293,18 +294,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) {
@@ -331,11 +332,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
@@ -362,7 +363,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") {
@@ -371,7 +372,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) {