|
| 1 | +require 'grape' |
| 2 | + |
| 3 | +class StaffNotesApi < Grape::API |
| 4 | + helpers AuthenticationHelpers |
| 5 | + helpers AuthorisationHelpers |
| 6 | + |
| 7 | + before do |
| 8 | + authenticated? |
| 9 | + end |
| 10 | + |
| 11 | + desc "Get all the staff notes for a project" |
| 12 | + params do |
| 13 | + requires :project_id, type: Integer, desc: 'Project to fetch staff notes for' |
| 14 | + end |
| 15 | + get '/projects/:project_id/staff_notes' do |
| 16 | + project = Project.find(params[:project_id]) |
| 17 | + |
| 18 | + unless authorise? current_user, project, :get_staff_note |
| 19 | + error!({ error: 'You do not have permission to access this project' }, 403) |
| 20 | + end |
| 21 | + |
| 22 | + result = project.staff_notes |
| 23 | + |
| 24 | + present result, with: Entities::StaffNoteEntity, user: current_user |
| 25 | + end |
| 26 | + |
| 27 | + desc "Create a new staff note for a project" |
| 28 | + params do |
| 29 | + requires :project_id, type: Integer, desc: 'Project to add the staff note for' |
| 30 | + requires :note, type: String, desc: 'The text to add to the staff note' |
| 31 | + optional :reply_to_id, type: Integer, desc: 'ID of the staff note this is being replied to' |
| 32 | + end |
| 33 | + post '/projects/:project_id/staff_notes' do |
| 34 | + project = Project.find(params[:project_id]) |
| 35 | + |
| 36 | + unless authorise? current_user, project, :create_staff_note |
| 37 | + error!({ error: 'You do not have permission to access this project' }, 403) |
| 38 | + end |
| 39 | + |
| 40 | + text_note = params[:note] |
| 41 | + |
| 42 | + reply_to_id = params[:reply_to_id] |
| 43 | + if reply_to_id.present? |
| 44 | + original_staff_note = StaffNote.find(reply_to_id) |
| 45 | + error!(error: 'You do not have permission to read the replied staff note') unless authorise?(current_user, original_staff_note.project, :get) |
| 46 | + error!(error: 'Original staff note is not in this project.') if project.staff_notes.find(reply_to_id).blank? |
| 47 | + end |
| 48 | + |
| 49 | + result = project.add_staff_note(current_user, text_note, reply_to_id) |
| 50 | + |
| 51 | + if result.nil? |
| 52 | + error!({ error: 'Duplicate note.' }, 403) |
| 53 | + else |
| 54 | + present result, with: Entities::StaffNoteEntity, user: current_user |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + desc "Delete a staff note for a project" |
| 59 | + delete '/projects/:project_id/staff_notes/:id' do |
| 60 | + project = Project.find(params[:project_id]) |
| 61 | + staff_note = StaffNote.find(params[:id]) |
| 62 | + |
| 63 | + unless authorise?(current_user, project, :delete_staff_note) || staff_note.user.id == current_user.id |
| 64 | + error!({ error: 'You do not have permission to delete this note.' }, 403) |
| 65 | + end |
| 66 | + |
| 67 | + error!({ error: 'Note does not belong to this project' }, 404) if staff_note.project_id != project.id |
| 68 | + |
| 69 | + staff_note.destroy |
| 70 | + error!({ error: staff_note.errors.full_messages.last }, 403) unless staff_note.destroyed? |
| 71 | + |
| 72 | + present staff_note.destroyed?, with: Grape::Presenters::Presenter |
| 73 | + end |
| 74 | + |
| 75 | + desc "Update a staff note for a project" |
| 76 | + params do |
| 77 | + requires :id, type: Integer, desc: 'The staff note id to update' |
| 78 | + requires :note, type: String, desc: 'The text to update the staff note with' |
| 79 | + end |
| 80 | + put '/projects/:project_id/staff_notes/:id' do |
| 81 | + project = Project.find(params[:project_id]) |
| 82 | + staff_note = StaffNote.find(params[:id]) |
| 83 | + |
| 84 | + unless authorise?(current_user, project, :create_staff_note) && staff_note.user.id == current_user.id |
| 85 | + error!({ error: 'You do not have permission to edit this note.' }, 403) |
| 86 | + end |
| 87 | + |
| 88 | + error!({ error: 'Note does not belong to this project' }, 404) if staff_note.project_id != project.id |
| 89 | + |
| 90 | + staff_note.update!(note: params[:note]) |
| 91 | + present staff_note, with: Entities::StaffNoteEntity, user: current_user |
| 92 | + end |
| 93 | + |
| 94 | +end |
0 commit comments