56 lines
2.2 KiB
JavaScript
56 lines
2.2 KiB
JavaScript
const router = require('express').Router();
|
||
const { getSupabaseClient } = require('./supabaseClient');
|
||
|
||
// Получить агрегированные сервисы с деталями и статусами оплаты для квартиры
|
||
router.get('/payment-services', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { apartment_id, user_id } = req.query;
|
||
if (!apartment_id || !user_id) return res.status(400).json({ error: 'apartment_id и user_id обязательны' });
|
||
|
||
// Получаем все агрегаторы
|
||
const { data: services, error: servicesError } = await supabase
|
||
.from('payment_services')
|
||
.select('id, name, icon');
|
||
if (servicesError) return res.status(400).json({ error: servicesError.message });
|
||
|
||
// Получаем детали по агрегаторам
|
||
const { data: details, error: detailsError } = await supabase
|
||
.from('payment_service_details')
|
||
.select('id, service_id, name, description');
|
||
if (detailsError) return res.status(400).json({ error: detailsError.message });
|
||
|
||
// Получаем платежи пользователя по деталям
|
||
const { data: payments, error: paymentsError } = await supabase
|
||
.from('payments')
|
||
.select('id, detail_id, amount, period, status, payment_method')
|
||
.eq('apartment_id', apartment_id)
|
||
.eq('user_id', user_id);
|
||
if (paymentsError) return res.status(400).json({ error: paymentsError.message });
|
||
|
||
// Формируем структуру для фронта
|
||
const result = services.map(service => {
|
||
const serviceDetails = details.filter(d => d.service_id === service.id).map(detail => {
|
||
const payment = payments.find(p => p.detail_id === detail.id) || {};
|
||
return {
|
||
id: detail.id,
|
||
name: detail.name,
|
||
description: detail.description,
|
||
amount: payment.amount || null,
|
||
period: payment.period || null,
|
||
status: payment.status || 'pending',
|
||
paymentMethod: payment.payment_method || null,
|
||
paymentId: payment.id || null,
|
||
};
|
||
});
|
||
return {
|
||
id: service.id,
|
||
name: service.name,
|
||
icon: service.icon,
|
||
details: serviceDetails,
|
||
};
|
||
});
|
||
|
||
res.json(result);
|
||
});
|
||
|
||
module.exports = router; |