From 23ba44bc57170151997aa96f3dde906a791e4401 Mon Sep 17 00:00:00 2001
From: Sylvain
Date: Fri, 3 Feb 2023 11:54:57 +0100
Subject: [PATCH] (bug) auto cancel trainings
- Wrong trainings sessions was cancelled (in the past instead of in the futur)
- Notifications about auto refunds was incorrect
---
app/services/trainings/auto_cancel_service.rb | 10 ++++++++--
..._notify_admin_training_auto_cancelled.json.jbuilder | 2 +-
...notify_member_training_auto_cancelled.json.jbuilder | 4 ++--
.../notify_admin_training_auto_cancelled.html.erb | 2 +-
.../notify_member_training_auto_cancelled.html.erb | 2 +-
5 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/app/services/trainings/auto_cancel_service.rb b/app/services/trainings/auto_cancel_service.rb
index 57609957b..521ade702 100644
--- a/app/services/trainings/auto_cancel_service.rb
+++ b/app/services/trainings/auto_cancel_service.rb
@@ -12,7 +12,10 @@ class Trainings::AutoCancelService
training.availabilities
.includes(slots: :slots_reservations)
- .where('availabilities.start_at >= ?', DateTime.current - training.auto_cancel_deadline.hours)
+ .where(availabilities: { lock: false })
+ .where('availabilities.start_at >= ? AND availabilities.start_at <= ?',
+ DateTime.current,
+ DateTime.current + training.auto_cancel_deadline.hours)
.find_each do |availability|
next if availability.reservations.count >= training.auto_cancel_threshold
@@ -72,8 +75,11 @@ class Trainings::AutoCancelService
# @param reservation [Reservation]
def refund_after_cancel(reservation)
invoice_item = reservation.invoice_items.joins(:invoice).where(invoices: { type: nil }).first
+ amount = (invoice_item&.amount_after_coupon || 0) / 100.00
+ return if amount.zero?
+
service = WalletService.new(user: reservation.user, wallet: reservation.user.wallet)
- transaction = service.credit(invoice_item.amount_after_coupon / 100.00)
+ transaction = service.credit(amount)
service.create_avoir(transaction, DateTime.current, I18n.t('trainings.refund_for_auto_cancel')) if transaction
end
end
diff --git a/app/views/api/notifications/_notify_admin_training_auto_cancelled.json.jbuilder b/app/views/api/notifications/_notify_admin_training_auto_cancelled.json.jbuilder
index 1545a3836..3f8522e27 100644
--- a/app/views/api/notifications/_notify_admin_training_auto_cancelled.json.jbuilder
+++ b/app/views/api/notifications/_notify_admin_training_auto_cancelled.json.jbuilder
@@ -4,4 +4,4 @@ json.title notification.notification_type
json.description "#{t('.auto_cancelled_training', {
TRAINING: notification.attached_object.trainings.first.name,
DATE: I18n.l(notification.attached_object.start_at.to_date)
- })} #{notification.meta_data['auto_refund'] == 'true' ? t('.auto_refund') : t('.manual_refund')}"
+ })} #{notification.meta_data['auto_refund'] ? t('.auto_refund') : t('.manual_refund')}"
diff --git a/app/views/api/notifications/_notify_member_training_auto_cancelled.json.jbuilder b/app/views/api/notifications/_notify_member_training_auto_cancelled.json.jbuilder
index af83b627c..219454622 100644
--- a/app/views/api/notifications/_notify_member_training_auto_cancelled.json.jbuilder
+++ b/app/views/api/notifications/_notify_member_training_auto_cancelled.json.jbuilder
@@ -3,5 +3,5 @@
json.title notification.notification_type
json.description "#{t('.auto_cancelled_training', {
TRAINING: notification.attached_object.reservation.reservable.name,
- DATE: I18n.l(notification.attached_object.start_at.to_date)
- })} #{notification.meta_data['auto_refund'] == 'true' ? t('.auto_refund') : ''}"
+ DATE: I18n.l(notification.attached_object.slot.start_at.to_date)
+ })} #{notification.meta_data['auto_refund'] ? t('.auto_refund') : ''}"
diff --git a/app/views/notifications_mailer/notify_admin_training_auto_cancelled.html.erb b/app/views/notifications_mailer/notify_admin_training_auto_cancelled.html.erb
index f8e04d6e0..8a62126d3 100644
--- a/app/views/notifications_mailer/notify_admin_training_auto_cancelled.html.erb
+++ b/app/views/notifications_mailer/notify_admin_training_auto_cancelled.html.erb
@@ -9,5 +9,5 @@
}) %>
- <%= @notification.get_meta_data(:auto_refund) == 'true' ? t('.body.auto_refund') : t('.body.manual_refund') %>
+ <%= @notification.get_meta_data(:auto_refund) ? t('.body.auto_refund') : t('.body.manual_refund') %>
diff --git a/app/views/notifications_mailer/notify_member_training_auto_cancelled.html.erb b/app/views/notifications_mailer/notify_member_training_auto_cancelled.html.erb
index cc3da13fe..ca8144c00 100644
--- a/app/views/notifications_mailer/notify_member_training_auto_cancelled.html.erb
+++ b/app/views/notifications_mailer/notify_member_training_auto_cancelled.html.erb
@@ -9,5 +9,5 @@
}) %>
- <%= @notification.get_meta_data(:auto_refund) == 'true' ? t('.body.auto_refund') : '' %>
+ <%= @notification.get_meta_data(:auto_refund) ? t('.body.auto_refund') : '' %>