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

Ticket Show API done #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions app/controllers/api/v1/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ def create
render json: { message: I18n.t('tickets.error.create') }, status: :unprocessable_entity
end
end

def show
result = Tickets::V1::Show.new(params).call
if result
render json: result
else
render json: { message: I18n.t('tickets.error.show') }, status: :unprocessable_entity
end
end
end
end
8 changes: 8 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ def send_notification
description = I18n.t("ticket.#{status}", ticket_type: ticket_type, resolver: resolver.name, requester: requester.name)
NotifyMailer.notify_status_change(resolver, requester, description, id).deliver_now
end

def department_name
self&.department&.name
end

def category_name
self&.category&.name
end
end
31 changes: 31 additions & 0 deletions app/services/tickets/v1/show.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Tickets::V1
class Show
def initialize(params)
@ticket_id = params[:id]
end

def call
find_ticket && response
end

def find_ticket
@ticket = Ticket.find_by(id: @ticket_id)
return true if @ticket

false
end

def response
@ticket.as_json(
only: %i[id status title description ticket_number ticket_type priority created_at resolved_at],
include: [{
activities: { only: %i[created_at asset_url description id] },
resolver: { only: %i[id name] }
}],
methods: %i[department_name category_name]
)
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ en:
create: "Ticket created successfully"
error:
create: "Unable to create ticket"
show: "Ticket not found"
ticket:
assigned: "%{ticket_type} has been assigned to: %{resolver}"
inprogress: "The work has been started on the %{ticket_type}"
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
api_version(:module => "Api::V1", :header => {
name: "Accept", :value => "application/vnd.providesk; version=1"}) do
resources :tickets, only: [:create]
resources :tickets, only: [:create, :show]
resources :sessions, only: :create
resources :categories, only: :create
resources :departments, only: :create
Expand Down
28 changes: 26 additions & 2 deletions spec/acceptance/v1/tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
let!(:category) { FactoryBot.create(:category, name: "Hardware", priority: 0, department_id: department.id)}
let!(:role) { FactoryBot.create(:role, name: 'super_admin')}
let!(:role1) { FactoryBot.create(:role, name: 'employee')}
let(:user) { FactoryBot.create(:user, role_id: role.id, department_id: department.id) }
let(:user1) { FactoryBot.create(:user, role_id: role.id) }
let(:user) { FactoryBot.create(:user, role_id: role.id, email: "[email protected]") }
let(:user1) { FactoryBot.create(:user, role_id: role.id, email: "[email protected]") }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any issue faced in the spec for for using Faker? - we have hard coded emails for creating user - we can use the factory right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but there is validation for using same email @varungadde99

let!(:ticket) { FactoryBot.create(:ticket, description: "For laptop with high graphics", ticket_number: "12", status: "assigned", priority: 0, department_id: department.id, category_id: category.id, ticket_type: "request", resolver_id: user.id, requester_id: user1.id) }

post '/tickets' do
before do
Expand Down Expand Up @@ -43,6 +44,29 @@
end
end

get '/tickets/:ticket_id'do
before do
header 'Accept', 'application/vnd.providesk; version=1'
header 'Authorization', JsonWebToken.encode({user_id: user.id, email: user.email, name: user.name})
end
context '200' do
example 'Ticket show success' do
do_request({ticket_id: ticket.id})
response_data = JSON.parse(response_body)
expect(response_status).to eq(200)
end
end

context '404' do
example 'Unable to show ticket' do
do_request({ticket_id: Faker::Number.numerify('#')})
response_data = JSON.parse(response_body)
expect(response_status).to eq(422)
expect(response_data["message"]).to eq(I18n.t('tickets.error.show'))
end
end
end

private

def create_params(title, description, category_id, department_id, ticket_type, resolver_id)
Expand Down