Class: Maintenance::PackingMaintenance

Inherits:
BaseService show all
Defined in:
app/services/maintenance/packing_maintenance.rb

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#processObject



3
4
5
6
7
8
9
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
# File 'app/services/maintenance/packing_maintenance.rb', line 3

def process
	PaperTrail.request(whodunnit: 'Maintenance::PackingMaintenance') do
    # Find packings without a relevant_md5 field populated, do 1000 at a time
     Packing.where(relevant_md5: nil).find_each do |packing|
       log_info "Calculating relevent item md5 hash and creating packing with this relevant md5 hash if not already present, for packing: #{packing.id}, md5: #{packing.md5}"
       # get an item hash from the packing i.e. hash with items as the keys and the item qty as the values
       packing_item_hash = extract_item_hash_from_packing_contents(packing.contents)
       if packing_item_hash.nil?
         # this means it's referencing an item that no longer exists, delete the packing and move on!
         log_error "Ooops, issue with packing: #{packing.id}, deleting the packing!"
         packing.destroy
       else
         begin
           # process it through our Md5HashItem service to find relevant md5 hash
           md5_result = Shipping::Md5HashItem.process(packing_item_hash)
           # update this packing to mark it as 'processed'
           packing.update_column(:relevant_md5, md5_result.md5)
           if packing.md5 == md5_result.relevant_md5
             # here we have no difference, so move along
             log_info "Relevant md5 is the same as the packing md5, updated packing: #{packing.id}, nothing more to do"
           else
             # here we do have a difference so see if we have a packing with this relevant_md5
             Packing.where(md5: md5_result.relevant_md5, service_type: packing.service_type).first_or_create! do |relevent_items_packing|
               # we don't have an existing one, so create one and populate it
               relevent_items_packing.packdims = packing.packdims
               relevent_items_packing.delivery = packing.delivery
               relevent_items_packing.origin = packing.origin
               relevent_items_packing.contents = md5_result.relevant_full_item_id_hash
               relevent_items_packing.created_at = packing.created_at
               relevent_items_packing.item_ids |= md5_result.relevant_kit_item_ids
               relevent_items_packing.save!
             end
           end
         rescue StandardError => exc
           log_error "Error processing packing id #{packing.id} - #{exc.backtrace.join("\n")}"
           ErrorReporting.error(exc, { packing_id: packing.id })
         end
       end
     end
	end
end