Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement email notifications #131

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@prisma/client": "^5.1.0",
"@rajesh896/broprint.js": "^2.1.1",
"@redis/json": "^1.0.6",
"@sendgrid/mail": "^8.1.4",
"@vercel/otel": "1.8.0",
"better-npm-audit": "^3.8.3",
"dayjs": "^1.11.10",
Expand Down Expand Up @@ -77,6 +78,7 @@
"@types/next": "^9.0.0",
"@types/next-auth": "^3.15.0",
"@types/node": "^20.4.5",
"@types/node-cron": "^3.0.11",
"@types/react": "^18.3.3",
"@types/react-dom": "18.2.7",
"@types/react-show-more-text": "^1.4.4",
Expand Down
45 changes: 45 additions & 0 deletions app/pages/api/email/userMentionedInComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// import sgMail, { verified_email_address } from "@/repository/sendgrid";


// export default async function sendEmail(req, res) {
// if (req.method === 'POST') {
// try {
// const {emails} = req.body;
// const msg = {
// to: [...emails],
// from: {
// email: verified_email_address,
// name: 'KFW',
// },
// subject: 'InnoVerse',
// html: `
// <html lang="en">
// <head>
// <style>
// p { margin-bottom: 16px; }
// </style>
// </head>
// <body>
// <h1>You have been mentioned in InnoVerse comment</h1>
// </body>
// </html>
// `,
// };

// sgMail
// .send(msg)
// .then(() => {
// console.log('Email sent')
// })
// .catch((error) => {
// console.error(error)
// })
// res.status(200).json({message: 'Email sent successfully'});
// } catch (error) {
// console.error('Error sending email:', error);
// res.status(500).json({message: 'Error sending email'});
// }
// } else {
// res.status(405).json({message: 'Method Not Allowed'});
// }
// }
10 changes: 10 additions & 0 deletions app/repository/sendgrid/sendgrid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sgMail from '@sendgrid/mail';

if (!process.env.SENDGRID_API_KEY) {
throw new Error('Missing SENDGRID_API_KEY');
}

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
export default sgMail;

export const verified_email_address = process.env.SENDGRID_VERIFIED_EMAIL_ADDRESS;
41 changes: 41 additions & 0 deletions strapi/config/cron-tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export default {
weeklyEmailJob: {
task: async ({ strapi }) => {

try {
// Uncomment and use the actual user fetching logic as needed
// const users = await strapi.entityService.findMany('api::inno-user.inno-user', {
// fields: ['email', 'name'],
// });

// Mock data for testing - replace with your own email address for testing
const users = [
{ id: 63, email: '[email protected]', name: 'Hrmo, S.' }
];

for (const user of users) {
try {
await strapi.plugin('email').service('email').send({
to: user.email,
subject: 'Weekly Update',
text: `Hello ${user.name}, this is a test email!`,
html: `<p>Hello ${user.name},</p><p>This is a test email!</p>`,
});

console.log(`Email sent to ${user.email}`);
} catch (error) {
console.error(`Failed to send email to ${user.email}:`, error);
}
}

console.log('Weekly email cron job executed successfully');
} catch (error) {
console.error('Error executing weekly email cron job:', error);
}
},
options: {
rule: '*/1 * * * *',
},
},
};

16 changes: 14 additions & 2 deletions strapi/config/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
export default ({ env }) => ({
graphql: {
config: {
generateArtifacts: true,
Expand All @@ -24,4 +24,16 @@ export default {
sizeLimit: 250 * 1024 * 1024, // 256mb in bytes
},
},
};
email: {
config: {
provider: 'sendgrid',
providerOptions: {
apiKey: env('SENDGRID_API_KEY'),
},
settings: {
defaultFrom: env('SENDGRID_VERIFIED_EMAIL_ADDRESS'),
defaultReplyTo: env('SENDGRID_VERIFIED_EMAIL_ADDRESS'),
},
},
},
});
6 changes: 6 additions & 0 deletions strapi/config/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cronTasks from "./cron-tasks";

export default ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
Expand All @@ -10,4 +12,8 @@ export default ({ env }) => ({
webhooks: {
populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
},
cron: {
enabled: true,
tasks: cronTasks,
},
});
Loading