feat: migrate app to local Fastify and Postgres stack
Replace Supabase auth and search runtime with a local Fastify API, PostgreSQL/PostGIS schema, and local session handling. Scaffold the worker and deep-research foundations while keeping the existing research, dashboard, and map flows running on the new backend.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import type { Pool } from 'pg';
|
||||
import { getEnv } from '../config/env.js';
|
||||
import { buildBusinessPayload, collectPlaces, geocodeLocation } from './google-places.js';
|
||||
import { completeSearchJob, createSearchJob, failSearchJob, updateSearchJobCenter, upsertBusiness, upsertSearchJobResult } from './repository.js';
|
||||
import type { RunSearchInput, RunSearchResult } from './types.js';
|
||||
|
||||
export async function runSearchForUser(db: Pool, userId: string, payload: RunSearchInput): Promise<RunSearchResult> {
|
||||
const env = getEnv();
|
||||
const job = await createSearchJob(db, userId, payload);
|
||||
const jobId = job.id;
|
||||
|
||||
try {
|
||||
if (!env.GOOGLE_MAPS_SERVER_KEY) {
|
||||
throw new Error('GOOGLE_MAPS_SERVER_KEY is required for running research.');
|
||||
}
|
||||
|
||||
const geocoded = await geocodeLocation(payload.location, env.GOOGLE_MAPS_SERVER_KEY);
|
||||
await updateSearchJobCenter(db, jobId, geocoded.lat, geocoded.lng);
|
||||
|
||||
const places = await collectPlaces({
|
||||
apiKey: env.GOOGLE_MAPS_SERVER_KEY,
|
||||
textQuery: [payload.businessType, payload.keywords].filter(Boolean).join(' '),
|
||||
lat: geocoded.lat,
|
||||
lng: geocoded.lng,
|
||||
radiusKm: payload.radiusKm,
|
||||
});
|
||||
|
||||
const matchedKeywords = payload.keywords
|
||||
? payload.keywords
|
||||
.split(',')
|
||||
.map((keyword) => keyword.trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
|
||||
const capturedAt = new Date().toISOString();
|
||||
let totalResults = 0;
|
||||
|
||||
for (const [index, place] of places.entries()) {
|
||||
if (!place.id || !place.displayName?.text) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const businessId = await upsertBusiness(db, userId, buildBusinessPayload(place, payload.businessType));
|
||||
await upsertSearchJobResult(db, {
|
||||
userId,
|
||||
searchJobId: jobId,
|
||||
businessId,
|
||||
matchedKeywords: matchedKeywords.length > 0 ? matchedKeywords : null,
|
||||
rank: index + 1,
|
||||
capturedAt,
|
||||
});
|
||||
|
||||
totalResults += 1;
|
||||
}
|
||||
|
||||
const completedJob = await completeSearchJob(db, jobId, totalResults);
|
||||
return {
|
||||
job: completedJob,
|
||||
totalResults,
|
||||
};
|
||||
} catch (error) {
|
||||
await failSearchJob(db, jobId);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user