From fce5c0d90b38edef44e8fdae37ec8b87c5758072 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 Mar 2023 15:38:46 -0300 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20hacer=20todo=20en=20una=20transacci?= =?UTF-8?q?=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/usuaries_controller.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/controllers/usuaries_controller.rb b/app/controllers/usuaries_controller.rb index ab116572..84bcc13f 100644 --- a/app/controllers/usuaries_controller.rb +++ b/app/controllers/usuaries_controller.rb @@ -86,15 +86,17 @@ class UsuariesController < ApplicationController next if Usuarie.where(id: @site.roles.pluck(:usuarie_id)).find_by(attributes) - usuarie = Usuarie.find_by attributes - usuarie ||= Usuarie.invite!(attributes).tap do |u| - u.send :generate_invitation_token! + Usuarie.transaction do + usuarie = Usuarie.find_by attributes + usuarie ||= Usuarie.invite!(attributes).tap do |u| + u.send :generate_invitation_token! + end + + @site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as) + + # XXX: La invitación tiene que ser enviada luego de crear el rol + usuarie.deliver_invitation end - - @site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as) - - # XXX: La invitación tiene que ser enviada luego de crear el rol - usuarie.deliver_invitation end redirect_to site_usuaries_path(@site) From 732db666c7bf10d03696c243d54c351bb8c4e85b Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 Mar 2023 15:42:20 -0300 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20usar=20el=20=C3=BAltimo=20sitio=20al?= =?UTF-8?q?=20que=20todav=C3=ADa=20no=20aceptamos=20invitaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/devise/mailer/invitation_instructions.html.haml | 2 +- app/views/devise/mailer/invitation_instructions.text.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/devise/mailer/invitation_instructions.html.haml b/app/views/devise/mailer/invitation_instructions.html.haml index e1fe6812..bc9d9e4e 100644 --- a/app/views/devise/mailer/invitation_instructions.html.haml +++ b/app/views/devise/mailer/invitation_instructions.html.haml @@ -1,4 +1,4 @@ -- site = @resource.sites.last +- site = @resource.roles.where(temporal: true).last&.site %p= t('devise.mailer.invitation_instructions.hello', email: @resource.email) diff --git a/app/views/devise/mailer/invitation_instructions.text.haml b/app/views/devise/mailer/invitation_instructions.text.haml index 353f2a12..a1360bb0 100644 --- a/app/views/devise/mailer/invitation_instructions.text.haml +++ b/app/views/devise/mailer/invitation_instructions.text.haml @@ -1,4 +1,4 @@ -- site = @resource.sites.last +- site = @resource.roles.where(temporal: true).last&.site = t('devise.mailer.invitation_instructions.hello', email: @resource.email) \ From 1fa37b20e1ee5f218e98b21da518927a2512f953 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 Mar 2023 15:47:06 -0300 Subject: [PATCH 3/4] fix: solo invitar una vez --- app/views/devise/mailer/invitation_instructions.html.haml | 2 +- app/views/devise/mailer/invitation_instructions.text.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/devise/mailer/invitation_instructions.html.haml b/app/views/devise/mailer/invitation_instructions.html.haml index bc9d9e4e..3cb9704e 100644 --- a/app/views/devise/mailer/invitation_instructions.html.haml +++ b/app/views/devise/mailer/invitation_instructions.html.haml @@ -8,7 +8,7 @@ %h1= site.title %p= site.description -- if @resource.created_by_invite? +- if @resource.created_by_invite? && !@resource.invitation_accepted? %p= link_to t('devise.mailer.invitation_instructions.accept'), accept_invitation_url(@resource, invitation_token: @token) diff --git a/app/views/devise/mailer/invitation_instructions.text.haml b/app/views/devise/mailer/invitation_instructions.text.haml index a1360bb0..2050c19c 100644 --- a/app/views/devise/mailer/invitation_instructions.text.haml +++ b/app/views/devise/mailer/invitation_instructions.text.haml @@ -9,7 +9,7 @@ \ = site.description \ -- if @resource.created_by_invite? +- if @resource.created_by_invite? && !@resource.invitation_accepted? = accept_invitation_url(@resource, invitation_token: @token) \ - if @resource.invitation_due_at From 1bfff4dfdb72ebdd676c324d0a60bcf16f5b2c5a Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 Mar 2023 16:24:53 -0300 Subject: [PATCH 4/4] fix: enviar las invitaciones luego de crear el rol --- app/controllers/usuaries_controller.rb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/app/controllers/usuaries_controller.rb b/app/controllers/usuaries_controller.rb index 84bcc13f..074694f1 100644 --- a/app/controllers/usuaries_controller.rb +++ b/app/controllers/usuaries_controller.rb @@ -81,21 +81,25 @@ class UsuariesController < ApplicationController authorize SiteUsuarie.new(@site, current_usuarie) # Enviar la invitación si es necesario y agregar al sitio - invitaciones.each do |invitacion| - attributes = { email: invitacion.address, skip_invitation: true } - - next if Usuarie.where(id: @site.roles.pluck(:usuarie_id)).find_by(attributes) + invitaciones.each do |address| + next if Usuarie.where(id: @site.roles.pluck(:usuarie_id)).find_by_email(address) Usuarie.transaction do - usuarie = Usuarie.find_by attributes - usuarie ||= Usuarie.invite!(attributes).tap do |u| + usuarie = Usuarie.find_by_email(address) + usuarie ||= Usuarie.invite!({ email: address, skip_invitation: true }).tap do |u| u.send :generate_invitation_token! end - @site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as) + role = @site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as) # XXX: La invitación tiene que ser enviada luego de crear el rol - usuarie.deliver_invitation + if role.persisted? + usuarie.deliver_invitation + else + raise ArgumentError, role.errors.full_messages + end + rescue ArgumentError => e + ExceptionNotifier.notify_exception(e, data: { site: @site.name, address: address }) end end @@ -137,6 +141,8 @@ class UsuariesController < ApplicationController private # Traer todas las invitaciones que al menos tengan usuarie y dominio + # + # @return [Array] def invitaciones # XXX: Podríamos usar EmailAddress pero hace chequeos más lentos params[:invitaciones]&.tr("\r", '')&.split("\n")&.map do |m| @@ -145,7 +151,7 @@ class UsuariesController < ApplicationController nil end.compact.select do |m| m.local && m.domain - end + end.map(&:address) end # El tipo de invitación que tenemos que enviar, si alguien mandó