saucy/app/controllers/members_controller.rb

95 lines
2.4 KiB
Ruby
Raw Normal View History

2022-06-25 12:48:46 +00:00
class MembersController < ApplicationController
2022-06-25 22:45:47 +00:00
before_action :require_login
before_action :set_member, only: %i[ show edit update delete destroy resend_registration ]
2022-06-25 12:48:46 +00:00
helper_method :sort_params
2022-07-16 10:27:43 +00:00
include MemberFilter
2022-06-25 12:48:46 +00:00
# GET /members
def index
2022-07-16 10:27:43 +00:00
@members = filtered_members
2022-06-25 12:48:46 +00:00
end
# GET /members/1
def show
end
# GET /members/new
def new
2023-03-31 01:35:11 +00:00
@member = Member.new(get_params)
2022-06-25 12:48:46 +00:00
end
# GET /members/1/edit
def edit
end
# POST /members
def create
@member = Member.new(member_params.merge(number: (Member.maximum(:number) || 0) + 1))
2022-06-25 12:48:46 +00:00
if @member.save
@member.generate_missing_ifthenpay_links!
2022-06-25 12:48:46 +00:00
@member.reset_status!
2023-08-07 12:50:50 +00:00
NotificationMailer.with(member: @member).registration.deliver_now!
Redo notification system Previously, saucy generated each member's future notification every day (marked as scheduled). And every day saucy would deliver every unsent notification scheduled for that day. This means that each member with expiration date in the future had ~6 notifications scheduled for them in the database. This was troublesome because if the cron job that delivers notification was down or the server was down for more than 24h, notifications were silently skipped and we had no easy way of detecting it: we had to check every member for missing sent notifications. It also had the disadvantage that we were deleting and recreating hundreds of database entries for no good reason. To fix this, the new system no longer stores future scheduled notifications. Instead, it now only stores sent notifications and notifications currently being delivered. Every day, the deliver task checks every member if there's a notification that needs to be sent in that day, by checking if we're past the date of sending a particular notification type and checking if in that window of time no notifications of that type have been sent. Let's suppose we send 6 notifications, one per month starting 60 days before the membership expires until 90 days after: -60d -30d t=0 30d 60d 90d ----|------|------|------|------|------|------ A B C D E F If we're on day t=-60d, we need to deliver a notification of type A. But first we check if no notifications of type A have been sent to that member on that day. From days t=-59d to t=-31d, we check if in the time range t=[-60d, -31d] any notification of type A was sent to that member. If not, we need to deliver it. If we're on days t=[-30d, -1d], we do the same but for notification of type B.
2024-03-23 15:22:04 +00:00
@member.notifications.create!(
template: "registration",
to_be_sent_on: Date.today,
sent_at: Time.current,
status: "sent",
)
2023-08-07 12:50:50 +00:00
2022-06-25 12:48:46 +00:00
redirect_to @member, notice: "Member was successfully created."
else
render :new, status: :unprocessable_entity
end
end
def resend_registration
NotificationMailer.with(member: @member).registration.deliver_now!
Redo notification system Previously, saucy generated each member's future notification every day (marked as scheduled). And every day saucy would deliver every unsent notification scheduled for that day. This means that each member with expiration date in the future had ~6 notifications scheduled for them in the database. This was troublesome because if the cron job that delivers notification was down or the server was down for more than 24h, notifications were silently skipped and we had no easy way of detecting it: we had to check every member for missing sent notifications. It also had the disadvantage that we were deleting and recreating hundreds of database entries for no good reason. To fix this, the new system no longer stores future scheduled notifications. Instead, it now only stores sent notifications and notifications currently being delivered. Every day, the deliver task checks every member if there's a notification that needs to be sent in that day, by checking if we're past the date of sending a particular notification type and checking if in that window of time no notifications of that type have been sent. Let's suppose we send 6 notifications, one per month starting 60 days before the membership expires until 90 days after: -60d -30d t=0 30d 60d 90d ----|------|------|------|------|------|------ A B C D E F If we're on day t=-60d, we need to deliver a notification of type A. But first we check if no notifications of type A have been sent to that member on that day. From days t=-59d to t=-31d, we check if in the time range t=[-60d, -31d] any notification of type A was sent to that member. If not, we need to deliver it. If we're on days t=[-30d, -1d], we do the same but for notification of type B.
2024-03-23 15:22:04 +00:00
@member.notifications.create!(
template: "registration",
to_be_sent_on: Date.today,
sent_at: Time.current,
status: "sent",
)
redirect_to @member, notice: "Payment reminder sent."
end
2022-06-25 12:48:46 +00:00
# PATCH/PUT /members/1
def update
if @member.update(member_params)
@member.reload.reset_status!
redirect_to @member, notice: "Member was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
# GET /members/1/delete
def delete
end
# DELETE /members/1
def destroy
@member.remove_personal_information!
redirect_to members_path, notice: "Member personal data permanently removed."
end
2022-06-25 12:48:46 +00:00
private
# Use callbacks to share common setup or constraints between actions.
def set_member
@member = Member.find(params[:id])
end
# Only allow a list of trusted parameters through.
def member_params
2023-03-31 01:24:16 +00:00
params.fetch(:member, {}).permit(:display_name, :legal_name, :pronouns, :email, :identification_number, :category, :address, :joined_on, :expires_on, :wants_mailing_list, :prefers_postal)
2022-06-25 12:48:46 +00:00
end
2023-03-31 01:35:11 +00:00
def get_params
params.permit(:display_name, :legal_name, :pronouns, :email, :identification_number, :category, :address, :joined_on, :expires_on, :wants_mailing_list, :prefers_postal)
end
2022-06-25 12:48:46 +00:00
end