diff --git a/app/controllers/match_proposals_controller.rb b/app/controllers/match_proposals_controller.rb index bcce797..936995e 100644 --- a/app/controllers/match_proposals_controller.rb +++ b/app/controllers/match_proposals_controller.rb @@ -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) diff --git a/app/models/match.rb b/app/models/match.rb index f06a9ef..0772406 100755 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index b1db6e9..395f990 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb index aed367f..b74343d 100644 --- a/app/views/messages/_message.html.erb +++ b/app/views/messages/_message.html.erb @@ -5,5 +5,5 @@

- To: <%= namelink message.recipient %> From: <%= namelink message.sender %> on <%= longdate message.created_at %> + To: <%= namelink message.recipient %> From: <% if message.sender_type == 'System' %>System<% else %><%= namelink message.sender %><% end %> on <%= longdate message.created_at %>

\ No newline at end of file diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 5a4d399..f0e033b 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -10,7 +10,7 @@
<%= raw message.text_parsed %>
-

Sent by: <%= namelink message.sender %> on <%= shortdate message.created_at %>

+

Sent by: <% if message.sender_type == 'System' %>System<% else %><%= namelink message.sender %><% end %> on <%= shortdate message.created_at %>

<%= link_to "Reply", { controller: "messages", action: "new", id: message.sender_type, id2: message.sender_id, title: message.title }, diff --git a/db/migrate/20170501121908_create_match_proposals.rb b/db/migrate/20171013154050_create_match_proposals.rb similarity index 50% rename from db/migrate/20170501121908_create_match_proposals.rb rename to db/migrate/20171013154050_create_match_proposals.rb index 3072119..7460482 100644 --- a/db/migrate/20170501121908_create_match_proposals.rb +++ b/db/migrate/20171013154050_create_match_proposals.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a260857..a960534 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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