Skip to content

Commit 7072f48

Browse files
authored
Merge pull request #935 from Merit-Systems/fix-discord-notifications
Fix Discord notifications not firing for new signups
2 parents fef677e + 98aa233 commit 7072f48

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

apps/scan/src/app/api/resources/sync/route.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { NextResponse } from 'next/server';
22

33
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';
58
import { upsertResource } from '@/services/db/resources/resource';
69

710
import { checkCronSecret } from '@/lib/cron';
11+
import { notifyNewServer } from '@/lib/discord-notifications';
812
import { getOriginFromUrl } from '@/lib/url';
913
import { normalizeChainId } from '@/lib/x402';
1014

@@ -56,6 +60,24 @@ export const GET = async (request: NextRequest) => {
5660
);
5761
}
5862

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+
5981
// Step 2: Process resources (upsert to database)
6082
console.log('Starting resource processing');
6183
const resourceProcessingStart = Date.now();
@@ -153,7 +175,19 @@ export const GET = async (request: NextRequest) => {
153175
};
154176

155177
// 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+
}
157191

158192
return { origin, success: true };
159193
} catch (error) {

apps/scan/src/lib/discord-notifications.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ interface DiscordConfig {
1818
appUrl: string;
1919
}
2020

21-
export function notifyNewServer(notification: NewServerNotification) {
21+
export function notifyNewServer(
22+
notification: NewServerNotification,
23+
options?: { merchantResearch?: boolean }
24+
) {
2225
scheduleDiscordNotification({
2326
webhookUrl: env.DISCORD_NOTIFICATIONS_WEBHOOK_URL,
2427
username: NEW_SERVER_USERNAME,
2528
embed: config => buildNewServerEmbed(notification, config),
2629
});
2730

28-
scheduleDiscordNotification({
29-
webhookUrl: env.DISCORD_MERCHANT_RESEARCH_WEBHOOK_URL,
30-
username: NEW_SERVER_USERNAME,
31-
embed: config => buildMerchantResearchEmbed(notification, config),
32-
});
31+
if (options?.merchantResearch ?? true) {
32+
scheduleDiscordNotification({
33+
webhookUrl: env.DISCORD_MERCHANT_RESEARCH_WEBHOOK_URL,
34+
username: NEW_SERVER_USERNAME,
35+
embed: config => buildMerchantResearchEmbed(notification, config),
36+
});
37+
}
3338
}
3439

3540
function scheduleDiscordNotification(options: {

apps/scan/src/services/db/resources/origin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const getOriginResourceCount = async (origin: string) => {
129129
select: {
130130
_count: {
131131
select: {
132-
resources: true,
132+
resources: { where: { deprecatedAt: null } },
133133
},
134134
},
135135
},

0 commit comments

Comments
 (0)