Class: Office::EmailConversation

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
lib/office/email_conversation.rb

Constant Summary collapse

STATE_UNREAD =
'state_unread'
STATE_READ =
'state_read'
STATES =
[ STATE_UNREAD, STATE_READ ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.in_emailtag(which) ⇒ Object



63
64
65
66
67
# File 'lib/office/email_conversation.rb', line 63

def self.in_emailtag which
  tag = WpTag.iso_get( which )
  email_conversation_tags = Office::EmailConversationTag.where({ wp_term_id: tag.id })
  where({ :id.in => email_conversation_tags.map(&:email_conversation_id) })
end

.not_in_emailtag(which) ⇒ Object



69
70
71
72
73
# File 'lib/office/email_conversation.rb', line 69

def self.not_in_emailtag which
  tag = WpTag.iso_get( which )
  email_conversation_tags = Office::EmailConversationTag.where({ wp_term_id: tag.id })
  where({ :id.nin => email_conversation_tags.map(&:email_conversation_id) })
end

Instance Method Details

#add_tag(which) ⇒ Object

Tested manually ok, does not pass the spec. @TODO: hire to make pass spec? vp 2023-03-07



39
40
41
42
43
44
45
46
# File 'lib/office/email_conversation.rb', line 39

def add_tag which
  tag = WpTag.iso_get which
  # puts!( tag.slug, "Adding tag" ) if DEBUG
  Office::EmailConversationTag.find_or_create_by!({
    email_conversation_id: id,
    wp_term_id:            tag.id,
  })
end

#apply_filter(filter) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/office/email_conversation.rb', line 76

def apply_filter filter
  case filter.kind

  when ::Office::EmailFilter::KIND_DESTROY_SCHS
    add_tag    ::WpTag::TRASH
    remove_tag ::WpTag::INBOX
    tmp_lead = ::Lead.where( email: self.part_txt.split("\n")[1] ).first
    if tmp_lead
      tmp_lead.schs.each do |sch|
        sch.update_attributes({ state: ::Sch::STATE_TRASH })
      end
    end

  when ::Office::EmailFilter::KIND_ADD_TAG
    add_tag filter.wp_term_id
    if ::WpTag::TRASH == ::WpTag.find( filter.wp_term_id ).slug
      remove_tag ::WpTag::INBOX
    end

  when ::Office::EmailFilter::KIND_REMOVE_TAG
    remove_tag filter.wp_term_id

  when ::Office::EmailFilter::KIND_AUTORESPOND_TMPL
    Ish::EmailContext.create({
      email_template: filter.email_template,
      lead_id:        lead.id,
      send_at:        Time.now + 22.minutes,
    })

  when ::Office::EmailFilter::KIND_AUTORESPOND_EACT
    ::Sch.create({
      email_action: filter.email_action,
      state:        ::Sch::STATE_ACTIVE,
      lead_id:      lead.id,
      perform_at:   Time.now + 22.minutes,
    })

  else
    raise "unknown filter kind: #{filter.kind}"
  end
end

#in_emailtag?(which) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/office/email_conversation.rb', line 58

def in_emailtag? which
  tag = WpTag.iso_get( which )
  email_conversation_tags.where({ wp_term_id: tag.id }).present?
end

#leadsObject

def lead_ids

email_conversation_leads.map( &:lead_id )

end field :lead_ids, type: :array, default: []



23
24
25
# File 'lib/office/email_conversation.rb', line 23

def leads
  Lead.find( lead_ties.map( &:lead_id ) )
end

#remove_tag(which) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/office/email_conversation.rb', line 48

def remove_tag which
  tag = WpTag.iso_get which
  # puts!( tag.slug, "Removing tag" ) if DEBUG
  Office::EmailConversationTag.where({
    email_conversation_id: id,
    wp_term_id:            tag.id,
  }).first&.delete
end

#rmtag(which) ⇒ Object



56
# File 'lib/office/email_conversation.rb', line 56

def rmtag which; remove_tag which; end

#tagsObject



34
35
36
# File 'lib/office/email_conversation.rb', line 34

def tags
  WpTag.find( email_conversation_tags.map( &:wp_term_id ) )
end

#wp_term_idsObject

@TODO: remove vp 2023-09-23



30
31
32
# File 'lib/office/email_conversation.rb', line 30

def wp_term_ids ## @TODO: remove _vp_ 2023-09-23
  email_conversation_tags.map( &:wp_term_id )
end