Class: Customer::ResetOpenActivityFlag

Inherits:
BaseService show all
Defined in:
app/services/customer/reset_open_activity_flag.rb

Overview

Service object: reset open activity flag.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(options = {}) ⇒ Result

Returns counts of evaluated customers and contacts.

Parameters:

  • options (Hash) (defaults to: {})

    reset options

Options Hash (options):

  • employee_id (Integer, nil)

    restrict the reset to this sales rep's customers

Returns:

  • (Result)

    counts of evaluated customers and contacts



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/customer/reset_open_activity_flag.rb', line 10

def process(options = {})
  result = Result.new(options: options)
  employee_id = options[:employee_id]
  logger.debug("Resetting open activity flag", employee_id: employee_id)

  # Use the new centralized tagging system with join to tags/taggings tables
  update_statement = <<-EOS
	open_sales_activity =
	EXISTS(
		select a.id
		from activities a
		inner join activity_types at on at.id = a.activity_type_id
		inner join taggings tg on tg.taggable_id = at.id and tg.taggable_type = 'ActivityType'
		inner join tags t on t.id = tg.tag_id
		where
			a.party_id = parties.id
			and t.name IN ('sale', 'sales-call')
			and a.activity_result_type_id IS NULL
		)
  EOS
  customers = Customer.where(state: %w[lead prospect customer])
  customers = customers.where(primary_sales_rep_id: employee_id) if employee_id.present?
  logger.debug("Resetting customers", count: customers.count)
  res = customers.update_all(update_statement)
  logger.debug("Customers updated", count: res)
  result.customers_evaluated = res

  contacts = Contact.joins(:customer).merge(customers)
  logger.debug("Resetting contacts", count: contacts.count)
  res = contacts.update_all(update_statement)
  logger.debug("Contacts updated", count: res)
  result.contacts_evaluated = res

  rollup_update_statement = <<-EOS
	open_sales_activity = exists(select id from parties cnt where cnt.type = 'Contact' and cnt.customer_id = parties.id and cnt.open_sales_activity = true)
  EOS
  customers_rollup = customers.where(open_sales_activity: false)
  logger.debug("Rolling up contact flags to customers", count: customers_rollup.count)
  customers_rollup.update_all(rollup_update_statement)
  logger.debug("Customers updated from contacts", count: res)
  result.contacts_to_customers_rollup = res

  result.success = true
  result
end