11import { NextResponse } from 'next/server' ;
22import { getClientPromise } from '@/utils/mongodb' ;
3+ import fallbackProviders from '@/data/service-providers.json' ;
34
45export async function GET ( req : Request ) {
56 const { searchParams } = new URL ( req . url ) ;
@@ -21,7 +22,6 @@ export async function GET(req: Request) {
2122 const servicesCol = db . collection ( 'ProvidedServices' ) ;
2223 const providersCol = db . collection ( 'ServiceProviders' ) ;
2324
24- // ✅ Always filter to only published services
2525 const query : any = {
2626 IsPublished : true
2727 } ;
@@ -42,7 +42,6 @@ export async function GET(req: Request) {
4242 . limit ( limit )
4343 . toArray ( ) ;
4444
45- // Embed related org info in correct shape for ServiceCard
4645 const results = await Promise . all (
4746 services . map ( async ( service ) => {
4847 const provider = await providersCol . findOne (
@@ -83,9 +82,55 @@ export async function GET(req: Request) {
8382 } ) ;
8483 } catch ( error ) {
8584 console . error ( '[API ERROR] /api/services:' , error ) ;
86- return NextResponse . json (
87- { status : 'error' , message : 'Failed to fetch services' } ,
88- { status : 500 }
89- ) ;
85+
86+ // ✅ Fallback with proper typing
87+ try {
88+ interface RawProvider {
89+ name : string ;
90+ slug : string ;
91+ verified : boolean ;
92+ services ?: any [ ] ;
93+ }
94+
95+ const rawProviders = fallbackProviders as RawProvider [ ] ;
96+
97+ const allServices = rawProviders . flatMap ( ( provider ) =>
98+ ( provider . services ?? [ ] ) . map ( ( service : any ) => ( {
99+ id : service . id ,
100+ name : service . name ,
101+ category : service . category ,
102+ subCategory : service . subCategory ,
103+ description : service . description ,
104+ openTimes : service . openTimes ?? [ ] ,
105+ clientGroups : service . clientGroups ?? [ ] ,
106+ latitude : service . latitude ,
107+ longitude : service . longitude ,
108+ organisation : {
109+ name : provider . name ,
110+ slug : provider . slug ,
111+ isVerified : provider . verified ,
112+ } ,
113+ organisationSlug : provider . slug ,
114+ } ) )
115+ ) ;
116+
117+ const total = allServices . length ;
118+ const start = ( page - 1 ) * limit ;
119+ const results = allServices . slice ( start , start + limit ) ;
120+
121+ return NextResponse . json ( {
122+ status : 'success' ,
123+ total,
124+ page,
125+ limit,
126+ results,
127+ } ) ;
128+ } catch ( fallbackError ) {
129+ console . error ( '[API ERROR] Fallback services failed:' , fallbackError ) ;
130+ return NextResponse . json (
131+ { status : 'error' , message : 'Failed to fetch services' } ,
132+ { status : 500 }
133+ ) ;
134+ }
90135 }
91- }
136+ }
0 commit comments