|
|
|
@@ -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) => {
|
|
|
|
|
// 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;
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|