diff --git a/app/controllers/light/users_controller.rb b/app/controllers/light/users_controller.rb index a53e4d2..1a339d2 100644 --- a/app/controllers/light/users_controller.rb +++ b/app/controllers/light/users_controller.rb @@ -43,10 +43,12 @@ def sendtest end def unsubscribe - if @user.present? && @user.sidekiq_status == 'Subscribed' - @user.update(is_subscribed: 'false', - unsubscribed_at: DateTime.now, - sidekiq_status: 'Unsubscribed') + if @user.present? && @user.sidekiq_status == Light::User::STATUS[:subscribed] + @user.update( + is_subscribed: false, + unsubscribed_at: DateTime.now, + sidekiq_status: Light::User::STATUS[:unsubscribed] + ) @message = 'Unsubscribed successfully!!' else @message = response_message('unsubscribed') @@ -54,9 +56,9 @@ def unsubscribe end def subscribe - if @user.present? && @user.sidekiq_status == 'Unsubscribed' - @user.update(is_subscribed: 'true', - sidekiq_status: 'Subscribed', + if @user.present? && @user.sidekiq_status == Light::User::STATUS[:unsubscribed] + @user.update(is_subscribed: true, + sidekiq_status: Light::User::STATUS[:subscribed], subscribed_at: DateTime.now, remote_ip: request.remote_ip, user_agent: request.env['HTTP_USER_AGENT']) @@ -116,16 +118,20 @@ def auto_opt_in def opt_in @user = Light::User.where(email_id: params[:email]).first if @user.present? - @user.update_attributes(is_subscribed: true, - sidekiq_status: 'Subscribed', - subscribed_at: DateTime.now) + @user.update_attributes( + is_subscribed: true, + sidekiq_status: Light::User::STATUS[:subscribed], + subscribed_at: DateTime.now + ) else u_name = params[:username].blank? ? params[:email] : params[:username] - @user = Light::User.new(username: u_name, - email_id: params[:email], - source: 'web subscription request', - subscribed_at: DateTime.now, - sidekiq_status: 'Subscribed') + @user = Light::User.new( + username: u_name, + email_id: params[:email], + source: 'web subscription request', + subscribed_at: DateTime.now, + sidekiq_status: Light::User::STATUS[:subscribed] + ) end respond_to do |format| format.json { head :no_content } @@ -139,7 +145,10 @@ def thank_you private def users_params - params.require(:user).permit(:id, :email_id, :is_subscribed, :joined_on, :source, :username) + params.require(:user).permit( + :id, :email_id, :is_subscribed, :joined_on, :is_blocked, + :sidekiq_status, :source, :username + ) end def user_with_token @@ -154,7 +163,7 @@ def response_message(status) if dummy_token? "#{status.capitalize} successfully!!" elsif @user.nil? - "Hey, it seems request you are trying to access is invalid. If you have any " + + "Hey, it seems request you are trying to access is invalid. If you have any " + "concerns about our newsletter's subscription, kindly get in touch with " + "hr@joshsoftware.com" else diff --git a/app/models/light/user.rb b/app/models/light/user.rb index 8309e65..fb0d354 100644 --- a/app/models/light/user.rb +++ b/app/models/light/user.rb @@ -4,7 +4,11 @@ class User include Mongoid::Slug include Mongoid::History::Trackable - NEW_USER = "new user" + STATUS = { + new_user: 'new_user', subscribed: 'Subscribed', unsubscribed: 'Unsubscribed', + block: 'Block', bounced: 'Bounced', spam: 'Spam', invalid: 'Invalid', + opt_in: 'Opt in mail sent' + } field :email_id, type: String field :username, type: String @@ -32,21 +36,21 @@ class User :user_agent, :is_subscribed, :unsubscribed_at] before_create do self.joined_on = Date.today - self.sidekiq_status = NEW_USER if self.sidekiq_status.blank? + self.sidekiq_status = STATUS[:new_user] if self.sidekiq_status.blank? self.token = Devise.friendly_token while User.where(token: self.token).present? self.token = Devise.friendly_token end end - scope :subscribed_users, -> { where(is_subscribed: true, sidekiq_status: 'Subscribed') } - scope :unsubscribed_users, -> { where(is_subscribed: false, sidekiq_status: 'Unsubscribed') } - scope :new_users, -> { where(is_subscribed: false, sidekiq_status: NEW_USER) } - scope :blocked_users, -> { where(is_subscribed: false, sidekiq_status: 'Block') } - scope :bounced_users, -> { where(is_subscribed: false, sidekiq_status: 'Bounced') } - scope :spam_users, -> { where(is_subscribed: false, sidekiq_status: 'Spam') } - scope :invalid_users, -> { where(is_subscribed: false, sidekiq_status: 'Invalid') } - scope :opt_in_users, -> { where(is_subscribed: false, sidekiq_status: 'Opt in mail sent') } + scope :subscribed_users, -> { where(is_subscribed: true, sidekiq_status: STATUS[:subscribed]) } + scope :unsubscribed_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:unsubscribed]) } + scope :new_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:new_user]) } + scope :blocked_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:block]) } + scope :bounced_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:bounced]) } + scope :spam_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:spam]) } + scope :invalid_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:invalid]) } + scope :opt_in_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:opt_in]) } def self.add_users_from_worksheet(worksheet, column = 1) fails = [] diff --git a/app/views/light/users/edit.html.haml b/app/views/light/users/edit.html.haml index a5a2d86..bc35051 100644 --- a/app/views/light/users/edit.html.haml +++ b/app/views/light/users/edit.html.haml @@ -1,15 +1,32 @@ = render 'light/newsletters/top_navbar' - -= simple_form_for (@user), html: {role: 'form', class: 'form-horizontal'} do |f| - %table - %tr - %td - = f.input :email_id, input_html:{class: 'span3'} - = f.input :username, input_html:{class: 'span3'} - = f.input :is_subscribed, :as => :select, input_html:{class: 'span3'} - = f.input :joined_on, input_html: { 'data-behaviour' => 'datepicker'}, input_html:{class: 'span3'} - = f.input :source, input_html:{class: 'span3'} - = f.button :submit, class: "btn btn-success controls" +%center + = simple_form_for (@user), html: {role: 'form', class: 'form-horizontal'} do |f| + %h2 Edit User + %br + %table + %tr + %td + = f.input :email_id, input_html: {class: 'span3'} + = f.input :username, input_html: {class: 'span3'} + = f.input :sent_on, label: 'Last Sent On', input_html: {class: 'span3', readonly: true}, value: @user.sent_on.last + = f.input :source, input_html: {class: 'span3', readonly: true} + = f.input :joined_on, input_html: { 'data-behaviour': 'datepicker'}, input_html:{class: 'span3', readonly: true} + = f.input :subscribed_at, input_html: {class: 'span3', readonly: true} + = f.input :unsubscribed_at, input_html: {class: 'span3', readonly: true} + = f.input :opt_in_mail_sent_at, input_html: {class: 'span3', readonly: true} + = f.input :sidekiq_status, as: :select, collection: Light::User::STATUS.values, label: 'Status', prompt: 'Select Status', selected: @user.sidekiq_status + %br + = f.label 'Blocked' + .make-switch{tabindex: 0, 'data-on': 'success', 'data-off': 'warning', 'data-on-label': 'Yes', 'data-off-label': 'No'} + = f.check_box :is_blocked, {}, 'true', 'false' + %br + %br + = f.label 'Subscribed' + .make-switch{tabindex: 0, 'data-on': 'success', 'data-off': 'warning', 'data-on-label': 'Yes', 'data-off-label': 'No'} + = f.check_box :is_subscribed, {}, 'true', 'false' + %br + %br + = f.button :submit, class: 'btn btn-success controls' :javascript $('[data-behaviour~=datepicker]').datepicker({ diff --git a/app/views/light/users/index.html.haml b/app/views/light/users/index.html.haml index ce54cf5..5328086 100644 --- a/app/views/light/users/index.html.haml +++ b/app/views/light/users/index.html.haml @@ -51,9 +51,9 @@ %td.text-center= '{{email_id}}' %td.text-center= '{{username}}' %td.text-center - %a{ :href => "/newsletter/users/{{_id.$oid}}/edit", :class => 'btn btn-sm btn-success'} Edit + %a{ :href => "/newsletter/users/{{id}}/edit", :class => 'btn btn-sm btn-success'} Edit - %a{ :href => "/newsletter/users/{{_id.$oid}}", data:{ :confirm => 'Are you sure?', method: 'delete'}, :class => 'btn btn-sm btn-danger' } Delete + %a{ :href => "/newsletter/users/{{id}}", data:{ :confirm => 'Are you sure?', method: 'delete'}, :class => 'btn btn-sm btn-danger' } Delete .col-lg-12.padding_none.pull-right #user_pagination.col-lg-12 diff --git a/app/workers/light/import_worker.rb b/app/workers/light/import_worker.rb index 3b3a522..fccd0bf 100644 --- a/app/workers/light/import_worker.rb +++ b/app/workers/light/import_worker.rb @@ -12,8 +12,13 @@ def perform(rows, email_id, source = "Business Card") rows.each do |row| email = "#{row[1]}" name = "#{row[0] || row[1]}" - user = Light::User.create(username: name, email_id: email, source: source, - is_subscribed: false, sidekiq_status: Light::User::NEW_USER) if email.present? or name.present? + user = Light::User.create( + username: name, + email_id: email, + source: source, + is_subscribed: false, + sidekiq_status: Light::User::STATUS[:new_user] + ) if email.present? or name.present? csv << [email, row[0], user.errors.messages] if user.present? and user.errors.present? end UserMailer.import_contacts_update(email_id, file_path).deliver