176 lines
4.6 KiB
TypeScript
176 lines
4.6 KiB
TypeScript
import { supabase } from './supabase';
|
|
import { Campaign, Proposition, Participant } from '@/types';
|
|
|
|
// Services pour les campagnes
|
|
export const campaignService = {
|
|
async getAll(): Promise<Campaign[]> {
|
|
const { data, error } = await supabase
|
|
.from('campaigns')
|
|
.select('*')
|
|
.order('created_at', { ascending: false });
|
|
|
|
if (error) throw error;
|
|
return data || [];
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async create(campaign: any): Promise<Campaign> {
|
|
const { data, error } = await supabase
|
|
.from('campaigns')
|
|
.insert(campaign)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async update(id: string, updates: any): Promise<Campaign> {
|
|
const { data, error } = await supabase
|
|
.from('campaigns')
|
|
.update(updates)
|
|
.eq('id', id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
async delete(id: string): Promise<void> {
|
|
console.log('Tentative de suppression de la campagne:', id);
|
|
|
|
// La suppression en cascade est gérée par la base de données
|
|
// grâce à ON DELETE CASCADE dans les contraintes de clés étrangères
|
|
const { error } = await supabase
|
|
.from('campaigns')
|
|
.delete()
|
|
.eq('id', id);
|
|
|
|
if (error) {
|
|
console.error('Erreur lors de la suppression:', error);
|
|
throw error;
|
|
}
|
|
|
|
console.log('Campagne supprimée avec succès');
|
|
},
|
|
|
|
async getStats(campaignId: string): Promise<{ propositions: number; participants: number }> {
|
|
const [propositionsResult, participantsResult] = await Promise.all([
|
|
supabase
|
|
.from('propositions')
|
|
.select('id', { count: 'exact', head: true })
|
|
.eq('campaign_id', campaignId),
|
|
supabase
|
|
.from('participants')
|
|
.select('id', { count: 'exact', head: true })
|
|
.eq('campaign_id', campaignId)
|
|
]);
|
|
|
|
if (propositionsResult.error) throw propositionsResult.error;
|
|
if (participantsResult.error) throw participantsResult.error;
|
|
|
|
return {
|
|
propositions: propositionsResult.count || 0,
|
|
participants: participantsResult.count || 0
|
|
};
|
|
}
|
|
};
|
|
|
|
// Services pour les propositions
|
|
export const propositionService = {
|
|
async getByCampaign(campaignId: string): Promise<Proposition[]> {
|
|
const { data, error } = await supabase
|
|
.from('propositions')
|
|
.select('*')
|
|
.eq('campaign_id', campaignId)
|
|
.order('created_at', { ascending: false });
|
|
|
|
if (error) throw error;
|
|
return data || [];
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async create(proposition: any): Promise<Proposition> {
|
|
const { data, error } = await supabase
|
|
.from('propositions')
|
|
.insert(proposition)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async update(id: string, updates: any): Promise<Proposition> {
|
|
const { data, error } = await supabase
|
|
.from('propositions')
|
|
.update(updates)
|
|
.eq('id', id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
async delete(id: string): Promise<void> {
|
|
const { error } = await supabase
|
|
.from('propositions')
|
|
.delete()
|
|
.eq('id', id);
|
|
|
|
if (error) throw error;
|
|
}
|
|
};
|
|
|
|
// Services pour les participants
|
|
export const participantService = {
|
|
async getByCampaign(campaignId: string): Promise<Participant[]> {
|
|
const { data, error } = await supabase
|
|
.from('participants')
|
|
.select('*')
|
|
.eq('campaign_id', campaignId)
|
|
.order('created_at', { ascending: false });
|
|
|
|
if (error) throw error;
|
|
return data || [];
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async create(participant: any): Promise<Participant> {
|
|
const { data, error } = await supabase
|
|
.from('participants')
|
|
.insert(participant)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async update(id: string, updates: any): Promise<Participant> {
|
|
const { data, error } = await supabase
|
|
.from('participants')
|
|
.update(updates)
|
|
.eq('id', id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
|
|
async delete(id: string): Promise<void> {
|
|
const { error } = await supabase
|
|
.from('participants')
|
|
.delete()
|
|
.eq('id', id);
|
|
|
|
if (error) throw error;
|
|
}
|
|
};
|