Class: NotificationChannel

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/notification_channel.rb

Overview

== Schema Information

Table name: notification_channels
Database name: primary

id :integer not null, primary key
fallback_for_subsidiaries :boolean default(TRUE), not null
notification_type :string not null
transmission_type :string(255)
created_at :datetime
updated_at :datetime
contact_point_id :integer
customer_id :integer not null

Indexes

index_notification_channel_unique (customer_id, contact_point_id, notification_type, COALESCE(transmission_type, ''::character varying)) UNIQUE
index_notification_channels_on_contact_point_id (contact_point_id)
index_notification_channels_on_customer_id (customer_id)

Foreign Keys

notification_channels_contact_point_id_fk (contact_point_id => contact_points.id) ON DELETE => cascade
notification_channels_customer_id_fkey (customer_id => parties.id)

Constant Summary collapse

ORDERS_SHIPMENT =

Orders shipment.

'ORS'.freeze
INVOICES =

Invoices.

'invoices'.freeze
RMA =

Rma.

'RMA'.freeze
CREDIT_MEMOS =

Credit memos.

'credit_memos'.freeze
STATEMENT_OF_ACCOUNTS =

Statement of accounts.

'statement_of_accounts'.freeze
EMAIL_OR_FAX =

Email or fax.

'Email/Fax'.freeze
MAIL =

Mail.

'Mail'.freeze
PORTAL =

Portal.

'Portal'.freeze
EDI =

Edi.

'EDI'.freeze
NOTIFICATION_TYPES =

Recognised notification types.

{ 'Order Shipment Notifications': ORDERS_SHIPMENT,
Invoices: INVOICES,
RMA: RMA,
'Credit Memos': CREDIT_MEMOS,
'Statement of Accounts': STATEMENT_OF_ACCOUNTS }.freeze
TRANSMISSION_TYPES =

Recognised transmission types.

[EMAIL_OR_FAX, MAIL, PORTAL, EDI].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#contact_point_idObject (readonly)



91
# File 'app/models/notification_channel.rb', line 91

validates :contact_point_id, presence: { unless: proc { |nc| [MAIL, EDI].include?(nc.transmission_type) }, message: "is required for that type of notification channel" }

#customer_idObject (readonly)



92
# File 'app/models/notification_channel.rb', line 92

validates :notification_type, :transmission_type, :customer_id, presence: true

#notification_typeObject (readonly)



92
# File 'app/models/notification_channel.rb', line 92

validates :notification_type, :transmission_type, :customer_id, presence: true

#transmission_typeObject (readonly)



92
# File 'app/models/notification_channel.rb', line 92

validates :notification_type, :transmission_type, :customer_id, presence: true

Class Method Details

.contact_points_for_select(customer) ⇒ Object



115
116
117
118
# File 'app/models/notification_channel.rb', line 115

def self.contact_points_for_select(customer)
  party_ids = customer.all_my_active_party_ids + [87] # Venu, harcoded
  ContactPoint.includes(:party).where(party_id: party_ids).map { |cp| ["#{cp.party.full_name} : #{cp.detail}", cp.id] }
end

.credit_memosActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are credit memos. Active Record Scope

Returns:

See Also:



103
# File 'app/models/notification_channel.rb', line 103

scope :credit_memos, -> { where(notification_type: CREDIT_MEMOS) }

.emailActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are email. Active Record Scope

Returns:

See Also:



101
# File 'app/models/notification_channel.rb', line 101

scope :email, -> { eager_load(:contact_point).merge(ContactPoint.emails) }

.fallback_enabledActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are fallback enabled. Active Record Scope

Returns:

See Also:



104
# File 'app/models/notification_channel.rb', line 104

scope :fallback_enabled, -> { where(fallback_for_subsidiaries: true) }

.faxActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are fax. Active Record Scope

Returns:

See Also:



102
# File 'app/models/notification_channel.rb', line 102

scope :fax, -> { eager_load(:contact_point).merge(ContactPoint.faxes) }

.for_customerActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are for customer. Active Record Scope

Returns:

See Also:



105
# File 'app/models/notification_channel.rb', line 105

scope :for_customer, ->(customer) { where(customer_id: customer.id).or(fallback_enabled.where(NotificationChannel[:customer_id].gt(0)).where(customer_id: customer.parent_id.to_i)) }

.invoicesActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are invoices. Active Record Scope

Returns:

See Also:



99
# File 'app/models/notification_channel.rb', line 99

scope :invoices, -> { where(notification_type: INVOICES) }

.notification_types_for_selectObject



107
108
109
# File 'app/models/notification_channel.rb', line 107

def self.notification_types_for_select
  NotificationChannel::NOTIFICATION_TYPES.map { |k, v| [k, v] }
end

.orders_shipmentActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are orders shipment. Active Record Scope

Returns:

See Also:



98
# File 'app/models/notification_channel.rb', line 98

scope :orders_shipment, -> { where(notification_type: ORDERS_SHIPMENT) }

.rmasActiveRecord::Relation<NotificationChannel>

A relation of NotificationChannels that are rmas. Active Record Scope

Returns:

See Also:



100
# File 'app/models/notification_channel.rb', line 100

scope :rmas, -> { where(notification_type: NotificationChannel::RMA) }

.transmission_types_for_selectObject



111
112
113
# File 'app/models/notification_channel.rb', line 111

def self.transmission_types_for_select
  NotificationChannel::TRANSMISSION_TYPES.map { |tt| [tt, tt] }
end

Instance Method Details

#clear_contact_point_idObject



132
133
134
# File 'app/models/notification_channel.rb', line 132

def clear_contact_point_id
  self.contact_point_id = nil
end

#contact_pointContactPoint



87
# File 'app/models/notification_channel.rb', line 87

belongs_to :contact_point, optional: true

#contact_point_is_transmittableObject



124
125
126
# File 'app/models/notification_channel.rb', line 124

def contact_point_is_transmittable
  errors.add(:contact_point_id, "must be transmittable for that type of notification channel") if contact_point.nil? || !contact_point.transmittable?
end

#contact_point_is_websiteObject



128
129
130
# File 'app/models/notification_channel.rb', line 128

def contact_point_is_website
  errors.add(:contact_point_id, "must be a website if #{PORTAL} is selected as transmission type") if contact_point.nil? || (contact_point.category != ContactPoint::WEBSITE)
end

#customerCustomer

Returns:

See Also:



86
# File 'app/models/notification_channel.rb', line 86

belongs_to :customer, optional: true

#is_email_or_faxObject



120
121
122
# File 'app/models/notification_channel.rb', line 120

def is_email_or_fax
  errors.add(:transmission_type, "must be #{EMAIL_OR_FAX} for that type of notification channel") if transmission_type != EMAIL_OR_FAX
end