diff --git a/appwrite.json b/appwrite.json index 511204e..54b8d81 100644 --- a/appwrite.json +++ b/appwrite.json @@ -575,10 +575,10 @@ "timeout": 15 }, { - "$id": "rejectProject", - "name": "rejectProject", + "$id": "reviewProject", + "name": "reviewProject", "runtime": "node-16.0", - "path": "functions/rejectProject", + "path": "functions/reviewProject", "entrypoint": "src/index.js", "ignore": [ "node_modules", diff --git a/functions/rejectProject/.gitignore b/functions/reviewProject/.gitignore similarity index 100% rename from functions/rejectProject/.gitignore rename to functions/reviewProject/.gitignore diff --git a/functions/rejectProject/README.md b/functions/reviewProject/README.md similarity index 89% rename from functions/rejectProject/README.md rename to functions/reviewProject/README.md index a491fc4..f96623a 100644 --- a/functions/rejectProject/README.md +++ b/functions/reviewProject/README.md @@ -1,10 +1,10 @@ -# rejectProject +# rewiewProject Welcome to the documentation of this function 👋 We strongly recommend keeping this file in sync with your function's logic to make sure anyone can easily understand your function in the future. If you don't need documentation, you can remove this file. ## 🤖 Documentation -A function to send a project rejection email once a rejection reason is entered. After the email is sent, the project is deleted from the database. +A function to send an email once a project is approved or rejected. A project is approved when `isPublished` is set to `true` and rejected when any `rejectionReason` is entered. After rejection, the project is also deleted from the database. diff --git a/functions/rejectProject/package-lock.json b/functions/reviewProject/package-lock.json similarity index 100% rename from functions/rejectProject/package-lock.json rename to functions/reviewProject/package-lock.json diff --git a/functions/rejectProject/package.json b/functions/reviewProject/package.json similarity index 100% rename from functions/rejectProject/package.json rename to functions/reviewProject/package.json diff --git a/functions/rejectProject/src/index.js b/functions/reviewProject/src/index.js similarity index 71% rename from functions/rejectProject/src/index.js rename to functions/reviewProject/src/index.js index ced4b34..d9e7fb9 100644 --- a/functions/rejectProject/src/index.js +++ b/functions/reviewProject/src/index.js @@ -1,6 +1,6 @@ const sdk = require("node-appwrite"); const nodemailer = require("nodemailer"); -const template = require("./template"); +const templates = require("./templates"); module.exports = async function (req, res) { const client = new sdk.Client(); @@ -29,12 +29,6 @@ module.exports = async function (req, res) { throw new Error("Invalid project update event."); } - if (!project.rejectionReason || project.isPublished) { - console.log("Project has no rejection reason or is already published."); - res.json({ ok: true }); - return; - } - console.log("Fetching project author"); const author = await user.get(project.userId); if (!author || !author.email) { @@ -55,29 +49,47 @@ module.exports = async function (req, res) { }, }); - console.log("Sending email to project author"); + if (project.isPublished) { + console.log("Sending approval email"); + try { + await transporter.sendMail({ + from: SMTP_USERNAME, + to: author.email, + bcc: req.variables["APPROVER_EMAILS"], + subject: "Project Review - builtwith.appwrite.io ", + text: templates.approval(project), + }); + console.log("Done - exiting."); + } catch (error) { + throw new Error(`Failed to send approval email: ${error}`); + } + return; + } + + if (!project.rejectionReason) { + console.log("Project is not rejected - exiting."); + return; + } + + console.log("Sending rejection email"); try { await transporter.sendMail({ from: SMTP_USERNAME, to: author.email, bcc: req.variables["APPROVER_EMAILS"], subject: "Project Review - builtwith.appwrite.io ", - text: template(project), + text: templates.rejection(project), }); console.log("Done"); } catch (error) { - throw new Error(`Failed to send email: ${error}`); + throw new Error(`Failed to send rejection email: ${error}`); } console.log("Deleting the project document"); try { await database.deleteDocument("main", "projects", project.$id); - console.log("Done"); + console.log("Done - exiting."); } catch (error) { throw new Error(`Failed to delete project: ${error}`); } - - res.json({ - ok: true, - }); }; diff --git a/functions/rejectProject/src/template.js b/functions/reviewProject/src/templates.js similarity index 81% rename from functions/rejectProject/src/template.js rename to functions/reviewProject/src/templates.js index 1d47752..a9dc2e3 100644 --- a/functions/rejectProject/src/template.js +++ b/functions/reviewProject/src/templates.js @@ -50,6 +50,7 @@ Thank you for submitting your project "${project.name}" to Built With Appwrite. Unfortunately, we have decided to reject it for the following reason: > ${project.rejectionReason} + Don't sweat it, though! This is all part of the process and an opportunity for growth. 🌱 After addressing the reason for rejection, feel free to submit the project again. We'd be excited to see your improved version! 🚀 @@ -60,4 +61,15 @@ Keep on coding and creating awesome stuff! 💻🔥 The Built With Appwrite Team`; } -module.exports = rejectionTemplate; +function approvalTemplate(project) { + return `Hey 👋, +Thank you for submitting your project "${project.name}" to Built With Appwrite. + +After reviewing your project, we have decided to approve it! 🎉 +You can now find it on our website: https://builtwith.appwrite.io/projects/${project.$id} + +Keep on coding and creating awesome stuff! 💻🔥 +The Built With Appwrite Team`; +} + +module.exports = { rejection: rejectionTemplate, approval: approvalTemplate };