Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aminedhobb committed Jan 19, 2025
1 parent a3cb00b commit 03cbe91
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def edit

def set_user_list_upload
@user_list_upload = UserListUpload.find(params[:user_list_upload_id])
authorize @user_list_upload, :edit?
end

def set_user_row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def set_user_list_upload
end

def set_user_row
@user_row = @user_list_upload.user_rows.find_by!(id: params[:user_row_id])
@user_row = @user_list_upload.user_rows.find(params[:user_row_id])
end

def selected_ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class extends Controller {
icon: "success",
title: "Organisation assignée avec succès",
showConfirmButton: false,
timer: 3000
timer: 5000
});
event.stopPropagation();
window.Turbo.visit(window.location.href, { action: "replace" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
initialize() {
// Bind the `refresh` function to ensure it's the same reference
this.handleTurboRender = this.refresh.bind(this);
document.addEventListener("turbo:render", this.handleTurboRender);
document.addEventListener("turbo:render", this.handleTurboRender);
}

connect() {
this.refresh();
}

disconnect() {
// Remove the `turbo:render` listener and restore the progress bar
document.removeEventListener("turbo:render", this.handleTurboRender);
}

refresh() {
setTimeout(() => {
// Trigger Turbo visit
window.Turbo.visit(window.location.href, { action: "replace" });
}, 1000);
}
Expand Down
46 changes: 34 additions & 12 deletions app/javascript/controllers/user_list_upload_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class extends Controller {
}

handleFileRemove() {
this.inputTarget.value = ""
this.#removeFileInput()
this.#updateFileName("")
this.#updateUserCount("")
this.fileInputInstructionTarget.classList.remove("d-none")
Expand All @@ -53,7 +53,7 @@ export default class extends Controller {
const button = this.submitButtonTarget
const { form } = event.target

this.#setLoading(button)
await this.#setLoadingButton(button)

this.userList = this.#transformRowsInUserList(this.rows)

Expand All @@ -66,8 +66,8 @@ export default class extends Controller {
this.#addInputsToForm(form)


form.addEventListener("turbo:submit-end", () => {
this.#resetLoading(button)
form.addEventListener("turbo:submit-end", (e) => {
this.#onSubmitEnd(e)
})

form.requestSubmit()
Expand Down Expand Up @@ -97,15 +97,33 @@ export default class extends Controller {
})
}

#setLoading(button) {
button.value = "Chargement des données usagers..."
button.classList.add("disabled")
async #setLoadingButton(button) {
this.originalButton = button

// Create a new button element to replace the input
const newButton = document.createElement("button")
newButton.type = "submit"
newButton.className = `${button.className} disabled`
newButton.innerHTML = `
<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>
Chargement des données usagers...
`
this.loadingButton = newButton

button.insertAdjacentElement("afterend", newButton)
button.remove()
}

#resetLoading(button) {
button.value = "Charger les données usagers"
button.classList.remove("disabled")
button.removeAttribute("disabled")
#onSubmitEnd(event) {
const { success } = event.detail
if (!success) {
this.handleFileRemove()
if (this.loadingButton) {
this.loadingButton.parentElement.appendChild(this.originalButton)
this.originalButton.classList.add("disabled")
this.loadingButton.remove()
}
}
}

#transformRowsInUserList(rows) {
Expand Down Expand Up @@ -145,11 +163,15 @@ export default class extends Controller {
if (await this.#readFile(file)) {
this.#setFileSelected(file)
} else {
this.inputTarget.value = ""
this.#removeFileInput()
}
}
}

#removeFileInput() {
this.inputTarget.value = ""
}

async #readFile(file) {
return new Promise((resolve) => {
const reader = new FileReader();
Expand Down
6 changes: 6 additions & 0 deletions app/jobs/user_list_upload/invite_user_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UserListUpload::InviteUserJob < ApplicationJob
def perform(user_row_id, format)
user_row = UserListUpload::UserRow.find(user_row_id)
user_row.invite_user(format)
end
end
4 changes: 4 additions & 0 deletions app/jobs/user_list_upload/invite_users_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class UserListUpload::InviteUsersJob < ApplicationJob
sidekiq_options retry: 0

def perform(user_list_upload_id, invitation_formats)
user_list_upload = UserListUpload.find(user_list_upload_id)
user_collection = user_list_upload.user_collection
return if user_collection.all_invitations_attempted?

user_collection.user_rows_marked_for_invitation.each do |user_row|
invitation_formats.each do |format|
user_row.invite_user(format) if user_row.invitable_by?(format)
Expand Down
8 changes: 7 additions & 1 deletion app/jobs/user_list_upload/save_users_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ class UserListUpload::SaveUsersJob < ApplicationJob

def perform(user_list_upload_id)
user_list_upload = UserListUpload.find(user_list_upload_id)
user_list_upload.user_rows.each(&:save_user)
return if user_list_upload.user_collection.all_saves_attempted?

user_list_upload.user_rows.each do |user_row|
next if user_row.user_save_succeded?

user_row.save_user
end
end
end
27 changes: 12 additions & 15 deletions app/models/user_list_upload/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ class UserListUpload::Collection
attr_reader :user_rows, :user_list_upload

delegate :each, to: :user_rows
delegate :motif_category, :matching_users, :referents_from_rows, :tags_from_rows, :organisations,
to: :user_list_upload

SEARCHABLE_ATTRIBUTES = %i[
first_name last_name email phone_number affiliation_number
].freeze

def initialize(user_list_upload:)
@user_list_upload = user_list_upload
@user_rows = build_user_rows
@user_rows = load_user_rows
end

def update_rows(rows_data)
Expand Down Expand Up @@ -66,10 +64,18 @@ def user_rows_with_user_save_errors
end
end

def all_saves_attempted?
user_rows_marked_for_user_save.all?(&:attempted_user_save?)
end

def user_rows_marked_for_invitation
user_rows.select(&:marked_for_invitation?)
end

def all_invitations_attempted?
user_rows_marked_for_invitation.all?(&:invitation_attempted?)
end

def user_rows_with_invitation_attempted
user_rows.select(&:invitation_attempted?)
end
Expand All @@ -78,10 +84,6 @@ def user_rows_with_invitation_errors
user_rows_with_invitation_attempted.select(&:all_invitations_failed?)
end

def all_saves_attempted?
user_rows_marked_for_user_save.all?(&:attempted_user_save?)
end

def mark_selected_rows_for_invitation!(selected_ids)
user_rows.each do |user_row|
user_row.mark_for_invitation! if selected_ids.include?(user_row.id)
Expand Down Expand Up @@ -118,22 +120,17 @@ def count
@user_rows.length
end

def find(user_list_id)
user_rows.find { |user_row| user_row.id == user_list_id }
def find(user_row_id)
user_rows.find { |user_row| user_row.id == user_row_id }
end

private

def build_user_rows
def load_user_rows
@user_list_upload.user_rows.preload(
matching_user: [:organisations, :motif_categories, :referents, :tags, :address_geocoding],
invitation_attempts: :invitation,
user_save_attempts: [user: [:address_geocoding, { invitations: [:follow_up] }]]
).order(created_at: :asc).to_a
end

def assign_row_attributes(user_list_id, data)
user_row = find(user_list_id)
user_row&.assign_attributes(data)
end
end
12 changes: 10 additions & 2 deletions app/models/user_list_upload/invitation_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class UserListUpload::InvitationAttempt < ApplicationRecord
belongs_to :user_row, class_name: "UserListUpload::UserRow"
belongs_to :invitation, optional: true

enum format: { sms: "sms", email: "email" }, _prefix: :format

def self.create_from_row(user_row:, format:)
invite_user_result = InviteUser.call(
user: user_row.saved_user,
Expand All @@ -11,13 +13,19 @@ def self.create_from_row(user_row:, format:)
invitation_attributes: { format: format }
)
rescue StandardError => e
invite_user_result = OpenStruct.new(success?: false, errors: [e.message])
invite_user_result = OpenStruct.new(
success?: false,
errors: ["Une erreur est survenue lors de l'invitation de l'usager."],
internal_error_message: e.detailed_message
)
Sentry.capture_exception(e)
ensure
user_row.invitation_attempts.create!(
success: invite_user_result.success?,
service_errors: invite_user_result.errors,
format: format,
invitation_id: invite_user_result.invitation&.id
invitation_id: invite_user_result.invitation&.id,
internal_error_message: invite_user_result.internal_error_message
)
end
end
11 changes: 9 additions & 2 deletions app/models/user_list_upload/user_save_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ class UserListUpload::UserSaveAttempt < ApplicationRecord
def self.create_from_row(user_row:)
save_user_result = UserListUpload::SaveUser.call(user_row: user_row)
rescue StandardError => e
save_user_result = OpenStruct.new(success?: false, errors: [e.message], error_type: e.class.name.underscore)
save_user_result = OpenStruct.new(
success?: false,
errors: ["Une erreur est survenue lors de la sauvegarde de l'usager."],
error_type: e.class.name.underscore,
internal_error_message: e.detailed_message
)
Sentry.capture_exception(e)
ensure
user_row.user_save_attempts.create!(
success: save_user_result.success?,
service_errors: save_user_result.errors,
error_type: save_user_result.error_type,
user_id: save_user_result.user&.id
user_id: save_user_result.user&.id,
internal_error_message: save_user_result.internal_error_message
)
end

Expand Down
1 change: 1 addition & 0 deletions db/migrate/20250113215003_create_user_save_attempts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def change
t.references :user_row, null: false, foreign_key: true, type: :uuid
t.references :user, foreign_key: true
t.string :error_type
t.string :internal_error_message
t.string :service_errors, array: true, default: []

t.timestamps
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20250113215536_create_invitation_attempts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def change
t.references :user_row, null: false, foreign_key: true, type: :uuid
t.references :invitation, foreign_key: true
t.string :service_errors, array: true, default: []
t.string :internal_error_message
t.string :format

t.timestamps
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
t.uuid "user_row_id", null: false
t.bigint "invitation_id"
t.string "service_errors", default: [], array: true
t.string "internal_error_message"
t.string "format"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down Expand Up @@ -543,6 +544,7 @@
t.uuid "user_row_id", null: false
t.bigint "user_id"
t.string "error_type"
t.string "internal_error_message"
t.string "service_errors", default: [], array: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down

0 comments on commit 03cbe91

Please sign in to comment.