|
| 1 | +// functions/src/notify-email.ts |
1 | 2 | import { createTransport } from 'nodemailer'; |
2 | | -import { firestore, functions, regionalFunctions } from './lib/utils'; |
| 3 | +import { connectMongo } from './lib/utils'; |
3 | 4 | import { EMAIL_API, EMAIL_API_PASSWORD, TARGET_EMAIL } from './lib/env'; |
4 | | -import type { Tweet, User } from './types'; |
5 | | - |
6 | | -export const notifyEmail = regionalFunctions.firestore |
7 | | - .document('tweets/{tweetId}') |
8 | | - .onCreate(async (snapshot): Promise<void> => { |
9 | | - functions.logger.info('Sending notification email.'); |
10 | | - |
11 | | - const { text, createdBy, images, parent } = snapshot.data() as Tweet; |
12 | | - |
13 | | - const imagesLength = images?.length ?? 0; |
14 | | - |
15 | | - const { name, username } = ( |
16 | | - await firestore().doc(`users/${createdBy}`).get() |
17 | | - ).data() as User; |
18 | | - |
19 | | - const client = createTransport({ |
20 | | - service: 'Gmail', |
21 | | - auth: { |
22 | | - user: EMAIL_API.value(), |
23 | | - pass: EMAIL_API_PASSWORD.value() |
24 | | - } |
25 | | - }); |
26 | | - |
27 | | - const tweetLink = `https://twitter-clone-ccrsxx.vercel.app/tweet/${snapshot.id}`; |
28 | | - |
29 | | - const emailHeader = `New Tweet${ |
30 | | - parent ? ' reply' : '' |
31 | | - } from ${name} (@${username})`; |
| 5 | +import { Tweet } from './mongo/models/Tweet'; |
| 6 | + |
| 7 | +export interface TweetData { |
| 8 | + id: string; |
| 9 | + text: string; |
| 10 | + createdBy: string; |
| 11 | + username: string; |
| 12 | + images?: string[]; |
| 13 | + parent?: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Send notification email when a new tweet is created |
| 18 | + */ |
| 19 | +export async function notifyEmail(tweetData: TweetData) { |
| 20 | + const { id, text, createdBy, username, images = [], parent } = tweetData; |
| 21 | + |
| 22 | + // Connect to MongoDB |
| 23 | + await connectMongo(); |
| 24 | + |
| 25 | + // Fetch user from MongoDB (fallback to payload if not found) |
| 26 | + const user = await Tweet.findOne({ _id: createdBy }); |
| 27 | + const name = user?.username || username; |
| 28 | + |
| 29 | + const client = createTransport({ |
| 30 | + service: 'Gmail', |
| 31 | + auth: { |
| 32 | + user: EMAIL_API.value(), |
| 33 | + pass: EMAIL_API_PASSWORD.value(), |
| 34 | + }, |
| 35 | + }); |
32 | 36 |
|
33 | | - const emailText = `${text ?? 'No text provided'}${ |
34 | | - images ? ` (${imagesLength} image${imagesLength > 1 ? 's' : ''})` : '' |
35 | | - }\n\nLink to Tweet: ${tweetLink}\n\n- Firebase Function.`; |
| 37 | + const imagesLength = images.length; |
| 38 | + const tweetLink = `https://twitter-clone-ccrsxx.vercel.app/tweet/${id}`; |
36 | 39 |
|
37 | | - await client.sendMail({ |
38 | | - from: EMAIL_API.value(), |
39 | | - to: TARGET_EMAIL.value(), |
40 | | - subject: emailHeader, |
41 | | - text: emailText |
42 | | - }); |
| 40 | + const emailHeader = `New Tweet${parent ? ' reply' : ''} from ${name} (@${username})`; |
| 41 | + const emailText = `${text ?? 'No text provided'}${ |
| 42 | + imagesLength ? ` (${imagesLength} image${imagesLength > 1 ? 's' : ''})` : '' |
| 43 | + }\n\nLink to Tweet: ${tweetLink}\n\n- Vercel Serverless Function`; |
43 | 44 |
|
44 | | - functions.logger.info('Notification email sent.'); |
| 45 | + await client.sendMail({ |
| 46 | + from: EMAIL_API.value(), |
| 47 | + to: TARGET_EMAIL.value(), |
| 48 | + subject: emailHeader, |
| 49 | + text: emailText, |
45 | 50 | }); |
| 51 | + |
| 52 | + console.log(`📧 Notification email sent for tweet ${id}`); |
| 53 | +} |
0 commit comments