liens publics pour voter pour les participants
This commit is contained in:
@@ -34,12 +34,28 @@ CREATE TABLE participants (
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Table des votes
|
||||
CREATE TABLE votes (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
campaign_id UUID NOT NULL REFERENCES campaigns(id) ON DELETE CASCADE,
|
||||
participant_id UUID NOT NULL REFERENCES participants(id) ON DELETE CASCADE,
|
||||
proposition_id UUID NOT NULL REFERENCES propositions(id) ON DELETE CASCADE,
|
||||
amount INTEGER NOT NULL CHECK (amount > 0),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(participant_id, proposition_id) -- Un seul vote par participant par proposition
|
||||
);
|
||||
|
||||
-- Index pour améliorer les performances
|
||||
CREATE INDEX idx_propositions_campaign_id ON propositions(campaign_id);
|
||||
CREATE INDEX idx_participants_campaign_id ON participants(campaign_id);
|
||||
CREATE INDEX idx_campaigns_status ON campaigns(status);
|
||||
CREATE INDEX idx_campaigns_created_at ON campaigns(created_at DESC);
|
||||
|
||||
-- Index pour optimiser les requêtes
|
||||
CREATE INDEX idx_votes_campaign_participant ON votes(campaign_id, participant_id);
|
||||
CREATE INDEX idx_votes_proposition ON votes(proposition_id);
|
||||
|
||||
-- Trigger pour mettre à jour updated_at automatiquement
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
@@ -54,10 +70,14 @@ CREATE TRIGGER update_campaigns_updated_at
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_votes_updated_at BEFORE UPDATE ON votes
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
-- Politique RLS (Row Level Security) - Activer pour toutes les tables
|
||||
ALTER TABLE campaigns ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE propositions ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE participants ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE votes ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Politiques pour permettre l'accès public (à adapter selon vos besoins d'authentification)
|
||||
CREATE POLICY "Allow public read access to campaigns" ON campaigns FOR SELECT USING (true);
|
||||
@@ -67,9 +87,16 @@ CREATE POLICY "Allow public delete access to campaigns" ON campaigns FOR DELETE
|
||||
|
||||
CREATE POLICY "Allow public read access to propositions" ON propositions FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow public insert access to propositions" ON propositions FOR INSERT WITH CHECK (true);
|
||||
CREATE POLICY "Allow public delete access to propositions" ON propositions FOR DELETE USING (true);
|
||||
|
||||
CREATE POLICY "Allow public read access to participants" ON participants FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow public insert access to participants" ON participants FOR INSERT WITH CHECK (true);
|
||||
CREATE POLICY "Allow public delete access to participants" ON participants FOR DELETE USING (true);
|
||||
|
||||
CREATE POLICY "Allow public read access to votes" ON votes FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow public insert access to votes" ON votes FOR INSERT WITH CHECK (true);
|
||||
CREATE POLICY "Allow public update access to votes" ON votes FOR UPDATE USING (true);
|
||||
CREATE POLICY "Allow public delete access to votes" ON votes FOR DELETE USING (true);
|
||||
|
||||
-- Données d'exemple (optionnel)
|
||||
INSERT INTO campaigns (title, description, status, budget_per_user, spending_tiers) VALUES
|
||||
|
||||
Reference in New Issue
Block a user