From ee97b93a497b6006f8e8b361468458d18a412846 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 23 Mar 2022 13:30:55 +0100 Subject: [PATCH] (style) replace Setting.find_by(name... by Setting.get --- app/pdfs/pdf/invoice.rb | 2 +- app/pdfs/pdf/payment_schedule.rb | 2 +- app/services/accounting_export_service.rb | 14 ++++---- app/workers/reservation_reminder_worker.rb | 38 ++++++++++++---------- test/integration/events/as_user_test.rb | 4 +-- test/integration/settings_test.rb | 2 +- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/pdfs/pdf/invoice.rb b/app/pdfs/pdf/invoice.rb index d0efe9aa8..115acfa2e 100644 --- a/app/pdfs/pdf/invoice.rb +++ b/app/pdfs/pdf/invoice.rb @@ -25,7 +25,7 @@ class PDF::Invoice < Prawn::Document ) # logo - img_b64 = Setting.find_by(name: 'invoice_logo') + img_b64 = Setting.get('invoice_logo') begin image StringIO.new(Base64.decode64(img_b64.value)), fit: [415, 40] rescue StandardError => e diff --git a/app/pdfs/pdf/payment_schedule.rb b/app/pdfs/pdf/payment_schedule.rb index 061bd8065..e0feb3b83 100644 --- a/app/pdfs/pdf/payment_schedule.rb +++ b/app/pdfs/pdf/payment_schedule.rb @@ -28,7 +28,7 @@ class PDF::PaymentSchedule < Prawn::Document ) # logo - img_b64 = Setting.find_by(name: 'invoice_logo') + img_b64 = Setting.get('invoice_logo') begin image StringIO.new(Base64.decode64(img_b64.value)), fit: [415, 40] rescue StandardError => e diff --git a/app/services/accounting_export_service.rb b/app/services/accounting_export_service.rb index a355bc3ee..05ae70ce0 100644 --- a/app/services/accounting_export_service.rb +++ b/app/services/accounting_export_service.rb @@ -207,35 +207,35 @@ class AccountingExportService def account(invoice, account, type: :code, means: :other) case account when :projets - Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value + Setting.get("accounting_#{means}_client_#{type}") when :vat - Setting.find_by(name: "accounting_VAT_#{type}")&.value + Setting.get("accounting_VAT_#{type}") when :subscription if invoice.subscription_invoice? - Setting.find_by(name: "accounting_subscription_#{type}")&.value + Setting.get("accounting_subscription_#{type}") else puts "WARN: Invoice #{invoice.id} has no subscription" end when :reservation if invoice.main_item.object_type == 'Reservation' - Setting.find_by(name: "accounting_#{invoice.main_item.object.reservable_type}_#{type}")&.value + Setting.get("accounting_#{invoice.main_item.object.reservable_type}_#{type}") else puts "WARN: Invoice #{invoice.id} has no reservation" end when :wallet if invoice.main_item.object_type == 'WalletTransaction' - Setting.find_by(name: "accounting_wallet_#{type}")&.value + Setting.get("accounting_wallet_#{type}") else puts "WARN: Invoice #{invoice.id} is not a wallet credit" end when :pack if invoice.main_item.object_type == 'StatisticProfilePrepaidPack' - Setting.find_by(name: "accounting_Pack_#{type}")&.value + Setting.get("accounting_Pack_#{type}") else puts "WARN: Invoice #{invoice.id} has no prepaid-pack" end when :error - Setting.find_by(name: "accounting_Error_#{type}")&.value + Setting.get("accounting_Error_#{type}") else puts "Unsupported account #{account}" end || '' diff --git a/app/workers/reservation_reminder_worker.rb b/app/workers/reservation_reminder_worker.rb index 5d5b509cd..2967fa7a6 100644 --- a/app/workers/reservation_reminder_worker.rb +++ b/app/workers/reservation_reminder_worker.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +# Send a reminder email to the user who has made a reservation class ReservationReminderWorker include Sidekiq::Worker @@ -5,25 +8,24 @@ class ReservationReminderWorker DEFAULT_REMINDER_DELAY = 24.hours def perform - enabled = Setting.find_by(name: 'reminder_enable').try(:value) - if enabled == 'true' - delay = Setting.find_by(name: 'reminder_delay').try(:value).try(:to_i).try(:hours) || DEFAULT_REMINDER_DELAY + return unless Setting.get('reminder_enable') - starting = DateTime.current.beginning_of_hour + delay - ending = starting + 1.hour + delay = Setting.find_by(name: 'reminder_delay').try(:value).try(:to_i).try(:hours) || DEFAULT_REMINDER_DELAY - Reservation.joins(:slots).where('slots.start_at >= ? AND slots.start_at <= ? AND slots.canceled_at IS NULL', starting, ending).each do |r| - already_sent = Notification.where( - attached_object_type: Reservation.name, - attached_object_id: r.id, - notification_type_id: NotificationType.find_by_name('notify_member_reservation_reminder') - ).count - unless already_sent > 0 - NotificationCenter.call type: 'notify_member_reservation_reminder', - receiver: r.user, - attached_object: r - end - end + starting = DateTime.current.beginning_of_hour + delay + ending = starting + 1.hour + + Reservation.joins(:slots).where('slots.start_at >= ? AND slots.start_at <= ? AND slots.canceled_at IS NULL', starting, ending).each do |r| + already_sent = Notification.where( + attached_object_type: Reservation.name, + attached_object_id: r.id, + notification_type_id: NotificationType.find_by_name('notify_member_reservation_reminder') + ).count + next if already_sent.positive? + + NotificationCenter.call type: 'notify_member_reservation_reminder', + receiver: r.user, + attached_object: r end end -end \ No newline at end of file +end diff --git a/test/integration/events/as_user_test.rb b/test/integration/events/as_user_test.rb index 701a87f57..7c39535a5 100644 --- a/test/integration/events/as_user_test.rb +++ b/test/integration/events/as_user_test.rb @@ -19,11 +19,11 @@ class Events::AsUserTest < ActionDispatch::IntegrationTest wallet_transactions_count = WalletTransaction.count # Enable the VAT at 19.6% - vat_active = Setting.find_by(name: 'invoice_VAT-active') + vat_active = Setting.get('invoice_VAT-active') vat_active.value = 'true' vat_active.save! - vat_rate = Setting.find_by(name: 'invoice_VAT-rate') + vat_rate = Setting.get('invoice_VAT-rate') vat_rate.value = '19.6' vat_rate.save! diff --git a/test/integration/settings_test.rb b/test/integration/settings_test.rb index c0b2c7deb..9dfe44ddc 100644 --- a/test/integration/settings_test.rb +++ b/test/integration/settings_test.rb @@ -22,7 +22,7 @@ class SettingsTest < ActionDispatch::IntegrationTest assert_equal 'Test Fablab', resp[:setting][:value] # Check record - setting = Setting.find_by_name(resp[:setting][:name]) + setting = Setting.get(resp[:setting][:name]) assert_not_nil setting, 'setting was not found in database' assert_equal 2, setting.history_values.count, 'all historical values were not found' assert_includes setting.history_values.map(&:value), 'Fab Lab de La Casemate', 'previous parameter was not saved'