saucy/app/models/member.rb

120 lines
3.0 KiB
Ruby
Raw Normal View History

2022-06-25 12:48:46 +00:00
class Member < ApplicationRecord
default_scope { where(excluded: false) }
2022-06-25 12:48:46 +00:00
has_many :contributions
has_many :notifications
2022-06-26 11:57:00 +00:00
has_paper_trail
2022-06-25 12:48:46 +00:00
2023-04-19 21:52:31 +00:00
def self.ransackable_attributes(auth_object = nil)
%w[display_name legal_name email identification_number]
end
2022-06-25 12:48:46 +00:00
def cancelled_on
expires_on + 90.days
end
def reset_status!
update(status: expected_status)
end
def employed?
category == "normal" || category == "employed"
end
def remove_personal_information!
update(
excluded: true,
display_name: "",
2023-03-31 01:24:16 +00:00
legal_name: "",
pronouns: "",
email: nil,
identification_number: "",
category: "",
address: "",
)
end
2022-06-25 12:48:46 +00:00
def expected_status
if joined_on.nil?
:pending
elsif (joined_on + 6.months).future?
:passive
elsif expires_on.future?
:active
elsif cancelled_on.future?
:expired
else
:cancelled
end
end
def handle_new_contribution(contribution, overriden_expires_on)
if joined_on.nil?
self.joined_on = contribution.payment_on
self.expires_on = overriden_expires_on.presence || (joined_on + 1.year)
else
self.expires_on = overriden_expires_on.presence || expires_on + 1.year
end
save!
end
def regenerate_notifications(from=Date.today)
2022-06-25 12:48:46 +00:00
notifications.where(status: 'scheduled').delete_all
2023-01-24 18:26:10 +00:00
dates = notifications.pluck(:to_be_sent_on)
2022-06-25 12:48:46 +00:00
return if expires_on.nil?
[
{ to_be_sent_on: expires_on - 60.days, template: "expiration_in_60d" },
2022-06-25 12:48:46 +00:00
{ to_be_sent_on: expires_on - 30.days, template: "expiration_in_30d" },
{ to_be_sent_on: expires_on + 0.days, template: "expired" },
{ to_be_sent_on: expires_on + 30.days, template: "expired_30d_ago" },
{ to_be_sent_on: expires_on + 60.days, template: "expired_60d_ago" },
{ to_be_sent_on: expires_on + 90.days, template: "cancelled" },
2023-01-24 18:26:10 +00:00
].reject do |n|
n[:to_be_sent_on].before?(from) || dates.include?(n[:to_be_sent_on])
2023-01-24 18:26:10 +00:00
end.each do |n|
2022-06-25 12:48:46 +00:00
notifications.create(n.merge(status: "scheduled"))
end
end
def generate_missing_ifthenpay_links!
self.regular_ifthenpay_link = IfThenPay.generate_gateway_link(
id: number,
amount: "30.00",
description: "Quotas ANSOL",
) unless self.regular_ifthenpay_link.present?
self.reduced_ifthenpay_link = IfThenPay.generate_gateway_link(
id: number,
amount: "6.00",
description: "Quotas ANSOL",
) unless self.reduced_ifthenpay_link.present?
save!
end
def self.reset_all_status!
Member.all.each do |member|
member.reset_status!
end
end
def self.generate_all_missing_ifthenpay_links!
Member.all.each do |member|
member.generate_missing_ifthenpay_links!
end
end
def self.regenerate_all_notifications
2023-04-05 22:04:48 +00:00
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
Member.all.each do |member|
member.regenerate_notifications
end
2022-06-25 12:48:46 +00:00
end
end
end