Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

отметки пользователей с 207 в sms #336

Merged
merged 4 commits into from
Oct 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/admin/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
column :adm_region, :sortable => :adm_region_id
column :region, :sortable => :region_id
column :full_name
column :phone
column :phone do |user|
%Q(<span #{user.wrong_phone ? 'style="color: red;"' : ''}>#{user.phone}</span>).html_safe
end
column :email
column :uic, :sortable => 'user_apps.uic'

Expand Down Expand Up @@ -131,6 +133,7 @@
filter :user_app_uic_matcher, as: :string, label: '№ УИК'
filter :full_name
filter :phone
filter :wrong_phone
filter :email
filter :user_app_created_at, as: :date_range, label: 'Дата подачи заявки'
filter :created_at, label: 'Дата создания'
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ def group_email(emails, subject, body)
mail(to: emails, subject: subject, body: body)
end

def sms_results email, failed
@failed = failed
mail(to: email, subject: 'Failed sms messages')
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def email_required?; false end
after_create :send_sms_with_password, :if => :may_login?

before_save :reset_role_cache
before_save :reset_wrong_phone, if: :phone_changed?

accepts_nested_attributes_for :user_current_roles, allow_destroy: true

Expand Down Expand Up @@ -211,6 +212,11 @@ def reset_role_cache
@has_role_cache = {}
end

def reset_wrong_phone
self.wrong_phone = false
true
end

def mark_user_app_state
if user_app.present?
user_app.approve! unless user_app.approved?
Expand Down
5 changes: 4 additions & 1 deletion app/services/sms_mass_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def self.perform(*args)
results[phone] = SmsService.send_message(phone, message)
end

WorkLog.find_by(id: params['work_log_id']).try(:complete!, results.to_json)
log = WorkLog.find_by id: params['work_log_id']
log.try :complete!, results.to_json
failed = results.count{|k,v| v == SmsService::ERRORS['207'] || v == '207'}
UserMailer.sms_results(log.user.email, failed).deliver if log && failed > 0
end
end
6 changes: 4 additions & 2 deletions app/services/sms_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SmsService

def self.send_message(number, text)
result = provider.send to: number, from: AppConfig['smsru_from'], text: text
User.find_by_phone(number).try :update_attributes, wrong_phone: (result == '207')
ERRORS[result] || result
end

Expand All @@ -55,9 +56,10 @@ def self.send_message_with_worklog(number, text, worklog_text = nil)
:params => options.to_json
#Resque.enqueue(SmsMassSender, options.merge(work_log_id: worklog.id))
results = {}
results[number] = provider.send to: number, from: AppConfig['smsru_from'], text: text
results[number] = send_message number, text

worklog.try(:complete!, results.to_json)
ERRORS[results[number]] || results[number]
results[number]
end

def self.provider
Expand Down
8 changes: 4 additions & 4 deletions app/views/user_apps/new_group_sms.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- phones = @users.pluck(:phone)
- phones = @users.pluck(:phone, :wrong_phone)
- count = phones.count
- display_limit = 200
.row
Expand All @@ -7,13 +7,13 @@
СМС будет направлено #{count} получателям
-if count > display_limit
(отображаются первые #{display_limit} из них)
- phones[0..display_limit-1].each do |phone|
= phone
- phones[0..display_limit-1].each do |phone, wrong|
%span{style: "color: #{wrong ? 'red' : ''}" }= phone
.span9
%legend
Содержимое смс
= form_for :group_sms, url: :send_group_sms, html: {class: 'form-horizontal'} do |f|
- phones.each do |phone|
- phones.each do |phone, wrong|
= f.hidden_field :phones, :multiple => true, :value => phone
.control-group
= f.label 'Сообщение', class: 'control-label'
Expand Down
2 changes: 2 additions & 0 deletions app/views/user_mailer/sms_results.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
= "Не удалось доставить #{@failed} СМС из-за неправильного номера"
= link_to "Результаты", control_users_url( q: { wrong_phone_eq: 1} )
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ ru:
password: Пароль
password_confirmation: Подтверждение пароля
phone: Телефон
wrong_phone: Неверный телефон
region: "Район"
adm_region: "Адм. округ"
created_at: Создано
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20131014160456_add_wrong_phone_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddWrongPhoneToUsers < ActiveRecord::Migration
def change
add_column :users, :wrong_phone, :boolean, null: false, default: false
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130907221736) do
ActiveRecord::Schema.define(version: 20131014160456) do

create_table "active_admin_comments", force: true do |t|
t.string "namespace"
Expand Down Expand Up @@ -263,7 +263,7 @@

create_table "users", force: true do |t|
t.string "email"
t.string "encrypted_password", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
Expand Down Expand Up @@ -291,6 +291,7 @@
t.text "address"
t.string "full_name", limit: 767
t.string "unique_session_id", limit: 20
t.boolean "wrong_phone", default: false, null: false
end

add_index "users", ["adm_region_id"], name: "index_users_on_adm_region_id", using: :btree
Expand Down
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
email {generate :email}
password 'password'
phone { generate :phone}
wrong_phone false

factory :user_with_role do
ignore do
Expand Down
8 changes: 8 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,13 @@

end

it 'resets wrong_phone on phone change' do
user = create :user
user.update_attribute :wrong_phone, true
user.phone = user.phone.succ
user.save
user.wrong_phone.should == false
end

end

6 changes: 6 additions & 0 deletions spec/services/sms_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
@provider.should_receive(:send).with(to: '555666', from: '79037701262', text: 'Hello').and_return('204')
expect(SmsService.send_message('555666', 'Hello')).to eq(SmsService::ERRORS['204'])
end

it 'sets user wrong_phone to true if service returned 207' do
user = create :user
@provider.should_receive(:send).with(to: user.phone, from: '79037701262', text: 'Hello').and_return('207')
expect{ SmsService.send_message(user.phone, 'Hello') }.to change{ user.reload.wrong_phone }.from(false).to(true)
end
end