|
| 1 | +# Payment Schedule System |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document explains the payment schedule system: what PaymentSchedule records represent, how they are created, how statuses work, and how they map to round progression and on-chain payouts. |
| 6 | + |
| 7 | +## What Is a Payment Schedule? |
| 8 | + |
| 9 | +A PaymentSchedule record represents a planned payout for a specific round of a circle. It tracks: |
| 10 | + |
| 11 | +- Which round the payout is for. |
| 12 | +- Which member is the payee (by rotation index). |
| 13 | +- How much they are expected to receive. |
| 14 | +- When the payout is due. |
| 15 | +- Whether it has been paid. |
| 16 | + |
| 17 | +## PaymentSchedule Model |
| 18 | + |
| 19 | +| Field | Type | Description | |
| 20 | +|---|---|---| |
| 21 | +| id | string | CUID primary key | |
| 22 | +| circleId | string | FK to Circle | |
| 23 | +| round | int | Round number covered by this schedule | |
| 24 | +| payeeIndex | int | 1-based index into the rotation order | |
| 25 | +| expectedAmount | float | contributionAmount x memberCount | |
| 26 | +| dueDate | datetime | When contributions for this round are due | |
| 27 | +| status | PaymentStatus | Current payout state | |
| 28 | +| paidAt | datetime? | Set when status becomes COMPLETED | |
| 29 | +| createdAt | datetime | Auto-set | |
| 30 | +| updatedAt | datetime | Auto-updated | |
| 31 | + |
| 32 | +## Payment Statuses |
| 33 | + |
| 34 | +| Status | Description | |
| 35 | +|---|---| |
| 36 | +| PENDING | Payout is scheduled but not yet initiated | |
| 37 | +| INITIATED | Payout process has started | |
| 38 | +| COMPLETED | Payout has been successfully delivered | |
| 39 | +| OVERDUE | Due date has passed without completion | |
| 40 | +| FAILED | Payout attempt failed | |
| 41 | + |
| 42 | +## How Payment Schedules Are Created |
| 43 | + |
| 44 | +Payment schedules are created by the seed script for test data. In production, they should be created when a circle is initialized or when a new round begins. |
| 45 | + |
| 46 | +Seed data example: |
| 47 | + |
| 48 | +```ts |
| 49 | +await prisma.paymentSchedule.create({ |
| 50 | + data: { |
| 51 | + circleId: circle1.id, |
| 52 | + round: 1, |
| 53 | + payeeIndex: 1, |
| 54 | + expectedAmount: 300, // 100 XLM x 3 members |
| 55 | + dueDate: nextWeek, |
| 56 | + status: 'COMPLETED', |
| 57 | + }, |
| 58 | +}); |
| 59 | + |
| 60 | +await prisma.paymentSchedule.create({ |
| 61 | + data: { |
| 62 | + circleId: circle1.id, |
| 63 | + round: 2, |
| 64 | + payeeIndex: 2, |
| 65 | + expectedAmount: 300, |
| 66 | + dueDate: nextTwoWeeks, |
| 67 | + status: 'PENDING', |
| 68 | + }, |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +## Relationship to Circle Rounds |
| 73 | + |
| 74 | +Each circle has maxRounds rounds. Ideally, one PaymentSchedule record should exist per round and be created upfront when the circle starts: |
| 75 | + |
| 76 | +- Round 1 -> PaymentSchedule { round: 1, payeeIndex: 1, dueDate: startDate + frequency } |
| 77 | +- Round 2 -> PaymentSchedule { round: 2, payeeIndex: 2, dueDate: startDate + 2 x frequency } |
| 78 | +- ... |
| 79 | +- Round N -> PaymentSchedule { round: N, payeeIndex: N, dueDate: startDate + N x frequency } |
| 80 | + |
| 81 | +expectedAmount = circle.contributionAmount x circle.members.length |
| 82 | + |
| 83 | +## Relationship to On-Chain State |
| 84 | + |
| 85 | +The PaymentSchedule table is the off-chain record of planned payouts. The actual payout is executed on-chain via claim_payout(member) on the Soroban contract. |
| 86 | + |
| 87 | +When a member successfully calls claim_payout on-chain, the corresponding PaymentSchedule record should be updated to: |
| 88 | + |
| 89 | +- status: COMPLETED |
| 90 | +- paidAt: now |
| 91 | + |
| 92 | +This synchronization step is not yet automated and is a known gap. |
| 93 | + |
| 94 | +## Overdue Detection |
| 95 | + |
| 96 | +A cron job or scheduled task should periodically check for PaymentSchedule records where: |
| 97 | + |
| 98 | +- status = PENDING |
| 99 | +- dueDate < now |
| 100 | + |
| 101 | +Then update them to status = OVERDUE and notify the circle organizer. |
| 102 | + |
| 103 | +This is referenced in the cron jobs issue as a task to implement. |
| 104 | + |
| 105 | +## Indexes |
| 106 | + |
| 107 | +The PaymentSchedule table has these indexes for query performance: |
| 108 | + |
| 109 | +- circleId |
| 110 | +- status |
| 111 | +- composite: [circleId, status] |
| 112 | + |
| 113 | +These support efficient queries such as finding all pending payment schedules for a circle. |
| 114 | + |
| 115 | +## Example Query |
| 116 | + |
| 117 | +```ts |
| 118 | +// Find all overdue payment schedules |
| 119 | +const overdue = await prisma.paymentSchedule.findMany({ |
| 120 | + where: { |
| 121 | + status: 'PENDING', |
| 122 | + dueDate: { lt: new Date() }, |
| 123 | + }, |
| 124 | + include: { |
| 125 | + circle: { select: { name: true, organizerId: true } }, |
| 126 | + }, |
| 127 | +}); |
| 128 | +``` |
0 commit comments