1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

improves api/notification controller to avoid failing when there is a notification with wrong notification_type in db

This commit is contained in:
Nicolas Florentin 2023-09-07 10:30:48 +02:00
parent 48d76fa783
commit a72c2e580c
6 changed files with 94 additions and 86 deletions

View File

@ -2,6 +2,8 @@
## next deploy
- improves api/notification controller to avoid failing when there is a notification with wrong notification_type in db
## v6.0.14 2023 September 6
- Fix a bug: for project categories, if there is no category : do not show categories panel in show view, do not show categories input field in edit view

View File

@ -15,6 +15,7 @@ class API::NotificationsController < API::APIController
def index
loop do
@notifications = current_user.notifications
.with_valid_notification_type
.delivered_in_system(current_user)
.includes(:attached_object)
.page(params[:page])
@ -24,8 +25,8 @@ class API::NotificationsController < API::APIController
break unless delete_obsoletes(@notifications)
end
@totals = {
total: current_user.notifications.delivered_in_system(current_user).count,
unread: current_user.notifications.delivered_in_system(current_user).where(is_read: false).count
total: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).count,
unread: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).where(is_read: false).count
}
render :index
end
@ -33,6 +34,7 @@ class API::NotificationsController < API::APIController
def last_unread
loop do
@notifications = current_user.notifications
.with_valid_notification_type
.delivered_in_system(current_user)
.includes(:attached_object)
.where(is_read: false)
@ -42,19 +44,20 @@ class API::NotificationsController < API::APIController
break unless delete_obsoletes(@notifications)
end
@totals = {
total: current_user.notifications.delivered_in_system(current_user).count,
unread: current_user.notifications.delivered_in_system(current_user).where(is_read: false).count
total: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).count,
unread: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).where(is_read: false).count
}
render :index
end
def polling
@notifications = current_user.notifications
.with_valid_notification_type
.where('is_read = false AND created_at >= :date', date: params[:last_poll])
.order('created_at DESC')
@totals = {
total: current_user.notifications.delivered_in_system(current_user).count,
unread: current_user.notifications.delivered_in_system(current_user).where(is_read: false).count
total: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).count,
unread: current_user.notifications.with_valid_notification_type.delivered_in_system(current_user).where(is_read: false).count
}
render :index
end

View File

@ -19,6 +19,8 @@ class Notification < ApplicationRecord
SQL
}
scope :with_valid_notification_type, -> { joins(:notification_type).where(notification_types: { name: NOTIFICATIONS_TYPES.map { |nt| nt[:name] } }) }
validates :receiver_id,
:receiver_type,
:attached_object_id,

View File

@ -2,7 +2,7 @@
# NotificationType defines the different types of Notification.
# To add a new notification type in db, you must add it in:
# - db/seeds/notification_types.rb
# - config/initializers/notification_types.rb
# - app/views/api/notifications/_XXXXXX.json.jbuilder
# - app/views/notifications_mailer/XXXXXX.html.erb
# - app/frontend/src/javascript/models/notification-type.ts

View File

@ -0,0 +1,80 @@
# frozen_string_literal: true
NOTIFICATIONS_TYPES = [
{ name: 'notify_admin_when_project_published', category: 'projects', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_project_collaborator_to_valid', category: 'projects', is_configurable: false },
{ name: 'notify_project_author_when_collaborator_valid', category: 'projects', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_training_valid', category: 'trainings', is_configurable: false },
{ name: 'notify_member_subscribed_plan', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_create_reservation', category: 'agenda', is_configurable: false },
{ name: 'notify_member_subscribed_plan_is_changed', category: 'deprecated', is_configurable: false },
{ name: 'notify_admin_member_create_reservation', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_slot_is_modified', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_slot_is_modified', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_when_user_is_created', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscribed_plan', category: 'subscriptions', is_configurable: true, roles: ['admin'] },
{ name: 'notify_user_when_invoice_ready', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_subscription_will_expire_in_7_days', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_subscription_is_expired', category: 'subscriptions', is_configurable: false },
{ name: 'notify_admin_subscription_will_expire_in_7_days', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscription_is_expired', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscription_canceled', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_subscription_canceled', category: 'subscriptions', is_configurable: false },
{ name: 'notify_user_when_avoir_ready', category: 'wallet', is_configurable: false },
{ name: 'notify_member_slot_is_canceled', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_slot_is_canceled', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_partner_subscribed_plan', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_subscription_extended', category: 'subscriptions', is_configurable: false },
{ name: 'notify_admin_subscription_extended', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_user_group_changed', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_user_group_changed', category: 'users_accounts', is_configurable: false },
{ name: 'notify_admin_when_user_is_imported', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_user_profile_complete', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_auth_migration', category: 'user', is_configurable: false },
{ name: 'notify_admin_user_merged', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_profile_complete', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_abuse_reported', category: 'projects', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_invoicing_changed', category: 'deprecated', is_configurable: false },
{ name: 'notify_user_wallet_is_credited', category: 'wallet', is_configurable: false },
{ name: 'notify_admin_user_wallet_is_credited', category: 'wallet', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_export_complete', category: 'exports', is_configurable: false },
{ name: 'notify_member_about_coupon', category: 'agenda', is_configurable: false },
{ name: 'notify_member_reservation_reminder', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_free_disk_space', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_close_period_reminder', category: 'accountings', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_archive_complete', category: 'accountings', is_configurable: true, roles: ['admin'] },
{ name: 'notify_privacy_policy_changed', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_import_complete', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_refund_created', category: 'wallet', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admins_role_update', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_role_update', category: 'users_accounts', is_configurable: false },
{ name: 'notify_admin_objects_stripe_sync', category: 'payments', is_configurable: false },
{ name: 'notify_user_when_payment_schedule_ready', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_failed', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_failed', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_check_deadline', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_payment_schedule_transfer_deadline', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_payment_schedule_error', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_error', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_gateway_canceled', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_gateway_canceled', category: 'payments', is_configurable: false },
{ name: 'notify_admin_user_supporting_document_files_created', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_user_supporting_document_files_updated', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_is_validated', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_is_invalidated', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_supporting_document_refusal', category: 'supporting_documents', is_configurable: false },
{ name: 'notify_admin_user_supporting_document_refusal', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_order_is_paid', category: 'shop', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_order_is_ready', category: 'shop', is_configurable: false },
{ name: 'notify_user_order_is_canceled', category: 'shop', is_configurable: false },
{ name: 'notify_user_order_is_refunded', category: 'shop', is_configurable: false },
{ name: 'notify_admin_low_stock_threshold', category: 'shop', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_training_auto_cancelled', category: 'trainings', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_training_auto_cancelled', category: 'trainings', is_configurable: false }
].freeze

View File

@ -1,84 +1,5 @@
# frozen_string_literal: true
NOTIFICATIONS_TYPES = [
{ name: 'notify_admin_when_project_published', category: 'projects', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_project_collaborator_to_valid', category: 'projects', is_configurable: false },
{ name: 'notify_project_author_when_collaborator_valid', category: 'projects', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_training_valid', category: 'trainings', is_configurable: false },
{ name: 'notify_member_subscribed_plan', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_create_reservation', category: 'agenda', is_configurable: false },
{ name: 'notify_member_subscribed_plan_is_changed', category: 'deprecated', is_configurable: false },
{ name: 'notify_admin_member_create_reservation', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_slot_is_modified', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_slot_is_modified', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_when_user_is_created', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscribed_plan', category: 'subscriptions', is_configurable: true, roles: ['admin'] },
{ name: 'notify_user_when_invoice_ready', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_subscription_will_expire_in_7_days', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_subscription_is_expired', category: 'subscriptions', is_configurable: false },
{ name: 'notify_admin_subscription_will_expire_in_7_days', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscription_is_expired', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_subscription_canceled', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_subscription_canceled', category: 'subscriptions', is_configurable: false },
{ name: 'notify_user_when_avoir_ready', category: 'wallet', is_configurable: false },
{ name: 'notify_member_slot_is_canceled', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_slot_is_canceled', category: 'agenda', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_partner_subscribed_plan', category: 'subscriptions', is_configurable: false },
{ name: 'notify_member_subscription_extended', category: 'subscriptions', is_configurable: false },
{ name: 'notify_admin_subscription_extended', category: 'subscriptions', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_user_group_changed', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_user_group_changed', category: 'users_accounts', is_configurable: false },
{ name: 'notify_admin_when_user_is_imported', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_user_profile_complete', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_auth_migration', category: 'user', is_configurable: false },
{ name: 'notify_admin_user_merged', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_profile_complete', category: 'users_accounts', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_abuse_reported', category: 'projects', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_invoicing_changed', category: 'deprecated', is_configurable: false },
{ name: 'notify_user_wallet_is_credited', category: 'wallet', is_configurable: false },
{ name: 'notify_admin_user_wallet_is_credited', category: 'wallet', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_export_complete', category: 'exports', is_configurable: false },
{ name: 'notify_member_about_coupon', category: 'agenda', is_configurable: false },
{ name: 'notify_member_reservation_reminder', category: 'agenda', is_configurable: false },
{ name: 'notify_admin_free_disk_space', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_close_period_reminder', category: 'accountings', is_configurable: true, roles: ['admin'] },
{ name: 'notify_admin_archive_complete', category: 'accountings', is_configurable: true, roles: ['admin'] },
{ name: 'notify_privacy_policy_changed', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_import_complete', category: 'app_management', is_configurable: false },
{ name: 'notify_admin_refund_created', category: 'wallet', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admins_role_update', category: 'users_accounts', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_role_update', category: 'users_accounts', is_configurable: false },
{ name: 'notify_admin_objects_stripe_sync', category: 'payments', is_configurable: false },
{ name: 'notify_user_when_payment_schedule_ready', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_failed', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_failed', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_check_deadline', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_payment_schedule_transfer_deadline', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_payment_schedule_error', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_error', category: 'payments', is_configurable: false },
{ name: 'notify_admin_payment_schedule_gateway_canceled', category: 'payments', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_payment_schedule_gateway_canceled', category: 'payments', is_configurable: false },
{ name: 'notify_admin_user_supporting_document_files_created', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_user_supporting_document_files_updated', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_is_validated', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_is_invalidated', category: 'users_accounts', is_configurable: false },
{ name: 'notify_user_supporting_document_refusal', category: 'supporting_documents', is_configurable: false },
{ name: 'notify_admin_user_supporting_document_refusal', category: 'supporting_documents', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_order_is_paid', category: 'shop', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_user_order_is_ready', category: 'shop', is_configurable: false },
{ name: 'notify_user_order_is_canceled', category: 'shop', is_configurable: false },
{ name: 'notify_user_order_is_refunded', category: 'shop', is_configurable: false },
{ name: 'notify_admin_low_stock_threshold', category: 'shop', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_admin_training_auto_cancelled', category: 'trainings', is_configurable: true, roles: ['admin', 'manager'] },
{ name: 'notify_member_training_auto_cancelled', category: 'trainings', is_configurable: false }
].freeze
NOTIFICATIONS_TYPES.each do |notification_type_attrs|
notification_type = NotificationType.find_by(name: notification_type_attrs[:name])