101 lines
4.4 KiB
JavaScript
101 lines
4.4 KiB
JavaScript
const router = require('express').Router();
|
||
const { getSupabaseClient } = require('./supabaseClient');
|
||
|
||
// Получить все предложения, инициативы status=review (по дому)
|
||
router.get('/initiatives-review', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { building_id } = req.query;
|
||
let query = supabase.from('initiatives').select('*').eq('status', 'review');
|
||
if (building_id) query = query.eq('building_id', building_id);
|
||
const { data, error } = await query;
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json(data);
|
||
});
|
||
|
||
// Получить все сборы, инициативы status=fundraising (по дому)
|
||
router.get('/initiatives-fundraising', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { building_id } = req.query;
|
||
let query = supabase.from('initiatives').select('*').eq('status', 'fundraising');
|
||
if (building_id) query = query.eq('building_id', building_id);
|
||
const { data, error } = await query;
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json(data);
|
||
});
|
||
|
||
// Получить инициативу по id (и optionally building_id)
|
||
router.get('/initiatives/:id', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { id } = req.params;
|
||
const { building_id } = req.query;
|
||
let query = supabase.from('initiatives').select('*').eq('id', id);
|
||
if (building_id) query = query.eq('building_id', building_id);
|
||
const { data, error } = await query.single();
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json(data);
|
||
});
|
||
|
||
// Создать инициативу
|
||
router.post('/initiatives', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { building_id, creator_id, title, description, status, target_amount, image_url } = req.body;
|
||
const { data, error } = await supabase.from('initiatives').insert([
|
||
{ building_id, creator_id, title, description, status, target_amount, image_url }
|
||
]).select().single();
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json(data);
|
||
});
|
||
|
||
// Обновить инициативу
|
||
router.put('/initiatives/:id', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { id } = req.params;
|
||
const { title, description, status, target_amount, current_amount, image_url } = req.body;
|
||
const { data, error } = await supabase.from('initiatives').update({
|
||
title, description, status, target_amount, current_amount, image_url
|
||
}).eq('id', id).select().single();
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json(data);
|
||
});
|
||
|
||
// Удалить инициативу
|
||
router.delete('/initiatives/:id', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { id } = req.params;
|
||
const { error } = await supabase.from('initiatives').delete().eq('id', id);
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
res.json({ success: true });
|
||
});
|
||
|
||
// Получить все инициативы по квартире с голосами пользователя
|
||
router.get('/initiatives/by-apartment', async (req, res) => {
|
||
const supabase = getSupabaseClient();
|
||
const { apartment_id, user_id } = req.query;
|
||
if (!apartment_id) return res.status(400).json({ error: 'apartment_id required' });
|
||
// Получаем building_id квартиры
|
||
const { data: apartments, error: err1 } = await supabase
|
||
.from('apartments')
|
||
.select('building_id')
|
||
.eq('id', apartment_id)
|
||
.single();
|
||
if (err1) return res.status(400).json({ error: err1.message });
|
||
const building_id = apartments.building_id;
|
||
// Получаем инициативы этого дома с голосами пользователя (если user_id передан)
|
||
let selectStr = '*, votes:initiatives(id, votes!left(user_id, vote_type))';
|
||
if (!user_id) selectStr = '*';
|
||
const { data, error } = await supabase
|
||
.from('initiatives')
|
||
.select(selectStr)
|
||
.eq('building_id', building_id);
|
||
if (error) return res.status(400).json({ error: error.message });
|
||
// Если user_id передан, фильтруем только голос текущего пользователя
|
||
if (user_id && data) {
|
||
data.forEach(initiative => {
|
||
initiative.user_vote = (initiative.votes || []).find(v => v.user_id === user_id) || null;
|
||
delete initiative.votes;
|
||
});
|
||
}
|
||
res.json(data);
|
||
});
|
||
|
||
module.exports = router;
|