Sha256: 9413560aee96c3a40b56e36b489f2d37affafd044551c9020c7d14cb7aacf088

Contents?: true

Size: 2 KB

Versions: 79

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/patients"

module Renalware
  module Patients
    class SyncGpsViaFileDownloadJob < ApplicationJob
      ODS_DOWNLOADABLES = {
        primary_care_physicians: {
          url: "https://files.digital.nhs.uk/assets/ods/current/egpcur.zip",
          filename: "egpcur.zip"
        },
        practice_memberships: {
          url: "https://files.digital.nhs.uk/assets/ods/current/epracmem.zip",
          filename: "epracmem.zip"
        }
      }.freeze

      # Do not retry this job
      discard_on(StandardError) do |_job, exception|
        Renalware::Engine.exception_notifier.notify(exception)
        Rails.logger.error exception
      end

      # Now we have brought practices up to date, update GPs and their membership in practices.
      # Download the relevant zip file, save it as a Feeds::File (the zip file will be copied to
      # the uploads dir) and kick off a sync job to import the data, which it does by unzipping
      # the file, extracting the csv and using a PG function to upsert the contents.
      # Note we cannot reliably use an aysnc job here as the order of execution of the 3 updates -
      # practices, gps, practice memberships - is strict.
      def perform
        ODS_DOWNLOADABLES.each do |type, options|
          # Download the ODS file to a temporary location - must be /tmp dir on linux
          # in order for PG to have access - hence using Tempfile
          tmp_file = Pathname(Tempfile.new(options[:filename]))
          `wget -O #{tmp_file} #{options[:url]}`
          file = create_feed_file(type, tmp_file)
          Renalware::Feeds::Files::ImportJobFactory.job_class_for(file.file_type).perform_now(file)
        end
      end

      private

      def create_feed_file(type, tmp_file)
        Renalware::Feeds::Files::CreateFeedFile.call(
          uploaded_file: tmp_file,
          file_type: Renalware::Feeds::FileType.find_by(name: type),
          user: Renalware::SystemUser.find
        )
      end
    end
  end
end

Version data entries

79 entries across 79 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.1.0 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.167 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.166 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.165 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.164 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.163 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.162 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.161 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.160 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.159 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.158 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.157 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.156 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.155 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.153 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.152 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.151 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.149 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.148 app/models/renalware/patients/sync_gps_via_file_download_job.rb