allowances: add a would_deactivate PREVIEW (deactivation stays gated off)
Report mode (and a send run while ALLOWANCE_DEACTIVATE is off) now reports a member whose reminder is past the grace window as would_deactivate, with the reminder date, grace length and deadline in the evidence. The real action is unchanged and still needs BOTH ALLOWANCE_POLICY=on AND ALLOWANCE_DEACTIVATE=on. The lapse decision is extracted into a pure function (allowance_lapse_decision) so it is unit-testable against a synthetic reminder time: tests/test_allowance_lapse.py (would_deactivate past grace; in_grace inside).
This commit is contained in:
1 parent
ee7ed77697
commit
8c85c912e8
2 files changed
+125
-11
No files matched your search
+52
-11
@@ -4064,6 +4064,10 @@ async def reconcile_memberships() -> dict:
|
||||
# reminded — no live recurring contribution; one reminder email is sent.
|
||||
# deactivated — reminded on an earlier pass and still unpaid after the grace
|
||||
# window; the member loses group access but keeps their account.
|
||||
# would_* — report mode (and a send run while ALLOWANCE_DEACTIVATE is off)
|
||||
# writes the `would_` form, including `would_deactivate` for a
|
||||
# reminder older than the grace window, so the deactivation the
|
||||
# policy WOULD apply is visible BEFORE the flag is enabled.
|
||||
# Every decision is written to allowance_log with the Open Collective evidence
|
||||
# (payment date, or its absence) so a dispute is answered from a record.
|
||||
ALLOWANCE_POLICY = (os.environ.get("ALLOWANCE_POLICY", "report") or "report").strip().lower()
|
||||
@@ -4433,6 +4437,32 @@ async def anchor_day_for_member(email: str, slug: str = "") -> int:
|
||||
return _clamp_anchor_day(datetime.now(timezone.utc).day)
|
||||
|
||||
|
||||
def allowance_lapse_decision(reminder_sent_at, now, grace_days, acting):
|
||||
"""What to do with a member who was reminded and is still unpaid (pure).
|
||||
|
||||
No I/O — `now`, the reminder time and the grace length are all arguments, so
|
||||
the lapse logic can be unit-tested against a SYNTHETIC reminder time without
|
||||
touching any member's rows (see tests/test_allowance_lapse.py). Returns
|
||||
`(decision, deadline)`:
|
||||
|
||||
* ("in_grace", None) — the grace window has not elapsed yet.
|
||||
* ("would_deactivate", deadline) — elapsed, but this caller may not act
|
||||
(a report run, or ALLOWANCE_DEACTIVATE still off). NOTHING changes.
|
||||
* ("deactivated", deadline) — elapsed AND the caller is authorised.
|
||||
|
||||
`acting` must be True only when BOTH gates hold — a verified send run
|
||||
(ALLOWANCE_POLICY=on) AND ALLOWANCE_DEACTIVATE=on — so enabling the preview
|
||||
never by itself removes anyone's access. `deadline` is the moment the grace
|
||||
window elapses (reminder sent + grace_days), carried into the decision log.
|
||||
"""
|
||||
if reminder_sent_at is None:
|
||||
return "in_grace", None
|
||||
deadline = reminder_sent_at + timedelta(days=grace_days)
|
||||
if now < deadline:
|
||||
return "in_grace", None
|
||||
return ("deactivated" if acting else "would_deactivate"), deadline
|
||||
|
||||
|
||||
async def reconcile_allowances(send: bool = False) -> dict:
|
||||
"""Decide every active member's allowance renewal from OC payment evidence.
|
||||
|
||||
@@ -4548,12 +4578,29 @@ async def reconcile_allowances(send: bool = False) -> dict:
|
||||
decision = "remind_failed"
|
||||
else:
|
||||
sent_at = _parse_oc_dt(str(reminded[0]))
|
||||
elapsed = (now - sent_at).days if sent_at else None
|
||||
if elapsed is not None and elapsed >= PAYMENT_GRACE_DAYS and ALLOWANCE_DEACTIVATE == "on":
|
||||
decision = label("deactivate")
|
||||
# Lapse decision is a pure, testable function: past the grace
|
||||
# window it reports `would_deactivate` in EVERY mode; the real
|
||||
# action needs `acting` — a verified send run AND the separate
|
||||
# ALLOWANCE_DEACTIVATE gate. So a report run (and a send run with
|
||||
# deactivate still off) PREVIEWS the deactivation and changes
|
||||
# nobody's access.
|
||||
acting = verified == "on" and ALLOWANCE_DEACTIVATE == "on"
|
||||
lapse, deadline = allowance_lapse_decision(
|
||||
sent_at, now, PAYMENT_GRACE_DAYS, acting)
|
||||
if lapse == "in_grace":
|
||||
decision = "in_grace"
|
||||
counts["in_grace"] += 1
|
||||
evidence = (f"{evidence}; reminder sent "
|
||||
f"{str(reminded[0])[:10] if reminded[0] else '?'} "
|
||||
f"(grace {int(PAYMENT_GRACE_DAYS)}d)")
|
||||
else:
|
||||
decision = lapse # would_deactivate | deactivated
|
||||
counts[decision] += 1
|
||||
evidence = f"{evidence}; reminder sent {str(reminded[0])[:10]}, grace {int(PAYMENT_GRACE_DAYS)}d elapsed"
|
||||
if verified == "on":
|
||||
dl = deadline.date().isoformat() if deadline else "?"
|
||||
evidence = (f"{evidence}; reminder sent {str(reminded[0])[:10]}, "
|
||||
f"grace {int(PAYMENT_GRACE_DAYS)}d elapsed, "
|
||||
f"deadline {dl}")
|
||||
if decision == "deactivated":
|
||||
urow = conn.execute(
|
||||
"SELECT cloudron_user_id FROM members WHERE email = ?", (email,)
|
||||
).fetchone()
|
||||
@@ -4562,12 +4609,6 @@ async def reconcile_allowances(send: bool = False) -> dict:
|
||||
deactivate_member(email)
|
||||
await revoke_member_litellm_access(email)
|
||||
await listmonk_sync(email, subscribe=False)
|
||||
else:
|
||||
decision = "in_grace"
|
||||
counts["in_grace"] += 1
|
||||
evidence = (f"{evidence}; reminder sent "
|
||||
f"{str(reminded[0])[:10] if reminded[0] else '?'} "
|
||||
f"(grace {int(PAYMENT_GRACE_DAYS)}d)")
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO allowance_log (email, cycle, decision, evidence, anchor_day)"
|
||||
|
||||
Reference in new issue
Block a user