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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
invoice_portal_link.py · 2.8 KB · 74 lines
| 1 | """system_invoice_portal_link -- give every issued invoice a pay link. |
| 2 | |
| 3 | HANDLES invoices writes: the moment an invoice stops being a draft, mint |
| 4 | its portal_token if it has none. This closes the gap left by minting |
| 5 | lazily: a link that only appears once an invoice goes overdue is a link |
| 6 | that exists only for the customers who already failed to pay. The point of |
| 7 | the portal is that the FIRST email a customer receives can carry a door -- |
| 8 | so the token has to exist when the invoice is issued, not when chasing |
| 9 | starts. |
| 10 | |
| 11 | Placement is doctrine #6: this is a REACTION, post-commit and best-effort, |
| 12 | not a gate. It never blocks or fails the write that issued the invoice -- |
| 13 | an invoice that could not get a token is still a perfectly good invoice, |
| 14 | and the next event (or a dunning pass, or the owner clicking regenerate) |
| 15 | will mint one. |
| 16 | |
| 17 | portal_token is schema read_only so no client can ever choose its own -- |
| 18 | a predictable capability URL is not a capability at all. Server-side |
| 19 | writers pass preserve_read_only to set it, which is exactly the narrow |
| 20 | escape hatch that flag exists for. |
| 21 | """ |
| 22 | |
| 23 | import os |
| 24 | import secrets |
| 25 | |
| 26 | import object_records |
| 27 | |
| 28 | HANDLES = [ |
| 29 | "invoices.record.created", |
| 30 | "invoices.record.updated", |
| 31 | ] |
| 32 | |
| 33 | ACTOR = "system_invoice_portal_link" |
| 34 | |
| 35 | # draft invoices deliberately get no link: nothing has been issued yet, and |
| 36 | # a payable URL for a document the owner is still editing is a mistake |
| 37 | # waiting to be forwarded. |
| 38 | LINKABLE = {"sent", "partial", "overdue", "paid"} |
| 39 | |
| 40 | |
| 41 | def _base_dir(): |
| 42 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 43 | |
| 44 | |
| 45 | def EVENT(request): |
| 46 | # The dispatcher's payload carries the RAW verb ("create"/"update"); |
| 47 | # the event NAME uses the participle. Accept both. |
| 48 | action = str(request.get("action") or "") |
| 49 | action = {"create": "created", "update": "updated", "delete": "deleted"}.get(action, action) |
| 50 | if action not in ("created", "updated"): |
| 51 | return {"ok": True, "skipped": "not a create or update"} |
| 52 | record_id = str(request.get("record_id") or "") |
| 53 | if not record_id: |
| 54 | return {"ok": True, "skipped": "no record id"} |
| 55 | |
| 56 | base = _base_dir() |
| 57 | try: |
| 58 | invoice = object_records.get_collection_record("invoices", record_id, base_dir=base) |
| 59 | except Exception: |
| 60 | return {"ok": True, "skipped": "invoice gone"} |
| 61 | |
| 62 | if (invoice.get("status") or "draft") not in LINKABLE: |
| 63 | return {"ok": True, "skipped": "not issued yet"} |
| 64 | if str(invoice.get("portal_token") or "").strip(): |
| 65 | return {"ok": True, "skipped": "already has a link"} |
| 66 | |
| 67 | try: |
| 68 | object_records.update_collection_record( |
| 69 | "invoices", record_id, |
| 70 | {"portal_token": secrets.token_urlsafe(32)}, |
| 71 | base_dir=base, actor=ACTOR, preserve_read_only=True) |
| 72 | except Exception as exc: # never break the dispatcher |
| 73 | return {"ok": False, "error": str(exc)[:200]} |
| 74 | return {"ok": True, "minted": True, "invoice_id": record_id} |