diff --git a/app.js b/app.js index 3f9b77b..461ee9a 100644 --- a/app.js +++ b/app.js @@ -67,14 +67,14 @@ app.get('/twitter-card.svg', (req, res) => { }); // Schedule cleanup job -cron.schedule('*/10 * * * *', async () => { - try { - await MessageService.cleanup(); - console.log('Daily cleanup completed'); - } catch (error) { - console.error('Cleanup failed:', error); - } -}); +// cron.schedule('*/10 * * * *', async () => { +// try { +// await MessageService.cleanup(); +// console.log('Daily cleanup completed'); +// } catch (error) { +// console.error('Cleanup failed:', error); +// } +// }); app.use(express.static(path.join(__dirname, 'client/build'))); diff --git a/scripts/.crons b/scripts/.crons new file mode 100644 index 0000000..4da9bfd --- /dev/null +++ b/scripts/.crons @@ -0,0 +1,2 @@ +0 * * * * /root/temp_mail/scripts/delete_messages_24h.sh >> /var/log/temp_mail/cleanup.log 2>&1 +0 0 * * * /root/temp_mail/scripts/delete_emails_14d.sh >> /var/log/temp_mail/cleanup.log 2>&1 diff --git a/scripts/delete_emails_14d.sh b/scripts/delete_emails_14d.sh new file mode 100644 index 0000000..2146698 --- /dev/null +++ b/scripts/delete_emails_14d.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -e # Exit on any error + +# Get the directory of the script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Find node executable +node=$(which node) + +# Get database credentials from config +db_user=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.user);") +if [ -z "$db_user" ]; then + echo "Error: Could not get database user" + exit 1 +fi + +db_pass=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.password);") +if [ -z "$db_pass" ]; then + echo "Error: Could not get database password" + exit 1 +fi + +db_name=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.database);") +if [ -z "$db_name" ]; then + echo "Error: Could not get database name" + exit 1 +fi + +# Execute MySQL commands +mysql -u"$db_user" -p"$db_pass" "$db_name" < { - if (!req.body.temp_email_id) { + const { temp_email_id } = req.body; + + if (!temp_email_id) { return res.status(400).json({ error: 'temp_email_id is required' }); } - const messages = await Message.query().where('temp_email_id', req.body.temp_email_id); - res.json(messages); + + const isInteger = Number.isInteger(Number(temp_email_id)); + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + const isEmail = typeof temp_email_id === 'string' && emailRegex.test(temp_email_id); + + if (!isInteger && !isEmail) { + return res.status(400).json({ error: 'temp_email_id must be either an integer or a valid email address' }); + } + + if (isInteger) { + const messages = await Message.query().where('temp_email_id', temp_email_id); + res.json(messages); + } else { + const tempEmail = await TempEmail.query().where('email', temp_email_id).first(); + if (!tempEmail) { + return res.status(404).json({ error: 'Temporary email not found' }); + } + const messages = await Message.query().where('temp_email_id', tempEmail.id); + res.json(messages); + } }); router.post('/read/:id', async (req, res) => {