Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3HANDLES invoices writes: the moment an invoice stops being a draft, mint
4its portal_token if it has none. This closes the gap left by minting
5lazily: a link that only appears once an invoice goes overdue is a link
6that exists only for the customers who already failed to pay. The point of
7the portal is that the FIRST email a customer receives can carry a door --
8so the token has to exist when the invoice is issued, not when chasing
9starts.
10
11Placement is doctrine #6: this is a REACTION, post-commit and best-effort,
12not a gate. It never blocks or fails the write that issued the invoice --
13an invoice that could not get a token is still a perfectly good invoice,
14and the next event (or a dunning pass, or the owner clicking regenerate)
15will mint one.
16
17portal_token is schema read_only so no client can ever choose its own --
18a predictable capability URL is not a capability at all. Server-side
19writers pass preserve_read_only to set it, which is exactly the narrow
20escape hatch that flag exists for.
21"""
22
23import os
24import secrets
25
26import object_records
27
28HANDLES = [
29 "invoices.record.created",
30 "invoices.record.updated",
31]
32
33ACTOR = "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.
38LINKABLE = {"sent", "partial", "overdue", "paid"}
39
40
41def _base_dir():
42 return os.environ.get("DBBASIC_DATA_DIR", "data")
43
44
45def 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}