From df81b903f3e8766f5b80867baf3b664b309c1266 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Wed, 18 Oct 2023 10:15:51 -0300 Subject: [PATCH] create issues creator --- app/services/application_service.rb | 8 ++++++ app/services/issues_creator.rb | 22 +++++++++++++++ app/views/issues/_issue.json.jbuilder | 2 +- spec/controllers/issues_controller_spec.rb | 12 ++++---- spec/services/issues_creator_spec.rb | 32 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 app/services/application_service.rb create mode 100644 app/services/issues_creator.rb create mode 100644 spec/services/issues_creator_spec.rb diff --git a/app/services/application_service.rb b/app/services/application_service.rb new file mode 100644 index 0000000..e341242 --- /dev/null +++ b/app/services/application_service.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# app/services/application_service.rb +class ApplicationService + def self.call(*, &) + new(*, &).call + end +end diff --git a/app/services/issues_creator.rb b/app/services/issues_creator.rb new file mode 100644 index 0000000..9e65319 --- /dev/null +++ b/app/services/issues_creator.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class IssuesCreator < ApplicationService + def initialize(project) + super() + @project = project + end + + def call + issues = Clients::Tts::Azure::Issue.new(@project).list + issues.map do |issue| + next unless issue.user + + Issue.create!( + effort: issue.effort, + user: issue.user, + state: issue.state, + closed_date: issue.closed_date + ) + end + end +end diff --git a/app/views/issues/_issue.json.jbuilder b/app/views/issues/_issue.json.jbuilder index 77d075e..d499ddb 100644 --- a/app/views/issues/_issue.json.jbuilder +++ b/app/views/issues/_issue.json.jbuilder @@ -3,5 +3,5 @@ json.extract! issue, :id, :effort, :user_id, :state, :closed_date json.user do |json| - json.partial! "users/user", user: issue.user + json.partial! 'users/user', user: issue.user end diff --git a/spec/controllers/issues_controller_spec.rb b/spec/controllers/issues_controller_spec.rb index f83cbd4..0aefa0b 100644 --- a/spec/controllers/issues_controller_spec.rb +++ b/spec/controllers/issues_controller_spec.rb @@ -1,12 +1,14 @@ -require "rails_helper" +# frozen_string_literal: true + +require 'rails_helper' RSpec.describe IssuesController, type: :controller do - include_context "authentication" + include_context 'authentication' render_views - describe "GET #index" do - it "returns a success response" do - issue = FactoryBot.create(:issue) + describe 'GET #index' do + it 'returns a success response' do + FactoryBot.create(:issue) get :index expect(response).to be_successful end diff --git a/spec/services/issues_creator_spec.rb b/spec/services/issues_creator_spec.rb new file mode 100644 index 0000000..d426aeb --- /dev/null +++ b/spec/services/issues_creator_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe IssuesCreator, type: :service do + let(:customer) do + FactoryBot.create(:customer, name: 'Ministry Brands', ticket_tracking_system_token: 'place-real-token-here') + end + + let(:user) do + user = FactoryBot.create(:user) + FactoryBot.create(:user_service_identifier, customer:, user:, + identifier: 'victor.carvalho@ministrybrands.com') + user + end + + let(:project) do + FactoryBot.create(:project, customer:, + metadata: { azure_project_name: '1ES', area_path: '1ES\\DDC UI Refresh\\2023' }) + end + + describe '#call' do + it 'creates a list of issues' do + VCR.use_cassette('clients#tts#azure#issue#list') do + user + expect do + IssuesCreator.call(project) + end.to change(Issue, :count).by(4) + end + end + end +end