Skip to content

Commit

Permalink
Merge pull request #123 from TreinaDev/feature/lider-recebe-email-de-…
Browse files Browse the repository at this point in the history
…solicitacao

Feature/lider recebe email de solicitacao
  • Loading branch information
oLucasAguilar authored Feb 16, 2024
2 parents 34283c3 + cd33e14 commit e9324d7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/controllers/api/v1/proposals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions app/mailers/proposal_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProposalMailer < ApplicationMailer
default from: '[email protected]'

def notify_leader
@proposal = params[:proposal]
@project = @proposal.project
@leader = @project.user

mail to: @leader.email, subject: t('.subject')
end
end
10 changes: 10 additions & 0 deletions app/views/proposal_mailer/notify_leader.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<main>
<h1><%= t '.project_received_a_new_proposal', project_title: @project.title %> </h1>

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

<%= link_to t('.view_proposal'),
project_portfoliorrr_profile_url(@project, @proposal.profile_id) %>
</main>

8 changes: 8 additions & 0 deletions config/locales/mailers/proposal_mailer.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions spec/mailers/proposal_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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: '[email protected]'
project = create :project, user: leader,
title: 'Canal de Youtube'
proposal = create :proposal, project:,
message: 'Gostaria de participar',
email: '[email protected]'

mail = ProposalMailer.with(proposal:).notify_leader

expect(mail.to).to include '[email protected]'
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: [email protected]'
expect(mail.body.encoded).to include 'Mensagem: Gostaria de participar'
link = project_portfoliorrr_profile_url project, proposal.profile_id
button = "<a href=\"#{link}\">Visualizar solicitação</a>"
expect(mail.body.encoded).to include button
end
end
end
10 changes: 10 additions & 0 deletions spec/requests/api/v1/proposals/proposal_creation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
message: 'Gostaria de participar!',
email: '[email protected]'
} }
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:)

Expand All @@ -26,6 +30,7 @@
expect(Proposal.last.message).to eq 'Gostaria de participar!'
expect(Proposal.last.email).to eq '[email protected]'
expect(Proposal.last.status).to eq 'pending'
expect(mail).to have_received :deliver
end

context 'sem sucesso' do
Expand All @@ -40,13 +45,18 @@
profile_id: contributor_portfoliorrr_profile_id,
email: '[email protected]'
} }
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:)

expect(Proposal.count).to eq 0
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
Expand Down

0 comments on commit e9324d7

Please sign in to comment.