Skip to content

Commit a0532e5

Browse files
authored
feat(upload): Handle archived users in upload (#2754)
* feat(upload): Handle archived users and closed follow ups * disable first and last override when arguments are passed * feat(app): Save user rows selection * fix bug when retrieving matching user * spell user_row method correctly * Update specs * linter fix * remove useless comments * Update wording on user_list_uploads#show page * Clarify cookie mechanism * fix linter warnings * fix grammar
1 parent 78484f2 commit a0532e5

37 files changed

+1024
-248
lines changed

app/controllers/user_list_uploads/invitation_attempts_controller.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def select_rows
77
@user_collection.sort_by!(**sort_params) if sort_params_valid?
88
@user_collection.search!(params[:search_query]) if params[:search_query].present?
99
@user_rows = @user_collection.user_rows_with_user_save_success
10+
@number_of_user_rows_selected = @user_list_upload.user_rows_selected_for_invitation.length
11+
@total_number_of_user_rows = @user_list_upload.user_rows.length
1012
end
1113

1214
def create_many
13-
@user_collection = @user_list_upload.user_collection
14-
@user_collection.mark_selected_rows_for_invitation!(selected_ids)
1515
UserListUpload::InviteUsersJob.perform_later(@user_list_upload.id, invitation_formats)
1616
redirect_to user_list_upload_invitation_attempts_path(user_list_upload_id: @user_list_upload.id)
1717
end
@@ -20,7 +20,7 @@ def index
2020
@user_collection = @user_list_upload.user_collection
2121
@user_collection.sort_by!(**sort_params) if sort_params_valid?
2222
@user_collection.search!(params[:search_query]) if params[:search_query].present?
23-
@user_rows = @user_collection.user_rows_marked_for_invitation
23+
@user_rows = @user_collection.user_rows_selected_for_invitation
2424
@user_rows_with_invitation_errors = @user_collection.user_rows_with_invitation_errors
2525
@user_rows_with_invitation_attempted = @user_collection.user_rows_with_invitation_attempted
2626
@all_invitations_attempted = @user_rows_with_invitation_attempted.count == @user_rows.count
@@ -33,10 +33,6 @@ def set_user_list_upload
3333
authorize @user_list_upload, :edit?
3434
end
3535

36-
def selected_ids
37-
params.permit(selected_ids: []).fetch(:selected_ids, [])
38-
end
39-
4036
def invitation_formats
4137
[params[:email], params[:sms]].compact.map(&:downcase)
4238
end

app/controllers/user_list_uploads/user_list_uploads_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def show
1010
@user_rows = @user_collection.user_rows
1111
@user_rows_with_errors = @user_collection.user_rows_with_errors
1212
@category_configuration = @user_list_upload.category_configuration
13+
@number_of_user_rows_selected = @user_list_upload.user_rows_selected_for_user_save.length
1314
end
1415

1516
def new; end

app/controllers/user_list_uploads/user_rows_controller.rb

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module UserListUploads
22
class UserRowsController < BaseController
3-
before_action :set_user_list_upload, :set_user_row
3+
before_action :set_user_list_upload
4+
before_action :set_user_row, only: [:show, :update, :show_details, :hide_details]
45
before_action :set_user_row_partial, only: [:show, :update]
56

67
def show
@@ -17,6 +18,14 @@ def update
1718
end
1819
end
1920

21+
def batch_update
22+
if @user_list_upload.update_rows(batch_update_params)
23+
redirect_to request.referer
24+
else
25+
turbo_stream_display_error_modal(@user_list_upload.errors.full_messages)
26+
end
27+
end
28+
2029
def show_details
2130
respond_to :turbo_stream
2231
end
@@ -37,7 +46,7 @@ def set_user_row
3746
end
3847

3948
def set_user_row_partial
40-
@user_row_partial = if @user_row.marked_for_user_save?
49+
@user_row_partial = if @user_row.selected_for_user_save?
4150
"user_list_uploads/user_save_attempts/user_row"
4251
else
4352
"user_list_uploads/user_list_uploads/user_row"
@@ -47,7 +56,13 @@ def set_user_row_partial
4756
def row_params
4857
params.expect(
4958
user_row: [:title, :first_name, :last_name, :affiliation_number, :phone_number, :email,
50-
:assigned_organisation_id]
59+
:assigned_organisation_id, :selected_for_user_save, :selected_for_invitation]
60+
)
61+
end
62+
63+
def batch_update_params
64+
params.expect(
65+
user_rows: [[:id, :selected_for_user_save, :selected_for_invitation]]
5166
)
5267
end
5368
end

app/controllers/user_list_uploads/user_save_attempts_controller.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ class UserSaveAttemptsController < BaseController
33
before_action :set_user_list_upload, only: [:create_many, :index]
44

55
def create_many
6-
@user_list_upload.user_collection.mark_selected_rows_for_user_save!(selected_ids)
76
UserListUpload::SaveUsersJob.perform_later(@user_list_upload.id)
87
redirect_to user_list_upload_user_save_attempts_path(user_list_upload_id: @user_list_upload)
98
end
@@ -12,7 +11,7 @@ def index
1211
@user_collection = @user_list_upload.user_collection
1312
@user_collection.sort_by!(**sort_params) if sort_params_valid?
1413
@user_collection.search!(params[:search_query]) if params[:search_query].present?
15-
@user_rows = @user_collection.user_rows_marked_for_user_save
14+
@user_rows = @user_collection.user_rows_selected_for_user_save
1615
@user_rows_with_user_save_errors = @user_collection.user_rows_with_user_save_errors
1716
@user_rows_with_user_save_attempted = @user_collection.user_rows_with_user_save_attempted
1817
@all_saves_attempted = @user_rows_with_user_save_attempted.count == @user_rows.count
@@ -24,9 +23,5 @@ def set_user_list_upload
2423
@user_list_upload = UserListUpload.find(params[:user_list_upload_id])
2524
authorize @user_list_upload, :edit?
2625
end
27-
28-
def selected_ids
29-
params.permit(selected_ids: []).fetch(:selected_ids, [])
30-
end
3126
end
3227
end

app/helpers/user_list_upload/invitation_attempts_helper.rb

+17
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@ def text_for_time_remaining_for_invitations(current_count, total_count)
5050
"environ #{time_remaining_in_minutes} min restante#{'s' if time_remaining_in_minutes > 1}"
5151
end
5252
end
53+
54+
def disable_invitation_for_user_row?(user_row)
55+
selected_invitation_formats(user_row.user_list_upload_id).none? { |format| user_row.invitable_by?(format) }
56+
end
57+
58+
def invitation_format_checked?(format, user_list_upload_id)
59+
selected_invitation_formats(user_list_upload_id).include?(format)
60+
end
61+
62+
def selected_invitation_formats(user_list_upload_id)
63+
cookie_data = JSON.parse(cookies["user_list_uploads"] || "{}")
64+
formats = cookie_data.dig(user_list_upload_id.to_s, "selected_invitation_formats")
65+
formats.is_a?(Array) ? formats : %w[sms email]
66+
rescue JSON::ParserError
67+
Sentry.capture_exception(JSON::ParserError, extra: { cookies: cookies["user_list_uploads"] })
68+
%w[sms email]
69+
end
5370
end

app/helpers/user_list_upload/user_list_upload_helper.rb

+96-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rubocop:disable Metrics/ModuleLength
12
module UserListUpload::UserListUploadHelper
23
def rows_with_errors?
34
params[:rows_with_errors].present?
@@ -13,6 +14,14 @@ def user_row_status_badge_class(user_row)
1314
}[user_row.before_user_save_status]
1415
end
1516

17+
def user_row_background_color(user_row)
18+
if user_row.archived?
19+
"background-brown-light"
20+
else
21+
"background-light"
22+
end
23+
end
24+
1625
def user_row_icon_for_status(errors)
1726
return if errors.empty?
1827

@@ -56,16 +65,97 @@ def attribute_to_highlight?(user_row, attribute)
5665
user_row.matching_user_attribute_changed?(attribute) || user_row.attribute_changed_by_cnaf_data?(attribute)
5766
end
5867

59-
def tooltip_for_user_row_errors(errors)
60-
return if errors.empty?
68+
def badge_class_for_user_row_organisation(user_row, organisation)
69+
if user_row.association_already_persisted?(organisation, :organisations)
70+
if user_row.archives.map(&:organisation_id).include?(organisation.id)
71+
"background-brown-light text-brown"
72+
else
73+
"background-blue-light text-dark-blue"
74+
end
75+
else
76+
"background-green-light text-dark-green"
77+
end
78+
end
6179

62-
tooltip_errors(
63-
title: "Erreurs des données du dossier",
64-
errors: errors.full_messages
65-
)
80+
def badge_class_for_user_row_motif_category(user_row, motif_category)
81+
if user_row.association_already_persisted?(motif_category, :motif_categories)
82+
if user_row.user.follow_up_for(motif_category)&.closed?
83+
"background-dark-green text-white"
84+
else
85+
"background-blue-light text-dark-blue"
86+
end
87+
else
88+
"background-green-light text-dark-green"
89+
end
90+
end
91+
92+
def tooltip_for_user_row(user_row)
93+
if user_row.user_errors.any?
94+
tooltip_errors(
95+
title: "Erreurs des données du dossier",
96+
errors: user_row.user_errors.full_messages
97+
)
98+
else
99+
tooltip(content: tooltip_content_for_user_row(user_row))
100+
end
101+
end
102+
103+
def tooltip_content_for_user_row(user_row)
104+
tooltip_content_array = []
105+
tooltip_content_array << tooltip_content_for_user_row_archived(user_row) if user_row.archived?
106+
tooltip_content_array << tooltip_content_for_user_row_follow_up_closed if user_row.matching_follow_up_closed?
107+
if tooltip_content_array.empty?
108+
tooltip_content_array << if user_row.matching_user_id
109+
"Ce dossier existe déjà. Si coché, les données seront mises à jour"
110+
else
111+
"Ce dossier n'existe pas encore. Si coché, celui-ci sera créé"
112+
end
113+
end
114+
safe_join(tooltip_content_array, safe_join([tag.br, tag.br]))
115+
end
116+
117+
# rubocop:disable Metrics/AbcSize
118+
def tooltip_content_for_user_row_archived(user_row)
119+
if user_row.department_level?
120+
safe_join(
121+
[
122+
tag.b("Dossier archivé sur #{strip_tags(user_row.archives.map(&:organisation).map(&:name).join(', '))}."),
123+
tag.br,
124+
"Motifs d'archivage : #{strip_tags(user_row.archiving_reasons.join(', '))}",
125+
tag.br,
126+
"Si mis à jour dans l'organisation d'archivage, le dossier sera désarchivé dans cette organisation."
127+
]
128+
)
129+
else
130+
safe_join([
131+
tag.b("Dossier archivé sur cette organisation."),
132+
tag.br,
133+
"Motif d'archivage : \"#{strip_tags(user_row.archiving_reasons.first)}\"",
134+
tag.br,
135+
"Si coché, le dossier sera désarchivé lors de la mise à jour."
136+
])
137+
end
138+
end
139+
# rubocop:enable Metrics/AbcSize
140+
141+
def tooltip_content_for_user_row_follow_up_closed
142+
safe_join([
143+
tag.b("Dossier traité sur cette catégorie"),
144+
tag.br,
145+
"Si coché, le dossier sera rouvert sur cette catégorie de suivi."
146+
])
66147
end
67148

68149
def show_row_attribute?(attribute_name, user_list_upload)
69150
user_list_upload.restricted_user_attributes.exclude?(attribute_name.to_sym)
70151
end
152+
153+
def checkbox_to_select_all_checked?(attribute_name, user_list_upload_id)
154+
cookie_data = JSON.parse(cookies["user_list_uploads"] || "{}")
155+
cookie_data.dig(user_list_upload_id.to_s, "checkbox_all", attribute_name.to_s) != false
156+
rescue JSON::ParserError
157+
Sentry.capture_exception(JSON::ParserError, extra: { cookies: cookies["user_list_uploads"] })
158+
false
159+
end
71160
end
161+
# rubocop:enable Metrics/ModuleLength

0 commit comments

Comments
 (0)