|
| 1 | +import { supabase } from '@/lib/supabase/client'; |
| 2 | + |
| 3 | +export interface LaboratoryUpvote { |
| 4 | + id: string; |
| 5 | + user_id: string; |
| 6 | + username: string; |
| 7 | + laboratory_id: string; |
| 8 | + description: string | null; |
| 9 | + created_at: string; |
| 10 | + updated_at: string; |
| 11 | +} |
| 12 | + |
| 13 | +export interface CreateUpvoteRequest { |
| 14 | + user_id: string; |
| 15 | + username: string; |
| 16 | + laboratory_id: string; |
| 17 | +} |
| 18 | + |
| 19 | +export interface CheckUpvoteResponse { |
| 20 | + hasUpvoted: boolean; |
| 21 | + upvote?: LaboratoryUpvote; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Create or update laboratory upvote |
| 26 | + */ |
| 27 | +export async function createOrUpdateUpvote(request: CreateUpvoteRequest): Promise<LaboratoryUpvote> { |
| 28 | + // Check if upvote already exists for this user + laboratory combination |
| 29 | + const { data: existingUpvote, error: checkError } = await supabase |
| 30 | + .from('laboratory_feedback') |
| 31 | + .select('*') |
| 32 | + .eq('user_id', request.user_id) |
| 33 | + .eq('laboratory_id', request.laboratory_id) |
| 34 | + .maybeSingle(); |
| 35 | + |
| 36 | + if (checkError) { |
| 37 | + throw new Error(`Failed to check existing upvote: ${checkError.message}`); |
| 38 | + } |
| 39 | + |
| 40 | + // If upvote exists, update timestamp |
| 41 | + if (existingUpvote) { |
| 42 | + const typedUpvote = existingUpvote as LaboratoryUpvote; |
| 43 | + const { data, error } = await supabase |
| 44 | + .from('laboratory_feedback') |
| 45 | + .update({ |
| 46 | + updated_at: new Date().toISOString(), |
| 47 | + }) |
| 48 | + .eq('id', typedUpvote.id) |
| 49 | + .select() |
| 50 | + .single(); |
| 51 | + |
| 52 | + if (error || !data) { |
| 53 | + throw new Error(`Failed to update upvote: ${error?.message ?? 'No data returned'}`); |
| 54 | + } |
| 55 | + |
| 56 | + return data as LaboratoryUpvote; |
| 57 | + } |
| 58 | + |
| 59 | + // Otherwise, create new upvote |
| 60 | + const { data, error } = await supabase |
| 61 | + .from('laboratory_feedback') |
| 62 | + .insert({ |
| 63 | + user_id: request.user_id, |
| 64 | + username: request.username, |
| 65 | + laboratory_id: request.laboratory_id, |
| 66 | + description: null, |
| 67 | + }) |
| 68 | + .select() |
| 69 | + .single(); |
| 70 | + |
| 71 | + if (error || !data) { |
| 72 | + throw new Error(`Failed to create upvote: ${error?.message ?? 'No data returned'}`); |
| 73 | + } |
| 74 | + |
| 75 | + return data as LaboratoryUpvote; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Check if user has already upvoted for a specific laboratory |
| 80 | + */ |
| 81 | +export async function checkUserUpvote(userId: string, laboratoryId: string): Promise<CheckUpvoteResponse> { |
| 82 | + const { data, error } = await supabase |
| 83 | + .from('laboratory_feedback') |
| 84 | + .select('*') |
| 85 | + .eq('user_id', userId) |
| 86 | + .eq('laboratory_id', laboratoryId) |
| 87 | + .maybeSingle(); |
| 88 | + |
| 89 | + if (error) { |
| 90 | + throw new Error(`Failed to check user upvote: ${error.message}`); |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + hasUpvoted: !!data, |
| 95 | + upvote: data ? (data as LaboratoryUpvote) : undefined, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Get all laboratory upvotes for a specific laboratory |
| 101 | + */ |
| 102 | +export async function getAllUpvotes(laboratoryId: string): Promise<LaboratoryUpvote[]> { |
| 103 | + const { data, error } = await supabase |
| 104 | + .from('laboratory_feedback') |
| 105 | + .select('*') |
| 106 | + .eq('laboratory_id', laboratoryId) |
| 107 | + .order('created_at', { ascending: false }); |
| 108 | + |
| 109 | + if (error || !data) { |
| 110 | + throw new Error(`Failed to get upvotes: ${error?.message ?? 'No data returned'}`); |
| 111 | + } |
| 112 | + |
| 113 | + return data as LaboratoryUpvote[]; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Get upvote count for a specific laboratory |
| 118 | + */ |
| 119 | +export async function getUpvoteCount(laboratoryId: string): Promise<number> { |
| 120 | + const { count, error } = await supabase |
| 121 | + .from('laboratory_feedback') |
| 122 | + .select('*', { count: 'exact', head: true }) |
| 123 | + .eq('laboratory_id', laboratoryId); |
| 124 | + |
| 125 | + if (error) { |
| 126 | + throw new Error(`Failed to get upvote count: ${error.message}`); |
| 127 | + } |
| 128 | + |
| 129 | + return count ?? 0; |
| 130 | +} |
0 commit comments