Skip to content

Commit

Permalink
Merge pull request #605 from weni-ai/feature/calculate_by_area
Browse files Browse the repository at this point in the history
Feature/calculate by area
  • Loading branch information
BarbosaJackson authored Jan 24, 2025
2 parents cbd9597 + aec8124 commit 9508e0d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: Initialize environment
run: |
pip install --upgrade pip poetry
pip install --upgrade pip poetry==1.7.0
poetry install
poetry run python contrib/gen_env.py
Expand Down
20 changes: 20 additions & 0 deletions marketplace/services/vtex/business/rules/calculate_by_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .interface import Rule
from marketplace.services.vtex.utils.data_processor import FacebookProductDTO


class CalculateByArea(Rule):
def apply(self, product: FacebookProductDTO, **kwargs) -> bool:
if self._calculate_by_area(product):
unit_multiplier = self._get_multiplier(product)
product.price *= unit_multiplier
product.sale_price *= unit_multiplier
return True

def _calculate_by_area(self, product: FacebookProductDTO):
measurementUnit = product.product_details.get("MeasurementUnit", "")
if len(measurementUnit) > 0 and measurementUnit == 'm²':
return True
return False

def _get_multiplier(self, product: FacebookProductDTO):
return product.product_details.get("UnitMultiplier", 1.0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .interface import Rule
from typing import Union
from marketplace.services.vtex.utils.data_processor import FacebookProductDTO
from decimal import Decimal, ROUND_FLOOR


class CurrencyBRLRoudingFloor(Rule):

def apply(self, product: FacebookProductDTO, **kwargs) -> bool:
product.price = self.format_price(product.price)
product.sale_price = self.format_price(product.sale_price)
return True

@staticmethod
def format_price(price: Union[int, float]) -> str:
final_price = Decimal(price / 100)
final_price = final_price.quantize(Decimal('0.01'), rounding=ROUND_FLOOR)
formatted_price = f"{final_price} BRL"
return formatted_price
4 changes: 4 additions & 0 deletions marketplace/services/vtex/business/rules/rule_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from .use_rich_description import UseRichDescription
from .set_default_image_url import SetDefaultImageURL
from .exclude_categories_co import ExcludeCustomizedCategoriesCO
from .calculate_by_area import CalculateByArea
from .currency_pt_br_round_floor import CurrencyBRLRoudingFloor


"""
Expand All @@ -29,4 +31,6 @@
"use_rich_description": UseRichDescription,
"set_default_image_url": SetDefaultImageURL,
"exclude_categories_co": ExcludeCustomizedCategoriesCO,
"calculate_by_area": CalculateByArea,
"currency_pt_br_round_floor": CurrencyBRLRoudingFloor,
}

0 comments on commit 9508e0d

Please sign in to comment.