Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona lógica de consumo da API de convite da Portfoliorrr #62

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ def show

def create
create_invitation
flash[:alert] = invitation_error unless @invitation.save
flash[:notice] = t('.success') if @invitation.save

if @invitation.save
PortfoliorrrInvitation.new(@invitation).post_invitation
flash[:notice] = t('.success')
else
flash[:alert] = invitation_error
end

redirect_to project_portfoliorrr_profile_path(@invitation.project, @invitation.profile_id)
end

def cancel
@invitation.processing!
if @invitation.cancelled!
redirect_to project_portfoliorrr_profile_path(@invitation.project, @invitation.profile_id), notice: t('.success')
else
@invitation.pending!
redirect_to root_path, alert: t('.fail')
end
end
Expand Down
12 changes: 9 additions & 3 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ class Invitation < ApplicationRecord
validate :check_member, on: :create
validate :days_to_date, on: :create

enum status: { pending: 0, accepted: 1, declined: 2, cancelled: 3, expired: 4, removed: 5 }
enum status: { processing: 0, pending: 1, accepted: 2, declined: 3, cancelled: 4, expired: 5 }

def pending!; end
def pending!
super if processing?
end

def cancelled!
def processing!
super if pending?
end

def cancelled!
super if processing?
end

def expired!
super if pending?
end
Expand Down
26 changes: 26 additions & 0 deletions app/models/portfoliorrr_invitation.rb
OtavioLins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class PortfoliorrrInvitation
def initialize(invitation)
@profile_id = invitation.profile_id,
@project_title = invitation.project.title,
@project_description = invitation.project.description,
@project_category = invitation.project.category,
@colabora_invitation_id = invitation.id,
@message = invitation.message,
@expiration_date = invitation.expiration_date
end

def post_invitation
invitation = Invitation.find(@colabora_invitation_id)
headers = { 'Content-Type': 'application/json' }
url = 'http://localhost:8000/invitations'
response = Faraday.post(url, { 'data' => self }, headers)

return invitation.cancelled! unless response.success?

invitation.portfoliorrr_invitation_id = response.body[:data][:id]
invitation.pending!
invitation.save
rescue Faraday::ConnectionFailed
invitation.cancelled!
end
end
4 changes: 3 additions & 1 deletion app/views/portfoliorrr_profiles/_invitation_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</div>
<% end %>

<% if current_invitation.present? %>
<% if current_invitation.present? && current_invitation.pending? %>
<p><%= I18n.t('invitations.create.pending_invitation') %></p>

<% unless current_invitation.message.blank? %>
Expand All @@ -25,6 +25,8 @@

<%= button_to I18n.t('invitations.cancel_button'), cancel_invitation_path(current_invitation.id),
method: :patch, class: 'btn btn-danger' %>
<% elsif current_invitation.present? && current_invitation.processing? %>
<p><%= I18n.t('invitations.create.success') %></p>
<% else %>
<%= form_with(model: invitation, url: project_portfoliorrr_profile_invitations_path(project, profile.id), local: true) do |form| %>
<div class="mb-3">
Expand Down
2 changes: 1 addition & 1 deletion config/locales/models/invitations.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pt-BR:
status: Status
invitations:
create:
success: Convite enviado com sucesso!
success: Convite em processamento!
fail: Não foi possível convidar o usuário
pending_invitation: Esse usuário possui convite pendente
cancel:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPortfoliorInvitationToInvitation < ActiveRecord::Migration[7.1]
def change
add_column :invitations, :portfoliorrr_invitation_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/factories/invitations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
expiration_days { 10 }
project
sequence(:profile_id) { |n| n }
sequence(:profile_email) { |n| "usuario#{n}@email.com" }
sequence(:profile_email) { |_n| 'invited@email.com' }
end
end
102 changes: 93 additions & 9 deletions spec/models/invitation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
end

describe '#cancelled?' do
it 'retorna verdadeiro se tentar mudar de pending para cancelled' do
invitation = build(:invitation, status: :pending)
it 'retorna verdadeiro se tentar mudar de processing para cancelled' do
invitation = build(:invitation, status: :processing)

invitation.cancelled!

Expand Down Expand Up @@ -130,16 +130,16 @@
end

describe '#expired?' do
context 'retorna verdadeiro se tentar mudar de expired para qualquer outro' do
it 'de expired para pending' do
context 'retorna verdadeiro se tentar mudar de expired' do
it 'para pending' do
invitation = build(:invitation, status: :expired)

invitation.pending!

expect(invitation.expired?).to eq true
end

it 'de expired para cancelled' do
it 'para cancelled' do
invitation = build(:invitation, status: :expired)

invitation.cancelled!
Expand Down Expand Up @@ -194,8 +194,8 @@
expect(invitation.accepted?).to eq false
end

it 'não altera status se estiver removed' do
invitation = build(:invitation, status: :removed)
it 'não altera status se estiver processing' do
invitation = build(:invitation, status: :processing)

invitation.accepted!

Expand Down Expand Up @@ -236,15 +236,99 @@
expect(invitation.declined?).to eq false
end

it 'não altera status se estiver removed' do
invitation = build(:invitation, status: :removed)
it 'não altera status se estiver processing' do
invitation = build(:invitation, status: :processing)

invitation.declined!

expect(invitation.declined?).to eq false
end
end

describe '#pending?' do
it 'altera status se estiver processing' do
invitation = build(:invitation, status: :processing)

invitation.pending!

expect(invitation.pending?).to eq true
end

it 'não altera status se estiver accepted' do
invitation = build(:invitation, status: :accepted)

invitation.pending!

expect(invitation.pending?).to eq false
end

it 'não altera status se estiver declined' do
invitation = build(:invitation, status: :declined)

invitation.pending!

expect(invitation.pending?).to eq false
end

it 'não altera status se estiver cancelled' do
invitation = build(:invitation, status: :cancelled)

invitation.pending!

expect(invitation.pending?).to eq false
end

it 'não altera status se estiver expired' do
invitation = build(:invitation, status: :expired)

invitation.pending!

expect(invitation.pending?).to eq false
end

describe '#processing?' do
it 'altera status se estiver pending' do
invitation = build(:invitation, status: :pending)

invitation.processing!

expect(invitation.processing?).to eq true
end

it 'não altera status se estiver cancelled' do
invitation = build(:invitation, status: :cancelled)

invitation.processing!

expect(invitation.processing?).to eq false
end

it 'não altera status se estiver accepted' do
invitation = build(:invitation, status: :accepted)

invitation.processing!

expect(invitation.processing?).to eq false
end

it 'não altera status se estiver declined' do
invitation = build(:invitation, status: :declined)

invitation.processing!

expect(invitation.processing?).to eq false
end

it 'não altera status se estiver expired' do
invitation = build(:invitation, status: :expired)

invitation.processing!

expect(invitation.processing?).to eq false
end
end
end

describe '#expiration_date' do
it 'calcula a data de validade ao ser criado' do
invitation = create(:invitation, expiration_days: 5, profile_email: 'joão@email.com')
Expand Down
52 changes: 52 additions & 0 deletions spec/models/portfoliorrr_invitation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'rails_helper'

RSpec.describe PortfoliorrrInvitation, type: :model do
context '#post_invitation' do
it 'com sucesso atualiza o status e o portfoliorrr_invitation_id' do
json = { data: { id: 3 } }
fake_response = double('faraday_response', status: 200, body: json, success?: true)
allow(Faraday).to receive(:post).and_return(fake_response)
invitation = create(:invitation)
portfoliorrr_invitation = PortfoliorrrInvitation.new(invitation)

portfoliorrr_invitation.post_invitation

expect(invitation.reload.portfoliorrr_invitation_id).to eq 3
expect(invitation.reload.status).to eq 'pending'
end

it 'bad request e convite é cancelado' do
fake_response = double('faraday_response', status: 404, success?: false)
allow(Faraday).to receive(:post).and_return(fake_response)
invitation = create(:invitation)
portfoliorrr_invitation = PortfoliorrrInvitation.new(invitation)

portfoliorrr_invitation.post_invitation

expect(invitation.reload.portfoliorrr_invitation_id).to eq nil
expect(invitation.reload.status).to eq 'cancelled'
end

it 'server error e convite é cancelado' do
fake_response = double('faraday_response', status: 500, success?: false)
allow(Faraday).to receive(:post).and_return(fake_response)
invitation = create(:invitation)
portfoliorrr_invitation = PortfoliorrrInvitation.new(invitation)

portfoliorrr_invitation.post_invitation

expect(invitation.reload.portfoliorrr_invitation_id).to eq nil
expect(invitation.reload.status).to eq 'cancelled'
end

it 'não consegue se conectar com a API' do
invitation = create(:invitation)
portfoliorrr_invitation = PortfoliorrrInvitation.new(invitation)

portfoliorrr_invitation.post_invitation

expect(invitation.reload.portfoliorrr_invitation_id).to eq nil
expect(invitation.reload.status).to eq 'cancelled'
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/invitations/user_answer_invitation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
project = create(:project)
invited = create(:user, cpf: '000.000.001-91')
not_invited = create(:user, cpf: '942.275.100-40')
invitation = create(:invitation, profile_email: invited.email, project:)
invitation = create(:invitation, profile_email: invited.email, project:, status: :pending)

login_as(not_invited)
patch(accept_invitation_path(invitation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
expect(response).to redirect_to root_path
expect(invitation.reload.status).to eq 'expired'
end
it 'de pendente para cancelado' do

it 'de processando para cancelado' do
leader = create(:user)
project = create(:project, user: leader)
invited = create(:user, cpf: '942.275.100-40')
invitation = create(:invitation, status: :pending, profile_email: invited.email, project:)
invitation = create(:invitation, status: :processing, profile_email: invited.email, project:)

login_as(leader)
patch cancel_invitation_path(invitation.id)
Expand All @@ -40,6 +41,7 @@
expect(response).to redirect_to project_portfoliorrr_profile_path(invitation.project, invitation.profile_id)
end
end

it 'para cancelado, mas não é o lider do projeto' do
leader = create(:user)
project = create(:project, user: leader)
Expand Down
8 changes: 4 additions & 4 deletions spec/system/invitations/user_answer_invitation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
user = create :user, cpf: '000.000.001-91'
profile = PortfoliorrrProfile.new(id: 92, name: 'Pedro', job_categories: [JobCategory.new(name: 'Designer')])
invitation = create :invitation, profile_id: profile.id, project:, profile_email: user.email,
message: 'Gostaria de te convidar', expiration_days: 8
message: 'Gostaria de te convidar', expiration_days: 8, status: :pending
second_invitation = create :invitation, profile_id: profile.id, project: second_project, profile_email: user.email,
message: 'Por favor aceite', expiration_days: 5
message: 'Por favor aceite', expiration_days: 5, status: :pending

login_as user
visit root_path
Expand All @@ -36,9 +36,9 @@
user = create :user, cpf: '000.000.001-91'
profile = PortfoliorrrProfile.new(id: 92, name: 'Pedro', job_categories: [JobCategory.new(name: 'Designer')])
invitation = create :invitation, profile_id: profile.id, project:, profile_email: user.email,
message: 'Gostaria de te convidar', expiration_days: 8
message: 'Gostaria de te convidar', expiration_days: 8, status: :pending
second_invitation = create :invitation, profile_id: profile.id, project: second_project, profile_email: user.email,
message: 'Por favor aceite', expiration_days: 5
message: 'Por favor aceite', expiration_days: 5, status: :pending

login_as user
visit root_path
Expand Down
Loading