Skip to content

Commit a71cd23

Browse files
james-crossJames-Cross
andauthored
fixes to findhelppage (#115)
* feat: fallback to static data if mongo fails * fixes find-help-page * fixes lint error --------- Co-authored-by: James-Cross <james@streetsupport.net>
1 parent 0a56a1b commit a71cd23

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/app/api/services/route.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextResponse } from 'next/server';
22
import { getClientPromise } from '@/utils/mongodb';
3+
import fallbackProviders from '@/data/service-providers.json';
34

45
export 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+
}

src/components/FindHelp/FindHelpResults.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ export default function FindHelpResults({ services }: Props) {
178178
<ServiceCard
179179
service={{
180180
...service,
181-
openTimes: service.openTimes.map(slot => ({
182-
day: slot.day.toString(),
183-
start: slot.start.toString(),
184-
end: slot.end.toString(),
181+
openTimes: (service.openTimes ?? []).map(slot => ({
182+
day: slot?.day?.toString?.() ?? '',
183+
start: slot?.start?.toString?.() ?? '',
184+
end: slot?.end?.toString?.() ?? '',
185185
})),
186186
}}
187187
isOpen={openDescriptionId === service.id}
@@ -215,4 +215,4 @@ export default function FindHelpResults({ services }: Props) {
215215
)}
216216
</section>
217217
);
218-
}
218+
}

0 commit comments

Comments
 (0)