2017-05-01 14:57:32 +00:00
|
|
|
class MatchProposalsController < ApplicationController
|
2017-06-04 18:30:33 +00:00
|
|
|
before_filter :get_match
|
2017-05-01 14:57:32 +00:00
|
|
|
def index
|
2017-10-07 10:28:06 +00:00
|
|
|
raise AccessError unless @match.user_in_match?(cuser)
|
2017-05-01 14:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2017-10-08 12:46:45 +00:00
|
|
|
# Don't allow creation of new proposals if there is a confirmed one already
|
|
|
|
if MatchProposal.exists?(
|
|
|
|
match_id: @match.id,
|
|
|
|
status: MatchProposal::STATUS_CONFIRMED
|
|
|
|
)
|
|
|
|
flash[:error] = 'Cannot create a new proposal if there is already a confirmed one'
|
|
|
|
redirect_to(match_proposals_path(@match)) && return
|
2017-10-07 12:02:14 +00:00
|
|
|
end
|
2017-06-04 18:30:33 +00:00
|
|
|
@proposal = MatchProposal.new
|
|
|
|
@proposal.match = @match
|
2017-05-11 20:26:18 +00:00
|
|
|
raise AccessError unless @proposal.can_create? cuser
|
2017-05-01 14:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-06-04 18:30:33 +00:00
|
|
|
@proposal = MatchProposal.new(params[:match_proposal])
|
|
|
|
@proposal.match = @match
|
|
|
|
raise AccessError unless @proposal.can_create? cuser
|
2017-05-11 20:26:18 +00:00
|
|
|
@proposal.team = cuser.team
|
|
|
|
@proposal.status = MatchProposal::STATUS_PENDING
|
2017-06-11 14:36:10 +00:00
|
|
|
if @proposal.save
|
2017-10-13 15:53:48 +00:00
|
|
|
# TODO: send message to teamleaders of opposite team
|
|
|
|
msg = Message.new
|
|
|
|
msg.sender_type = 'System'
|
|
|
|
msg.recipient_type = 'Team'
|
|
|
|
msg.title = 'New Scheduling Proposal'
|
|
|
|
recipient = @match.get_opposing_team(cuser.team)
|
|
|
|
msg.recipient = recipient
|
|
|
|
msg.text = "There is a new scheduling proposal for your match against #{recipient.name}.\n" \
|
|
|
|
"Find it [url=#{match_proposals_path(@match)}]here[/url]"
|
|
|
|
msg.save
|
2017-05-11 20:26:18 +00:00
|
|
|
flash[:notice] = 'Created new proposal'
|
2017-06-11 14:36:10 +00:00
|
|
|
redirect_to(match_proposals_path(@match))
|
2017-05-11 20:26:18 +00:00
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
2017-05-01 14:57:32 +00:00
|
|
|
end
|
2017-05-11 20:26:18 +00:00
|
|
|
|
|
|
|
def update
|
2017-10-07 10:28:06 +00:00
|
|
|
raise AccessError unless request.xhr? # Only respond to ajax requests
|
|
|
|
rjson = {}
|
|
|
|
proposal = MatchProposal.find(params[:id])
|
|
|
|
unless proposal
|
|
|
|
rjson[:error] = {
|
|
|
|
code: 404,
|
|
|
|
message: "No proposal with id #{params[:id]}"
|
|
|
|
}
|
|
|
|
render(json: rjson, status: :not_found) && return
|
|
|
|
end
|
|
|
|
unless proposal.can_update?(cuser, params[:match_proposal])
|
|
|
|
rjson[:error] = {
|
|
|
|
code: 403,
|
|
|
|
message: "You are not allowed to update the state to #{MatchProposal.status_strings[params[:match_proposal][:status].to_i]}"
|
|
|
|
}
|
|
|
|
render(json: rjson, status: :forbidden) && return
|
|
|
|
end
|
2017-10-15 12:14:26 +00:00
|
|
|
new_status = params[:match_proposal][:status].to_i
|
2017-10-13 16:22:33 +00:00
|
|
|
curr_status = proposal.status
|
|
|
|
status_updated = curr_status != new_status
|
|
|
|
proposal.status = new_status
|
2017-10-07 10:28:06 +00:00
|
|
|
if proposal.save
|
2017-10-13 16:22:33 +00:00
|
|
|
|
|
|
|
if status_updated
|
|
|
|
msg = Message.new
|
|
|
|
msg.sender_type = 'System'
|
|
|
|
msg.recipient_type = 'Team'
|
2017-10-15 12:14:26 +00:00
|
|
|
msg.title = 'Scheduling Proposal Update'
|
2017-10-13 16:22:33 +00:00
|
|
|
recipient = @match.get_opposing_team(cuser.team)
|
|
|
|
msg.recipient = recipient
|
2017-10-15 12:14:26 +00:00
|
|
|
msg.text = message_text(new_status, recipient)
|
2017-10-13 16:22:33 +00:00
|
|
|
msg.save if msg.text
|
|
|
|
end
|
|
|
|
|
2017-10-07 10:28:06 +00:00
|
|
|
rjson[:status] = MatchProposal.status_strings[proposal.status]
|
|
|
|
rjson[:message] = "Successfully updated status to #{MatchProposal.status_strings[proposal.status]}"
|
2017-10-07 12:02:14 +00:00
|
|
|
render(json: rjson, status: :accepted)
|
2017-06-11 14:36:10 +00:00
|
|
|
else
|
2017-10-07 10:28:06 +00:00
|
|
|
rjson[:error] = {
|
|
|
|
code: 500,
|
|
|
|
message: 'Something went wrong! Please try again.'
|
|
|
|
}
|
|
|
|
render(json: rjson, status: 500)
|
2017-06-11 14:36:10 +00:00
|
|
|
end
|
2017-05-11 20:26:18 +00:00
|
|
|
end
|
|
|
|
|
2017-06-04 18:30:33 +00:00
|
|
|
private
|
|
|
|
def get_match
|
|
|
|
@match = Match.find params[:match_id]
|
|
|
|
end
|
2017-10-13 16:22:33 +00:00
|
|
|
|
2017-10-15 12:14:26 +00:00
|
|
|
def message_text(new_status, recipient)
|
2017-10-13 16:22:33 +00:00
|
|
|
case new_status
|
|
|
|
when MatchProposal::STATUS_CONFIRMED
|
2017-10-15 12:14:26 +00:00
|
|
|
"A scheduling proposal for your match against [b]#{recipient.name}[/b] was confirmed!.\n" \
|
2017-10-13 16:22:33 +00:00
|
|
|
"Find it [url=#{match_proposals_path(@match)}]here[/url]"
|
|
|
|
when MatchProposal::STATUS_REJECTED
|
2017-10-15 12:14:26 +00:00
|
|
|
"A scheduling proposal for your match against [b]#{recipient.name}[/b] was rejected!.\n" \
|
2017-10-13 16:22:33 +00:00
|
|
|
"Find it [url=#{match_proposals_path(@match)}]here[/url]"
|
|
|
|
when MatchProposal::STATUS_REVOKED
|
2017-10-15 12:14:26 +00:00
|
|
|
"A scheduling proposal for your match against [b]#{recipient.name}[/b] was revoked!.\n" \
|
2017-10-13 16:22:33 +00:00
|
|
|
"Find it [url=#{match_proposals_path(@match)}]here[/url]"
|
|
|
|
when MatchProposal::STATUS_DELAYED
|
2017-10-15 12:14:26 +00:00
|
|
|
"Delaying for your match against [b]#{recipient.name}[/b] was permitted!.\n" \
|
2017-10-13 16:22:33 +00:00
|
|
|
"Schedule a new time as soon as possible [url=#{match_proposals_path(@match)}]here[/url]"
|
|
|
|
else
|
|
|
|
false # Should not happen as transition to any other state is not allowed
|
|
|
|
end
|
|
|
|
end
|
2017-05-01 14:57:32 +00:00
|
|
|
end
|