mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-11-02 02:19:43 +00:00
Update
This commit is contained in:
1 parent
8b73781272
commit
75509b0e45
41 files changed
+5896
-2931
No files matched your search
@@ -0,0 +1,145 @@
|
||||
import { supabase } from '@/config';
|
||||
import { auditedSupabase } from '@/hooks/useAuditedSupabase';
|
||||
|
||||
// =============================================================================
|
||||
// Types
|
||||
// =============================================================================
|
||||
|
||||
export interface Question {
|
||||
id: string;
|
||||
text: string;
|
||||
partner_text: string | null;
|
||||
intensity: number;
|
||||
pack_id: string;
|
||||
created_at: string | null;
|
||||
}
|
||||
|
||||
export interface QuestionFormData {
|
||||
text: string;
|
||||
partner_text: string;
|
||||
intensity: number;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Read Operations
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Fetch all questions for a pack
|
||||
*/
|
||||
export async function fetchQuestionsForPack(packId: string): Promise<Question[]> {
|
||||
const { data, error } = await supabase
|
||||
.from('questions')
|
||||
.select('*')
|
||||
.eq('pack_id', packId)
|
||||
.order('created_at', { ascending: true });
|
||||
|
||||
if (error) throw error;
|
||||
return data || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single question by ID
|
||||
*/
|
||||
export async function fetchQuestionById(id: string): Promise<Question | null> {
|
||||
const { data, error } = await supabase
|
||||
.from('questions')
|
||||
.select('*')
|
||||
.eq('id', id)
|
||||
.maybeSingle();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch question count for a pack
|
||||
*/
|
||||
export async function fetchQuestionCount(packId: string): Promise<number> {
|
||||
const { count, error } = await supabase
|
||||
.from('questions')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('pack_id', packId);
|
||||
|
||||
if (error) throw error;
|
||||
return count || 0;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Write Operations
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Create a new question
|
||||
*/
|
||||
export async function createQuestion(
|
||||
packId: string,
|
||||
data: QuestionFormData
|
||||
): Promise<Question> {
|
||||
const { data: created, error } = await auditedSupabase.insert('questions', {
|
||||
text: data.text,
|
||||
partner_text: data.partner_text || null,
|
||||
intensity: data.intensity,
|
||||
pack_id: packId,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
return created as Question;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing question
|
||||
*/
|
||||
export async function updateQuestion(
|
||||
id: string,
|
||||
data: Partial<QuestionFormData>
|
||||
): Promise<void> {
|
||||
const { error } = await auditedSupabase.update('questions', id, {
|
||||
text: data.text,
|
||||
partner_text: data.partner_text || null,
|
||||
intensity: data.intensity,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a question
|
||||
*/
|
||||
export async function deleteQuestion(id: string): Promise<void> {
|
||||
const { error } = await auditedSupabase.delete('questions', id);
|
||||
if (error) throw error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete multiple questions
|
||||
*/
|
||||
export async function deleteQuestions(ids: string[]): Promise<void> {
|
||||
await Promise.all(
|
||||
ids.map(id => auditedSupabase.delete('questions', id))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk create questions (for AI generation)
|
||||
*/
|
||||
export async function bulkCreateQuestions(
|
||||
packId: string,
|
||||
questions: Array<{ text: string; partner_text?: string; intensity: number }>
|
||||
): Promise<Question[]> {
|
||||
const results: Question[] = [];
|
||||
|
||||
for (const q of questions) {
|
||||
const { data, error } = await auditedSupabase.insert('questions', {
|
||||
pack_id: packId,
|
||||
text: q.text,
|
||||
partner_text: q.partner_text || null,
|
||||
intensity: q.intensity,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (data) results.push(data as Question);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
Reference in new issue
Block a user