|
1 | 1 | import { NextResponse } from 'next/server'; |
2 | 2 |
|
3 | 3 | import { scrapeOriginData } from '@/services/scraper'; |
4 | | -import { upsertOrigin } from '@/services/db/resources/origin'; |
| 4 | +import { |
| 5 | + getOriginResourceCount, |
| 6 | + upsertOrigin, |
| 7 | +} from '@/services/db/resources/origin'; |
5 | 8 | import { upsertResource } from '@/services/db/resources/resource'; |
6 | 9 |
|
7 | 10 | import { checkCronSecret } from '@/lib/cron'; |
| 11 | +import { notifyNewServer } from '@/lib/discord-notifications'; |
8 | 12 | import { getOriginFromUrl } from '@/lib/url'; |
9 | 13 | import { normalizeChainId } from '@/lib/x402'; |
10 | 14 |
|
@@ -56,6 +60,24 @@ export const GET = async (request: NextRequest) => { |
56 | 60 | ); |
57 | 61 | } |
58 | 62 |
|
| 63 | + // Step 1.5: Snapshot current resource counts per origin so we can detect |
| 64 | + // new origins after upserts (origins that go from 0 → >0 active resources). |
| 65 | + const preexistingOrigins = new Set<string>(); |
| 66 | + const allOrigins = new Set<string>(); |
| 67 | + for (const resource of resources) { |
| 68 | + try { |
| 69 | + allOrigins.add(getOriginFromUrl(resource.resource)); |
| 70 | + } catch { |
| 71 | + // Origin extraction failed, skip |
| 72 | + } |
| 73 | + } |
| 74 | + await Promise.all( |
| 75 | + Array.from(allOrigins).map(async origin => { |
| 76 | + const count = await getOriginResourceCount(origin); |
| 77 | + if (count > 0) preexistingOrigins.add(origin); |
| 78 | + }) |
| 79 | + ); |
| 80 | + |
59 | 81 | // Step 2: Process resources (upsert to database) |
60 | 82 | console.log('Starting resource processing'); |
61 | 83 | const resourceProcessingStart = Date.now(); |
@@ -153,7 +175,19 @@ export const GET = async (request: NextRequest) => { |
153 | 175 | }; |
154 | 176 |
|
155 | 177 | // Upsert origin to database |
156 | | - await upsertOrigin(originData); |
| 178 | + const upsertedOrigin = await upsertOrigin(originData); |
| 179 | + |
| 180 | + if (!preexistingOrigins.has(origin) && upsertedOrigin) { |
| 181 | + notifyNewServer( |
| 182 | + { |
| 183 | + originId: upsertedOrigin.id, |
| 184 | + origin, |
| 185 | + title: originData.title ?? null, |
| 186 | + description: originData.description ?? null, |
| 187 | + }, |
| 188 | + { merchantResearch: false } |
| 189 | + ); |
| 190 | + } |
157 | 191 |
|
158 | 192 | return { origin, success: true }; |
159 | 193 | } catch (error) { |
|
0 commit comments