liens publics pour voter pour les participants
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { supabase } from './supabase';
|
||||
import { Campaign, Proposition, Participant } from '@/types';
|
||||
import { Campaign, Proposition, Participant, Vote, ParticipantWithVoteStatus } from '@/types';
|
||||
|
||||
// Services pour les campagnes
|
||||
export const campaignService = {
|
||||
@@ -173,3 +173,99 @@ export const participantService = {
|
||||
if (error) throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Services pour les votes
|
||||
export const voteService = {
|
||||
async getByParticipant(campaignId: string, participantId: string): Promise<Vote[]> {
|
||||
const { data, error } = await supabase
|
||||
.from('votes')
|
||||
.select('*')
|
||||
.eq('campaign_id', campaignId)
|
||||
.eq('participant_id', participantId);
|
||||
|
||||
if (error) throw error;
|
||||
return data || [];
|
||||
},
|
||||
|
||||
async getByProposition(propositionId: string): Promise<Vote[]> {
|
||||
const { data, error } = await supabase
|
||||
.from('votes')
|
||||
.select('*')
|
||||
.eq('proposition_id', propositionId);
|
||||
|
||||
if (error) throw error;
|
||||
return data || [];
|
||||
},
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async create(vote: any): Promise<Vote> {
|
||||
const { data, error } = await supabase
|
||||
.from('votes')
|
||||
.insert(vote)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async update(id: string, updates: any): Promise<Vote> {
|
||||
const { data, error } = await supabase
|
||||
.from('votes')
|
||||
.update(updates)
|
||||
.eq('id', id)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async upsert(vote: { campaign_id: string; participant_id: string; proposition_id: string; amount: number }): Promise<Vote> {
|
||||
const { data, error } = await supabase
|
||||
.from('votes')
|
||||
.upsert(vote, { onConflict: 'participant_id,proposition_id' })
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
const { error } = await supabase
|
||||
.from('votes')
|
||||
.delete()
|
||||
.eq('id', id);
|
||||
|
||||
if (error) throw error;
|
||||
},
|
||||
|
||||
async getParticipantVoteStatus(campaignId: string): Promise<ParticipantWithVoteStatus[]> {
|
||||
const { data: participants, error: participantsError } = await supabase
|
||||
.from('participants')
|
||||
.select('*')
|
||||
.eq('campaign_id', campaignId);
|
||||
|
||||
if (participantsError) throw participantsError;
|
||||
|
||||
const { data: votes, error: votesError } = await supabase
|
||||
.from('votes')
|
||||
.select('*')
|
||||
.eq('campaign_id', campaignId);
|
||||
|
||||
if (votesError) throw votesError;
|
||||
|
||||
return participants.map(participant => {
|
||||
const participantVotes = votes.filter(vote => vote.participant_id === participant.id);
|
||||
const totalVotedAmount = participantVotes.reduce((sum, vote) => sum + vote.amount, 0);
|
||||
|
||||
return {
|
||||
...participant,
|
||||
has_voted: participantVotes.length > 0,
|
||||
total_voted_amount: totalVotedAmount
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user