Class: Auth::Work::Communication

Inherits:
Object
  • Object
show all
Includes:
Concerns::ImageLoadConcern, Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/auth/work/communication.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_cart_itemObject

Returns the value of attribute _cart_item.



16
17
18
# File 'app/models/auth/work/communication.rb', line 16

def _cart_item
  @_cart_item
end

#_cycleObject

Returns the value of attribute _cycle.



21
22
23
# File 'app/models/auth/work/communication.rb', line 21

def _cycle
  @_cycle
end

#_cycle_indexObject

Returns the value of attribute _cycle_index.



22
23
24
# File 'app/models/auth/work/communication.rb', line 22

def _cycle_index
  @_cycle_index
end

#_indexObject

the communications own index in the instruction, or whatever is its parent.



19
20
21
# File 'app/models/auth/work/communication.rb', line 19

def _index
  @_index
end

#_instructionObject

since we already find the cart item, instruction, cycle or product at the beginning, will store these as attr accessors, not naming as instruction/cycle/cart_item/product because i don’t want to interfere with mongoid, which ideally should be setting these , but is not.



14
15
16
# File 'app/models/auth/work/communication.rb', line 14

def _instruction
  @_instruction
end

#_instruction_indexObject

Returns the value of attribute _instruction_index.



15
16
17
# File 'app/models/auth/work/communication.rb', line 15

def _instruction_index
  @_instruction_index
end

#_productObject

Returns the value of attribute _product.



20
21
22
# File 'app/models/auth/work/communication.rb', line 20

def _product
  @_product
end

Class Method Details

.find_communication(arguments) ⇒ Object

CLASS METHODS.



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/models/auth/work/communication.rb', line 344

def self.find_communication(arguments)
  communication_id = arguments[:communication_id]
    instruction_id = arguments[:instruction_id]
    cart_item_id = arguments[:cart_item_id]
    if communication_id && instruction_id && cart_item_id
        if cart_item = Auth.configuration.cart_item_class.constantize.find(cart_item_id)
          instruction = nil
          instruction_index = nil
          communication = nil
          communication_index = nil

          cart_item.instructions.each_with_index{|value,key|
           if value.id.to_s == instruction_id
             instruction = value
             instruction_index = key
           end
          }

          instruction.communications.each_with_index{|value,key|

           if value.id.to_s == communication_id
             communication = value
             communication_index = key
           end

          }

          instruction.cart_item_id = cart_item_id
          communication._instruction = instruction
          communication._cart_item = cart_item
          communication._instruction_index = instruction_index
          communication._index = communication_index

          return communication
        end
    elsif communication_id && instruction_id && product_id

    end 
end

Instance Method Details

#cycleObject

now give the view to show this



34
# File 'app/models/auth/work/communication.rb', line 34

embedded_in :cycle, :class_name => "Auth::Work::Cycle", :polymorphic => true

#decrement_repeated_timesObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/auth/work/communication.rb', line 165

def decrement_repeated_times
  
  if self._product
    Auth.configuration.product_class.constantize.where({
    "instructions.#{self._instruction_index}.communications.#{self._index}._id" => BSON::ObjectId(self.id.to_s) 
    }).find_one_and_update(
      {
        "$inc" => {
          "instructions.#{self._instruction_index}.communications.#{self._index}.repeated_times" => 1
        }
      },
      {
        :return_document => :after
      }
    )
  elsif self._cart_item
    Auth.configuration.cart_item_class.constantize.where({
        "instructions.#{self._instruction_index}.communications.#{self._index}._id" => BSON::ObjectId(self.id.to_s) 
      }).find_one_and_update(
      {
        "$inc" => {
          "instructions.#{self._instruction_index}.communications.#{self._index}.repeated_times" => 1
        }
      },
      {
        :return_document => :after
      }
    )
  elsif self._cycle
    #coll = Auth.configuration.cycle_class.constantize.collection
  end
  
end

#defer_jobObject

@return : will return false if there is no method defined which can help to determine if the job should be done or deferred. Will otherwise call that method, and return its result. that method is expected to return a Time object that will be used by the communicationJob to requeue itself.



241
242
243
244
# File 'app/models/auth/work/communication.rb', line 241

def defer_job
  return false if self.method_to_determine_defer_job.nil?
  self.send("#{self.method_to_determine_defer_job}",self.id.to_s)
end

#deliver_allObject

@return : returns a time object at which this job should be reenqueud. If it does not return a time object, then the job is not to be reenqueud, and basically was done immediately.



247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/auth/work/communication.rb', line 247

def deliver_all
  defer_job_result = defer_job
  unless defer_job_result == false
        return defer_job_result
  end
  set_recipients
  deliver_email
  deliver_sms
  deliver_mobile_notification
  enqueue_repeat
  return nil
end

#deliver_emailObject



264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/models/auth/work/communication.rb', line 264

def deliver_email
  return unless self.send_email == "on"
  self.recipients[:users].each do |recipient|
    puts "checking email recipient."
    puts recipient.email.to_s
    puts recipient.confirmed_at.to_s
    if recipient.email && recipient.confirmed_at
      puts "sending email."
      Auth::SendMail.send_email({to: recipient.email, subject: get_parent_object.get_email_subject, content: get_parent_object.get_email_content, link: get_parent_object.get_link}).deliver
    end
  end
end

#deliver_mobile_notificationObject



331
332
333
# File 'app/models/auth/work/communication.rb', line 331

def deliver_mobile_notification

end

#deliver_smsObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'app/models/auth/work/communication.rb', line 277

def deliver_sms 
  self.recipients[:users].each do |recipient|
    if recipient.
      ###################################################
      ## SET NUMBER
      ###################################################
      to_number = recipient.
        
      ###################################################
      ## SET TEMPLATE NAME
      ###################################################
      template_name = Auth.configuration.two_factor_otp_transactional_sms_template_name
      
      ###################################################
      ##
      ##
      ## SET VAR HASH
      ##
      ##
      ###################################################
      var_hash = {:var2 => get_parent_object.get_link, :var1 => get_parent_object.get_sms_content}
      
      ########################################################
      ## SET TEMPLATE SENDER ID
      #######################################################  
      template_sender_id = Auth.configuration.two_factor_otp_transactional_sms_template_sender_id

      ########################################################
      ## ONLY SEND THE SMS IF THE VAR HASH CONTAINS DATA.
            
      url = "https://2factor.in/API/R1/?module=TRANS_SMS"
  
      params = {
        apikey: Auth.configuration.third_party_api_keys[:two_factor_sms_api_key],
        to: to_number,
        from: template_sender_id,
        templatename: template_name,
      }.merge(var_hash)
      
      request = Typhoeus::Request.new(
        url,
        params: params,
        timeout: 10
      )

      response = request.run
        
    end
  end
end

#enqueue_at_timeObject

enqueue_at_time this is not a permitted parameters.



114
# File 'app/models/auth/work/communication.rb', line 114

field :enqueue_at_time, type: Time

#enqueue_repeatObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/models/auth/work/communication.rb', line 199

def enqueue_repeat
  if self.repeated_times  < self.repeat_times
    if self.repeat
      if self.decrement_repeated_times
        puts "repeated times"
        puts self.repeated_times
        puts "repeat times."
        puts self.repeat_times
      
      
        
        enqueue_repeat_at = nil
        
        case self.repeat
        when "Daily"
          enqueue_repeat_at = Time.now + 1.day
        when "Weekly"
          enqueue_repeat_at = Time.now + 1.week
        when "Monthly"
          enqueue_repeat_at = Time.now + 1.month
        when "Yearly"
          enqueue_repeat_at = Time.now + 1.year
        when "Half-Monthly"
          enqueue_repeat_at = Time.now + 6.months
        end

        args = {}
        args[:instruction_id] = self._instruction.id.to_s if self._instruction
        args[:communication_id] = self.id.to_s
        args[:cart_item_id] = self._cart_item.id.to_s  if self._cart_item
        args[:product_id] = self._product.id.to_s  if self._product
        args[:cycle_id] = self._cycle.id.to_s  if self._cycle

        CommunicationJob.set(wait_until: enqueue_repeat_at).perform_later(args)
      end
    end
  end
end

#get_parent_objectObject



260
261
262
# File 'app/models/auth/work/communication.rb', line 260

def get_parent_object
  self._instruction || self._cycle
end

#instructionObject

add embedded in products.



37
# File 'app/models/auth/work/communication.rb', line 37

embedded_in :instruction, :class_name => "Auth::Work::Instruction", :polymorphic => true

#method_to_determine_communication_timingObject

this is called to determine when to send this communication.



110
# File 'app/models/auth/work/communication.rb', line 110

field :method_to_determine_communication_timing, type: String

#method_to_determine_recipientsObject

this method is called on self. and the self id is passed as an argument. eg : self.send(“instruction.cart_item.get_recipients”,self.id.to_s)



82
# File 'app/models/auth/work/communication.rb', line 82

field :method_to_determine_recipients, type: String

#recipientsObject

this is got by calling the above method. this is not a permitted parameter. so this consists of what exactly ? the hash is checked for the existence of two keys.

how to configure notification repeats ? for eg someone books a 3 times a year diabetes plan. when should the notifications be set ? how to configure that ?



93
# File 'app/models/auth/work/communication.rb', line 93

field :recipients, type: Hash

#repeatObject

“daily,weekly,monthly,yearly”



104
# File 'app/models/auth/work/communication.rb', line 104

field :repeat, type: String

#repeat_optionsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/auth/work/communication.rb', line 150

def repeat_options
  [
    ["Daily","Daily"],
    ["Weekly","Weekly"],
    ["Monthly","Monthly"],
    ["Yearly","Yearly"],
    ["Half-Monthly","Half-Monthly"],
    ["Saturday","Saturday"],
    ["Sunday","Sunday"],
    ["Monday","Monday"],
    ["Tuesday","Tuesday"],
    ["Wednesday","Wednesday"]
  ]
end

#repeat_timesObject

how many times to repeat it.



107
# File 'app/models/auth/work/communication.rb', line 107

field :repeat_times, type: Integer, default: 1

#repeated_timesObject

how many times was this notification repeated till now?



117
# File 'app/models/auth/work/communication.rb', line 117

field :repeated_times, type: Integer, default: 0

#set_enqueue_atObject



141
142
143
144
145
146
147
148
# File 'app/models/auth/work/communication.rb', line 141

def set_enqueue_at
  if self.method_to_determine_communication_timing.nil?
    self.enqueue_at_time = Time.now 
  else
    self.enqueue_at_time = self.send("#{self.method_to_determine_communication_timing}",self.id.to_s)
  end
  self.enqueue_at_time
end

#set_recipientsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/auth/work/communication.rb', line 119

def set_recipients
  self.recipients = {:users => [],:topics => []}
  if self.method_to_determine_recipients.nil?
    puts "no method to determine recipients."
    if self._instruction 
      if self._cart_item
        resource_id = self._cart_item.resource_id
        resource_class = self._cart_item.resource_class
        self.recipients[:users] << resource_class.constantize.find(resource_id)
      elsif self.product_id
        resource_id = self._product.resource_id
        resource_class = self._product.resource_class
        self.recipients[:users] << resource_class.constantize.find(resource_id)
      end
    else
      puts "no instruction found."
    end   
  else
    self.recipients = self.send("#{self.method_to_determine_recipients}",self.id.to_s)
  end
end