diff options
author | jessib <jessib@riseup.net> | 2013-12-31 11:48:18 -0800 |
---|---|---|
committer | jessib <jessib@riseup.net> | 2013-12-31 11:48:18 -0800 |
commit | f9c96f8844205afe3c310b5b914752068728d38f (patch) | |
tree | e9e9d4e2d5e7d03946a3c7d4929c53b71ef1d952 | |
parent | 4dae5eaa1bdb211d9d3ff29d5a9e9a86424e9748 (diff) |
Cleanup of code for messages API and cron job for 1 month payment warning. Authentication still remaining piece.
-rw-r--r-- | users/app/controllers/v1/messages_controller.rb | 15 | ||||
-rw-r--r-- | users/app/models/user.rb | 47 | ||||
-rw-r--r-- | users/config/schedule.rb | 2 | ||||
-rw-r--r-- | users/test/functional/v1/messages_controller_test.rb | 10 |
4 files changed, 9 insertions, 65 deletions
diff --git a/users/app/controllers/v1/messages_controller.rb b/users/app/controllers/v1/messages_controller.rb index 18f9f46..42a88f7 100644 --- a/users/app/controllers/v1/messages_controller.rb +++ b/users/app/controllers/v1/messages_controller.rb @@ -12,20 +12,11 @@ module V1 # routes ensure this is only for PUT def mark_read -=begin - user_message = UserMessage.find_by_user_id_and_message_id([params[:user_id], params[:message_id]]) - user_message.seen = true if user_message - # TODO what to return? - if user_message and user_message.save - render json: true - else - render json: false - end - end -=end + # make sure user and message exist if (user = User.find(params[:user_id])) && Message.find(params[:message_id]) - user.message_ids_seen << params[:message_id] if !user.message_ids_seen.include?(params[:message_id]) #is it quicker to instead run uniq after adding? + + user.message_ids_seen << params[:message_id] if !user.message_ids_seen.include?(params[:message_id]) #TODO: is it quicker to instead call uniq! after adding? user.message_ids_to_see.delete(params[:message_id]) user.save render json: true diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 30b9ee7..6e445be 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -78,16 +78,6 @@ class User < CouchRest::Model::Base def messages(unseen = true) -=begin - user_messages = unseen ? UserMessage.by_user_id_and_seen(:key => [self.id, false]).all : UserMessage.by_user_id(:key => self.id).all - - messages = [] - user_messages.each do |um| - messages << Message.find(um.message.id) - end - messages -=end - message_ids = unseen ? self.message_ids_to_see : self.message_ids_to_see + self.message_ids_seen # TODO check unique? messages = [] @@ -137,49 +127,22 @@ class User < CouchRest::Model::Base end - def self.send_one_month_warnings # class not instance method - -=begin - # get all users who are not customers with active subscription and have existed for exactly a month (take account of months having difft amount of days. Maybe just those who signed up 30 days ago?) - #users_to_warn = User.find_by_created_at(Time.now-1.month).all #NO, this will require time to be right - #users_1_month_old = User.by_created_at.startkey(Time.now-1.month-1.day).endkey(Time.now-1.month).al - users_30_days_old = User.by_created_at.startkey(Time.now-31.days).endkey(Time.now-30.days).all - # TODO, above really is quite problematic, in that if the cron job fails to run on 1 day, say, the warning will not get created. - - users_30_days_old.each do |user| - - # create a user message for each user that does not has a braintree customer, or - # has a braintree customer w/out an active subscription. - unless ((customer = Customer.find_by_user(user.id)) && customer.subscriptions) - - if !@message - # create a message for today's date - # only want to create once, and only if it will be used. - @message = Message.new(:text => t(:payment_one_month_warning, :date_in_one_month => (Time.now+1.month).strftime("%Y-%d-%m"))) - @message.save - end - - user_message = UserMessage.new(:message_id => @message.id, :user_id => user.id) - # is following preferred?? - # user_message = UserMessage.new(:message => @message, :user => user) - user_message.save - end - end -=end + def self.send_one_month_warnings - #to determine warnings to send, need to get all users where one_month_warning_sent is not set, and where it was created greater than or equal to 1 month ago. this will likely be custom js view/design + # To determine warnings to send, need to get all users where one_month_warning_sent is not set, and where it was created greater than or equal to 1 month ago. + # TODO: might want to further limit to enabled accounts, and, based on provider's service level configuration, for particular service levels. users_to_warn = User.by_created_at_and_one_month_warning_not_sent.endkey(Time.now-1.month) users_to_warn.each do |user| if !@message # create a message for today's date # only want to create once, and only if it will be used. - @message = Message.new(:text => t(:payment_one_month_warning, :date_in_one_month => (Time.now+1.month).strftime("%Y-%d-%m"))) + @message = Message.new(:text => I18n.t(:payment_one_month_warning, :date_in_one_month => (Time.now+1.month).strftime("%Y-%d-%m"))) @message.save end user.message_ids_to_see << @message.id user.one_month_warning_sent = true - user.save #?? + user.save end end diff --git a/users/config/schedule.rb b/users/config/schedule.rb index 9ddcf2f..4ecbe7c 100644 --- a/users/config/schedule.rb +++ b/users/config/schedule.rb @@ -20,5 +20,5 @@ # Learn more: http://github.com/javan/whenever every 1.day, :at => '1am' do - runner "User.send_one_month_warnings + runner "User.send_one_month_warnings" end diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index d7ada82..7666ba3 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -10,14 +10,9 @@ class V1::MessagesControllerTest < ActionController::TestCase @user = FactoryGirl.build(:user) @user.message_ids_to_see << @message.id @user.save - - # @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id) - # @user_message.save - end teardown do - # @user_message.destroy @user.destroy @message.destroy end @@ -29,13 +24,10 @@ class V1::MessagesControllerTest < ActionController::TestCase end test "mark message read for user" do - #assert !@user_message.seen assert @user.message_ids_to_see.include?(@message.id) assert !@user.message_ids_seen.include?(@message.id) put :mark_read, :user_id => @user.id, :message_id => @message.id - #@user_message.reload - #assert @user_message.seen @user.reload assert !@user.message_ids_to_see.include?(@message.id) assert @user.message_ids_seen.include?(@message.id) @@ -43,8 +35,6 @@ class V1::MessagesControllerTest < ActionController::TestCase end test "do not get seen messages" do - # @user_message.seen = true - # @user_message.save put :mark_read, :user_id => @user.id, :message_id => @message.id @user.reload get :user_messages, :user_id => @user.id |