|
12 | 12 |
|
13 | 13 | const { Router } = require('express'); |
14 | 14 | const { pool } = require('../db'); |
15 | | -const { calendarId, feedToken, inboundToken } = require('../lib/ids'); |
| 15 | +const { calendarId, feedToken, inboundToken, eventId } = require('../lib/ids'); |
16 | 16 | const { logError } = require('../lib/errors'); |
| 17 | +const { sendInviteEmail, parseAttendees } = require('../lib/outbound'); |
17 | 18 |
|
18 | 19 | const router = Router(); |
19 | 20 |
|
@@ -98,6 +99,46 @@ router.post('/', async (req, res) => { |
98 | 99 | [id, req.agent.id, name, tz, email, token, inbToken, agentmail_api_key || null] |
99 | 100 | ); |
100 | 101 |
|
| 102 | + // Create welcome event: "Send Peter feedback" at 9am tomorrow in calendar's timezone |
| 103 | + const evtId = eventId(); |
| 104 | + const tomorrow = new Date(); |
| 105 | + tomorrow.setUTCDate(tomorrow.getUTCDate() + 1); |
| 106 | + const dateStr = tomorrow.toISOString().slice(0, 10); // YYYY-MM-DD |
| 107 | + |
| 108 | + // Use Postgres AT TIME ZONE to convert 9am in the calendar's timezone to UTC |
| 109 | + const icalUid = evtId + '@caldave.ai'; |
| 110 | + await pool.query( |
| 111 | + `INSERT INTO events (id, calendar_id, title, description, start_time, end_time, attendees, ical_uid, invite_sent) |
| 112 | + VALUES ($1, $2, $3, $4, |
| 113 | + ($5::date + TIME '09:00') AT TIME ZONE $7, |
| 114 | + ($5::date + TIME '09:30') AT TIME ZONE $7, |
| 115 | + $6, $8, false)`, |
| 116 | + [ |
| 117 | + evtId, id, |
| 118 | + 'Send Peter (founder of CalDave) feedback', |
| 119 | + "Email or iMessage him at peterclark@me.com — he'd love to hear from you!", |
| 120 | + dateStr, |
| 121 | + JSON.stringify(['peter.clark@gmail.com']), |
| 122 | + tz, |
| 123 | + icalUid, |
| 124 | + ] |
| 125 | + ); |
| 126 | + |
| 127 | + // Fire-and-forget: send invite to Peter |
| 128 | + setImmediate(async () => { |
| 129 | + try { |
| 130 | + const { rows: evtRows } = await pool.query('SELECT * FROM events WHERE id = $1', [evtId]); |
| 131 | + if (!evtRows[0]) return; |
| 132 | + const calendar = { id, name, email }; |
| 133 | + const result = await sendInviteEmail(evtRows[0], calendar, ['peter.clark@gmail.com']); |
| 134 | + if (result.sent) { |
| 135 | + await pool.query('UPDATE events SET invite_sent = true WHERE id = $1', [evtId]); |
| 136 | + } |
| 137 | + } catch (err) { |
| 138 | + console.error('[outbound] Welcome invite error:', err.message); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
101 | 142 | const inboundUrl = `https://${DOMAIN}/inbound/${inbToken}`; |
102 | 143 | res.status(201).json({ |
103 | 144 | calendar_id: id, |
|
0 commit comments