Skip to content

Commit

Permalink
Refactor invitation acceptance and rejection logic; add expiration ha…
Browse files Browse the repository at this point in the history
…ndling and email sending functionality
  • Loading branch information
andrew committed Nov 28, 2024
1 parent eef9093 commit 275b4f6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
12 changes: 6 additions & 6 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ def show
end

def accept
# find invitation by token
# invitation.accept
# redirect to invitation.html_url
@invitation = Invitation.find_by_token(params[:token])
invitation.accept!
redirect_to invitation_path(@invitation.token)
end

def reject
# find invitation by token
# invitation.reject
# redirect back invitation.html_url
@invitation = Invitation.find_by_token(params[:token])
invitation.reject!
redirect_to invitation_path(@invitation.token)
end
end
38 changes: 34 additions & 4 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,45 @@ def generate_token
end
end

def accept
update!(accepted_at: Time.zone.now)
def decline_deadline
project_allocation.decline_deadline
end

def reject
update!(rejected_at: Time.zone.now)
def expired?
Time.zone.now > decline_deadline
end

def accept!
return if expired?
update!(accepted_at: Time.zone.now, rejected_at: nil)
project_allocation.accept_funding!
end

def reject!
return if expired?
update!(rejected_at: Time.zone.now, accepted_at: nil)
project_allocation.reject_funding!
end

def accepted?
accepted_at.present?
end

def rejected?
rejected_at.present?
end

def send_email
MaintainerMailer.invitation_email(
project.contact_email,
project.to_s,
funder_names,
"$#{amount_cents / 100.0}",
"https://example.com/invite",
decline_deadline.strftime("%B %d, %Y")
).deliver_now
end

def html_url
"https://staging.opencollective.com/#{ENV['OPENCOLLECTIVE_PARENT_SLUG']}/projects/#{project_allocation.fund.oc_project_slug}/expenses/#{member_invitation_id}"
end
Expand Down
16 changes: 7 additions & 9 deletions app/models/project_allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,16 @@ def decline_deadline
created_at + 14.days
end

def send_invitation_email
MaintainerMailer.invitation_email(
project.contact_email,
project.to_s,
funder_names,
"$#{amount_cents / 100.0}",
"https://example.com/invite",
decline_deadline.strftime("%B %d, %Y")
)
def send_invitation
# create invitation record
# send email
end

def reject_funding!
project.update!(funding_rejected: true)
end

def accept_funding!
project.update!(funding_rejected: false)
end
end
16 changes: 12 additions & 4 deletions app/views/invitations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<h1>Accept or reject the invitation!</h1>

<p>Accept confirmation button</p>
<% if @invitation.expired? %>

<p>or they can update their funding.yml and we'll check it again</p>
<p>This invitation has expired</p>

<p>also an option to reject</p>
<% else %>

<p>if it's been accepted/rejected already then show a message</p>
<p>Accept confirmation button</p>

<p>or they can update their funding.yml and we'll check it again</p>

<p>also an option to reject</p>

<p>if it's been accepted/rejected already then show a message</p>

<% end %>

0 comments on commit 275b4f6

Please sign in to comment.