Class: InspectionUploadWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/inspection_upload_worker.rb

Instance Method Summary collapse

Instance Method Details

#generate_report(property_id, report_type, export_type, current_user_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/workers/inspection_upload_worker.rb', line 76

def generate_report(property_id, report_type, export_type,current_user_id)
  params = {
    "property_id" => property_id,
    "report_type" => report_type,
    "export_type" => export_type,
    "created_by_id" => current_user_id
  }
  @report_request = ReportRequest.new(params)
  if @report_request.save
    ::ReportWorker.perform_async(@report_request.id)
  end
end

#perform(inspection_params_array, current_user_id) ⇒ Object



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
# File 'app/workers/inspection_upload_worker.rb', line 6

def perform(inspection_params_array,current_user_id)
  begin

    # save_data_to_json_file(inspection_params_array, inspection_params_array[0]["property_id"])

    property  = Property.find_by_id(inspection_params_array[0]["property_id"])

    InspectionDetail.where(property_id: inspection_params_array[0]["property_id"]).delete_all
    property["properties"]["defect_status"] = "Not Uploaded"
    property.save

    inspection_params_array.each do |data|
      if InspectableItem.where(id: data["inspectable_item_id"]).present?
        store_inspection_details(data,current_user_id)
      end
    end

    property["properties"]["defect_status"] = "Uploaded"
    property.save
    # generate_report(property.id, 'all_nspire', "zip", current_user_id)
    Sentry.capture_message("Inspections Uploaded Successfully! #{inspection_params_array[0]["property_id"]} #{property.property_name}", level: :log, extra: { data: inspection_params_array })

  rescue => exception
    property["properties"]["defect_status"] = "Error"
    property.save
    Sentry.capture_message("Error while uploading Inspections #{inspection_params_array[0]["property_id"]} #{property.property_name}", level: :log, extra: { data: inspection_params_array })
  end
end

#save_data_to_json_file(data, property_id) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/workers/inspection_upload_worker.rb', line 89

def save_data_to_json_file(data, property_id)
  # Create a directory for PDFs if it doesn't exist
  pdfs_dir = Rails.root.join('pdfs')
  FileUtils.mkdir_p(pdfs_dir) unless File.directory?(pdfs_dir)

  # Generate a unique filename for the JSON file
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  filename = "#{property_id}_#{timestamp}.json"

  # Build the full path to the JSON file
  json_file_path = File.join(pdfs_dir, filename)

  # Write the data to the JSON file
  File.open(json_file_path, 'w') do |file|
    file.write(data.to_json)
  end
end

#store_inspection_details(data, current_user_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/workers/inspection_upload_worker.rb', line 35

def store_inspection_details(data,current_user_id)
  properties = {}
  properties[:level] = data["level"] && !data["level"].to_f.zero? ? 'L' + data["level"].to_s : nil
  properties[:status] = data["status"] || 'N/A'
  properties[:image_name] = data["image_name"] || nil
  properties[:defect_date] = data["updated_ts"] || ''
  properties[:local_inspection_details_id] = data["id"] || ''

  inspection_details = InspectionDetail.new do |inspection_detail|
    inspection_detail.property_id = data["property_id"]
    inspection_detail.building_id = nil
    inspection_detail.unit_id = nil
    if(data["building_id"].present? && data["building_id"] != 0 && data["building_id"] != -1)
      building = Building.where('id=? OR app_id=?', data["building_id"], data["building_id"])
      if building.nil?
        building = Building.where('old_id=? OR app_id=?', data["building_id"], data["building_id"])
      end
      building_id = building.present? ? building.first.id : nil
      return if building_id.nil?
      inspection_detail.building_id = building_id
    end
    if(data["unit_id"].present? && data["unit_id"] != 0 &&  data["unit_id"] != -1)
      unit = Unit.where('id=? OR app_id=?', data["unit_id"], data["unit_id"])
      if unit.nil?
        unit = Unit.where('old_id=? OR app_id=?', data["unit_id"], data["unit_id"])
      end
      unit_id = unit.present? ? unit.first.id : nil
      return if unit_id.nil?
      inspection_detail.unit_id = unit_id
    end
    inspection_detail.inspectable_item_id = data["inspectable_item_id"]
    inspection_detail.deficiency_definition_id = data["deficiency_id"].present? && data["deficiency_id"] != -1 ? data["deficiency_id"] : nil
    inspection_detail.comments = data["comments"]
    inspection_detail.location = data["location"]
    inspection_detail.properties = properties
    inspection_detail.created_by_id = current_user_id
  end

  inspection_details.save
  inspection_details
end