1// Backfill notifications for existing domain registration requests
2// Run with: node scripts/backfill_domain_notifications.js
4const pool = require('../services/db');
9async function backfillNotifications() {
10 const client = await pool.connect();
13 console.log('Starting notification backfill for domain requests...');
16 const rootTenantResult = await client.query(
17 `SELECT tenant_id FROM tenants WHERE subdomain = 'admin' OR is_msp = true LIMIT 1`
20 if (rootTenantResult.rows.length === 0) {
21 console.error('Root tenant not found');
25 const rootTenantId = rootTenantResult.rows[0].tenant_id;
26 console.log(`Root tenant ID: ${rootTenantId}`);
28 // Get all domain requests that don't have notifications
29 const requestsResult = await client.query(`
30 SELECT dr.request_id, dr.domain_name, dr.customer_id, dr.requested_at,
32 FROM domain_registration_requests dr
34 SELECT 1 FROM notifications n
35 WHERE n.message LIKE '%' || dr.domain_name || '%'
37 ORDER BY dr.requested_at DESC
40 console.log(`Found ${requestsResult.rows.length} requests without notifications`);
42 // Create notifications for each request
44 for (const request of requestsResult.rows) {
45 const registrant = request.registrant_contact;
46 const fullName = `${registrant.firstName || ''} ${registrant.lastName || ''}`.trim() || 'Unknown';
49 `INSERT INTO notifications (tenant_id, title, message, type, customer_id, created_at)
50 VALUES ($1, $2, $3, $4, $5, $6)`,
53 'New Domain Registration Request',
54 `${request.domain_name} registration requested by ${fullName}`,
62 console.log(`Created notification for ${request.domain_name}`);
65 console.log(`\n✅ Successfully created ${created} notifications`);
68 console.error('Error backfilling notifications:', err);
76backfillNotifications()
78 console.log('Backfill complete');
82 console.error('Backfill failed:', err);