lib/eco/api/session/batch/job.rb in eco-helpers-2.0.22 vs lib/eco/api/session/batch/job.rb in eco-helpers-2.0.23

- old
+ new

@@ -162,16 +162,21 @@ # - for success requests, it consolidates the associated entries (see `Ecoportal::API::V1::Person#consolidate!`) # - launches specific error handlers, if there were **errors** from the Server as a result of the `batch.launch`, and there are `Error::Handlers` defined # @return [Eco::API::Session::Batch::Status] def launch(simulate: false) pqueue = processed_queue - @requests = pqueue.map {|e| as_update(e)} + @requests = as_update(pqueue) pre_checks(requests, simulate: simulate) - unless simulate + if simulate + if options.dig(:requests, :backup) + req_backup = as_update(pqueue, add_feedback: false) + backup_update(req_backup, simulate: simulate) + end + else if pqueue.length > 0 - req_backup = pqueue.map {|e| as_update(e, add_feedback: false)} + req_backup = as_update(pqueue, add_feedback: false) backup_update(req_backup) session.batch.launch(pqueue, method: type).tap do |job_status| @status = job_status status.root = self status.errors.print @@ -218,17 +223,30 @@ [].tap do |msg| subjobs.map {|subjob| msg << subjob.summary} end.join("\n") end - def as_update(*args) - feedback.as_update(*args) + def as_update(data, *args) + if data.is_a?(Array) + data.map do |e| + feedback.as_update(e, *args) + end.compact.select {|e| e && !e.empty?} + else + feedback.as_update(data, *args) + end end def processed_queue @queue.each {|e| @callbacks[e].call(e) if @callbacks.key?(e) } - apply_policies(api_included(@queue)).select {|e| !as_update(e).empty?} + apply_policies(api_included(@queue)).select do |e| + !as_update(e).empty? + end.select do |e| + next true unless e.is_a?(Ecoportal::API::V1::Person) + next true unless e.new? + # new people should either have account or details + e.account || e.details + end end # if there is a config definition to exclude entries # and the current batch is not a creation batch # - filter out excluded entries from the api update @@ -305,12 +323,13 @@ end end end # Keep a copy of the requests for future reference - def backup_update(requests) + def backup_update(requests, simulate: false) + dry_run = simulate ? "_dry_run" : "" dir = config.people.requests_folder - file = File.join(dir, "#{type}_data.json") + file = File.join(dir, "#{type}_data#{dry_run}.json") file_manager.save_json(requests, file, :timestamp) end # Adds a job tied to the current job # Used with error handlers that need their own job to run