diff --git a/src/app/api/postcode/LookupOsPlaces.ts b/src/app/api/postcode/LookupOsPlaces.ts index 1bdf590..7a8d320 100644 --- a/src/app/api/postcode/LookupOsPlaces.ts +++ b/src/app/api/postcode/LookupOsPlaces.ts @@ -24,9 +24,9 @@ async function fetchOsPlacesPage( retries: number, delay: number ): Promise<{ status: number; data?: OSPlacesResponse; error?: string }> { - const url = `${OS_API_URL}?postcode=${encodeURIComponent( + const url = `https://api.os.uk/search/places/v1/postcode?postcode=${encodeURIComponent( postcode - )}&key=${OS_API_KEY}&maxresults=${maxResults}&offset=${offset}`; + )}&dataset=DPA,LPI&maxresults=100&offset=${offset}&key=${process.env.OS_API_KEY}`; try { const res = await fetch(url, { method: "GET" }); diff --git a/src/app/api/postcode/[postcode]/addresses/route.ts b/src/app/api/postcode/[postcode]/addresses/route.ts index 0a4c9c6..08886f0 100644 --- a/src/app/api/postcode/[postcode]/addresses/route.ts +++ b/src/app/api/postcode/[postcode]/addresses/route.ts @@ -53,10 +53,20 @@ export async function GET( const addresses = mapOsPlacesToFlat(result.results); const total = result.data.header?.totalresults ?? addresses.length; - await db.insert(postcodeSearch).values({ - postcode: postcode, - resultData: result.data, - }); + // We shouldn't really run into conflicts here, but just in case, we allow for an update + await db + .insert(postcodeSearch) + .values({ + postcode, + resultData: result.data, + }) + .onConflictDoUpdate({ + target: postcodeSearch.postcode, + set: { + resultData: result.data, + createdAt: new Date(), // update timestamp + }, + }); // Step 4: return results return NextResponse.json({