Skip to content

Instantly share code, notes, and snippets.

@macejmic
Created July 26, 2018 08:22
Show Gist options
  • Save macejmic/31ef97e3f89f7877c874efaf167ffc6c to your computer and use it in GitHub Desktop.
Save macejmic/31ef97e3f89f7877c874efaf167ffc6c to your computer and use it in GitHub Desktop.
class Broker < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :confirmable, :lockable, :async
mount_uploader :photo, PhotoUploader
has_many :offers, dependent: :destroy
has_many :reviews, dependent: :destroy
has_many :robot_offers, dependent: :destroy
has_many :notes, dependent: :destroy
has_one :assistant, dependent: :destroy, class_name: 'BrokerAssistant'
accepts_nested_attributes_for :reviews, :robot_offers, :assistant, allow_destroy: true
attr_accessor :gdpr_personal_data
validates :first_name, presence: true
validates :surname, presence: true
validates :email, presence: true
validates :phone, presence: true
validates :password, presence: true, allow_blank: true
validates :gdpr_personal_data, inclusion: { in: %w(1) }, allow_blank: true
scope :ordered, -> { order(created_at: :desc) }
scope :active, -> { where(active_robot: true) }
scope :shadows, -> { where(robot: true) }
scope :with_robot, -> { joins(:robot_offers) }
scope :not_me, ->(broker) { where.not(id: broker.id) }
scope :with_region, ->(region) { where('regions @> ?', "{#{region}}") }
scope :from_date, ->(date) {
where(Broker.arel_table[:created_at].gt(date))
}
scope :with_credit, -> {
where('credit >= ?', BrokerServices::ReduceCredit::OFFER_CREDIT)
}
end
class DemandServices::Finish < BaseService
object :demand
def execute
@winning_offer = demand.offers.by_percentage.first
finish_offer!
send_email_to_broker!
@winning_offer
end
private
def finish_offer!
OfferServices::Finish.run! offer: @winning_offer
end
def send_email_to_broker!
BrokerMailer.new_contact_request(@winning_offer).deliver_now
end
end
class VerificationCodeServices::Create < BaseService
object :demand
def execute
@verification_code = VerificationCode.new params
send_sms! if @verification_code.save
@verification_code
end
private
def send_sms!
SmsServices::Send.run! demand: demand, verification_code: @verification_code
end
def params
{
demand: demand,
code: code
}
end
def code
Rails.env.development? ? 'aaaa' : generate_random_code
end
private
def generate_random_code
rand.to_s[2..5]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment