askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
counter_customer_history.py · 4.4 KB · 103 lines
| 1 | """action_counter_customer_history -- complaints per customer per month. |
| 2 | |
| 3 | POST {"customer_email": "...", "since": "2026-09"} (both optional) |
| 4 | |
| 5 | A manager deciding whether to give a regular a free coffee, or whether |
| 6 | somebody is working the refund desk, needs one answer: how often has this |
| 7 | customer complained, and what did it cost? Corner Cup's managers could not |
| 8 | see the shop's disputes or refunds at all (owner-scoped), let alone fold |
| 9 | them. This folds, live and never stored (the house rule for queues and |
| 10 | counts: a stored tally drifts from the rows it counts): |
| 11 | |
| 12 | - disputes (app-disputes), by their customer_email; |
| 13 | - refunds that carry a reason code (a counter correction), by the bill's |
| 14 | customer; |
| 15 | - goodwill, by its customer_email or its order's; |
| 16 | - remakes, by the order's customer. |
| 17 | |
| 18 | Grouped by customer (email, lower-cased; rows with none are "(no email)") |
| 19 | and by calendar month of when the row was written. Only rows the permission |
| 20 | engine lets the caller read are counted. Execute: shift lead, manager, |
| 21 | owner. |
| 22 | """ |
| 23 | |
| 24 | from __future__ import annotations |
| 25 | |
| 26 | import os |
| 27 | |
| 28 | import object_access |
| 29 | import object_collections |
| 30 | import object_corrections |
| 31 | import object_records |
| 32 | |
| 33 | |
| 34 | def _base_dir(): |
| 35 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 36 | |
| 37 | |
| 38 | def _visible(access, base, collection): |
| 39 | try: |
| 40 | # access: decided by access.visible on the same line's result |
| 41 | rows = object_records.read_collection_records(collection, base_dir=base) |
| 42 | except object_collections.CollectionNotFoundError: |
| 43 | return [] |
| 44 | return access.visible(collection, rows) |
| 45 | |
| 46 | |
| 47 | def POST(request): |
| 48 | text = object_corrections.text |
| 49 | base = _base_dir() |
| 50 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 51 | if not access.signed_in: |
| 52 | return {"status": 401, "error": "Sign in to read customer history."} |
| 53 | staff = object_corrections.staff_refusal(access, "action_counter_customer_history", doing="read customer history here") |
| 54 | if staff: |
| 55 | return staff |
| 56 | wanted = text(request.get("customer_email")).lower() |
| 57 | since = text(request.get("since"))[:7] |
| 58 | |
| 59 | orders = {text(o.get("id")): o for o in _visible(access, base, "orders")} |
| 60 | invoices = {text(i.get("id")): i for i in _visible(access, base, "invoices")} |
| 61 | table: dict = {} |
| 62 | |
| 63 | def add(email, when, kind, cents=0): |
| 64 | email = text(email).lower() or "(no email)" |
| 65 | month = text(when)[:7] |
| 66 | if (wanted and email != wanted) or (since and month < since) or not month: |
| 67 | return |
| 68 | row = table.setdefault(email, {}).setdefault(month, { |
| 69 | "month": month, "disputes": 0, "refunds": 0, "refunded_cents": 0, |
| 70 | "goodwill": 0, "goodwill_cents": 0, "remakes": 0}) |
| 71 | row[kind] += 1 |
| 72 | if kind == "refunds": |
| 73 | row["refunded_cents"] += cents |
| 74 | if kind == "goodwill": |
| 75 | row["goodwill_cents"] += cents |
| 76 | |
| 77 | for dispute in _visible(access, base, "disputes"): |
| 78 | email = text(dispute.get("customer_email")) or text( |
| 79 | orders.get(text(dispute.get("order_id")), {}).get("customer_email")) |
| 80 | add(email, dispute.get("created_at"), "disputes") |
| 81 | for refund in _visible(access, base, "refunds"): |
| 82 | if not text(refund.get("reason_code")): |
| 83 | continue |
| 84 | invoice = invoices.get(text(refund.get("invoice_id")), {}) |
| 85 | add(invoice.get("customer_email"), refund.get("refunded_on") or refund.get("created_at"), |
| 86 | "refunds", object_corrections.cents(refund.get("amount_cents"))) |
| 87 | for given in _visible(access, base, "goodwill"): |
| 88 | email = text(given.get("customer_email")) or text( |
| 89 | orders.get(text(given.get("order_id")), {}).get("customer_email")) |
| 90 | add(email, given.get("created_at"), "goodwill", object_corrections.cents(given.get("value_cents"))) |
| 91 | for remake in _visible(access, base, "remakes"): |
| 92 | add(orders.get(text(remake.get("order_id")), {}).get("customer_email"), |
| 93 | remake.get("created_at"), "remakes") |
| 94 | |
| 95 | customers = [] |
| 96 | for email, months in table.items(): |
| 97 | rows = [months[m] for m in sorted(months, reverse=True)] |
| 98 | for row in rows: |
| 99 | row["complaints"] = row["disputes"] + row["refunds"] + row["goodwill"] + row["remakes"] |
| 100 | customers.append({"customer": email, "months": rows, |
| 101 | "complaints": sum(r["complaints"] for r in rows)}) |
| 102 | customers.sort(key=lambda c: (-c["complaints"], c["customer"])) |
| 103 | return {"status": 200, "ok": True, "customers": customers} |