Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions app/api/companies/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,49 @@ export async function GET(
}
}

// Get count of approved events for this company
// Get count of approved events and hackathons for this company
const supabase = await createClient()
const { count: approvedCount } = await supabase
const { count: approvedEventsCount } = await supabase
.from('events')
.select('*', { count: 'exact', head: true })
.eq('company_id', company.id)
.eq('approval_status', 'approved')

// Add approved_events_count to company object
const companyWithApprovedCount = {
const { count: approvedHackathonsCount } = await supabase
.from('hackathons')
.select('*', { count: 'exact', head: true })
.eq('company_id', company.id)
.eq('approval_status', 'approved')

// Get total participants from events
const { data: events } = await supabase
.from('events')
.select('registered')
.eq('company_id', company.id)
.eq('approval_status', 'approved')

// Get total participants from hackathons
const { data: hackathons } = await supabase
.from('hackathons')
.select('registered')
.eq('company_id', company.id)
.eq('approval_status', 'approved')

const eventParticipants = events?.reduce((sum, event) => sum + (event.registered || 0), 0) || 0
const hackathonParticipants = hackathons?.reduce((sum, hackathon) => sum + (hackathon.registered || 0), 0) || 0

// Add calculated fields to company object
const enrichedCompany = {
...company,
approved_events_count: approvedCount || 0,
approved_events_count: approvedEventsCount || 0,
approved_hackathons_count: approvedHackathonsCount || 0,
total_participants: eventParticipants + hackathonParticipants,
}

// Cache the result
await UnifiedCache.set(cacheKey, { company: companyWithApprovedCount }, 'API_STANDARD')
await UnifiedCache.set(cacheKey, { company: enrichedCompany }, 'API_STANDARD')

return UnifiedCache.createResponse({ company: companyWithApprovedCount }, 'API_STANDARD')
return UnifiedCache.createResponse({ company: enrichedCompany }, 'API_STANDARD')
} catch (error) {
console.error('Error in GET /api/companies/[slug]:', error)

Expand Down
47 changes: 26 additions & 21 deletions app/companies/[slug]/hackathons/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ interface Hackathon {
description: string
excerpt: string
image?: string
start_date: string
end_date: string
location: string
mode: string
date: string
registration_deadline?: string
location?: string
mode?: string
prize_pool?: string
max_team_size: number
min_team_size: number
registered_teams: number
max_teams: number
max_team_size?: number
min_team_size?: number
registered?: number
capacity?: number
status: string
tags: string[]
organizer: string
organizer?: string
total_registrations?: number
duration?: string
}

export default function CompanyHackathonsPage() {
Expand Down Expand Up @@ -336,27 +338,30 @@ export default function CompanyHackathonsPage() {
<div className="grid grid-cols-2 gap-2 text-xs mb-2">
<div className="flex items-center gap-1">
<Calendar className="h-3 w-3" />
{new Date(hackathon.start_date).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
})}
{hackathon.date && !isNaN(new Date(hackathon.date).getTime())
? new Date(hackathon.date).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
})
: "TBA"}
</div>
<div className="flex items-center gap-1">
<Clock className="h-3 w-3" />
{Math.ceil(
(new Date(hackathon.end_date).getTime() -
new Date(hackathon.start_date).getTime()) /
(1000 * 60 * 60 * 24)
)}{" "}
days
{hackathon.registration_deadline && !isNaN(new Date(hackathon.registration_deadline).getTime())
? `Reg: ${new Date(hackathon.registration_deadline).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
})}`
: hackathon.duration || "TBA"}
</div>
<div className="flex items-center gap-1">
<MapPin className="h-3 w-3" />
{hackathon.mode}
{hackathon.location || hackathon.mode || "TBA"}
</div>
<div className="flex items-center gap-1">
<Users className="h-3 w-3" />
{hackathon.registered_teams}/{hackathon.max_teams} teams
{hackathon.registered || 0} registered
</div>
</div>

Expand Down
14 changes: 10 additions & 4 deletions components/companies/CompanyCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ export function CompanyCard({
{showStats && (
<CardFooter className="border-t pt-4">
<div className="flex items-center justify-between w-full text-xs text-muted-foreground">
<div className="flex items-center gap-1">
<Calendar className="h-3.5 w-3.5" />
<span>{company.approved_events_count ?? company.total_events ?? 0} events</span>
<div className="flex items-center gap-4">
<div className="flex items-center gap-1">
<Calendar className="h-3.5 w-3.5" />
<span>{company.approved_events_count ?? company.total_events ?? 0} events</span>
</div>
<div className="flex items-center gap-1">
<Calendar className="h-3.5 w-3.5" />
<span>{company.approved_hackathons_count ?? company.total_hackathons ?? 0} hackathons</span>
</div>
</div>
<div className="flex items-center gap-1">
<Users className="h-3.5 w-3.5" />
<span>{company.total_participants} participants</span>
<span>{company.total_participants ?? 0} participants</span>
</div>
</div>
</CardFooter>
Expand Down
6 changes: 3 additions & 3 deletions components/companies/CompanyProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ export function CompanyProfile({ company, isOwner = false, className }: CompanyP
</div>
<div className="space-y-1">
<p className="text-2xl font-bold text-primary">
{company.total_hackathons || 0}
{company.approved_hackathons_count ?? company.total_hackathons ?? 0}
</p>
<p className="text-xs text-muted-foreground">Hackathons</p>
</div>
<div className="space-y-1">
<p className="text-2xl font-bold text-primary">
{company.total_registrations || 0}
{company.total_participants ?? 0}
</p>
<p className="text-xs text-muted-foreground">Registrations</p>
<p className="text-xs text-muted-foreground">Participants</p>
</div>
</div>
</CardContent>
Expand Down
27 changes: 26 additions & 1 deletion lib/services/company-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class CompanyService {
)
}

// For each company, get the count of approved events
// For each company, get the count of approved events, hackathons, and total participants
const companiesWithApprovedCount = await Promise.all(
(companies || []).map(async (company) => {
const { count: approvedCount } = await supabase
Expand All @@ -380,9 +380,34 @@ class CompanyService {
.eq('company_id', company.id)
.eq('approval_status', 'approved')

const { count: hackathonsCount } = await supabase
.from('hackathons')
.select('*', { count: 'exact', head: true })
.eq('company_id', company.id)
.eq('approval_status', 'approved')

// Get total participants from events
const { data: events } = await supabase
.from('events')
.select('registered')
.eq('company_id', company.id)
.eq('approval_status', 'approved')

// Get total participants from hackathons
const { data: hackathons } = await supabase
.from('hackathons')
.select('registered')
.eq('company_id', company.id)
.eq('approval_status', 'approved')

const eventParticipants = events?.reduce((sum, event) => sum + (event.registered || 0), 0) || 0
const hackathonParticipants = hackathons?.reduce((sum, hackathon) => sum + (hackathon.registered || 0), 0) || 0

return {
...company,
approved_events_count: approvedCount || 0,
approved_hackathons_count: hackathonsCount || 0,
total_participants: eventParticipants + hackathonParticipants,
}
})
)
Expand Down
1 change: 1 addition & 0 deletions types/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Company {
total_participants: number
total_registrations: number
approved_events_count?: number // Count of approved events only (for public display)
approved_hackathons_count?: number // Count of approved hackathons only (for public display)
}

export interface CompanyAddress {
Expand Down
Loading