Files
Sauci/apps/admin/src/services/questions.ts
T
jjbrunton 4e40b92b16 Add MCP server and enhance AI content tools
Implement self-hosted MCP server for admin actions, update AI generation tools in admin dashboard, and refine mobile app stores and components.
2026-01-10 19:24:06 +00:00

147 lines
3.8 KiB
TypeScript

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;
if (!created || created.length === 0) throw new Error('Failed to create question');
return created[0] 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 && data.length > 0) results.push(data[0] as Question);
}
return results;
}