Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[File d'attente] Envoi de l'alerte #382

Merged
18 commits merged into from
Feb 6, 2020
18 changes: 17 additions & 1 deletion app/controllers/users/rdvs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Users::RdvsController < UserAuthController
before_action :set_rdv, only: [:confirmation, :cancel]
before_action :set_rdv, only: [:confirmation, :cancel, :change_creneau]

def index
@rdvs = policy_scope(Rdv).includes(:motif, :rdvs_users, :users)
Expand Down Expand Up @@ -51,6 +51,22 @@ def create
end
end

def change_creneau
authorize(@rdv)
@starts_at = params[:starts_at].to_time
@state = nil
unless params[:confirmed] == 'true' && @starts_at.present? && @rdv.update(starts_at: @starts_at)
lieu = Lieu.find_by(address: @rdv.location)
@creneau = Creneau.new(starts_at: @starts_at, motif: @rdv.motif, lieu_id: lieu.id)
if @creneau.available?
@state = true
else
@state = false
flash.now[:error] = "Malheureusement, ce créneau n'est plus disponible."
end
end
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est pas un creneaux#new ?

def confirmation
authorize(@rdv)
end
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/twilio_sender_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TwilioSenderJob < ApplicationJob
def perform(status, rdv, user)
TwilioTextMessenger.new(status, rdv, user).send_sms
def perform(status, rdv, user, options = {})
TwilioTextMessenger.new(status, rdv, user, options).send_sms
end
end
3 changes: 2 additions & 1 deletion app/mailers/file_attente_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class FileAttenteMailer < ApplicationMailer
def send_notification(rdv, user)
def send_notification(rdv, user, creneau_starts_at)
@rdv = rdv
@creneau_starts_at = creneau_starts_at
mail(to: user.email, subject: "Un créneau vient de se liberer !")
end
end
4 changes: 2 additions & 2 deletions app/models/file_attente.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def valid_for_notification?(creneau, end_time)

def send_notification(last_creneau_sent_starts_at)
rdv.users.map(&:user_to_notify).uniq.each do |user|
TwilioSenderJob.perform_later(:file_attente, rdv, user) if user.formated_phone
FileAttenteMailer.send_notification(rdv, user).deliver_later if user.email
TwilioSenderJob.perform_later(:file_attente, rdv, user, creneau_starts_at: last_creneau_sent_starts_at) if user.formated_phone
FileAttenteMailer.send_notification(rdv, user, last_creneau_sent_starts_at).deliver_later if user.email
update!(last_creneau_sent_starts_at: last_creneau_sent_starts_at, notifications_sent: notifications_sent + 1)
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/policies/user/rdv_policy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class User::RdvPolicy < ApplicationPolicy
def change_creneau?
rdv_belongs_to_user_or_children?
end

def create?
rdv_belongs_to_user_or_children?
end
Expand Down
5 changes: 3 additions & 2 deletions app/services/twilio_text_messenger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ class TwilioTextMessenger

attr_reader :user, :rdv, :from, :type

def initialize(type, rdv, user)
def initialize(type, rdv, user, options)
@type = type
@user = user
@rdv = rdv
@options = options
@from = ENV["TWILIO_PHONE_NUMBER"]
end

Expand Down Expand Up @@ -58,7 +59,7 @@ def reminder
def file_attente
message = sms_header
message += "Un RDV #{@rdv.motif.name} - #{@rdv.motif.service.name} #{I18n.l(@rdv.starts_at, format: :human)} s'est libéré.\n"
message += "Cliquez pour vérifier la disponibilité: TODO\n"
message += "Cliquez pour vérifier la disponibilité: #{users_change_creneau_url(rdv_id: @rdv.id, starts_at: @options[:creneau_starts_at].to_s, host: "https://#{ENV["HOST"]}")}"
message
end
end
2 changes: 1 addition & 1 deletion app/views/file_attente_mailer/send_notification.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ div
p
| Vous pouvez avancer la date de votre RDV en cliquant sur le lien ci-dessous.
.btn-wrapper
= link_to 'Verifier les disponibilités', '/TODO', class:'btn btn-primary'
= link_to 'Verifier la disponibilité', users_change_creneau_url(rdv_id: @rdv.id, starts_at: @creneau_starts_at.to_s), class:'btn btn-primary'
3 changes: 2 additions & 1 deletion app/views/users/rdvs/_rdv.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
i.fa.fa-exclamation-triangle>
strong Informations supplémentaires :
= simple_format(rdv.motif.instruction_for_rdv, class:"pl-3 pt-1")
= render "file_attente", rdv:rdv
- unless defined?(hide_file_attente_infos) && hide_file_attente_infos
= render "file_attente", rdv: rdv
- unless defined?(hide_cancellation_infos) && hide_cancellation_infos
- if rdv.cancellable?
.text-right.mt-2
Expand Down
21 changes: 21 additions & 0 deletions app/views/users/rdvs/change_creneau.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.row
- if @state
h2 Un créneau s'est libéré
p.mb-5 Un créneau s'est liberé le #{l(@starts_at, format: :human)}. Vous pouvez prendre ce creneau si vous souhaitez avancer votre RDV à cette date ou vous pouvez conserver la date initiale.
.col.text-center
h4 Nouveau rendez-vous proposé
p= l(@starts_at, format: :human).capitalize
= link_to 'Changer de créneau', "?starts_at=#{@starts_at.to_s}&confirmed=true", class:"btn btn-primary"

.col-sm-12.mt-3.text-center
p.text-muted
| Si ce créneau ne vous convient pas, votre RDV restera le #{l(@rdv.starts_at, format: :human)}
- elsif @state.nil?
h2.mb-4 Votre RDV a bien été modifié
.mx-auto
= render partial: 'rdv', locals: { rdv: @rdv, hide_file_attente_infos: true }
- elsif @state == false
h2 Créneau indisponible
p.mb-5 Désolé le RDV a déjà été réattribué. Vous recevrez d'autres propositions si un créneau se libère.
.mx-auto
= render partial: 'rdv', locals: { rdv: @rdv, hide_file_attente_infos: true }
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
put :cancel
get :confirmation
end
get 'change_creneau/:rdv_id', to: 'rdvs#change_creneau', as: 'change_creneau'
post 'file_attente', to: 'file_attentes#create_or_delete'
end
resources :stats, only: :index
Expand Down