saucy/app/models/member.rb

101 lines
2.5 KiB
Ruby

class Member < ApplicationRecord
default_scope { where(excluded: false) }
has_many :contributions
has_many :notifications
def cancelled_on
expires_on + 90.days
end
def reset_status!
update(status: expected_status)
end
def remove_personal_information!
update(
excluded: true,
display_name: "",
email: nil,
identification_number: "",
category: "",
address: "",
)
end
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
notifications.where(status: 'scheduled').delete_all
return if expires_on.nil?
[
{ to_be_sent_on: expires_on - 90.days, template: "expiration_in_60d" },
{ 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" },
].reject { |n| n[:to_be_sent_on].past? }.each do |n|
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
Member.all.each do |member|
member.regenerate_notifications
end
end
end