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.0.147 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.146 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.145 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.144 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.143 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.142 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.141 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.140 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.139 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.138 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.137 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.136 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.135 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.134 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.133 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.132 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.131 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.130 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.129 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.128 app/models/renalware/patients/sync_gps_via_file_download_job.rb