|
| 1 | +import { prisma } from '@/lib/db/client' |
| 2 | +import { fetchSubredditPosts, type RedditPost } from '@/lib/reddit' |
| 3 | +import { fetchHNStories, type HNStory } from '@/lib/hn' |
| 4 | +import { scorePosts, type PostToScore } from '@/lib/scorer' |
| 5 | + |
| 6 | +interface NormalizedPost { |
| 7 | + externalId: string |
| 8 | + title: string |
| 9 | + body: string |
| 10 | + author: string |
| 11 | + score: number |
| 12 | + commentCount: number |
| 13 | + url: string |
| 14 | + postedAt: Date |
| 15 | + platform: string |
| 16 | + subreddit: string | null |
| 17 | +} |
| 18 | + |
| 19 | +function fromReddit(post: RedditPost): NormalizedPost { |
| 20 | + return { ...post, platform: 'reddit', subreddit: post.subreddit } |
| 21 | +} |
| 22 | + |
| 23 | +function fromHN(story: HNStory): NormalizedPost { |
| 24 | + return { ...story, platform: 'hn', subreddit: null } |
| 25 | +} |
| 26 | + |
| 27 | +function matchesKeywords(post: NormalizedPost, keywords: string[]): boolean { |
| 28 | + if (keywords.length === 0) return false |
| 29 | + const haystack = `${post.title} ${post.body}`.toLowerCase() |
| 30 | + return keywords.some((kw) => { |
| 31 | + const phrase = kw.toLowerCase() |
| 32 | + if (haystack.includes(phrase)) return true |
| 33 | + const words = phrase.split(/\s+/).filter((w) => w.length > 4) |
| 34 | + return words.some((word) => haystack.includes(word)) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +export async function refreshOpportunitiesForUser(userId: string): Promise<number> { |
| 39 | + const profile = await prisma.profile.findUnique({ |
| 40 | + where: { id: userId }, |
| 41 | + select: { keywords: true, subreddits: true, productDescription: true, targetCustomer: true }, |
| 42 | + }) |
| 43 | + |
| 44 | + if (!profile || profile.keywords.length === 0) { |
| 45 | + console.log(`[refresh] User ${userId} — no keywords, skipping`) |
| 46 | + return 0 |
| 47 | + } |
| 48 | + |
| 49 | + // Fetch Reddit posts for each of the user's subreddits |
| 50 | + const redditPosts: NormalizedPost[] = [] |
| 51 | + await Promise.all( |
| 52 | + profile.subreddits.map(async (subreddit) => { |
| 53 | + try { |
| 54 | + const posts = await fetchSubredditPosts(subreddit) |
| 55 | + redditPosts.push(...posts.map(fromReddit)) |
| 56 | + console.log(`[refresh] r/${subreddit}: fetched ${posts.length} post(s)`) |
| 57 | + } catch { |
| 58 | + console.error(`[refresh] Failed to fetch r/${subreddit}`) |
| 59 | + } |
| 60 | + }) |
| 61 | + ) |
| 62 | + |
| 63 | + // Fetch HN stories |
| 64 | + let hnStories: NormalizedPost[] = [] |
| 65 | + try { |
| 66 | + hnStories = (await fetchHNStories(150)).map(fromHN) |
| 67 | + console.log(`[refresh] HN: fetched ${hnStories.length} story(ies)`) |
| 68 | + } catch { |
| 69 | + console.error('[refresh] Failed to fetch HN stories') |
| 70 | + } |
| 71 | + |
| 72 | + const candidates = [...redditPosts, ...hnStories] |
| 73 | + const matched = candidates.filter((post) => matchesKeywords(post, profile.keywords)) |
| 74 | + |
| 75 | + console.log(`[refresh] User ${userId} — candidates: ${candidates.length}, matched: ${matched.length}`) |
| 76 | + |
| 77 | + if (matched.length === 0) return 0 |
| 78 | + |
| 79 | + // Cap at 30 most recent |
| 80 | + const capped = matched |
| 81 | + .sort((a, b) => b.postedAt.getTime() - a.postedAt.getTime()) |
| 82 | + .slice(0, 30) |
| 83 | + |
| 84 | + const postsToScore: PostToScore[] = capped.map((post) => ({ |
| 85 | + externalId: post.externalId, |
| 86 | + title: post.title, |
| 87 | + body: post.body, |
| 88 | + subreddit: post.subreddit, |
| 89 | + postedAt: post.postedAt, |
| 90 | + commentCount: post.commentCount, |
| 91 | + })) |
| 92 | + |
| 93 | + let scored = await scorePosts( |
| 94 | + postsToScore, |
| 95 | + profile.productDescription ?? '', |
| 96 | + profile.targetCustomer ?? '' |
| 97 | + ).catch((err) => { |
| 98 | + console.error(`[refresh] Scoring failed for user ${userId}:`, err) |
| 99 | + return [] |
| 100 | + }) |
| 101 | + |
| 102 | + scored = scored.filter((p) => p.relevanceScore >= 40) |
| 103 | + console.log(`[refresh] User ${userId} — scored (relevance>=40): ${scored.length}`) |
| 104 | + |
| 105 | + if (scored.length === 0) return 0 |
| 106 | + |
| 107 | + const metaMap = new Map(capped.map((p) => [p.externalId, p])) |
| 108 | + |
| 109 | + const result = await prisma.opportunity.createMany({ |
| 110 | + data: scored.map((p) => { |
| 111 | + const meta = metaMap.get(p.externalId)! |
| 112 | + return { |
| 113 | + userId, |
| 114 | + platform: meta.platform, |
| 115 | + externalId: p.externalId, |
| 116 | + url: meta.url, |
| 117 | + title: p.title, |
| 118 | + body: p.body || null, |
| 119 | + subreddit: p.subreddit, |
| 120 | + author: meta.author, |
| 121 | + score: meta.score, |
| 122 | + commentCount: p.commentCount, |
| 123 | + postedAt: p.postedAt, |
| 124 | + relevanceScore: p.relevanceScore, |
| 125 | + intentLevel: p.intentLevel, |
| 126 | + reasoning: p.reasoning, |
| 127 | + suggestedReplies: [], |
| 128 | + } |
| 129 | + }), |
| 130 | + skipDuplicates: true, |
| 131 | + }) |
| 132 | + |
| 133 | + console.log(`[refresh] User ${userId} — saved ${result.count} new opportunity(ies)`) |
| 134 | + return result.count |
| 135 | +} |
0 commit comments