Skip to content

Commit

Permalink
add possibility to send emails when user is added to a project
Browse files Browse the repository at this point in the history
  • Loading branch information
khansadaoudi committed Jan 3, 2025
1 parent 24031a8 commit 1966f1e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/user/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .interface import UserInterface
from .model import User
from .schema import UserSchema
from .service import UserService
from .service import UserService, EmailService

api = Namespace("User", description="Single namespace, single entity") # noqa

Expand Down Expand Up @@ -68,3 +68,15 @@ def get(self):
logout_user()
js = json.dumps({"logout": True}, default=str)
return Response(js, status=200, mimetype="application/json")

@api.route("/send-email")
class SendEmailResource(Resource):

def post(self):
"""Send email to user when they are added to a new project"""
data = request.get_json()
username = data.get("username")
role = data.get("role")
project_name = data.get("projectName")

EmailService.send_email_to_user(username, role, project_name)
24 changes: 24 additions & 0 deletions app/user/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,30 @@ def get_github_user_information(access_token: str):
class EmailService:
"""Class contains method to send alert using email"""

@staticmethod
def send_email_to_user(username: str, role: str, project_name: str):
"""
Send email to user when they are added to a new project
Args:
username (str)
role (str): role of the user in the project
project_name (str)
"""
user_email = UserService.get_by_username(username).email

if user_email:
mail_message = Message(
"You are added to a new project on ArboratorGrew",
recipients=[user_email]
)
mail_message.body = f"""Hello {username},\n\nYou are added to the project '{project_name}'
on ArboratorGrew as a {role} role.\nHere is the link of the project
https://arborator.grew.fr/#/projects/{project_name}\n\nBest regards,\nArboratorGrew team."""
mail.send(mail_message)
else:
return

@staticmethod
def send_alert_email(title: str, alert: str):
"""
Expand Down

0 comments on commit 1966f1e

Please sign in to comment.