- Remove non-existent DutchieClient import from product-resync and entry-point-discovery - Remove non-existent DiscoveryCrawler import from store-discovery - Use scrapeStore from scraper-v2 for product resync - Use discoverState from discovery module for store discovery - Fix Pool type by using getPool() instead of pool wrapper - Update FullDiscoveryResult property access to use correct field names 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
/**
|
|
* Store Discovery Handler
|
|
*
|
|
* Discovers new stores by crawling location APIs and adding them
|
|
* to discovery_locations table.
|
|
*/
|
|
|
|
import { TaskContext, TaskResult } from '../task-worker';
|
|
import { discoverState } from '../../discovery';
|
|
|
|
export async function handleStoreDiscovery(ctx: TaskContext): Promise<TaskResult> {
|
|
const { pool, task } = ctx;
|
|
const platform = task.platform || 'default';
|
|
|
|
console.log(`[StoreDiscovery] Starting discovery for platform: ${platform}`);
|
|
|
|
try {
|
|
// Get states to discover
|
|
const statesResult = await pool.query(`
|
|
SELECT code FROM states WHERE active = true ORDER BY code
|
|
`);
|
|
const stateCodes = statesResult.rows.map(r => r.code);
|
|
|
|
if (stateCodes.length === 0) {
|
|
return { success: true, storesDiscovered: 0, message: 'No active states to discover' };
|
|
}
|
|
|
|
let totalDiscovered = 0;
|
|
let totalPromoted = 0;
|
|
|
|
// Run discovery for each state
|
|
for (const stateCode of stateCodes) {
|
|
// Heartbeat before each state
|
|
await ctx.heartbeat();
|
|
|
|
console.log(`[StoreDiscovery] Discovering stores in ${stateCode}...`);
|
|
|
|
try {
|
|
const result = await discoverState(pool, stateCode);
|
|
totalDiscovered += result.totalLocationsFound || 0;
|
|
totalPromoted += result.totalLocationsUpserted || 0;
|
|
console.log(`[StoreDiscovery] ${stateCode}: found ${result.totalLocationsFound}, upserted ${result.totalLocationsUpserted}`);
|
|
} catch (error: unknown) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
console.error(`[StoreDiscovery] Error discovering ${stateCode}:`, errorMessage);
|
|
// Continue with other states
|
|
}
|
|
}
|
|
|
|
console.log(`[StoreDiscovery] Complete: ${totalDiscovered} discovered, ${totalPromoted} promoted`);
|
|
|
|
return {
|
|
success: true,
|
|
storesDiscovered: totalDiscovered,
|
|
storesPromoted: totalPromoted,
|
|
statesProcessed: stateCodes.length,
|
|
};
|
|
} catch (error: unknown) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
console.error(`[StoreDiscovery] Error:`, errorMessage);
|
|
return {
|
|
success: false,
|
|
error: errorMessage,
|
|
};
|
|
}
|
|
}
|