105 lines
3.8 KiB
JavaScript
105 lines
3.8 KiB
JavaScript
const router = require('express').Router();
|
|
const { getSupabaseClient } = require('./supabaseClient');
|
|
|
|
// Получить все голоса по инициативе
|
|
router.get('/votes/:initiative_id', async (req, res) => {
|
|
const supabase = getSupabaseClient();
|
|
const { initiative_id } = req.params;
|
|
const { data, error } = await supabase.from('votes').select('*').eq('initiative_id', initiative_id);
|
|
if (error)
|
|
return res.status(400).json({ error: error.message });
|
|
res.json(data);
|
|
});
|
|
|
|
// Получить голос пользователя по инициативе
|
|
router.get('/votes/:initiative_id/user/:user_id', async (req, res) => {
|
|
const supabase = getSupabaseClient();
|
|
const { initiative_id, user_id } = req.params;
|
|
const { data, error } = await supabase.from('votes').select('*').eq('initiative_id', initiative_id).eq('user_id', user_id).single();
|
|
if (error) {
|
|
console.log(error, '/votes/:initiative_id/:user_id')
|
|
console.log(initiative_id, user_id)
|
|
return res.status(400).json({ error: error.message });
|
|
}
|
|
res.json(data);
|
|
});
|
|
|
|
// Получить статистику голосов по инициативе
|
|
router.get('/votes/stats/:initiative_id', async (req, res) => {
|
|
const supabase = getSupabaseClient();
|
|
const { initiative_id } = req.params;
|
|
|
|
const { data, error } = await supabase
|
|
.from('votes')
|
|
.select('vote_type')
|
|
.eq('initiative_id', initiative_id);
|
|
console.log(data, error)
|
|
if (error) {
|
|
console.log('/votes/:initiative_id/stats')
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
const stats = {
|
|
for: data.filter(vote => vote.vote_type === 'for').length,
|
|
against: data.filter(vote => vote.vote_type === 'against').length,
|
|
total: data.length
|
|
};
|
|
|
|
res.json(stats);
|
|
});
|
|
|
|
// Проголосовать (создать, обновить или удалить голос)
|
|
router.post('/votes', async (req, res) => {
|
|
const supabase = getSupabaseClient();
|
|
const { initiative_id, user_id, vote_type } = req.body;
|
|
|
|
// Проверяем существующий голос
|
|
const { data: existingVote, error: checkError } = await supabase
|
|
.from('votes')
|
|
.select('*')
|
|
.eq('initiative_id', initiative_id)
|
|
.eq('user_id', user_id)
|
|
.single();
|
|
|
|
if (checkError && checkError.code !== 'PGRST116') {
|
|
console.log('1/votes')
|
|
return res.status(400).json({ error: checkError.message });
|
|
}
|
|
|
|
if (existingVote) {
|
|
if (existingVote.vote_type === vote_type) {
|
|
// Если нажали тот же тип голоса - УДАЛЯЕМ (отменяем голос)
|
|
const { error: deleteError } = await supabase
|
|
.from('votes')
|
|
.delete()
|
|
.eq('initiative_id', initiative_id)
|
|
.eq('user_id', user_id);
|
|
|
|
if (deleteError) return res.status(400).json({ error: deleteError.message });
|
|
res.json({ message: 'Vote removed', action: 'removed', previous_vote: existingVote.vote_type });
|
|
} else {
|
|
// Если нажали другой тип голоса - ОБНОВЛЯЕМ
|
|
const { data, error } = await supabase
|
|
.from('votes')
|
|
.update({ vote_type })
|
|
.eq('initiative_id', initiative_id)
|
|
.eq('user_id', user_id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) return res.status(400).json({ error: error.message });
|
|
res.json({ ...data, action: 'updated', previous_vote: existingVote.vote_type });
|
|
}
|
|
} else {
|
|
// Если голоса нет - СОЗДАЕМ новый
|
|
const { data, error } = await supabase
|
|
.from('votes')
|
|
.insert([{ initiative_id, user_id, vote_type }])
|
|
.select()
|
|
.single();
|
|
|
|
if (error) return res.status(400).json({ error: error.message });
|
|
res.json({ ...data, action: 'created' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|