From 4d6ff768c49fefc218e681f5fe329efcbf702fd3 Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Wed, 2 Nov 2022 10:30:41 +0100 Subject: [PATCH] =?UTF-8?q?Ajoute=20les=20dossiers=20d=C3=A9pos=C3=A9s=20d?= =?UTF-8?q?ans=20les=20statistiques=20Datagouv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cron/datagouv/file_depose_by_month_job.rb | 19 +++++++++++++++++++ .../datagouv/file_depose_by_month_job_spec.rb | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/jobs/cron/datagouv/file_depose_by_month_job.rb create mode 100644 spec/jobs/cron/datagouv/file_depose_by_month_job_spec.rb diff --git a/app/jobs/cron/datagouv/file_depose_by_month_job.rb b/app/jobs/cron/datagouv/file_depose_by_month_job.rb new file mode 100644 index 000000000..1083d6c28 --- /dev/null +++ b/app/jobs/cron/datagouv/file_depose_by_month_job.rb @@ -0,0 +1,19 @@ +class Cron::Datagouv::FileDeposeByMonthJob < Cron::CronJob + include DatagouvCronSchedulableConcern + self.schedule_expression = "every month at 3:00" + FILE_NAME = "nb_dossiers_deposes_par_mois" + + def perform(*args) + GenerateOpenDataCsvService.save_csv_to_tmp(FILE_NAME, data) do |file| + begin + APIDatagouv::API.upload(file, :statistics_dataset) + ensure + FileUtils.rm(file) + end + end + end + + def data + Dossier.where(depose_at: 1.month.ago.beginning_of_month..1.month.ago.end_of_month).count + end +end diff --git a/spec/jobs/cron/datagouv/file_depose_by_month_job_spec.rb b/spec/jobs/cron/datagouv/file_depose_by_month_job_spec.rb new file mode 100644 index 000000000..b942692d6 --- /dev/null +++ b/spec/jobs/cron/datagouv/file_depose_by_month_job_spec.rb @@ -0,0 +1,19 @@ +RSpec.describe Cron::Datagouv::FileDeposeByMonthJob, type: :job do + let!(:dossier) { create(:dossier, created_at: 1.month.ago) } + let(:status) { 200 } + let(:body) { "ok" } + let(:stub) { stub_request(:post, /https:\/\/www.data.gouv.fr\/api\/.*\/upload\//) } + + describe 'perform' do + before do + stub + end + + subject { Cron::Datagouv::FileByMonthJob.perform_now } + + it 'send POST request to datagouv' do + subject + expect(stub).to have_been_requested + end + end +end