|
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' |
3 | 3 |
|
4 | 4 | export async function GET(req: Request) { |
5 | 5 | try { |
6 | 6 | const url = new URL(req.url) |
7 | 7 | 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), |
18 | 14 | } |
19 | 15 |
|
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 }) |
25 | 18 | } |
26 | 19 |
|
27 | | - if (types?.length) { |
28 | | - where.eventType = { in: types } |
29 | | - } |
| 20 | + const { events, error } = await getEvents(params) |
30 | 21 |
|
31 | | - if (groups?.length) { |
32 | | - where.eventGroup = { in: groups } |
| 22 | + if (error) { |
| 23 | + return NextResponse.json(events, { status: 500 }) |
33 | 24 | } |
34 | 25 |
|
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', |
52 | 29 | }, |
53 | | - ) |
| 30 | + }) |
54 | 31 | } catch (error) { |
55 | 32 | console.error('[Dynamic Events Endpoint Error]:', error) |
56 | 33 | return Response.json( |
|
0 commit comments