From 70b9c30c63723a402a8d79440a0bd329f55cd5c0 Mon Sep 17 00:00:00 2001 From: JCEEE <0xjceee@proton.me> Date: Fri, 11 Sep 2026 13:04:02 +0100 Subject: [PATCH] fix email bug for tags --- frontend/src/lib/server/email.ts | 1 - frontend/src/lib/server/services/chat.ts | 49 +++++++++--------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/frontend/src/lib/server/email.ts b/frontend/src/lib/server/email.ts index 03bc8a5..f91486b 100644 --- a/frontend/src/lib/server/email.ts +++ b/frontend/src/lib/server/email.ts @@ -132,7 +132,6 @@ export async function sendMentionEmail(opts: { message: string; dashboardUrl: string; }) { - console.log('[email] sendMentionEmail called to:', opts.to); const { to, parentName, authorName, message, dashboardUrl } = opts; await resend.emails.send({ diff --git a/frontend/src/lib/server/services/chat.ts b/frontend/src/lib/server/services/chat.ts index 5551fe3..1ea2844 100644 --- a/frontend/src/lib/server/services/chat.ts +++ b/frontend/src/lib/server/services/chat.ts @@ -1,4 +1,5 @@ import { sendMentionEmail } from '../email'; +import { pbAdmin } from '$lib/server/pocketbase'; export type ChatActor = { id: string; @@ -43,13 +44,12 @@ export async function send( createdAt: new Date().toISOString(), clientId: body.clientId ? String(body.clientId).slice(0, 64) : '' }); -// Email any @mentioned parents (their in-app toast is client-side, via the -// same mention tokens). Fire-and-forget — never fail the message itself. -console.log('[chat] calling notifyMentionedParents for famId:', famId, 'content:', content.slice(0, 50)); -notifyMentionedParents(pb, famId, actor, record, body.siteUrl || '').catch((err: any) => { - console.error('[chat] notifyMentionedParents top-level catch:', err); -}); -return record; + // Email any @mentioned parents (their in-app toast is client-side, via the + // same mention tokens). Fire-and-forget — never fail the message itself. + notifyMentionedParents(pb, famId, actor, record, body.siteUrl || '').catch((err: any) => { + console.error('[chat] notifyMentionedParents top-level catch:', err); + }); + return record; } // Scan the message for `@Parent Name` tokens (case-insensitive) and email every @@ -61,8 +61,10 @@ async function notifyMentionedParents( message: any, siteUrl: string ) { - console.log('[chat] notifyMentionedParents called', { famId, actorName: actor.name, contentLen: String(message.content || '').length }); const content = String(message.content || ''); + // Find parents whose name appears in the message content. + // getFullList for auth users may not return email, so we fetch + // each matched parent individually via pbAdmin to get the address. const parents = await pb .collection('users') .getFullList({ filter: `famId = '${famId}' && role = 'parent'` }) @@ -70,47 +72,34 @@ async function notifyMentionedParents( console.error('[chat] Failed to fetch parents:', err); return []; }); - console.log('[chat] parents found:', parents.length, 'mentioned parents:', parents.filter((u: any) => { - if (u.id === actor.id || !u.email) return false; - const name = (u.name || '').trim().toLowerCase(); - return !!name && content.toLowerCase().includes('@' + name); - }).length); const mentioned = parents.filter((u: any) => { - if (u.id === actor.id || !u.email) return false; + if (u.id === actor.id) return false; const name = (u.name || '').trim().toLowerCase(); return !!name && content.toLowerCase().includes('@' + name); }); - if (!mentioned.length) { - console.log('[chat] no mentioned parents found'); - return; - } + if (!mentioned.length) return; const fam = await pb .collection('fams') .getOne(famId) - .catch((err: any) => { - console.error('[chat] Failed to fetch fam:', err); - return null; - }); + .catch(() => null); // Don't send emails for the demo family. - if (fam?.slug === 'showboaters') { - console.log('[chat] skipping demo family'); - return; - } + if (fam?.slug === 'showboaters') return; const dashboardUrl = `${siteUrl}/${fam?.slug || famId}`; - console.log('[chat] sending mention emails to', mentioned.length, 'parents'); for (const parent of mentioned) { try { + const parentRec = await pbAdmin.getOne('users', parent.id); + const parentEmail = parentRec.email; + if (!parentEmail) continue; await sendMentionEmail({ - to: parent.email, + to: parentEmail, parentName: parent.name || 'there', authorName: message.authorName || 'Family member', message: content, dashboardUrl }); - console.log('[chat] email sent to', parent.email); } catch (err) { - console.error(`[chat] Failed to send mention email to ${parent.email}:`, err); + console.error(`[chat] Failed to send mention email to ${parent.name}:`, err); } } }