EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
backfill_domain_notifications.js
Go to the documentation of this file.
1// Backfill notifications for existing domain registration requests
2// Run with: node scripts/backfill_domain_notifications.js
3
4const pool = require('../services/db');
5
6/**
7 *
8 */
9async function backfillNotifications() {
10 const client = await pool.connect();
11
12 try {
13 console.log('Starting notification backfill for domain requests...');
14
15 // Get root tenant
16 const rootTenantResult = await client.query(
17 `SELECT tenant_id FROM tenants WHERE subdomain = 'admin' OR is_msp = true LIMIT 1`
18 );
19
20 if (rootTenantResult.rows.length === 0) {
21 console.error('Root tenant not found');
22 return;
23 }
24
25 const rootTenantId = rootTenantResult.rows[0].tenant_id;
26 console.log(`Root tenant ID: ${rootTenantId}`);
27
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,
31 dr.registrant_contact
32 FROM domain_registration_requests dr
33 WHERE NOT EXISTS (
34 SELECT 1 FROM notifications n
35 WHERE n.message LIKE '%' || dr.domain_name || '%'
36 )
37 ORDER BY dr.requested_at DESC
38 `);
39
40 console.log(`Found ${requestsResult.rows.length} requests without notifications`);
41
42 // Create notifications for each request
43 let created = 0;
44 for (const request of requestsResult.rows) {
45 const registrant = request.registrant_contact;
46 const fullName = `${registrant.firstName || ''} ${registrant.lastName || ''}`.trim() || 'Unknown';
47
48 await client.query(
49 `INSERT INTO notifications (tenant_id, title, message, type, customer_id, created_at)
50 VALUES ($1, $2, $3, $4, $5, $6)`,
51 [
52 rootTenantId,
53 'New Domain Registration Request',
54 `${request.domain_name} registration requested by ${fullName}`,
55 'info',
56 request.customer_id,
57 request.requested_at
58 ]
59 );
60
61 created++;
62 console.log(`Created notification for ${request.domain_name}`);
63 }
64
65 console.log(`\n✅ Successfully created ${created} notifications`);
66
67 } catch (err) {
68 console.error('Error backfilling notifications:', err);
69 throw err;
70 } finally {
71 client.release();
72 await pool.end();
73 }
74}
75
76backfillNotifications()
77 .then(() => {
78 console.log('Backfill complete');
79 process.exit(0);
80 })
81 .catch(err => {
82 console.error('Backfill failed:', err);
83 process.exit(1);
84 });