From e6620f16e875619144d865c66e31e3e4b76bfbf6 Mon Sep 17 00:00:00 2001 From: stepantishhen Date: Sun, 5 Nov 2023 12:54:32 +0300 Subject: [PATCH] added HW9 --- app/interactors/tasks/create.rb | 13 +++++++++ app/interactors/tasks/destroy.rb | 12 ++++++++ app/interactors/tasks/update.rb | 10 +++++++ app/mailers/task_mailer.rb | 29 +++++++++++++++++++ .../task_mailer/task_created_member.html.erb | 7 +++++ .../task_mailer/task_created_owner.html.erb | 8 +++++ app/views/task_mailer/task_deleted.html.erb | 2 ++ app/views/task_mailer/task_updated.html.erb | 2 ++ 8 files changed, 83 insertions(+) create mode 100644 app/mailers/task_mailer.rb create mode 100644 app/views/task_mailer/task_created_member.html.erb create mode 100644 app/views/task_mailer/task_created_owner.html.erb create mode 100644 app/views/task_mailer/task_deleted.html.erb create mode 100644 app/views/task_mailer/task_updated.html.erb diff --git a/app/interactors/tasks/create.rb b/app/interactors/tasks/create.rb index 8b75ccf7..a7d7d0b7 100644 --- a/app/interactors/tasks/create.rb +++ b/app/interactors/tasks/create.rb @@ -2,7 +2,20 @@ module Tasks class Create include Interactor::Organizer + delegate :project, :task, to: :context + organize Tasks::Creates::PrepareParams, Tasks::Save + after do + task = context.task + project = task.project + owner = project.users.find_by(project_memberships: { role: "owner" }) + members = project.users.where.not(id: owner.id) + + TaskMailer.task_created_owner(owner, task).deliver_later if owner.present? + members.each do |member| + TaskMailer.task_created_member(member, task).deliver_later + end + end end end diff --git a/app/interactors/tasks/destroy.rb b/app/interactors/tasks/destroy.rb index 78b2c21c..0310073c 100644 --- a/app/interactors/tasks/destroy.rb +++ b/app/interactors/tasks/destroy.rb @@ -4,8 +4,20 @@ class Destroy delegate :task, to: :context + before do + context.task = task + context.project = task.project + context.users = User.where(id: task.project.users.pluck(:id)) + end + def call task.destroy end + + after do + context.users.each do |user| + TaskMailer.task_deleted(user).deliver_later + end + end end end diff --git a/app/interactors/tasks/update.rb b/app/interactors/tasks/update.rb index 04bf6fbc..ee2e53c9 100644 --- a/app/interactors/tasks/update.rb +++ b/app/interactors/tasks/update.rb @@ -3,5 +3,15 @@ class Update include Interactor::Organizer organize Tasks::Save + + after do + task = context.task + project = task.project + users = project.users + + users.each do |user| + TaskMailer.task_updated(user, task).deliver_later + end + end end end diff --git a/app/mailers/task_mailer.rb b/app/mailers/task_mailer.rb new file mode 100644 index 00000000..e4f68e58 --- /dev/null +++ b/app/mailers/task_mailer.rb @@ -0,0 +1,29 @@ +class TaskMailer < ApplicationMailer + def task_created_owner(user, task) + @user = user + @task = task + @project = task.project + + mail(to: @user.email, subject: "A new task has been successfully created") + end + + def task_created_member(user, task) + @user = user + @task = task + @project = task.project + + mail(to: @user.email, subject: "A new task has been created in your project") + end + + def task_updated(user, task) + @user = user + @task = task + @project = task.project + mail(to: @user.email, subject: "A task in your project has been updated") + end + + def task_deleted(user) + @user = user + mail(to: @user.email, subject: "A task in your project has been deleted") + end +end diff --git a/app/views/task_mailer/task_created_member.html.erb b/app/views/task_mailer/task_created_member.html.erb new file mode 100644 index 00000000..854f1eab --- /dev/null +++ b/app/views/task_mailer/task_created_member.html.erb @@ -0,0 +1,7 @@ +

A new task has been created in your project!

+ \ No newline at end of file diff --git a/app/views/task_mailer/task_created_owner.html.erb b/app/views/task_mailer/task_created_owner.html.erb new file mode 100644 index 00000000..9b3602d2 --- /dev/null +++ b/app/views/task_mailer/task_created_owner.html.erb @@ -0,0 +1,8 @@ +

A new task "<%= @task.name %>" has been successfully created in your project "<%= @project.name %>"!

+

Only for project owner

+ \ No newline at end of file diff --git a/app/views/task_mailer/task_deleted.html.erb b/app/views/task_mailer/task_deleted.html.erb new file mode 100644 index 00000000..f2261506 --- /dev/null +++ b/app/views/task_mailer/task_deleted.html.erb @@ -0,0 +1,2 @@ +

A task in your project has been deleted!

+

The task "<%= @task.name %>" in the project "<%= @project.name %>" has been deleted.

\ No newline at end of file diff --git a/app/views/task_mailer/task_updated.html.erb b/app/views/task_mailer/task_updated.html.erb new file mode 100644 index 00000000..05d735a0 --- /dev/null +++ b/app/views/task_mailer/task_updated.html.erb @@ -0,0 +1,2 @@ +

A task in your project has been updated!

+

The task "<%= @task.name %>" in the project "<%= @project.name %>" has recently been updated.

\ No newline at end of file