diff --git a/app/controllers/api/v1/proposals_controller.rb b/app/controllers/api/v1/proposals_controller.rb index e4cf2f9..094937d 100644 --- a/app/controllers/api/v1/proposals_controller.rb +++ b/app/controllers/api/v1/proposals_controller.rb @@ -4,12 +4,12 @@ class ProposalsController < Api::V1::ApiController before_action :set_project, only: %i[create] def create - proposal = @project.proposals.new(proposal_params.except(:invitation_request_id)) - proposal.portfoliorrr_proposal_id = proposal_params[:invitation_request_id] + @proposal = @project.proposals.new(proposal_params.except(:invitation_request_id)) + @proposal.portfoliorrr_proposal_id = proposal_params[:invitation_request_id] - return render json: { data: { proposal_id: proposal.id } }, status: :created if proposal.save + return success_result if @proposal.save - render json: { errors: proposal.errors.full_messages }, status: :conflict + render json: { errors: @proposal.errors.full_messages }, status: :conflict end def update @@ -23,6 +23,11 @@ def update private + def success_result + ProposalMailer.with(proposal: @proposal).notify_leader.deliver + render json: { data: { proposal_id: @proposal.id } }, status: :created + end + def set_project @project = Project.find(proposal_params[:project_id]) rescue ActiveRecord::RecordNotFound diff --git a/app/mailers/proposal_mailer.rb b/app/mailers/proposal_mailer.rb new file mode 100644 index 0000000..5837c1d --- /dev/null +++ b/app/mailers/proposal_mailer.rb @@ -0,0 +1,11 @@ +class ProposalMailer < ApplicationMailer + default from: 'notification@colabora.com' + + def notify_leader + @proposal = params[:proposal] + @project = @proposal.project + @leader = @project.user + + mail to: @leader.email, subject: t('.subject') + end +end diff --git a/app/views/proposal_mailer/notify_leader.html.erb b/app/views/proposal_mailer/notify_leader.html.erb new file mode 100644 index 0000000..475f51a --- /dev/null +++ b/app/views/proposal_mailer/notify_leader.html.erb @@ -0,0 +1,10 @@ +
+

<%= t '.project_received_a_new_proposal', project_title: @project.title %>

+ +

<%= t '.proposer', email: @proposal.email %>

+

<%= t '.message', message: @proposal.message %>

+ + <%= link_to t('.view_proposal'), + project_portfoliorrr_profile_url(@project, @proposal.profile_id) %> +
+ diff --git a/config/locales/mailers/proposal_mailer.yml b/config/locales/mailers/proposal_mailer.yml new file mode 100644 index 0000000..6e4fe47 --- /dev/null +++ b/config/locales/mailers/proposal_mailer.yml @@ -0,0 +1,8 @@ +pt-BR: + proposal_mailer: + notify_leader: + subject: Nova solicitação para seu projeto! + project_received_a_new_proposal: Seu projeto %{project_title} recebeu uma nova solicitação! + proposer: "Solicitante: %{email}" + message: "Mensagem: %{message}" + view_proposal: Visualizar solicitação \ No newline at end of file diff --git a/spec/mailers/proposal_mailer_spec.rb b/spec/mailers/proposal_mailer_spec.rb new file mode 100644 index 0000000..6921851 --- /dev/null +++ b/spec/mailers/proposal_mailer_spec.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +RSpec.describe ProposalMailer, type: :mailer do + describe '#notify_leader' do + it 'envia email para o líder do projeto com sucesso' do + leader = create :user, email: 'leader@email.com' + project = create :project, user: leader, + title: 'Canal de Youtube' + proposal = create :proposal, project:, + message: 'Gostaria de participar', + email: 'proposer_portfoliorrr@email.com' + + mail = ProposalMailer.with(proposal:).notify_leader + + expect(mail.to).to include 'leader@email.com' + expect(mail.subject).to include 'Nova solicitação para seu projeto!' + expect(mail.body.encoded).to include 'Seu projeto Canal de Youtube recebeu uma nova solicitação!' + expect(mail.body.encoded).to include 'Solicitante: proposer_portfoliorrr@email.com' + expect(mail.body.encoded).to include 'Mensagem: Gostaria de participar' + link = project_portfoliorrr_profile_url project, proposal.profile_id + button = "Visualizar solicitação" + expect(mail.body.encoded).to include button + end + end +end diff --git a/spec/requests/api/v1/proposals/proposal_creation_spec.rb b/spec/requests/api/v1/proposals/proposal_creation_spec.rb index 3239aec..3ecf7ca 100644 --- a/spec/requests/api/v1/proposals/proposal_creation_spec.rb +++ b/spec/requests/api/v1/proposals/proposal_creation_spec.rb @@ -11,6 +11,10 @@ message: 'Gostaria de participar!', email: 'proposer@email.com' } } + mail = double 'mail', deliver: true + mailer = double 'ProposalMailer' + allow(ProposalMailer).to receive(:with).and_return mailer + allow(mailer).to receive(:notify_leader).and_return mail post(api_v1_proposals_path, params:) @@ -26,6 +30,7 @@ expect(Proposal.last.message).to eq 'Gostaria de participar!' expect(Proposal.last.email).to eq 'proposer@email.com' expect(Proposal.last.status).to eq 'pending' + expect(mail).to have_received :deliver end context 'sem sucesso' do @@ -40,6 +45,10 @@ profile_id: contributor_portfoliorrr_profile_id, email: 'contributor@email.com' } } + mail = double 'mail', deliver: true + mailer = double 'ProposalMailer' + allow(ProposalMailer).to receive(:with).and_return mailer + allow(mailer).to receive(:notify_leader).and_return mail post(api_v1_proposals_path, params:) @@ -47,6 +56,7 @@ json_response = JSON.parse(response.body) expect(response).to have_http_status :conflict expect(json_response['errors']).to eq ['Usuário já faz parte deste projeto'] + expect(mail).not_to have_received :deliver end it 'se já existe uma solicitação pendente' do