-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from smdthiranjaya/dev
Clean the code.
- Loading branch information
Showing
10 changed files
with
488 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const pool = require('../db'); | ||
|
||
async function addSubscription(req, res) { | ||
const { email, city } = req.body; | ||
try { | ||
const client = await pool.connect(); | ||
const insertQuery = ` | ||
INSERT INTO subscriptions (email, city) | ||
VALUES ($1, $2) | ||
RETURNING *; | ||
`; | ||
const result = await client.query(insertQuery, [email, city]); | ||
client.release(); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
message: 'Subscription added successfully', | ||
subscription: result.rows[0] | ||
}); | ||
} catch (error) { | ||
console.error('Error adding subscription:', error); | ||
res.status(500).json({ | ||
success: false, | ||
message: 'Error adding subscription' | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { addSubscription }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const { addSubscription } = require('../controllers/subscriptionController'); | ||
|
||
router.post('/subscribe', addSubscription); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
const nodemailer = require('nodemailer'); | ||
const pool = require('../db'); | ||
|
||
async function sendWeatherUpdates() { | ||
const client = await pool.connect(); | ||
try { | ||
const { rows: subscriptions } = await client.query('SELECT DISTINCT city FROM subscriptions;'); | ||
for (const { city } of subscriptions) { | ||
const weatherData = await fetchWeatherDataForCity(city); | ||
const { rows: subscribers } = await client.query('SELECT email FROM subscriptions WHERE city = $1;', [city]); | ||
for (const { email } of subscribers) { | ||
await sendEmail(email, city, weatherData); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Failed to send weather updates:', error); | ||
} finally { | ||
client.release(); | ||
} | ||
} | ||
|
||
async function fetchWeatherDataForCity(city) { | ||
const client = await pool.connect(); | ||
try { | ||
const query = ` | ||
SELECT MAX(temperature) AS max_temp, MIN(temperature) AS min_temp, AVG(temperature) AS avg_temp | ||
FROM weather_data | ||
WHERE city = $1; | ||
`; | ||
const result = await client.query(query, [city]); | ||
return result.rows[0]; | ||
} catch (error) { | ||
console.error(`Failed to fetch weather data for city ${city}:`, error); | ||
throw error; | ||
} finally { | ||
client.release(); | ||
} | ||
} | ||
|
||
|
||
async function sendEmail(recipient, city, weatherData) { | ||
let transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: 'geo360.live@gmail.com', | ||
pass: 'aymj fcyu udnc ghpd' | ||
} | ||
}); | ||
|
||
let mailOptions = { | ||
from: 'geo360.live@gmail.com', | ||
to: recipient, | ||
subject: `Weather Update for ${city}`, | ||
text: `Good day! ✨ | ||
Here's your latest weather update for ${city}: | ||
🔺 - Maximum Temperature: ${weatherData.max_temp}°C | ||
🔻 - Minimum Temperature: ${weatherData.min_temp}°C | ||
🔍 - Average Temperature: ${parseFloat(weatherData.avg_temp).toFixed(2)}°C | ||
Stay informed and plan your day accordingly! | ||
Best, | ||
Geo360 Team.` | ||
}; | ||
|
||
transporter.sendMail(mailOptions, function(error, info){ | ||
if (error) { | ||
console.log('Email send error:', error); | ||
} else { | ||
console.log('Email sent: ' + info.response); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { sendWeatherUpdates }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters