Sha256: da349bc8582fdf80edbe620cac1e9389a664fe4f94987926598268e81744e82d

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 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
          tmp_file = Rails.root.join("tmp").join(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

7 entries across 7 versions & 1 rubygems

Version Path
renalware-core-2.0.85 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.84 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.83 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.82 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.81 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.80 app/models/renalware/patients/sync_gps_via_file_download_job.rb
renalware-core-2.0.79 app/models/renalware/patients/sync_gps_via_file_download_job.rb