From 67b1dc02603f699e7570cde4ba76fef3c465b009 Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Tue, 13 Feb 2024 14:22:28 -0300 Subject: [PATCH 1/6] =?UTF-8?q?Cria=20endpoint=20de=20atualiza=C3=A7=C3=A3?= =?UTF-8?q?o=20de=20invitation=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rodrigo Gyodai --- .../api/v1/invitation_requests_controller.rb | 11 +++++++ config/routes.rb | 1 + db/schema.rb | 6 ++-- .../leader_denies_invitation_request_spec.rb | 29 +++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 app/controllers/api/v1/invitation_requests_controller.rb create mode 100644 spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb diff --git a/app/controllers/api/v1/invitation_requests_controller.rb b/app/controllers/api/v1/invitation_requests_controller.rb new file mode 100644 index 0000000..0e49c26 --- /dev/null +++ b/app/controllers/api/v1/invitation_requests_controller.rb @@ -0,0 +1,11 @@ +module Api + module V1 + class InvitationRequestsController < ApiController + def update + invitation_request = InvitationRequest.find(params[:id]) + invitation_request.refused! + render status: :ok, json: invitation_request.as_json(except: %i[created_at updated_at]) + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 6d68a5e..6bc46c7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,7 @@ resources :job_categories, only: %i[index show] resources :profiles, only: %i[show index] resources :invitations, only: %i[create update] + resources :invitation_requests, only: %i[update] get 'projects/request_invitation', controller: :projects end diff --git a/db/schema.rb b/db/schema.rb index ad348c5..244d60f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_12_195603) do +ActiveRecord::Schema[7.1].define(version: 2024_02_12_172630) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -133,7 +133,7 @@ t.integer "notifiable_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "status", default: 0 + t.boolean "read", default: false t.index ["notifiable_type", "notifiable_id"], name: "index_notifications_on_notifiable" t.index ["profile_id"], name: "index_notifications_on_profile_id" end @@ -160,7 +160,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "pin", default: 0 - t.datetime "edited_at", default: "2024-02-12 12:29:41" + t.datetime "edited_at", default: "2024-02-13 14:46:51" t.integer "status", default: 0 t.datetime "published_at" t.string "old_status" diff --git a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb new file mode 100644 index 0000000..733f681 --- /dev/null +++ b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb @@ -0,0 +1,29 @@ +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 + invitation_request = create(:invitation_request, status: :pending) + + patch "/api/v1/invitation_requests/#{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 invitation_request.id + expect(json_response['profile_id']).to eq invitation_request.profile.id + expect(json_response['message']).to eq invitation_request.message + expect(json_response['project_id']).to eq invitation_request.project_id + 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 + + pending 'retorna erro 409 se a solicitação já estiver recusada' + pending 'retorna erro 404 se a solicitação não for encontrada' + pending 'retorna erro 400 se a requisição não contiver um id de solicitação de convite' + pending 'retorna erro 500, com mensagem identificando erro do servidor' + end +end From cc63258c6047c932ec194c9bf938d34ddaabe22d Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Tue, 13 Feb 2024 14:41:11 -0300 Subject: [PATCH 2/6] =?UTF-8?q?Adiciona=20regra=20para=20validar=20status?= =?UTF-8?q?=20da=20solicita=C3=A7=C3=A3o=20de=20convite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Retorna status 409 para requisições em solicitações de convite que não estejam pendentes. Co-authored-by: Rodrigo Gyodai --- .../api/v1/invitation_requests_controller.rb | 8 +++-- config/locales/invitation_request.pt-BR.yml | 3 +- .../leader_denies_invitation_request_spec.rb | 35 ++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/invitation_requests_controller.rb b/app/controllers/api/v1/invitation_requests_controller.rb index 0e49c26..5d3993e 100644 --- a/app/controllers/api/v1/invitation_requests_controller.rb +++ b/app/controllers/api/v1/invitation_requests_controller.rb @@ -3,8 +3,12 @@ module V1 class InvitationRequestsController < ApiController def update invitation_request = InvitationRequest.find(params[:id]) - invitation_request.refused! - render status: :ok, json: invitation_request.as_json(except: %i[created_at updated_at]) + 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 diff --git a/config/locales/invitation_request.pt-BR.yml b/config/locales/invitation_request.pt-BR.yml index 69cffe4..4aca6ed 100644 --- a/config/locales/invitation_request.pt-BR.yml +++ b/config/locales/invitation_request.pt-BR.yml @@ -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}' \ No newline at end of file + time_ago: 'Enviada há %{time}' + api_not_pending_error: 'Solicitação de convite não está pendente para ser recusada.' \ No newline at end of file diff --git a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb index 733f681..54ec2ba 100644 --- a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb +++ b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb @@ -21,7 +21,40 @@ expect(json_response['status']).to eq 'refused' end - pending 'retorna erro 409 se a solicitação já estiver recusada' + 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_requests/#{request1.id}" + response_status2 = patch "/api/v1/invitation_requests/#{request2.id}" + response_status3 = patch "/api/v1/invitation_requests/#{request3.id}" + response_status4 = patch "/api/v1/invitation_requests/#{request4.id}" + response_status5 = patch "/api/v1/invitation_requests/#{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 + pending 'retorna erro 404 se a solicitação não for encontrada' pending 'retorna erro 400 se a requisição não contiver um id de solicitação de convite' pending 'retorna erro 500, com mensagem identificando erro do servidor' From d93cbc165413e5bb3b2496f2a3081166f2e4c805 Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Tue, 13 Feb 2024 15:04:15 -0300 Subject: [PATCH 3/6] =?UTF-8?q?Adiciona=20testes=20para=20erro=20interno?= =?UTF-8?q?=20do=20servidor=20e=20recurso=20n=C3=A3o=20encontrado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rodrigo Gyodai --- .../api/v1/invitation_requests_controller.rb | 2 ++ .../leader_denies_invitation_request_spec.rb | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/invitation_requests_controller.rb b/app/controllers/api/v1/invitation_requests_controller.rb index 5d3993e..79d573c 100644 --- a/app/controllers/api/v1/invitation_requests_controller.rb +++ b/app/controllers/api/v1/invitation_requests_controller.rb @@ -2,6 +2,8 @@ module Api module V1 class InvitationRequestsController < ApiController def update + raise ActiveRecord::ParameterMissing if params[:id].blank? + invitation_request = InvitationRequest.find(params[:id]) if invitation_request.pending? invitation_request.refused! diff --git a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb index 54ec2ba..1ee031a 100644 --- a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb +++ b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb @@ -55,8 +55,24 @@ expect(json_last_response['errors']).to include 'Solicitação de convite não está pendente para ser recusada.' end - pending 'retorna erro 404 se a solicitação não for encontrada' - pending 'retorna erro 400 se a requisição não contiver um id de solicitação de convite' - pending 'retorna erro 500, com mensagem identificando erro do servidor' + it 'retorna erro 404 se a solicitação não for encontrada' do + patch '/api/v1/invitation_requests/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_requests/#{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 From 4638dc8f28805bf83c1be6871e307be5c54b30b7 Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Tue, 13 Feb 2024 15:15:14 -0300 Subject: [PATCH 4/6] =?UTF-8?q?Adiciona=20documenta=C3=A7=C3=A3o=20do=20en?= =?UTF-8?q?dpoint=20=C3=A0=20api=5Fdoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rodrigo Gyodai --- api_doc.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/api_doc.md b/api_doc.md index 3a17fc9..a0f0426 100644 --- a/api_doc.md +++ b/api_doc.md @@ -425,3 +425,66 @@ Retorno esperado: } ``` + +## 7. Alterar status de solicitação de convite + +
+PATCH /api/v1/invitation_requests/:id + +
+ +### 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." +} +``` +
\ No newline at end of file From c873b86753677753e042fdf97d7a00ba57e16a05 Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Wed, 14 Feb 2024 10:30:28 -0300 Subject: [PATCH 5/6] =?UTF-8?q?Altera=20teste=20para=20garantir=20que=20el?= =?UTF-8?q?e=20=C3=A9=20auto-contido?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: rodrigo.gyodai@gmail.com --- .../leader_denies_invitation_request_spec.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb index 1ee031a..7a9dab2 100644 --- a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb +++ b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb @@ -3,7 +3,8 @@ describe 'API solicitações' do context 'PATCH /api/v1/invitation_requests/:id' do it 'altera o status para recusado com sucesso' do - invitation_request = create(:invitation_request, status: :pending) + user = create(:user) + invitation_request = create(:invitation_request, profile: user.profile, status: :pending, message: 'oi projeto') patch "/api/v1/invitation_requests/#{invitation_request.id}" invitation_request.reload @@ -12,10 +13,10 @@ 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 invitation_request.id - expect(json_response['profile_id']).to eq invitation_request.profile.id - expect(json_response['message']).to eq invitation_request.message - expect(json_response['project_id']).to eq invitation_request.project_id + 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' From 505fb7b97b67473a48bb6ee333f7e15bffb0c5c8 Mon Sep 17 00:00:00 2001 From: Eliseu Ramos Date: Wed, 14 Feb 2024 10:42:10 -0300 Subject: [PATCH 6/6] =?UTF-8?q?Altera=20rota=20do=20endpoint=20para=20ter?= =?UTF-8?q?=20coes=C3=A3o=20com=20o=20Cola=3FBora!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A rota que o Cola?Bora! acessa por padrão está no singular, e a nossa rota estava com a nomenclatura no plural. Co-authored-by: rodrigo.gyodai@gmail.com --- ...oller.rb => invitation_request_controller.rb} | 2 +- config/routes.rb | 2 +- .../leader_denies_invitation_request_spec.rb | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) rename app/controllers/api/v1/{invitation_requests_controller.rb => invitation_request_controller.rb} (89%) diff --git a/app/controllers/api/v1/invitation_requests_controller.rb b/app/controllers/api/v1/invitation_request_controller.rb similarity index 89% rename from app/controllers/api/v1/invitation_requests_controller.rb rename to app/controllers/api/v1/invitation_request_controller.rb index 79d573c..1245e61 100644 --- a/app/controllers/api/v1/invitation_requests_controller.rb +++ b/app/controllers/api/v1/invitation_request_controller.rb @@ -1,6 +1,6 @@ module Api module V1 - class InvitationRequestsController < ApiController + class InvitationRequestController < ApiController def update raise ActiveRecord::ParameterMissing if params[:id].blank? diff --git a/config/routes.rb b/config/routes.rb index 8b96851..3315c1a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,7 +60,7 @@ resources :job_categories, only: %i[index show] resources :profiles, only: %i[show index] resources :invitations, only: %i[create update] - resources :invitation_requests, only: %i[update] + resources :invitation_request, only: %i[update] get 'projects/request_invitation', controller: :projects end diff --git a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb index 7a9dab2..1afa1e7 100644 --- a/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb +++ b/spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb @@ -6,7 +6,7 @@ user = create(:user) invitation_request = create(:invitation_request, profile: user.profile, status: :pending, message: 'oi projeto') - patch "/api/v1/invitation_requests/#{invitation_request.id}" + patch "/api/v1/invitation_request/#{invitation_request.id}" invitation_request.reload expect(response).to have_http_status(200) @@ -29,11 +29,11 @@ request4 = create(:invitation_request, status: :error) request5 = create(:invitation_request, status: :aborted) - response_status1 = patch "/api/v1/invitation_requests/#{request1.id}" - response_status2 = patch "/api/v1/invitation_requests/#{request2.id}" - response_status3 = patch "/api/v1/invitation_requests/#{request3.id}" - response_status4 = patch "/api/v1/invitation_requests/#{request4.id}" - response_status5 = patch "/api/v1/invitation_requests/#{request5.id}" + 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 @@ -57,7 +57,7 @@ end it 'retorna erro 404 se a solicitação não for encontrada' do - patch '/api/v1/invitation_requests/1939' + patch '/api/v1/invitation_request/1939' expect(response.status).to eq 404 expect(response.content_type).to include 'application/json' @@ -68,7 +68,7 @@ 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_requests/#{invitation_request.id}" + patch "/api/v1/invitation_request/#{invitation_request.id}" expect(response.status).to eq 500 expect(response.content_type).to include 'application/json'