feat: extend notification system with batch sender, bulk dialog, and logging

Add NotificationLog schema extensions (nullable userId, email, roundId,
projectId, batchId fields), batch notification sender service, and bulk
notification dialog UI. Include utility scripts for debugging and seeding.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 13:29:06 +01:00
parent 8f2f054c57
commit f24bea3df2
15 changed files with 1316 additions and 16 deletions

View File

@@ -0,0 +1,112 @@
/**
* Backfill all projects into the intake round (and any intermediate rounds
* between intake and their earliest assigned round) with COMPLETED state.
*
* Usage: npx tsx scripts/backfill-intake-round.ts
* Add --dry-run to preview without making changes.
*/
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const dryRun = process.argv.includes('--dry-run')
async function main() {
console.log(dryRun ? '🔍 DRY RUN — no changes will be made\n' : '🚀 Backfilling intake round states...\n')
// Find the intake round
const intakeRound = await prisma.round.findFirst({
where: { roundType: 'INTAKE' },
select: { id: true, name: true, sortOrder: true, competitionId: true },
})
if (!intakeRound) {
console.log('❌ No INTAKE round found')
return
}
console.log(`Intake round: "${intakeRound.name}" (sortOrder: ${intakeRound.sortOrder})`)
// Get all rounds in the competition ordered by sortOrder
const allRounds = await prisma.round.findMany({
where: { competitionId: intakeRound.competitionId },
select: { id: true, name: true, sortOrder: true },
orderBy: { sortOrder: 'asc' },
})
// Find all projects NOT in the intake round
const projects = await prisma.project.findMany({
where: {
projectRoundStates: {
none: { roundId: intakeRound.id },
},
},
select: {
id: true,
title: true,
projectRoundStates: {
select: { roundId: true, round: { select: { sortOrder: true } } },
orderBy: { round: { sortOrder: 'asc' } },
},
},
})
console.log(`${projects.length} projects not in intake round\n`)
if (projects.length === 0) {
console.log('✅ All projects already in intake round')
return
}
// For each project, create COMPLETED states for intake + any intermediate rounds
const toCreate: Array<{ projectId: string; roundId: string; state: 'COMPLETED' }> = []
for (const project of projects) {
// Find the earliest round this project is already in
const earliestSortOrder = project.projectRoundStates.length > 0
? Math.min(...project.projectRoundStates.map(ps => ps.round.sortOrder))
: Infinity
const existingRoundIds = new Set(project.projectRoundStates.map(ps => ps.roundId))
// Add COMPLETED for intake + all intermediate rounds before the earliest assigned round
for (const round of allRounds) {
if (round.sortOrder >= earliestSortOrder) break
if (existingRoundIds.has(round.id)) continue
toCreate.push({
projectId: project.id,
roundId: round.id,
state: 'COMPLETED',
})
}
}
console.log(`Creating ${toCreate.length} ProjectRoundState records...`)
if (!dryRun) {
await prisma.projectRoundState.createMany({
data: toCreate,
skipDuplicates: true,
})
}
// Summary by round
const byRound = new Map<string, number>()
for (const r of toCreate) {
const name = allRounds.find(ar => ar.id === r.roundId)?.name ?? r.roundId
byRound.set(name, (byRound.get(name) ?? 0) + 1)
}
for (const [name, count] of byRound) {
console.log(` ${name}: ${count} projects`)
}
console.log(`\n✅ Done! ${toCreate.length} records ${dryRun ? 'would be' : ''} created`)
}
main()
.catch((e) => {
console.error('❌ Error:', e)
process.exit(1)
})
.finally(() => prisma.$disconnect())

View File

@@ -0,0 +1,78 @@
/**
* Backfill TeamMember records for all projects that have a submittedByUserId
* but no corresponding TeamMember link.
*
* Usage: npx tsx scripts/backfill-team-leads.ts
* Add --dry-run to preview without making changes.
*/
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const dryRun = process.argv.includes('--dry-run')
async function main() {
console.log(dryRun ? '🔍 DRY RUN — no changes will be made\n' : '🚀 Backfilling team leads...\n')
// Find all projects with a submitter but no TeamMember link for that user
const projects = await prisma.project.findMany({
where: {
submittedByUserId: { not: null },
},
select: {
id: true,
title: true,
submittedByUserId: true,
teamMembers: {
select: { userId: true },
},
},
})
let created = 0
let alreadyLinked = 0
let noSubmitter = 0
for (const project of projects) {
if (!project.submittedByUserId) {
noSubmitter++
continue
}
const alreadyHasLink = project.teamMembers.some(
(tm) => tm.userId === project.submittedByUserId
)
if (alreadyHasLink) {
alreadyLinked++
continue
}
console.log(` + Linking "${project.title}" → user ${project.submittedByUserId}`)
if (!dryRun) {
await prisma.teamMember.create({
data: {
projectId: project.id,
userId: project.submittedByUserId,
role: 'LEAD',
},
})
}
created++
}
console.log(`\n✅ Done!`)
console.log(` ${created} TeamMember records ${dryRun ? 'would be' : ''} created`)
console.log(` ${alreadyLinked} projects already had the submitter linked`)
console.log(` ${noSubmitter} projects had no submitter`)
console.log(` ${projects.length} total projects checked`)
}
main()
.catch((e) => {
console.error('❌ Error:', e)
process.exit(1)
})
.finally(() => prisma.$disconnect())

32
scripts/check-invites.cjs Normal file
View File

@@ -0,0 +1,32 @@
const { PrismaClient } = require('@prisma/client');
const p = new PrismaClient({ datasourceUrl: 'postgresql://mopc:devpassword@localhost:5433/mopc' });
(async () => {
const members = await p.teamMember.findMany({
orderBy: { joinedAt: 'desc' },
take: 10,
include: {
user: { select: { id: true, name: true, email: true, status: true, inviteToken: true } },
project: { select: { title: true } }
}
});
for (const m of members) {
console.log(m.role, '|', m.user.name, '|', m.user.email, '|', m.user.status, '|', m.project.title, '|', m.joinedAt.toISOString().slice(0,16), '| token:', m.user.inviteToken ? 'yes' : 'no');
}
const logs = await p.notificationLog.findMany({
where: { type: 'TEAM_INVITATION' },
orderBy: { createdAt: 'desc' },
take: 5,
});
if (logs.length) {
console.log('\n--- Notification logs:');
for (const l of logs) {
console.log(l.status, '|', l.channel, '|', l.errorMsg, '|', l.createdAt.toISOString().slice(0,16));
}
} else {
console.log('\n--- No TEAM_INVITATION notification logs found');
}
await p.$disconnect();
})();

20
scripts/check-rounds.cjs Normal file
View File

@@ -0,0 +1,20 @@
const { PrismaClient } = require('@prisma/client');
const p = new PrismaClient({ datasourceUrl: 'postgresql://mopc:devpassword@localhost:5433/mopc' });
(async () => {
const rounds = await p.round.findMany({
orderBy: { sortOrder: 'asc' },
select: { id: true, name: true, roundType: true, status: true, sortOrder: true, competitionId: true },
});
for (const r of rounds) console.log(r.sortOrder, '|', r.name, '|', r.roundType, '|', r.status, '|', r.id);
console.log('\n--- File Requirements:');
const reqs = await p.fileRequirement.findMany({ include: { round: { select: { name: true } } } });
for (const r of reqs) console.log(r.round.name, '|', r.name, '|', r.isRequired, '|', r.id);
console.log('\n--- Submission Windows:');
const wins = await p.submissionWindow.findMany({ select: { id: true, name: true, roundNumber: true, windowOpenAt: true, windowCloseAt: true, competitionId: true } });
for (const w of wins) console.log(w.name, '| round#', w.roundNumber, '| open:', w.windowOpenAt?.toISOString().slice(0,16), '| close:', w.windowCloseAt?.toISOString().slice(0,16));
await p.$disconnect();
})();

View File

@@ -0,0 +1,71 @@
const { PrismaClient } = require('@prisma/client');
const p = new PrismaClient({ datasourceUrl: 'postgresql://mopc:devpassword@localhost:5433/mopc' });
(async () => {
// R2 - AI Screening round ID
const roundId = 'cmmafe7et00ldy53kxpdhhvf0';
// Check existing
const existing = await p.fileRequirement.count({ where: { roundId } });
if (existing > 0) {
console.log(`Round already has ${existing} file requirements, skipping.`);
await p.$disconnect();
return;
}
const requirements = [
{
roundId,
name: 'Executive Summary',
description: 'A 2-page executive summary of your project (PDF format, max 10MB)',
acceptedMimeTypes: ['application/pdf'],
maxSizeMB: 10,
isRequired: true,
sortOrder: 0,
},
{
roundId,
name: 'Business Plan',
description: 'Full business plan or project proposal (PDF format, max 25MB)',
acceptedMimeTypes: ['application/pdf'],
maxSizeMB: 25,
isRequired: true,
sortOrder: 1,
},
{
roundId,
name: 'Pitch Presentation',
description: 'Slide deck presenting your project (PDF or PowerPoint, max 50MB)',
acceptedMimeTypes: ['application/pdf', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
maxSizeMB: 50,
isRequired: true,
sortOrder: 2,
},
{
roundId,
name: 'Video Pitch',
description: 'A short video (max 3 minutes) explaining your project (MP4, max 200MB). Optional but recommended.',
acceptedMimeTypes: ['video/mp4', 'video/quicktime', 'video/webm'],
maxSizeMB: 200,
isRequired: false,
sortOrder: 3,
},
{
roundId,
name: 'Supporting Documents',
description: 'Any additional supporting documents such as research papers, letters of support, etc. (PDF, max 20MB)',
acceptedMimeTypes: ['application/pdf'],
maxSizeMB: 20,
isRequired: false,
sortOrder: 4,
},
];
for (const req of requirements) {
const created = await p.fileRequirement.create({ data: req });
console.log('Created:', created.name, '| required:', created.isRequired, '| id:', created.id);
}
console.log('\nDone! Created', requirements.length, 'file requirements for R2.');
await p.$disconnect();
})();

View File

@@ -0,0 +1,68 @@
import { PrismaClient } from '@prisma/client'
import crypto from 'crypto'
import { sendInvitationEmail } from '../src/lib/email'
const prisma = new PrismaClient()
async function main() {
// Find a program to attach the project to
const program = await prisma.program.findFirst()
if (!program) throw new Error('No program found - run seed first')
// Create applicant user
const inviteToken = crypto.randomBytes(32).toString('hex')
const user = await prisma.user.create({
data: {
id: 'test_applicant_matt_ciaccio',
name: 'Matt Ciaccio',
email: 'matt.ciaccio@gmail.com',
role: 'APPLICANT',
roles: ['APPLICANT'],
status: 'INVITED',
mustSetPassword: true,
inviteToken,
inviteTokenExpiresAt: new Date(Date.now() + 72 * 60 * 60 * 1000),
},
})
console.log('Created user:', user.id)
// Create test project
const project = await prisma.project.create({
data: {
id: 'test_project_qa',
title: 'OceanWatch AI',
description: 'AI-powered ocean monitoring platform for marine conservation',
programId: program.id,
submittedByUserId: user.id,
},
})
console.log('Created project:', project.id)
// Create team member (LEAD)
await prisma.teamMember.create({
data: {
id: 'test_tm_lead',
projectId: project.id,
userId: user.id,
role: 'LEAD',
},
})
console.log('Created team member (LEAD)')
// Send styled invitation email
const url = `http://localhost:3000/accept-invite?token=${inviteToken}`
console.log('Invite URL:', url)
await sendInvitationEmail(
'matt.ciaccio@gmail.com',
'Matt Ciaccio',
url,
'APPLICANT',
72
)
console.log('Styled invitation email sent!')
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect().then(() => process.exit(0)))

View File

@@ -0,0 +1,165 @@
/**
* Seed NotificationLog with confirmed SMTP delivery data.
*
* Sources:
* 1. 33 emails confirmed delivered in Poste.io SMTP logs (2026-03-04)
* 2. Users with status ACTIVE who are LEADs on PASSED projects
* (they clearly received and used their invite link)
*
* Usage: npx tsx scripts/seed-notification-log.ts
* Add --dry-run to preview without making changes.
*/
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const dryRun = process.argv.includes('--dry-run')
// Emails confirmed delivered via SMTP logs on 2026-03-04
const CONFIRMED_SMTP_EMAILS = new Set([
'fbayong@balazstudio.com',
'gnoel@kilimora.africa',
'amal.chebbi@pigmentoco.com',
'nairita@yarsi.net',
'martin.itamalo@greenbrinetechnologies.com',
'petervegan1223@gmail.com',
'dmarinov@redget.io',
'adrien@seavium.com',
'l.buob@whisper-ef.com',
'silvia@omnivorus.com',
'marzettisebastian@gmail.com',
'fiona.mcomish@algae-scope.com',
'karimeguillen@rearvora.com',
'info@skywatt.tech',
'julia@nereia-coatings.com',
'info@janmaisenbacher.com',
'xbm_0201@qq.com',
'irinakharitonova0201@gmail.com',
'seablocksrecif@gmail.com',
'oscar@seafuser.com',
'charles.maher@blueshadow.dk',
'sabirabokhari@gmail.com',
'munayimbabura@gmail.com',
'amritha.ramadevu@edu.escp.eu',
'nele.jordan@myhsba.de',
'karl.mihhels@aalto.fi',
'christine.a.kurz@gmail.com',
'aki@corall.eco',
'topias.kilpinen@hotmail.fi',
'nina.riutta.camilla@gmail.com',
'sofie.boggiosella@my.jcu.edu.au',
'giambattistafigari@gmail.com',
'mussinig0@gmail.com',
])
const SENT_AT = new Date('2026-03-04T01:00:00Z')
async function main() {
console.log(dryRun ? '--- DRY RUN ---\n' : 'Seeding NotificationLog...\n')
// Find LEAD team members on PASSED projects
const passedLeads = await prisma.teamMember.findMany({
where: {
role: 'LEAD',
project: {
projectRoundStates: {
some: { state: 'PASSED' },
},
},
},
select: {
userId: true,
projectId: true,
project: {
select: {
projectRoundStates: {
where: { state: 'PASSED' },
select: { roundId: true },
take: 1,
},
},
},
user: {
select: {
id: true,
email: true,
status: true,
inviteToken: true,
},
},
},
})
console.log(`Found ${passedLeads.length} LEAD team members on PASSED projects\n`)
let created = 0
let skipped = 0
for (const lead of passedLeads) {
const email = lead.user.email?.toLowerCase()
if (!email) {
skipped++
continue
}
// Check if a NotificationLog already exists for this project+email
const existing = await prisma.notificationLog.findFirst({
where: {
email,
projectId: lead.projectId,
type: 'ADVANCEMENT_NOTIFICATION',
status: 'SENT',
},
})
if (existing) {
skipped++
continue
}
// Determine confidence of delivery
const isConfirmedSMTP = CONFIRMED_SMTP_EMAILS.has(email)
const isActive = lead.user.status === 'ACTIVE'
const isInvited = lead.user.status === 'INVITED' && !!lead.user.inviteToken
// Only seed for confirmed deliveries or active users
if (!isConfirmedSMTP && !isActive && !isInvited) {
console.log(` SKIP ${email} (status=${lead.user.status}, not in SMTP logs)`)
skipped++
continue
}
const roundId = lead.project.projectRoundStates[0]?.roundId ?? null
const label = isConfirmedSMTP ? 'SMTP-confirmed' : isActive ? 'user-active' : 'invite-sent'
console.log(` ${dryRun ? 'WOULD CREATE' : 'CREATE'} ${email} [${label}] project=${lead.projectId}`)
if (!dryRun) {
await prisma.notificationLog.create({
data: {
userId: lead.user.id,
channel: 'EMAIL',
type: 'ADVANCEMENT_NOTIFICATION',
status: 'SENT',
email,
projectId: lead.projectId,
roundId,
batchId: 'seed-2026-03-04',
createdAt: SENT_AT,
},
})
created++
} else {
created++
}
}
console.log(`\nDone. Created: ${created}, Skipped: ${skipped}`)
}
main()
.catch((err) => {
console.error('Error:', err)
process.exit(1)
})
.finally(() => prisma.$disconnect())

View File

@@ -0,0 +1,120 @@
import nodemailer from 'nodemailer';
// Import just the template helper without hitting DB
// We'll construct the email manually since the DB connection fails
const BRAND = {
red: '#de0f1e',
darkBlue: '#053d57',
white: '#fefefe',
teal: '#557f8c',
};
const token = '6f974b1da9fae95f74bbcd2419df589730979ac945aeaa5413021c00311b5165';
const url = 'http://localhost:3000/accept-invite?token=' + token;
// Replicate the styled email template from email.ts
function getStyledHtml(name: string, inviteUrl: string) {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>You're invited to join the MOPC Portal</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f8fafc; font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;">
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: #f8fafc;">
<tr>
<td align="center" style="padding: 40px 20px;">
<table role="presentation" width="600" cellspacing="0" cellpadding="0" border="0" style="max-width: 600px; width: 100%;">
<!-- Header -->
<tr>
<td style="background: linear-gradient(135deg, ${BRAND.darkBlue} 0%, ${BRAND.teal} 100%); border-radius: 16px 16px 0 0; padding: 32px 40px; text-align: center;">
<h1 style="color: ${BRAND.white}; font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -0.02em;">
Monaco Ocean Protection Challenge
</h1>
<p style="color: rgba(255,255,255,0.8); font-size: 13px; font-weight: 300; margin: 8px 0 0 0; letter-spacing: 0.05em; text-transform: uppercase;">
Together for a healthier ocean
</p>
</td>
</tr>
<!-- Body -->
<tr>
<td style="background-color: #ffffff; padding: 40px; border-radius: 0 0 16px 16px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);">
<h2 style="color: ${BRAND.darkBlue}; font-size: 20px; font-weight: 600; margin: 0 0 24px 0;">
Hello ${name},
</h2>
<p style="color: #475569; font-size: 15px; line-height: 1.7; margin: 0 0 16px 0; font-weight: 400;">
You've been invited to join the Monaco Ocean Protection Challenge platform as an <strong>applicant</strong>.
</p>
<p style="color: #475569; font-size: 15px; line-height: 1.7; margin: 0 0 24px 0; font-weight: 400;">
Click the button below to set up your account and get started.
</p>
<!-- CTA Button -->
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="margin: 28px 0;">
<tr>
<td align="center">
<a href="${inviteUrl}" style="display: inline-block; background: linear-gradient(135deg, ${BRAND.red} 0%, #c40d19 100%); color: #ffffff; text-decoration: none; padding: 14px 36px; border-radius: 10px; font-size: 15px; font-weight: 600; letter-spacing: 0.02em; box-shadow: 0 4px 14px rgba(222, 15, 30, 0.3);">
Accept Invitation
</a>
</td>
</tr>
</table>
<!-- Info Box -->
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="margin: 20px 0;">
<tr>
<td style="background-color: #eff6ff; border-left: 4px solid ${BRAND.darkBlue}; border-radius: 0 8px 8px 0; padding: 16px 20px;">
<p style="color: #1e40af; margin: 0; font-size: 13px; line-height: 1.6;">
This link will expire in 3 days.
</p>
</td>
</tr>
</table>
</td>
</tr>
<!-- Footer -->
<tr>
<td style="padding: 24px 40px; text-align: center;">
<p style="color: #94a3b8; font-size: 12px; line-height: 1.6; margin: 0;">
Monaco Ocean Protection Challenge<br>
<span style="color: #cbd5e1;">Together for a healthier ocean.</span>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>`;
}
async function main() {
console.log('Creating transporter...');
const transporter = nodemailer.createTransport({
host: 'mail.monaco-opc.com',
port: 587,
secure: false,
auth: {
user: 'noreply@monaco-opc.com',
pass: '9EythPDcz1Fya4M88iigkB1wojNf8QEVPuRRnD9dJMBpT3pk2',
},
});
console.log('Sending styled invitation email...');
const info = await transporter.sendMail({
from: 'MOPC Portal <noreply@monaco-opc.com>',
to: 'matt.ciaccio@gmail.com',
subject: "You're invited to join the MOPC Portal",
text: `Hello Matt Ciaccio,\n\nYou've been invited to join the Monaco Ocean Protection Challenge platform as an applicant.\n\nClick the link below to set up your account:\n\n${url}\n\nThis link will expire in 3 days.\n\n---\nMonaco Ocean Protection Challenge\nTogether for a healthier ocean.`,
html: getStyledHtml('Matt Ciaccio', url),
});
console.log('SUCCESS! Message ID:', info.messageId);
process.exit(0);
}
main().catch(err => {
console.error('FAILED:', err);
process.exit(1);
});

26
scripts/send-invite.ts Normal file
View File

@@ -0,0 +1,26 @@
import { sendInvitationEmail } from '../src/lib/email';
const token = '6f974b1da9fae95f74bbcd2419df589730979ac945aeaa5413021c00311b5165';
const url = 'http://localhost:3000/accept-invite?token=' + token;
async function main() {
console.log('Sending styled invitation email...');
console.log('To: matt.ciaccio@gmail.com');
console.log('URL:', url);
try {
await sendInvitationEmail(
'matt.ciaccio@gmail.com',
'Matt Ciaccio',
url,
'APPLICANT',
72
);
console.log('SUCCESS: Styled invitation email sent!');
} catch (err: any) {
console.error('FAILED:', err.message || err);
}
process.exit(0);
}
main();

20
scripts/test-db.cjs Normal file
View File

@@ -0,0 +1,20 @@
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
async function main() {
console.log('DATABASE_URL:', process.env.DATABASE_URL);
const p = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] });
try {
const result = await p.$queryRawUnsafe('SELECT 1 as ok');
console.log('Connected!', result);
} catch (e) {
console.error('Error code:', e.code);
console.error('Error meta:', JSON.stringify(e.meta, null, 2));
console.error('Message:', e.message);
} finally {
await p.$disconnect();
process.exit(0);
}
}
main();