Skip to content

Commit

Permalink
added logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hemant10yadav committed Nov 7, 2024
1 parent c70fa93 commit 1ff0ff0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion commcare_connect/opportunity/migrations/0061_exchangerate.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Generated by Django 4.2.5 on 2024-11-06 05:16
import logging

from django.db import migrations, models
from django.db.models.functions import TruncDate

from commcare_connect.opportunity.visit_import import get_exchange_rate

logger = logging.getLogger(__name__)


def update_exchange_rate(apps, schema_editor):
Payment = apps.get_model("opportunity.Payment")
payments = (
Payment.objects.annotate(date_only=TruncDate("date_paid"))
.values("date_only", "opportunity_access__opportunity__currency")
.values("id", "date_only", "amount", "opportunity_access__opportunity__currency")
.distinct()
)

Expand All @@ -22,6 +25,9 @@ def update_exchange_rate(apps, schema_editor):
exchange_rate = 1
else:
exchange_rate = get_exchange_rate(currency, date_paid)
logger.info(
f"Payment ID: {payment.id}, original USD: {payment.amount_usd}, USD acc. to new rate: {payment.amount / exchange_rate}"
)
if not exchange_rate:
raise Exception(f"Invalid currency code {currency}")

Expand Down
6 changes: 4 additions & 2 deletions commcare_connect/opportunity/visit_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def get_exchange_rate(currency_code, date=None):
return rate

base_url = "https://openexchangerates.org/api"

if date:
url = f"{base_url}/historical/{date.strftime('%Y-%m-%d')}.json"
else:
Expand All @@ -299,8 +300,9 @@ def get_exchange_rate(currency_code, date=None):

rate = rates["rates"].get(currency_code)

rate_date = date if date else now().date()
ExchangeRate.objects.create(currency_code=currency_code, rate=rate, rate_date=rate_date)
if rate:
rate_date = date if date else now().date()
ExchangeRate.objects.create(currency_code=currency_code, rate=rate, rate_date=rate_date)

return rate

Expand Down

0 comments on commit 1ff0ff0

Please sign in to comment.