-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhackathons.ts
More file actions
339 lines (280 loc) · 10.2 KB
/
hackathons.ts
File metadata and controls
339 lines (280 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Server-side service for hackathons
import { createClient } from '@/lib/supabase/server'
import { Hackathon, HackathonsFilters, HackathonsResponse } from '@/types/hackathons'
// Re-export types for convenience
export type { Hackathon, HackathonsFilters, HackathonsResponse }
// Simple in-memory cache for development
const cache = new Map<string, { data: unknown; timestamp: number }>()
const CACHE_DURATION = 5 * 60 * 1000 // 5 minutes
function getCachedData(key: string) {
const cached = cache.get(key)
if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
return cached.data
}
return null
}
function setCachedData(key: string, data: unknown) {
cache.set(key, { data, timestamp: Date.now() })
}
class HackathonsService {
async getHackathons(filters: HackathonsFilters = {}): Promise<HackathonsResponse> {
const cacheKey = `hackathons:${JSON.stringify(filters)}`
const cached = getCachedData(cacheKey)
if (cached) {
return cached as HackathonsResponse
}
const supabase = await createClient()
let query = supabase
.from('hackathons')
.select(`
*,
company:companies(*)
`, { count: 'exact' })
// Only show approved hackathons by default (unless approval_status filter is explicitly provided)
if (filters.approval_status) {
query = query.eq('approval_status', filters.approval_status)
} else {
query = query.eq('approval_status', 'approved')
}
// Apply filters
if (filters.search) {
query = query.or(`title.ilike.%${filters.search}%,description.ilike.%${filters.search}%`)
}
if (filters.category) {
query = query.eq('category', filters.category)
}
if (filters.status) {
query = query.eq('status', filters.status)
}
if (filters.featured !== undefined) {
query = query.eq('featured', filters.featured)
}
if (filters.company_id) {
query = query.eq('company_id', filters.company_id)
}
// Filter by company industry
if (filters.company_industry) {
query = query.eq('company.industry', filters.company_industry)
}
// Filter by company size
if (filters.company_size) {
query = query.eq('company.company_size', filters.company_size)
}
if (filters.dateFilter === 'upcoming') {
query = query.gte('date', new Date().toISOString().split('T')[0])
} else if (filters.dateFilter === 'past') {
query = query.lt('date', new Date().toISOString().split('T')[0])
}
// Apply pagination
const limit = filters.limit || 10
const offset = filters.offset || 0
query = query.range(offset, offset + limit - 1)
// Order by date
query = query.order('date', { ascending: true })
const { data: hackathons, error, count } = await query
if (error) {
console.error('Error fetching hackathons:', error)
throw new Error('Failed to fetch hackathons')
}
const total = count || 0
const hasMore = offset + limit < total
const result = {
hackathons: hackathons || [],
total,
hasMore
}
setCachedData(cacheKey, result)
return result
}
async getHackathonBySlug(slug: string): Promise<Hackathon | null> {
const cacheKey = `hackathon:${slug}`
const cached = getCachedData(cacheKey)
if (cached) {
return cached as Hackathon
}
const supabase = await createClient()
const { data: hackathon, error } = await supabase
.from('hackathons')
.select(`
*,
company:companies(*)
`)
.eq('slug', slug)
.single()
if (error) {
if (error.code === 'PGRST116') {
return null // Hackathon not found
}
console.error('Error fetching hackathon:', error)
throw new Error('Failed to fetch hackathon')
}
setCachedData(cacheKey, hackathon)
return hackathon
}
async getFeaturedHackathons(limit: number = 5) {
const cacheKey = `featured_hackathons:${limit}`
const cached = getCachedData(cacheKey)
if (cached) {
return cached as Hackathon[]
}
try {
const supabase = await createClient()
const { data: hackathons, error } = await supabase
.from('hackathons')
.select(`
*,
company:companies(*)
`)
.eq('featured', true)
.eq('approval_status', 'approved')
.gte('date', new Date().toISOString().split('T')[0])
.order('date', { ascending: true })
.limit(limit)
if (error) {
console.error('Error fetching featured hackathons:', error)
// Return empty array instead of throwing to prevent 500 errors
return []
}
const result = hackathons || []
setCachedData(cacheKey, result)
return result
} catch (error) {
console.error('Error in getFeaturedHackathons:', error)
// Return empty array as fallback
return []
}
}
async createHackathon(hackathonData: Omit<Hackathon, 'id' | 'created_at' | 'updated_at'>): Promise<Hackathon> {
const supabase = await createClient()
const { data: hackathon, error } = await supabase
.from('hackathons')
.insert([hackathonData])
.select()
.single()
if (error) {
console.error('Error creating hackathon:', error)
throw new Error('Failed to create hackathon')
}
// Clear cache after creating new hackathon
cache.clear()
return hackathon
}
async updateHackathon(
slug: string,
hackathonData: Partial<Omit<Hackathon, 'id' | 'created_at' | 'updated_at'>>,
userId?: string
): Promise<Hackathon> {
const supabase = await createClient()
// Get existing hackathon first
const existingHackathon = await this.getHackathonBySlug(slug)
if (!existingHackathon) {
throw new Error('Hackathon not found')
}
// Check if this is an approved hackathon being edited
const needsReapproval = existingHackathon.approval_status === 'approved'
// Prepare update payload
const updatePayload: Record<string, unknown> = {
...hackathonData,
updated_at: new Date().toISOString(),
}
// If hackathon was approved, reset to pending for re-approval
if (needsReapproval) {
updatePayload.approval_status = 'pending'
updatePayload.approved_by = null
updatePayload.approved_at = null
console.log(`🔄 Hackathon ${existingHackathon.id} was approved, resetting to pending for re-approval`)
}
const { data: hackathon, error } = await supabase
.from('hackathons')
.update(updatePayload)
.eq('slug', slug)
.select(`
*,
company:companies(*)
`)
.single()
if (error) {
console.error('Error updating hackathon:', error)
throw new Error('Failed to update hackathon')
}
// If hackathon needed re-approval, create notifications and log
if (needsReapproval && userId) {
const hackathonId = existingHackathon.id
if (!hackathonId) {
console.error('Hackathon ID is missing, cannot create notifications')
cache.clear()
return hackathon
}
// Import services dynamically to avoid circular dependencies
const { NotificationService } = await import('./notification-service')
const { moderationService } = await import('./moderation-service')
// Log the edit action
await moderationService.logModerationAction('edited', undefined, hackathonId, userId, 'Hackathon edited after approval - requires re-approval')
// Notify admins about the updated hackathon
const { data: adminUsers } = await supabase
.from('profiles')
.select('id')
.eq('role', 'admin')
if (adminUsers && adminUsers.length > 0) {
const notifications = adminUsers.map(admin => ({
user_id: admin.id,
company_id: existingHackathon.company_id,
type: 'hackathon_updated' as const,
title: 'Hackathon Updated - Requires Re-approval',
message: `"${existingHackathon.title}" has been edited and requires re-approval`,
action_url: `/admin/moderation/hackathons/${hackathonId}`,
action_label: 'Review Hackathon',
hackathon_id: hackathonId.toString(),
metadata: {
hackathon_title: existingHackathon.title,
hackathon_slug: existingHackathon.slug
}
}))
await supabase.from('notifications').insert(notifications)
console.log(`📧 Notified ${adminUsers.length} admin(s) about updated hackathon`)
}
// Notify company members about the status change
if (existingHackathon.company_id) {
await NotificationService.notifyCompanyMembers(existingHackathon.company_id, {
type: 'hackathon_status_changed',
title: 'Hackathon Requires Re-approval',
message: `Your hackathon "${existingHackathon.title}" has been edited and requires re-approval`,
company_id: existingHackathon.company_id,
action_url: `/dashboard/company/hackathons/${existingHackathon.slug}`,
action_label: 'View Hackathon',
hackathon_id: hackathonId.toString(),
metadata: {
hackathon_title: existingHackathon.title,
old_status: 'approved',
new_status: 'pending'
}
})
}
}
// Clear cache after updating hackathon
cache.clear()
return hackathon
}
async deleteHackathon(slug: string) {
const supabase = await createClient()
console.log('🗑️ Deleting hackathon with slug:', slug)
const { data, error } = await supabase
.from('hackathons')
.delete()
.eq('slug', slug)
.select()
if (error) {
console.error('❌ Error deleting hackathon from database:', error)
throw new Error(`Failed to delete hackathon: ${error.message}`)
}
if (!data || data.length === 0) {
console.warn('⚠️ No hackathon was deleted. Slug might not exist or RLS policy blocked deletion.')
throw new Error('Hackathon not found or you do not have permission to delete it')
}
console.log('✅ Hackathon deleted from database:', data)
// Clear cache after deleting hackathon
cache.clear()
return true
}
}
export const hackathonsService = new HackathonsService()