Skip to content

Commit 0e4af8f

Browse files
authored
Feature/exit menu item (#28)
1 parent 713c99d commit 0e4af8f

24 files changed

Lines changed: 3630 additions & 10 deletions

docs/superpowers/plans/2026-06-14-member-exit-policy.md

Lines changed: 1286 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Full EMI prepayment: delete pending installments and close the loan
2+
3+
**Date:** 2026-06-14
4+
**Status:** Approved
5+
**Area:** EMI loan model — `prepayLoan` server action
6+
7+
## Problem
8+
9+
When an EMI loan is fully prepaid (the advance covers the entire outstanding
10+
principal), the current code flips every remaining `scheduled` / `overdue`
11+
installment to `status = 'waived'`:
12+
13+
```ts
14+
// src/lib/actions/emi.ts:212-219 (current)
15+
if (newOutstanding === 0) {
16+
await supabase.from('loan_emi_schedule')
17+
.update({ status: 'waived' })
18+
.eq('loan_id', loanId)
19+
.in('status', ['scheduled', 'overdue'])
20+
}
21+
```
22+
23+
This is misleading. "Waived" means the obligation was *forgiven without
24+
payment*, but in a full prepayment the member **paid** the outstanding
25+
principal. The schedule then shows a row of "Waived" badges for installments
26+
the member actually settled in cash. Two further gaps:
27+
28+
- The loan is **not** formally closed — `loans.status` stays `active` even
29+
though the balance is zero.
30+
- A `partially_paid` installment's unpaid remainder is part of
31+
`pending_principal`; the lump sum covers it, but nothing reconciles that row.
32+
33+
## Goal
34+
35+
After a full prepayment, the loan record should read like a clean, closed
36+
loan: only real installments remain on the schedule, the advance is captured
37+
as one transaction, and the loan is marked paid.
38+
39+
## Design
40+
41+
Change applies only to the **full-prepay branch** (`newOutstanding === 0`) of
42+
`prepayLoan` in `src/lib/actions/emi.ts`. The transaction insert (current
43+
line 193) and the optional bank-balance bump (line 207) are unchanged. Partial
44+
prepayment (`newOutstanding > 0`) is unchanged.
45+
46+
New behavior when `newOutstanding === 0`:
47+
48+
1. **Settle `partially_paid` rows from the lump sum.** The entered amount
49+
equals `pending_principal`, which *includes* the unpaid remainder of any
50+
partially-paid EMI. For each such row, set `principal_paid = principal_due`,
51+
`status = 'paid'`, `paid_at = now()`. The row keeps its payment history and
52+
stops reporting a dangling balance. (Without this, `loan_emi_balances` would
53+
still show that remainder and the loan would not net to zero.)
54+
2. **Delete `scheduled` + `overdue` rows.** These never received a payment, so
55+
they have no `loan_emi_payments` junction rows and the `ON DELETE RESTRICT`
56+
FK (migration 038, line 74) does not block the delete. Paid /
57+
partially-paid rows — which we keep — *do* have junction rows, which is an
58+
additional reason the deletion scope is exactly `scheduled` + `overdue`.
59+
3. **Close the loan:** set `loans.status = 'paid'`.
60+
61+
### What is kept vs deleted
62+
63+
| Installment status | Action |
64+
| :----------------- | :----- |
65+
| `paid` | kept untouched |
66+
| `partially_paid` | kept, completed to `paid` (step 1) |
67+
| `scheduled` | deleted |
68+
| `overdue` | deleted |
69+
70+
### Downstream — no changes required
71+
72+
- `loan_emi_balances` (migration 040) sums `principal_due - principal_paid`
73+
over non-`waived` rows. After the change all remaining rows are fully paid →
74+
`pending_principal = 0`.
75+
- `loan-timeline-section.tsx` renders whatever rows exist; it simply shows
76+
fewer rows. No badge logic change.
77+
- Authorization, `runAction` wrapper, `updateTag('dashboard')` /
78+
`revalidatePath` calls remain as-is.
79+
80+
## Verification
81+
82+
- `recomputeAfterPrepayment` (pure helper) is untouched; its existing unit
83+
tests still pass.
84+
- Add / extend a unit test asserting the full-prepay branch: paid rows kept,
85+
partially-paid completed, scheduled/overdue gone, `loans.status = 'paid'`,
86+
balance zero. (Confirm existing test harness for the action during
87+
implementation; fall back to a focused test of any extractable pure logic if
88+
the action itself is not unit-testable.)
89+
- Manual check on a seeded EMI loan: full prepay → timeline shows no "Waived"
90+
rows, loan listed as closed/paid, KPIs show zero outstanding.
91+
92+
## Out of scope
93+
94+
- Partial prepayment behavior (unchanged).
95+
- The accrual loan model.
96+
- Backfill of loans already fully prepaid under the old waive logic. Noted as a
97+
possible follow-up cleanup script; no historical rows touched here unless
98+
requested.

docs/superpowers/specs/2026-06-14-member-exit-policy-design.md

Lines changed: 189 additions & 0 deletions
Large diffs are not rendered by default.

docs/v3-revised-rules.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
FCF Revised Rules
2+
3+
This document consolidates the current operating rules of the Friends Cooperative Fund (FCF), superseding the earlier V1 and V2 rules.
4+
5+
1. Member Classification
6+
7+
The earlier distinction between Refundable and Non-Refundable members is discontinued. All members will be treated equally with respect to monthly contributions, benefits, loan eligibility, social support, and exit-related decisions.
8+
9+
2. Monthly Contributions
10+
11+
Each member shall make monthly contributions to FCF.
12+
13+
The monthly contribution is ₹750 per member.
14+
15+
Members are encouraged to enable auto-payment or standing instructions to avoid missed contributions.
16+
17+
The monthly contribution amount shall be reviewed annually and may be revised based on inflation, fund requirements, and members’ financial circumstances.
18+
19+
3. Contribution Discipline and Pending Contributions
20+
21+
All members are expected to make contributions regularly and on time.
22+
23+
Members with missed or pending contributions shall regularize the pending amount within 6 months.
24+
25+
The Treasurer or Committee shall identify members with pending contributions and communicate the required regularization amount and timeline.
26+
27+
Repeated delay or irregular contribution may be reviewed by the Committee and discussed with the concerned member.
28+
29+
4. Fixed Corpus Fund
30+
31+
FCF shall maintain a fixed corpus fund target of ₹5,00,000.
32+
33+
The corpus target may be increased annually by 20% or adjusted based on inflation, fund growth, and member approval.
34+
35+
Members shall continue contributing until the corpus target and future revised targets are achieved.
36+
37+
The corpus fund shall be used carefully to ensure long-term sustainability of FCF.
38+
39+
5. Loan Eligibility
40+
41+
Loans may be provided to members based on fund availability, repayment capacity, loan purpose, and Committee/member approval.
42+
43+
The maximum loan eligibility per member is capped at ₹2,00,000.
44+
45+
Loan approval shall depend on available financial reserves at the time of request.
46+
47+
Priority shall be given in the following order:
48+
49+
1. Medical emergency loans
50+
2. Other urgent financial necessities
51+
3. Personal loans, subject to conditions and fund availability
52+
53+
6. Medical Emergency Loans
54+
55+
Medical emergency loans shall continue to be available even if the principal fund has not reached the fixed corpus target.
56+
57+
Medical emergency cases may be treated separately with flexible conditions.
58+
59+
Immediate health assistance shall primarily apply to immediate family members, including parents, spouse, and children.
60+
61+
Requests involving siblings, in-laws, or other family members may be considered under the Social Fund or as an exception, based on member agreement.
62+
63+
7. Personal Loans
64+
65+
Personal loans should be treated as a last option after the member has explored other reasonable alternatives.
66+
67+
Personal loans may be provided only when sufficient funds are available after protecting the medical emergency reserve and corpus requirements.
68+
69+
Until the principal fund reaches the fixed corpus target, personal loans may be restricted or avoided, except in special cases approved by the Committee/members.
70+
71+
Personal loans for repetitive personal reasons may be subject to stricter review, shorter repayment timelines, or rejection.
72+
73+
Personal loans may be approved with specific conditions, including repayment within a shorter period such as 6 months, where applicable.
74+
75+
8. Loan Repayment Model
76+
77+
All loans going forward shall be operated on an EMI-based repayment model.
78+
79+
Members taking loans shall repay through monthly EMI payments, with each EMI covering principal plus interest at the fixed rate of 8% per annum (Section 10).
80+
81+
The borrower may be allowed to choose the repayment tenure, subject to the maximum tenure approved by FCF.
82+
83+
Existing long-pending loans shall be reviewed, and borrowers may be requested to start or complete repayment if the loan has crossed a reasonable threshold.
84+
85+
9. Loan Tenure
86+
87+
The standard maximum loan tenure shall be 24 months.
88+
89+
Any exception to this tenure, such as 18 months or 30 months, shall be approved separately based on the loan type, borrower situation, and fund condition.
90+
91+
For personal loans, shorter repayment periods may be preferred.
92+
93+
For medical emergency loans, flexible repayment conditions may be considered.
94+
95+
10. Loan Interest Rate / Repayment Rate
96+
97+
The loan interest rate is fixed at 8% per annum for all loans.
98+
99+
This is the standard rate applied to every loan going forward, calculated on the loan within the EMI repayment model (Section 8).
100+
101+
Repetitive personal loans may attract stricter repayment conditions (shorter tenure, additional review), subject to Committee/member approval, but the interest rate remains 8% per annum.
102+
103+
11. EMI Calculation
104+
105+
EMIs are calculated on the loan principal at the fixed interest rate of 8% per annum (Section 10) over the chosen repayment tenure, subject to the maximum tenure in Section 9.
106+
107+
The exact monthly EMI for a given loan depends on its principal and tenure and shall be confirmed at the time of disbursement using the standard 8% per annum rate.
108+
109+
12. Loan Disbursement Priority
110+
111+
Loan requests shall be prioritized as follows:
112+
113+
1. Medical emergencies
114+
2. Critical financial necessities
115+
3. Personal needs, subject to corpus availability and approval
116+
117+
Loan approval shall also consider existing outstanding loans, pending contributions, member discipline, and overall fund position.
118+
119+
13. Social Help Budget
120+
121+
Social help shall operate on a monthly budget basis.
122+
123+
The monthly social help budget is 20% of the contributions collected in that month. Any unused amount is carried forward to the following month.
124+
125+
Requests for social help should be flagged early so that members and the Committee have enough time to review and respond.
126+
127+
Social help usage shall be reviewed periodically based on actual historical spending and fund availability.
128+
129+
14. Donations
130+
131+
Donations received or donation-related decisions shall be handled transparently.
132+
133+
Where applicable, donations may be added to the common fund, distributed equally among members, or used for social causes, based on member approval.
134+
135+
A clear donation policy shall be finalized by the Committee and approved by members.
136+
137+
15. Member Exit Policy
138+
139+
A member willing to exit FCF shall formally express their decision to the Committee in writing. The exit settlement shall then be computed using the following steps.
140+
141+
Step 1 — Settle outstanding obligations first.
142+
143+
Before any refund is calculated, the exiting member shall clear all open obligations to FCF:
144+
145+
* Any outstanding loan principal (and accrued interest) must be repaid or netted in full. A member cannot exit while holding an open loan against the fund.
146+
* Any pending or missed contributions must be regularized.
147+
148+
The exit refund is computed only after these obligations are settled.
149+
150+
Step 2 — Determine the member’s total eligible contributions.
151+
152+
The eligible amount is the sum of all monthly contributions made by the member during their membership.
153+
154+
Step 3 — Deduct the member’s equal share of shared costs.
155+
156+
The total bad debts and the total donations paid out by FCF to date are treated as shared costs of the fund. These are divided equally across all members on a per-capita basis (1/N, where N is the total number of members), and the exiting member’s single equal share is deducted from their eligible contributions:
157+
158+
```
159+
share of shared costs = (total bad debts + total donations) ÷ total number of members
160+
residual amount = total eligible contributions − share of shared costs
161+
```
162+
163+
Step 4 — Member decides what to do with the residual.
164+
165+
Once the residual amount is determined, the exiting member may choose to either:
166+
167+
1. Offer the residual amount to FCF as a donation to the common fund, or
168+
2. Withdraw the residual amount.
169+
170+
The chosen settlement, the figures used (eligible contributions, total bad debts, total donations, per-capita share, and residual), and the member’s decision shall be documented clearly and approved by the Committee/members.
171+
172+
16. Contribution Refunds
173+
174+
Contribution refunds — whether on individual member exit (Section 15) or at closure of FCF — follow the same settlement basis: eligible contributions, less the member’s equal per-capita share of bad debts and donations, with all outstanding loans and pending contributions settled first.
175+
176+
The member may choose to withdraw the residual amount or donate it to FCF / charity. The refund or donation process shall be documented clearly.
177+
178+
17. Bad Debts and Donations as Shared Costs
179+
180+
Bad debts (unrecovered loans, written-off amounts) and donations paid out are treated as shared costs of the fund and borne equally by all members on a per-capita basis.
181+
182+
At the time of member exit or FCF closure, each member’s equal share of these shared costs shall be deducted from their refundable amount, as set out in Section 15.
183+
184+
18. Account Management and Withdrawals
185+
186+
FCF shall move towards joint account management wherever feasible.
187+
188+
Financial bookkeeping responsibilities may be rotated among trusted members to improve transparency and shared responsibility.
189+
190+
Quarterly financial statements shall be shared with all members.
191+
192+
Any withdrawal above ₹50,000 shall require approval from at least two authorized signatories.
193+
194+
Bank account credentials, access, and authorization rights shall be handled securely and responsibly.
195+
196+
19. Financial Reporting
197+
198+
The Treasurer or assigned Committee member shall share financial statements with all members on a quarterly basis.
199+
200+
The statement shall include:
201+
202+
* Opening balance
203+
* Monthly contributions received
204+
* Pending contributions
205+
* Loans disbursed
206+
* Loan repayments received
207+
* Interest received
208+
* Social help or donations paid
209+
* Expenses incurred
210+
* Closing balance
211+
* Outstanding loans
212+
213+
20. Periodic Review Meetings
214+
215+
FCF shall conduct quarterly or half-yearly catch-ups to review fund status, loan performance, pending contributions, social help, donations, and rule improvements.
216+
217+
Rules may be amended based on member feedback, fund condition, and majority approval.

0 commit comments

Comments
 (0)