Skip to content

Commit

Permalink
ip
Browse files Browse the repository at this point in the history
  • Loading branch information
tvcam committed Jun 14, 2023
1 parent c1abe89 commit 1689c58
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 90 deletions.
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ GEM
actionmailer (>= 3.2)
letter_opener (~> 1.0)
railties (>= 3.2)
libv8 (3.16.14.19)
little-plugger (1.1.4)
logging (2.2.2)
little-plugger (~> 1.1)
Expand Down Expand Up @@ -693,6 +694,7 @@ GEM
redis (>= 3, < 5)
redis-store (1.6.0)
redis (>= 2.2, < 5)
ref (2.0.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
Expand Down Expand Up @@ -812,6 +814,9 @@ GEM
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
text (1.3.1)
therubyracer (0.12.3)
libv8 (~> 3.16.14.15)
ref
thin (1.7.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
Expand Down Expand Up @@ -931,6 +936,7 @@ DEPENDENCIES
draper (~> 2.1)
dropzonejs-rails (~> 0.7.3)
enumerize (~> 2.3.1)
execjs (~> 2.6.0)
factory_girl_rails (~> 4.5)
ffaker (~> 2.1.0)
flay (~> 2.12, >= 2.12.1)
Expand Down Expand Up @@ -996,6 +1002,7 @@ DEPENDENCIES
spreadsheet (~> 1.1.3)
sysrandom (~> 1.0, >= 1.0.5)
text (~> 1.3, >= 1.3.1)
therubyracer (~> 0.12.3)
thin (~> 1.7)
thredded (~> 0.6.1)
timecop (~> 0.8.1)
Expand Down
20 changes: 20 additions & 0 deletions app/assets/javascripts/case_notes/form.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ CIF.Case_notesNew = CIF.Case_notesCreate = CIF.Case_notesEdit = CIF.Case_notesUp
_taskProgressNoteToggle()
_initTaskProgressNoteTooltip()

$("#case_note_meeting_date").on "change", _submitFormViaAjax
$("#case_note_interaction_type").on "change", _submitFormViaAjax
$("#case_note_attendee").on "keyup", _submitFormViaAjax
$("#case_note_note").on "keyup", _submitFormViaAjax
$("#case_note_domain_group_ids").on "change", ->
_submitFormViaAjax()

_submitFormViaAjax = ->
if $("#case-note-form").data("autosave")
$.ajax
url: $("#case-note-form").attr("action") + "&draft=true"
type: "PUT"
data: $("#case-note-form").serialize()
dataType: "json"
success: (response) ->
history.replaceState(null, "", response.edit_url) if response.edit_url
console.log(response)

_initICheckBox = ->
$('.i-checks').iCheck(
checkboxClass: 'icheckbox_square-green'
Expand Down Expand Up @@ -69,6 +87,8 @@ CIF.Case_notesNew = CIF.Case_notesCreate = CIF.Case_notesEdit = CIF.Case_notesUp


_checkCasenoteSelectedValue = (selectedObject)->
console.log(selectedObject)

# $("#domain-#{event.val}").show('slow')
if $(selectedObject).children(":selected").length > 0
$(".ibox.case-note-domain-group.without-assessments#domain-#{$(selectedObject).children(":selected").val()}").show()
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ def set_raven_context
Raven.user_context(ip: request.ip)
end
end

def save_draft?
request.format.json? && params[:draft].present?
end
end
59 changes: 50 additions & 9 deletions app/controllers/case_notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CaseNotesController < AdminController
load_and_authorize_resource
load_and_authorize_resource only: :destroy

include CreateBulkTask
include CaseNoteConcern
include GoogleCalendarServiceConcern
Expand Down Expand Up @@ -66,25 +67,55 @@ def show
end

def edit
authorize @case_note, :edit? if Organization.ratanak?
unless current_user.admin? || current_user.strategic_overviewer?
redirect_to root_path, alert: t('unauthorized.default') unless current_user.permission.case_notes_editable
if params[:id] == "draft"
if params[:custom] == 'true'
@case_note.custom = true
@case_note.custom_assessment = @custom_assessment_setting

@case_note.assessment = @client.assessments.custom_latest_record if @current_setting.enable_default_assessment
@case_note.populate_notes(@case_note.custom_assessment_setting_id, params[:custom])
else
@case_note.assessment = @client.assessments.default_latest_record
@case_note.populate_notes(nil, 'false')
end
else
authorize @case_note, :edit? if Organization.ratanak?

unless current_user.admin? || current_user.strategic_overviewer?
redirect_to root_path, alert: t('unauthorized.default') unless current_user.permission.case_notes_editable
end
end
end

def update
if @case_note.update_attributes(case_note_params)
saved = if save_draft?
@case_note.assign_attributes(case_note_params.merge(last_auto_save_at: Time.current))
@case_note.save(validate: false)
else
@case_note.draft = false
@case_note.update_attributes(case_note_params)
end

if saved
if params.dig(:case_note, :case_note_domain_groups_attributes)
add_more_attachments(params[:case_note][:attachments]) if params.dig(:case_note, :attachments)
@case_note.complete_tasks(params[:case_note][:case_note_domain_groups_attributes], current_user.id)
end

create_bulk_task(params[:task], @case_note) if params.has_key?(:task)
@case_note.complete_screening_tasks(params) if params[:case_note].has_key?(:tasks_attributes)
create_task_task_progress_notes
delete_events if session[:authorization]
redirect_to client_case_notes_path(@client), notice: t('.successfully_updated')

respond_to do |format|
format.html { redirect_to(client_case_notes_path(@client), notice: t('.successfully_updated')) }
format.json { render json: { resource: @case_note, edit_url: edit_client_case_note_url(@client, @case_note) }, status: 200 }
end
else
render :edit
respond_to do |format|
format.html { render :edit }
format.json { render json: @case_note.errors, status: 422 }
end
end
end

Expand Down Expand Up @@ -138,15 +169,25 @@ def set_client
end

def set_case_note
@case_note = @client.case_notes.find(params[:id])
if params[:id] == "draft"
@case_note = @client.find_or_create_draft_case_note
else
@case_note = @client.case_notes.find(params[:id])
end
end

def set_custom_assessment_setting
@custom_assessment_setting = CustomAssessmentSetting.find_by(custom_assessment_name: params[:custom_name])
end

def authorize_case_note
authorize @case_note

if params[:id] == "draft"
return true if current_user.admin?
authorize @client, :create?
else
authorize @case_note
end
end

def authorize_client
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/concerns/case_note_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def assign_params_to_case_note_domain_groups_params(default_params)

def fetch_domain_group
@domain_groups = []
if params[:action].in? ['edit', 'update']

if params[:action].in?(['edit', 'update']) && !@case_note.draft?
if @case_note.domain_groups.present?
@domain_groups = @case_note.domain_groups
else
Expand Down
18 changes: 18 additions & 0 deletions app/helpers/case_note_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ def new_link(obj=@client)
end
end

def new_draft_case_note_link(custom_assessment_name = nil)
if custom_assessment_name && case_notes_editable? && policy(@client).create?
link_to edit_client_case_note_path(@client, id: :draft, custom: true, custom_name: custom_assessment_name) do
custom_assessment_name
end
elsif custom_assessment_name.blank? && case_notes_editable? && (policy(@client).create? || policy(CaseNote).create?)
link_to edit_client_case_note_path(@client, id: :draft) do
@current_setting.default_assessment
end
else
link_to_if false, '' do
content_tag :a, class: 'disabled' do
custom_assessment_name
end
end
end
end

def new_custom_link(custom_assessment_name, obj=@client)
if case_notes_editable? && policy(obj).create?
link_to new_polymorphic_path([obj, 'case_note'], custom: true, custom_name: custom_assessment_name) do
Expand Down
2 changes: 2 additions & 0 deletions app/models/case_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class CaseNote < ActiveRecord::Base

scope :most_recents, -> { order(created_at: :desc) }
scope :recent_meeting_dates, -> { order(meeting_date: :desc) }
scope :draft, -> { where(draft: true) }
scope :draft_untouch, -> { draft.where(last_auto_save_at: nil) }

scope :no_case_note_in, ->(value) { where('meeting_date <= ? AND id = (SELECT MAX(cn.id) FROM CASE_NOTES cn where CASE_NOTES.client_id = cn.client_id)', value) }

Expand Down
7 changes: 7 additions & 0 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ def self.date_of_birth_matching(dob1, dob2)
percentage < 0 ? nil : percentage
end

def find_or_create_draft_case_note
case_note = case_notes.draft_untouch.last
case_note ||= case_notes.new(draft: true)
case_note.save(validate: false)
case_note
end

def exit_ngo?
status == 'Exited'
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/case_notes/_form.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.row
.col-xs-12
= simple_form_for([@case_note.client, @case_note], html: { id: 'case-note-form', novalidate: false, data: { translate: t('cannot_be_blank') } }) do |f|
= simple_form_for([@case_note.client, @case_note], html: { id: 'case-note-form', novalidate: false, data: { translate: t('cannot_be_blank'), autosave: @case_note.persisted?, draft: @case_note.draft? } }) do |f|
= hidden_field_tag :current_user, current_user.roles
= hidden_field_tag :from_controller, @from_controller
= f.input :custom_assessment_setting_id, as: :hidden, input_html: { value: @custom_assessment_setting&.id || @case_note.custom_assessment_setting_id }
Expand Down
9 changes: 3 additions & 6 deletions app/views/case_notes/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
%span.caret
%ul.dropdown-menu#case-note-dropdown
- if @current_setting.enable_default_assessment
%li
= new_link
%li= new_draft_case_note_link
- if @custom_assessment_settings.present?
- @custom_assessment_settings.each do |cs|
- if cs.domains.any? && Domain.custom_csi_domain_setting(cs.id).present?
%li
= link_to new_client_case_note_path(@client, default: false, custom_name: cs.custom_assessment_name), class: 'btn btn-default custom-assessment-link' do
= new_custom_link(cs.custom_assessment_name)
%li= new_draft_case_note_link(cs.custom_assessment_name)
- else
%li.disabled
= link_to '#', data: { toggle: 'modal', target: '#customDomainsModal' } do
Expand All @@ -44,7 +41,7 @@
= t('case_notes.index.please_add_custom_domains')
- elsif case_notes_editable? && policy(@client).create?
= link_to t('case_notes.index.new_case_note'), new_client_case_note_path(@client, custom: @current_setting.enable_default_assessment), class: 'btn btn-primary'
= link_to t('case_notes.index.new_case_note'), edit_client_case_note_path(@client, id: :draft, custom: @current_setting.enable_default_assessment), class: 'btn btn-primary'
.ibox-content
- is_ratanak = Organization.ratanak?
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230614000546_add_draft_tocase_notes_.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDraftTocaseNotes < ActiveRecord::Migration
def change
add_column :case_notes, :draft, :boolean, default: false, null: false unless column_exists?(:case_notes, :draft)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLastAutoSaveAtToCaseNotes < ActiveRecord::Migration
def change
add_column :case_notes, :last_auto_save_at, :datetime
end
end
Loading

0 comments on commit 1689c58

Please sign in to comment.