Populate payment method filter from real data instead of a guessed list

The previous fix hardcoded apple_pay/google_pay as extra filter options,
but Payment.method is free-text set by whatever the gateway reports, so
guessing at literal values was fragile and still didn't surface them for
this user. Add GET /api/payments/mypayments/methods returning the
distinct method values actually present in the user's payments, and have
the dashboard filter build its options from that instead. Also switch
the method filter from a startsWith match to an exact match, since the
values now come straight from the same column being filtered.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 08:55:58 +02:00
co-authored by Claude Sonnet 5
parent 90bea25338
commit 59194349d2
7 changed files with 53 additions and 11 deletions
+29 -1
View File
@@ -369,7 +369,7 @@ const getUserPayments = async (req, res) => {
};
if (req.query.method) {
where.method = { startsWith: String(req.query.method), mode: 'insensitive' };
where.method = { equals: String(req.query.method), mode: 'insensitive' };
}
if (req.query.kind === 'refund') {
@@ -405,6 +405,33 @@ const getUserPayments = async (req, res) => {
}
};
// @desc Get the distinct payment methods actually present in the user's own payments,
// so the dashboard filter never has to guess at gateway-reported values
// (e.g. apple_pay, google_pay) — it only ever offers what's really there.
// @route GET /api/payments/mypayments/methods
// @access Private
const getUserPaymentMethods = async (req, res) => {
try {
const rows = await prisma.payment.findMany({
where: {
isDonation: false,
method: { not: null },
OR: [
{ userId: req.user.id },
{ registration: { userId: req.user.id } }
]
},
distinct: ['method'],
select: { method: true },
orderBy: { method: 'asc' }
});
res.json({ methods: rows.map(r => r.method).filter(Boolean) });
} catch (error) {
res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) });
}
};
// @desc Get payment by ID
// @route GET /api/payments/:id
// @access Private
@@ -1224,6 +1251,7 @@ module.exports = {
createPayment,
getPayments,
getUserPayments,
getUserPaymentMethods,
getPaymentById,
getPaymentsByRegistration,
getPaymentsByEvent,