Skip to content

Commit

Permalink
use username for payment import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
calellowitz committed Oct 11, 2023
1 parent 64d9f43 commit cd358ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions commcare_connect/opportunity/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def _schema_sort(item):


def export_empty_payment_table(opportunity: Opportunity) -> Dataset:
headers = ["Phone Number", "Name", "Payment Amount"]
headers = ["Username", "Phone Number", "Name", "Payment Amount"]
dataset = Dataset(title="Export", headers=headers)

access_objects = OpportunityAccess.objects.filter(opportunity=opportunity).select_related("user")
for access in access_objects:
row = (access.user.phone_number, access.user.name, "")
row = (access.user.username, access.user.phone_number, access.user.name, "")
dataset.append(row)
return dataset
28 changes: 14 additions & 14 deletions commcare_connect/opportunity/visit_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

VISIT_ID_COL = "visit id"
STATUS_COL = "status"
PHONE_COL = "phone number"
USERNAME_COL = "username"
AMOUNT_COL = "payment amount"
REQUIRED_COLS = [VISIT_ID_COL, STATUS_COL]

Expand Down Expand Up @@ -54,7 +54,7 @@ def __len__(self):
def get_missing_message(self):
joined = ", ".join(self.missing_visits)
missing = textwrap.wrap(joined, width=115, break_long_words=False, break_on_hyphens=False)
return f"<br>{len(self.missing_users)} phone numbers were not found:<br>{'<br>'.join(missing)}"
return f"<br>{len(self.missing_users)} usernames were not found:<br>{'<br>'.join(missing)}"


def bulk_update_visit_status(opportunity: Opportunity, file: UploadedFile) -> VisitImportStatus:
Expand Down Expand Up @@ -148,35 +148,35 @@ def _bulk_update_payments(opportunity: Opportunity, imported_data: Dataset) -> P
if not headers:
raise ImportException("The uploaded file did not contain any headers")

phone_col_index = _get_header_index(headers, PHONE_COL)
username_col_index = _get_header_index(headers, USERNAME_COL)
amount_col_index = _get_header_index(headers, AMOUNT_COL)
invalid_rows = []
payments = {}
for row in imported_data:
row = list(row)
phone = str(row[phone_col_index])
username = str(row[username_col_index])
amount_raw = row[amount_col_index]
if amount_raw:
if not phone:
invalid_rows.append((row, "phone number required"))
if not username:
invalid_rows.append((row, "username required"))
try:
amount = int(amount_raw)
except ValueError:
invalid_rows.append((row, "amount must be an integer"))
payments[phone] = amount
payments[username] = amount

if invalid_rows:
raise ImportException(f"{len(invalid_rows)} have errors", invalid_rows)

seen_users = set()
missing_users = set()
with transaction.atomic():
phone_numbers = list(payments)
users = OpportunityAccess.objects.filter(
user__phone_number__in=phone_numbers, opportunity=opportunity
).select_related("user")
usernames = list(payments)
users = OpportunityAccess.objects.filter(user__username__in=usernames, opportunity=opportunity).select_related(
"user"
)
for access in users:
Payment.objects.create(opportunity_access=access, amount=payments[access.user.phone_number])
seen_users.add(access.user.phone_number)
missing_users = set(phone_numbers) - seen_users
Payment.objects.create(opportunity_access=access, amount=payments[access.user.username])
seen_users.add(access.user.username)
missing_users = set(usernames) - seen_users
return PaymentImportStatus(seen_users, missing_users)
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ <h4>Learn App Modules</h4>
<div class="tab-pane fade" id="payment-tab-pane" role="tabpanel" aria-labelledby="payment-tab" tabindex="0">
<div hx-get="{% url "opportunity:payment_table" org_slug=request.org.slug pk=opportunity.pk %}{% querystring %}"
hx-trigger="load" hx-swap="outerHTML">
{% include "tables/table_placeholder.html" with num_cols=2 %}
{% include "tables/table_placeholder.html" with num_cols=4 %}
</div>
</div>
<div class="tab-pane fade" id="user-status-tab-pane" role="tabpanel" aria-labelledby="user-status-tab" tabindex="0">
Expand Down Expand Up @@ -295,7 +295,7 @@ <h1 class="modal-title fs-5">{% translate "Import Payment Records" %}</h1>
<div class="col-auto">
<span id="importFileHelp" class="form-text">
{% blocktrans %}
The file must contain at least the "Phone Number" and "Amount" column.
The file must contain at least the "Username" and "Amount" column.
{% endblocktrans %}
</span>
</div>
Expand Down

0 comments on commit cd358ce

Please sign in to comment.