change services api
This commit is contained in:
@@ -1,17 +1,56 @@
|
||||
const router = require('express').Router();
|
||||
const { getSupabaseClient } = require('./supabaseClient');
|
||||
|
||||
// Получить все платежи по конкретной квартире с данными сервиса
|
||||
router.get('/utility-payments', async (req, res) => {
|
||||
// Получить агрегированные сервисы с деталями и статусами оплаты для квартиры
|
||||
router.get('/payment-services', async (req, res) => {
|
||||
const supabase = getSupabaseClient();
|
||||
const { apartment_id } = req.query;
|
||||
if (!apartment_id) return res.status(400).json({ error: 'apartment_id required' });
|
||||
const { data, error } = await supabase
|
||||
.from('utility_payments')
|
||||
.select('*, payment_services(*)')
|
||||
.eq('apartment_id', apartment_id);
|
||||
if (error) return res.status(400).json({ error: error.message });
|
||||
res.json(data);
|
||||
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;
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user