fix(payments): advance-only last_paid_at, no dues regression (M4/M5)

logCashPayment and linkPaymentToMember unconditionally set last_paid_at to the
(possibly backdated) transaction_date and forced payment_status='current', so
logging/linking a historical payment moved a member's dues state backward.
New pure resolveDuesAdvance() keeps the later of existing/incoming date and only
marks 'current' when the payment actually advances; both sites read the current
date first. Identical in the common (newest-payment) case.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
Moheeb Zara
2026-08-01 21:31:34 -07:00
co-authored by Claude Opus 4.8
parent 90c68faa25
commit 4b98c21e2b
3 changed files with 89 additions and 9 deletions
+21 -9
View File
@@ -15,6 +15,7 @@ import {
linkPaymentSchema,
importPaymentsCsvSchema,
} from '@/lib/validations'
import { resolveDuesAdvance } from '@/lib/dues-payments-logic'
import type { Enums, TablesInsert } from '@/types/database'
type PaymentPlatform = Enums<'payment_platform'>
@@ -53,12 +54,17 @@ export async function logCashPayment(formData: {
if (error) return { error: error.message }
if (v.data.member_id) {
// Advance-only: a backdated cash payment must not move dues state backward.
const { data: existing } = await supabase
.from('space_members')
.select('last_paid_at')
.eq('id', v.data.member_id)
.eq('space_id', member.space_id)
.single()
await supabase
.from('space_members')
.update({
last_paid_at: transactionDate,
payment_status: 'current',
})
.update(resolveDuesAdvance(existing?.last_paid_at ?? null, transactionDate))
.eq('id', v.data.member_id)
.eq('space_id', member.space_id)
}
@@ -100,13 +106,19 @@ export async function linkPaymentToMember(paymentId: string, memberId: string) {
if (error) return { error: error.message }
if (payment) {
if (payment?.transaction_date) {
// Advance-only: linking a historical payment must not move dues state
// backward if the member has a more recent payment on file.
const { data: existing } = await supabase
.from('space_members')
.select('last_paid_at')
.eq('id', v.data.memberId)
.eq('space_id', self.space_id)
.single()
await supabase
.from('space_members')
.update({
last_paid_at: payment.transaction_date,
payment_status: 'current',
})
.update(resolveDuesAdvance(existing?.last_paid_at ?? null, payment.transaction_date))
.eq('id', v.data.memberId)
.eq('space_id', self.space_id)
}