class Multiwoven::Integrations::Destination::Sftp::Client

def write(sync_config, records, _action = "insert")

def write(sync_config, records, _action = "insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  file_path = generate_file_path(sync_config)
  local_file_name = generate_local_file_name(sync_config)
  csv_content = generate_csv_content(records)
  write_success = 0
  write_failure = 0
  Tempfile.create([local_file_name, ".csv"]) do |tempfile|
    tempfile.write(csv_content)
    tempfile.close
    with_sftp_client(connection_config) do |sftp|
      sftp.upload!(tempfile.path, file_path)
      write_success += records.size
    rescue StandardError => e
      handle_exception("SFTP:RECORD:WRITE:EXCEPTION", "error", e)
      write_failure += records.size
    end
  end
  tracking_message(write_success, write_failure)
rescue StandardError => e
  handle_exception(
    "SFTP:WRITE:EXCEPTION",
    "error",
    e
  )
end