From 4dae5eaa1bdb211d9d3ff29d5a9e9a86424e9748 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 30 Dec 2013 16:22:38 -0800 Subject: Fixes to initial go at job to send one month warnings. --- ...by_created_at_and_one_month_warning_not_sent.js | 5 +++++ users/app/models/user.rb | 23 +++++++++++++++++++++- users/config/schedule.rb | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 users/app/designs/user/by_created_at_and_one_month_warning_not_sent.js (limited to 'users') diff --git a/users/app/designs/user/by_created_at_and_one_month_warning_not_sent.js b/users/app/designs/user/by_created_at_and_one_month_warning_not_sent.js new file mode 100644 index 0000000..53a95de --- /dev/null +++ b/users/app/designs/user/by_created_at_and_one_month_warning_not_sent.js @@ -0,0 +1,5 @@ +function (doc) { + if ((doc['type'] == 'User') && (doc['created_at'] != null) && (doc['one_month_warning_sent'] == null)) { + emit(doc['created_at'], 1); + } +} diff --git a/users/app/models/user.rb b/users/app/models/user.rb index d9b03ec..30b9ee7 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -15,6 +15,7 @@ class User < CouchRest::Model::Base property :message_ids_to_see, [String] property :message_ids_seen, [String] + property :one_month_warning_sent, TrueClass before_save :update_effective_service_level @@ -135,7 +136,10 @@ class User < CouchRest::Model::Base ServiceLevel.new({id: code}) end - def one_month_warning_to_pay + + 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 @@ -161,6 +165,23 @@ class User < CouchRest::Model::Base user_message.save end end +=end + + #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 + 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.save + end + + user.message_ids_to_see << @message.id + user.one_month_warning_sent = true + user.save #?? + end + end protected diff --git a/users/config/schedule.rb b/users/config/schedule.rb index 1b2b171..9ddcf2f 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.one_month_warning_to_pay" + runner "User.send_one_month_warnings end -- cgit v1.2.3