From 25ad9da0fae217c2318d30c566c7963db0a2f350 Mon Sep 17 00:00:00 2001 From: Robert-Anthony Lee-Faison <108823963+leefaisonr@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:02:25 -0500 Subject: [PATCH] Format timestamp for mediaflux (#533) * Update to new date format Co-authored-by: Bess Sadler * Update time format Co-authored-by: Bess Sadler * Progress towards standardizing timestamps Co-authored-by: Bess Sadler * rubocop corrections --------- Co-authored-by: Bess Sadler --- .gitignore | 2 ++ app/models/project_mediaflux.rb | 11 +++++++++-- app/models/project_metadata.rb | 4 ++-- spec/controllers/projects_controller_spec.rb | 2 +- spec/factories/project.rb | 8 ++++---- .../mediaflux/http/create_asset_request_spec.rb | 8 ++++---- .../mediaflux/http/update_asset_request_spec.rb | 6 +++--- spec/models/project_mediaflux_spec.rb | 12 ++++++++++++ 8 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 14c6f261..55f65e3b 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,5 @@ node_modules # Do not check aterm.jar into the repo aterm.jar + +public diff --git a/app/models/project_mediaflux.rb b/app/models/project_mediaflux.rb index a8744a7a..0f5a4351 100644 --- a/app/models/project_mediaflux.rb +++ b/app/models/project_mediaflux.rb @@ -32,6 +32,13 @@ def self.update(project:, session_id:) Mediaflux::Http::UpdateAssetRequest.new(session_token: session_id, id: project.mediaflux_id, tigerdata_values: tigerdata_values).resolve end + # This method is used for transforming iso8601 dates to dates that MediaFlux likes + # Take a string like "2024-02-26T10:33:11-05:00" and convert this string to "22-FEB-2024 13:57:19" + def self.format_date_for_mediaflux(iso8601_date) + return if iso8601_date.nil? + Time.parse(iso8601_date).strftime("%e-%b-%Y %H:%M:%S").upcase + end + # Translates database record into mediaflux meta document. # This is where the XML payload is generated. # rubocop:disable Metrics/MethodLength @@ -47,9 +54,9 @@ def self.project_values(project:) data_user_read_only: project.metadata[:data_user_read_only], data_user_read_write: project.metadata[:data_user_read_write], departments: project.metadata[:departments], - created_on: project.metadata[:created_on], + created_on: format_date_for_mediaflux(project.metadata[:created_on]), created_by: project.metadata[:created_by], - updated_on: project.metadata[:updated_on], + updated_on: format_date_for_mediaflux(project.metadata[:updated_on]), updated_by: project.metadata[:updated_by], project_id: project.metadata[:project_id], storage_capacity: project.metadata[:storage_capacity_requested], diff --git a/app/models/project_metadata.rb b/app/models/project_metadata.rb index 483ab390..6cbfaf40 100644 --- a/app/models/project_metadata.rb +++ b/app/models/project_metadata.rb @@ -50,13 +50,13 @@ def project_timestamps timestamps = {} if project.metadata[:created_by].nil? timestamps[:created_by] = current_user.uid - timestamps[:created_on] = DateTime.now.strftime("%d-%b-%Y %H:%M:%S") + timestamps[:created_on] = Time.current.in_time_zone("America/New_York").iso8601 else timestamps[:created_by] = project.metadata[:created_by] timestamps[:created_on] = project.metadata[:created_on] timestamps[:updated_by] = current_user.uid - timestamps[:updated_on] = DateTime.now.strftime("%d-%b-%Y %H:%M:%S") + timestamps[:updated_on] = Time.current.in_time_zone("America/New_York").iso8601 end timestamps end diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index c1f60dc8..80071d7c 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -43,7 +43,7 @@ " #{project.metadata[:data_manager]}\n" \ " RDSS\n" \ " PRDS\n" \ - " #{project.metadata[:created_on]}\n" \ + " #{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:created_on])}\n" \ " #{project.metadata[:created_by]}\n" \ " abc-123\n" \ " 500 GB\n" \ diff --git a/spec/factories/project.rb b/spec/factories/project.rb index 1ed069d6..15a2f7ff 100644 --- a/spec/factories/project.rb +++ b/spec/factories/project.rb @@ -8,8 +8,8 @@ data_user_read_only { [] } data_user_read_write { [] } title { FFaker::Movie.title } - created_on { DateTime.now } - updated_on { DateTime.now } + created_on { Time.current.in_time_zone("America/New_York").iso8601 } + updated_on { Time.current.in_time_zone("America/New_York").iso8601 } project_id { nil } status { "pending" } storage_capacity { "500 GB" } @@ -28,9 +28,9 @@ directory: directory, title: title, description: "a random description", - created_on: created_on.strftime("%d-%b-%Y %H:%M:%S"), + created_on: created_on, created_by: FactoryBot.create(:user).uid, - updated_on: created_on.strftime("%d-%b-%Y %H:%M:%S"), + updated_on: updated_on, updated_by: FactoryBot.create(:user).uid, project_id: project_id, status: status, diff --git a/spec/models/mediaflux/http/create_asset_request_spec.rb b/spec/models/mediaflux/http/create_asset_request_spec.rb index 0c663dee..55f7d1e6 100644 --- a/spec/models/mediaflux/http/create_asset_request_spec.rb +++ b/spec/models/mediaflux/http/create_asset_request_spec.rb @@ -30,10 +30,10 @@ before do stub_request(:post, mediflux_url).to_return(status: 200, body: create_response, headers: {}) end - it "send the metadata to the server" do + it "sends the metadata to the server" do data_user_ro = FactoryBot.create :user data_user_rw = FactoryBot.create :user - created_on = DateTime.now + created_on = Time.current.in_time_zone("America/New_York").iso8601 project = FactoryBot.create :project, data_user_read_only: [data_user_ro.uid], data_user_read_write: [data_user_rw.uid], created_on: created_on tigerdata_values = ProjectMediaflux.project_values(project:) create_request = described_class.new(session_token: "secretsecret/2/31", name: "testasset", collection: false, tigerdata_values: tigerdata_values) @@ -46,7 +46,7 @@ expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{project.metadata[:data_manager]}") }).to have_been_made expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{project.metadata[:departments].first}") }).to have_been_made expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{project.metadata[:departments].last}") }).to have_been_made - expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{created_on.strftime("%d-%b-%Y %H:%M:%S")}") }).to have_been_made + expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{ProjectMediaflux.format_date_for_mediaflux(created_on)}") }).to have_been_made expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{project.metadata[:created_by]}") }).to have_been_made expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{data_user_ro.uid}") }).to have_been_made expect(a_request(:post, mediflux_url).with { |req| req.body.include?("#{data_user_rw.uid}") }).to have_been_made @@ -116,7 +116,7 @@ " #{project.metadata[:data_manager]}\n" \ " RDSS\n" \ " PRDS\n" \ - " #{project.metadata[:created_on]}\n" \ + " #{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:created_on])}\n" \ " #{project.metadata[:created_by]}\n" \ " abc-123\n" \ " 500 GB\n" \ diff --git a/spec/models/mediaflux/http/update_asset_request_spec.rb b/spec/models/mediaflux/http/update_asset_request_spec.rb index d6878a8f..dc3db9cd 100644 --- a/spec/models/mediaflux/http/update_asset_request_spec.rb +++ b/spec/models/mediaflux/http/update_asset_request_spec.rb @@ -46,7 +46,7 @@ it "sends the metadata to the server" do data_user_ro = FactoryBot.create :user data_user_rw = FactoryBot.create :user - updated_on = DateTime.now + updated_on = Time.current.in_time_zone("America/New_York").iso8601 project = FactoryBot.create :project, data_user_read_only: [data_user_ro.uid], data_user_read_write: [data_user_rw.uid], updated_on: updated_on tigerdata_values = ProjectMediaflux.project_values(project:) update_request = described_class.new(session_token: "secretsecret/2/31", id: "1234", tigerdata_values: tigerdata_values) @@ -57,7 +57,7 @@ expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{project.metadata[:data_manager]}") }).to have_been_made expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{project.metadata[:departments].first}") }).to have_been_made expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{project.metadata[:departments].last}") }).to have_been_made - expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{updated_on.strftime("%d-%b-%Y %H:%M:%S")}") }).to have_been_made + expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:updated_on])}") }).to have_been_made expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{project.metadata[:updated_by]}") }).to have_been_made expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{data_user_ro.uid}") }).to have_been_made expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("#{data_user_rw.uid}") }).to have_been_made @@ -84,7 +84,7 @@ " #{project.metadata[:data_sponsor]}\n" \ " #{project.metadata[:data_manager]}\n" \ " #{project.metadata[:updated_by]}\n" \ - " #{project.metadata[:updated_on]}\n" \ + " #{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:updated_on])}\n" \ " RDSS\n" \ " PRDS\n" \ " \n" \ diff --git a/spec/models/project_mediaflux_spec.rb b/spec/models/project_mediaflux_spec.rb index 52e2cf0a..0bb77dfb 100644 --- a/spec/models/project_mediaflux_spec.rb +++ b/spec/models/project_mediaflux_spec.rb @@ -26,6 +26,18 @@ allow(create_asset_request).to receive(:id).and_return("123") end + context "it formats dates correctly for MediaFlux" do + let(:project) { FactoryBot.build(:project, created_on: "2024-02-22T13:57:19-05:00", updated_on: "2024-02-26T13:57:19-05:00") } + it "formats created_on as expected" do + project_values = ProjectMediaflux.project_values(project: project) + expect(project_values[:created_on]).to eq "22-FEB-2024 13:57:19" + end + it "formats updated_on as expected" do + project_values = ProjectMediaflux.project_values(project: project) + expect(project_values[:updated_on]).to eq "26-FEB-2024 13:57:19" + end + end + it "creates a project namespace and collection and returns the mediaflux id" do mediaflux_id = described_class.create!(project: project, session_id: "test-session-token") expect(namespace_request).to have_received("error?")