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

Feat/api endpoint recusa solicitação convite #202

Merged
merged 8 commits into from
Feb 14, 2024
63 changes: 63 additions & 0 deletions api_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,66 @@ Retorno esperado:
}
```
</details>

## 7. Alterar status de solicitação de convite

<details>
<summary>PATCH /api/v1/invitation_requests/:id</summary>

<br>

### Endpoint

```shell
PATCH /api/v1/invitation_requests/:id
```

Retorno esperado caso a requisição seja bem sucedida. **(Status: 200)**

Resposta:
```json
{
"id": 1,
"profile_id": 1,
"message": "exemplo de mensagem enviada pelo usuário do Portfoliorrr",
"project_id": 1,
"status": "refused"
}
```

### Erros tratados

Erro quando a solicitação não estiver com status Pendente **(Status: 409)**

Resposta:
```json
{
"errors": "Solicitação de convite não está pendente para ser recusada."
}
```

Erro para id de solicitação de convite inválido **(Status: 404)**

Este erro acontece quando a requisição é feita com um id de solicitação de convite que não existe.

```shell
PATCH /api/v1/invitation_requests/999999999999999
```

Retorno esperado:

```json
{
"error": "Não encontrado"
}
```

Erro interno do servidor **(Status: 500)**

Resposta:
```json
{
"errors": "Houve um erro interno no servidor ao processar sua solicitação."
}
```
</details>
17 changes: 17 additions & 0 deletions app/controllers/api/v1/invitation_request_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Api
module V1
class InvitationRequestController < ApiController
def update
raise ActiveRecord::ParameterMissing if params[:id].blank?

invitation_request = InvitationRequest.find(params[:id])
if invitation_request.pending?
invitation_request.refused!
render status: :ok, json: invitation_request.as_json(except: %i[created_at updated_at])
else
render status: :conflict, json: { errors: I18n.t('.api_not_pending_error') }
end
end
end
end
end
3 changes: 2 additions & 1 deletion config/locales/invitation_request.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ pt-BR:
project_api_error: Não foi possível carregar informações dos projetos. Tente mais tarde
no_invitation_request_msg: Você ainda não fez nenhuma solicitação de convite.
no_filter_results_msg: Não existem solicitações com o status selecionado.
time_ago: 'Enviada há %{time}'
time_ago: 'Enviada há %{time}'
api_not_pending_error: 'Solicitação de convite não está pendente para ser recusada.'
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
resources :job_categories, only: %i[index show]
resources :profiles, only: %i[show index]
resources :invitations, only: %i[create update]

resources :invitation_request, only: %i[update]

get 'projects/request_invitation', controller: :projects
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'rails_helper'

describe 'API solicitações' do
context 'PATCH /api/v1/invitation_requests/:id' do
it 'altera o status para recusado com sucesso' do
user = create(:user)
invitation_request = create(:invitation_request, profile: user.profile, status: :pending, message: 'oi projeto')

patch "/api/v1/invitation_request/#{invitation_request.id}"
invitation_request.reload

expect(response).to have_http_status(200)
expect(response.content_type).to include('application/json')
json_response = JSON.parse(response.body)
expect(json_response.class).to eq Hash
expect(json_response['id']).to eq 1
expect(json_response['profile_id']).to eq 1
expect(json_response['message']).to eq 'oi projeto'
expect(json_response['project_id']).to eq 1
expect(json_response.keys).not_to include 'created_at'
expect(json_response.keys).not_to include 'updated_at'
expect(json_response['status']).to eq 'refused'
end

it 'retorna erro 409 se a solicitação não estiver pendente' do
request1 = create(:invitation_request, status: :processing)
request2 = create(:invitation_request, status: :accepted)
request3 = create(:invitation_request, status: :refused)
request4 = create(:invitation_request, status: :error)
request5 = create(:invitation_request, status: :aborted)

response_status1 = patch "/api/v1/invitation_request/#{request1.id}"
response_status2 = patch "/api/v1/invitation_request/#{request2.id}"
response_status3 = patch "/api/v1/invitation_request/#{request3.id}"
response_status4 = patch "/api/v1/invitation_request/#{request4.id}"
response_status5 = patch "/api/v1/invitation_request/#{request5.id}"
request1.reload
request2.reload
request3.reload
request4.reload
request5.reload

expect(response_status1).to eq 409
expect(response_status2).to eq 409
expect(response_status3).to eq 409
expect(response_status4).to eq 409
expect(response_status5).to eq 409
expect(request1).to be_processing
expect(request2).to be_accepted
expect(request3).to be_refused
expect(request4).to be_error
expect(request5).to be_aborted
expect(response.content_type).to include 'application/json'
json_last_response = JSON.parse(response.body)
expect(json_last_response.class).to eq Hash
expect(json_last_response['errors']).to include 'Solicitação de convite não está pendente para ser recusada.'
end

it 'retorna erro 404 se a solicitação não for encontrada' do
patch '/api/v1/invitation_request/1939'

expect(response.status).to eq 404
expect(response.content_type).to include 'application/json'
json_response = JSON.parse(response.body)
expect(json_response['error']).to include 'Não encontrado'
end

it 'retorna erro 500, com mensagem identificando erro do servidor' do
allow(InvitationRequest).to receive(:find).and_raise(ActiveRecord::ActiveRecordError)
invitation_request = create(:invitation_request)
patch "/api/v1/invitation_request/#{invitation_request.id}"

expect(response.status).to eq 500
expect(response.content_type).to include 'application/json'
json_response = JSON.parse(response.body)
expect(json_response['error']).to include 'Houve um erro interno no servidor ao processar sua solicitação.'
end
end
end