Skip to content

Commit ad49cb9

Browse files
committed
WIP - fix events endpoint
1 parent a664a70 commit ad49cb9

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

src/app/api/events/route.ts

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,33 @@
1-
import config from '@/payload.config'
2-
import { getPayload, Where } from 'payload'
1+
import { getEvents } from '@/utilities/queries/getEvents'
2+
import { NextResponse } from 'next/server'
33

44
export async function GET(req: Request) {
55
try {
66
const url = new URL(req.url)
77
const searchParams = url.searchParams
8-
9-
const tenantId = searchParams.get('tenantId')
10-
const maxEvents = parseInt(searchParams.get('limit') || '4')
11-
const depth = parseInt(searchParams.get('depth') || '1')
12-
const types = searchParams.get('types')?.split(',').filter(Boolean)
13-
const groups = searchParams.get('groups')?.split(',').filter(Boolean)
14-
const tags = searchParams.get('tags')?.split(',').filter(Boolean)
15-
16-
if (!tenantId) {
17-
return Response.json({ error: 'tenantId is required' }, { status: 400 })
8+
const params = {
9+
center: String(searchParams.get('tenantId')),
10+
limit: parseInt(searchParams.get('limit') || '4'),
11+
types: searchParams.get('types')?.split(',').filter(Boolean),
12+
groups: searchParams.get('groups')?.split(',').filter(Boolean),
13+
tags: searchParams.get('tags')?.split(',').filter(Boolean),
1814
}
1915

20-
const payload = await getPayload({ config })
21-
22-
const where: Where = {
23-
tenant: { equals: tenantId },
24-
startDate: { greater_than: new Date().toISOString() },
16+
if (!params.center) {
17+
return Response.json({ error: 'tenantId is required' }, { status: 400 })
2518
}
2619

27-
if (types?.length) {
28-
where.eventType = { in: types }
29-
}
20+
const { events, error } = await getEvents(params)
3021

31-
if (groups?.length) {
32-
where.eventGroup = { in: groups }
22+
if (error) {
23+
return NextResponse.json(events, { status: 500 })
3324
}
3425

35-
if (tags?.length) {
36-
where.eventTags = { in: tags }
37-
}
38-
39-
const data = await payload.find({
40-
collection: 'events',
41-
where,
42-
limit: maxEvents,
43-
depth,
44-
})
45-
46-
return Response.json(
47-
{ docs: data.docs || [] },
48-
{
49-
headers: {
50-
'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=300',
51-
},
26+
return NextResponse.json(events, {
27+
headers: {
28+
'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=300',
5229
},
53-
)
30+
})
5431
} catch (error) {
5532
console.error('[Dynamic Events Endpoint Error]:', error)
5633
return Response.json(

src/fields/EventQuery/QueriedEventsComponent.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ export const QueriedEventsComponent = ({ path, field }: QueriedEventsComponentPr
5454
if (!tenantId) return
5555

5656
const params = new URLSearchParams({
57+
tenantId: String(tenantId),
5758
limit: String(maxEvents || 4),
5859
depth: '1',
59-
'where[tenant][equals]': String(tenantId),
6060
})
6161

6262
if (sortBy) {
@@ -71,7 +71,7 @@ export const QueriedEventsComponent = ({ path, field }: QueriedEventsComponentPr
7171
const groupIds = filterByEventGroups.filter(Boolean)
7272

7373
if (groupIds.length > 0) {
74-
params.append('where[type][in]', groupIds.join(','))
74+
params.append('groups', groupIds.join(','))
7575
}
7676
}
7777

@@ -83,18 +83,18 @@ export const QueriedEventsComponent = ({ path, field }: QueriedEventsComponentPr
8383
const typeIds = filterByEventTypes.filter(Boolean)
8484

8585
if (typeIds.length > 0) {
86-
params.append('where[type][in]', typeIds.join(','))
86+
params.append('types', typeIds.join(','))
8787
}
8888
}
8989

9090
if (filterByEventTags && Array.isArray(filterByEventTags) && filterByEventTags.length > 0) {
9191
const tagIds = filterByEventTags.filter(Boolean)
9292

9393
if (tagIds.length > 0) {
94-
params.append('where[tags][in]', tagIds.join(','))
94+
params.append('tags', tagIds.join(','))
9595
}
9696
}
97-
params.append('where[startDate][greater_than]', new Date().toISOString())
97+
// params.append('startDate', new Date().toISOString())
9898

9999
const response = await fetch(`/api/events?${params.toString()}`)
100100
if (!response.ok) {

0 commit comments

Comments
 (0)