Made System send a message to opposing team on proposal creation, created a clean migration for match_proposals table

This commit is contained in:
Absurdon 2017-10-13 17:53:48 +02:00
parent 4435f7f5e6
commit fa68d2ef06
7 changed files with 35 additions and 18 deletions

View file

@ -24,8 +24,17 @@ class MatchProposalsController < ApplicationController
raise AccessError unless @proposal.can_create? cuser
@proposal.team = cuser.team
@proposal.status = MatchProposal::STATUS_PENDING
if @proposal.save
# 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
flash[:notice] = 'Created new proposal'
redirect_to(match_proposals_path(@match))
else
@ -51,8 +60,10 @@ class MatchProposalsController < ApplicationController
}
render(json: rjson, status: :forbidden) && return
end
send_msg = proposal.status != params[:match_proposal][:status]
proposal.status = params[:match_proposal][:status]
if proposal.save
# TODO: send message to opposite team leaders if send_msg
rjson[:status] = MatchProposal.status_strings[proposal.status]
rjson[:message] = "Successfully updated status to #{MatchProposal.status_strings[proposal.status]}"
render(json: rjson, status: :accepted)

View file

@ -189,6 +189,11 @@ class Match < ActiveRecord::Base
end
end
def get_opposing_team(team)
team == contester1.team ? contester2.team : contester1.team
end
def set_hltv
get_hltv if match_time.future?
end

View file

@ -44,11 +44,15 @@ class Message < ActiveRecord::Base
end
def thread
Message.find_by_sql ["
if sender_type == 'System'
Message.where(recipient_id: recipient.id, sender_type: 'System')
else
Message.find_by_sql ["
(SELECT `messages`.* FROM `messages` WHERE `messages`.`sender_id` = ? AND `messages`.`sender_type` = 'User' AND `messages`.`recipient_id` = ?)
UNION
(SELECT `messages`.* FROM `messages` WHERE `messages`.`sender_id` = ? AND `messages`.`sender_type` = 'User' AND `messages`.`recipient_id` = ?)
ORDER BY id", sender.id, recipient.id, recipient.id, sender.id]
end
end
def parse_text

View file

@ -5,5 +5,5 @@
</p>
<p>
To: <%= namelink message.recipient %> From: <%= namelink message.sender %> on <em><%= longdate message.created_at %></em></em>
To: <%= namelink message.recipient %> From: <% if message.sender_type == 'System' %>System<% else %><%= namelink message.sender %><% end %> on <em><%= longdate message.created_at %></em></em>
</p>

View file

@ -10,7 +10,7 @@
<div class="content">
<%= raw message.text_parsed %>
</div>
<p>Sent by: <%= namelink message.sender %> on <%= shortdate message.created_at %></p>
<p>Sent by: <% if message.sender_type == 'System' %>System<% else %><%= namelink message.sender %><% end %> on <%= shortdate message.created_at %></p>
<p>
<%= link_to "Reply",
{ controller: "messages", action: "new", id: message.sender_type, id2: message.sender_id, title: message.title },

View file

@ -1,14 +1,14 @@
class CreateMatchProposals < ActiveRecord::Migration
def change
def up
create_table :match_proposals do |t|
t.references :match
t.references :team
t.integer :status
t.references :match, index: true, forign_key: true
t.references :team, forign_key: true
t.datetime :proposed_time
t.timestamps
t.integer :status
end
add_index :match_proposals, :match_id
add_index :match_proposals, :team_id
add_index :match_proposals, :status
end
def down; end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20170702150454) do
ActiveRecord::Schema.define(:version => 20171013154050) do
create_table "admin_requests", :force => true do |t|
t.string "addr"
@ -435,14 +435,11 @@ ActiveRecord::Schema.define(:version => 20170702150454) do
create_table "match_proposals", :force => true do |t|
t.integer "match_id"
t.integer "team_id"
t.integer "status"
t.datetime "proposed_time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "status"
end
add_index "match_proposals", ["match_id"], :name => "index_match_proposals_on_match_id"
add_index "match_proposals", ["team_id"], :name => "index_match_proposals_on_team_id"
add_index "match_proposals", ["status"], :name => "index_match_proposals_on_status"
create_table "matchers", :force => true do |t|
t.integer "match_id", :null => false