Fix handling of date values in repositories [SCI-6551] ()

This commit is contained in:
Alex Kriuchykhin 2022-02-21 12:08:35 +01:00 committed by GitHub
parent 4877455e9c
commit 6b51b8a35c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 146 additions and 81 deletions

View file

@ -245,13 +245,13 @@ var DateTimeHelper = (function() {
>
<div class="start-time ${mode}"
data-current-datetime="${startDatetime}">
${dateInputField(startDate, startDateDataValue)}
${dateInputField(startDate, startDatetime)}
${timeInputField(startTime)}
</div>
<div class="separator"></div>
<div class="end-time ${mode}"
data-current-datetime="${endDatetime}">
${dateInputField(endDate, endDateDataValue)}
${dateInputField(endDate, endDatetime)}
${timeInputField(endTime)}
</div>
</div>

View file

@ -61,7 +61,8 @@ $.fn.dataTable.render.defaultRepositoryDateValue = function() {
};
$.fn.dataTable.render.RepositoryDateValue = function(data) {
return `<span data-datetime="${data.value.datetime}" data-date="${data.value.formatted}">${data.value.formatted}</span>`;
return `<span data-datetime="${data.value.datetime}"
data-date="${data.value.formatted}">${data.value.formatted}</span>`;
};
$.fn.dataTable.render.defaultRepositoryDateTimeValue = function() {

View file

@ -34,7 +34,7 @@ module Api
def update
value = update_inventory_cell_params[:value]
if @inventory_cell.value.data_changed?(value)
if @inventory_cell.value.data_different?(value)
@inventory_cell.value.update_data!(value, current_user)
render jsonapi: @inventory_cell, serializer: InventoryCellSerializer
else

View file

@ -68,7 +68,7 @@ module Api
inventory_cells_params.each do |cell_params|
cell = @inventory_item.repository_cells.find(cell_params[:id])
cell_value = cell_params.dig(:attributes, :value)
next unless cell.value.data_changed?(cell_value)
next unless cell.value.data_different?(cell_value)
cell.value.update_data!(cell_value, current_user)
item_changed = true

View file

@ -54,7 +54,7 @@ class RepositoryAssetValue < ApplicationRecord
asset.file_name
end
def data_changed?(_new_data)
def data_different?(_new_data)
true
end

View file

@ -60,7 +60,7 @@ class RepositoryChecklistValue < ApplicationRecord
repository_checklist_items.map { |i| { value: i.id, label: i.data } }
end
def data_changed?(new_data)
def data_different?(new_data)
if new_data.is_a?(String)
JSON.parse(new_data) != repository_checklist_items.pluck(:id)
else

View file

@ -1,10 +1,15 @@
# frozen_string_literal: true
class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase
def data_changed?(new_data)
self.skip_time_zone_conversion_for_attributes = %i(start_time end_time)
before_save -> { self.start_time = start_time.to_date }, if: :start_time_changed?
before_save -> { self.end_time = end_time.to_date }, if: :end_time_changed?
def data_different?(new_data)
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
st = Time.zone.parse(data[:start_time])
et = Time.zone.parse(data[:end_time])
st = Date.parse(data[:start_time])
et = Date.parse(data[:end_time])
st.to_date != start_time.to_date || et.to_date != end_time.to_date
end
@ -17,21 +22,21 @@ class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase
case filter_element.operator
when 'equal_to'
repository_rows.where("#{join_alias}.start_time::date = ? AND #{join_alias}.end_time::date = ?",
Time.zone.parse(parameters['start_date']), Time.zone.parse(parameters['end_date']))
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
when 'unequal_to'
repository_rows.where.not("#{join_alias}.start_time::date = ? AND #{join_alias}.end_time::date = ?",
Time.zone.parse(parameters['start_date']), Time.zone.parse(parameters['end_date']))
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
when 'greater_than'
repository_rows.where("#{join_alias}.start_time::date > ?", Time.zone.parse(parameters['end_date']))
repository_rows.where("#{join_alias}.start_time::date > ?", Date.parse(parameters['end_date']))
when 'greater_than_or_equal_to'
repository_rows.where("#{join_alias}.start_time::date >= ?", Time.zone.parse(parameters['end_date']))
repository_rows.where("#{join_alias}.start_time::date >= ?", Date.parse(parameters['end_date']))
when 'less_than'
repository_rows.where("#{join_alias}.end_time::date < ?", Time.zone.parse(parameters['start_date']))
repository_rows.where("#{join_alias}.end_time::date < ?", Date.parse(parameters['start_date']))
when 'less_than_or_equal_to'
repository_rows.where("#{join_alias}.end_time::date <= ?", Time.zone.parse(parameters['start_date']))
repository_rows.where("#{join_alias}.end_time::date <= ?", Date.parse(parameters['start_date']))
when 'between'
repository_rows.where("#{join_alias}.start_time::date > ? AND #{join_alias}.end_time::date < ?",
Time.zone.parse(parameters['start_date']), Time.zone.parse(parameters['end_date']))
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
else
raise ArgumentError, 'Wrong operator for RepositoryDateRangeValue!'
end
@ -41,10 +46,18 @@ class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase
data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload
value = new(attributes)
value.start_time = Time.zone.parse(data[:start_time])
value.end_time = Time.zone.parse(data[:end_time])
value.start_time = Date.parse(data[:start_time])
value.end_time = Date.parse(data[:end_time])
value
end
def update_data!(new_data, user)
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
self.start_time = Date.parse(data[:start_time])
self.end_time = Date.parse(data[:end_time])
self.last_modified_by = user
save!
end
alias export_formatted formatted
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class RepositoryDateTimeRangeValue < RepositoryDateTimeRangeValueBase
def data_changed?(new_data)
def data_different?(new_data)
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
st = Time.zone.parse(data[:start_time])
et = Time.zone.parse(data[:end_time])

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class RepositoryDateTimeValue < RepositoryDateTimeValueBase
def data_changed?(new_data)
def data_different?(new_data)
new_time = Time.zone.parse(new_data)
new_time.to_i != data.to_i
end
@ -32,14 +32,10 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase
repository_rows.where("#{join_alias}.data = ?", Time.zone.parse(parameters['datetime']))
when 'unequal_to'
repository_rows.where.not("#{join_alias}.data = ?", Time.zone.parse(parameters['datetime']))
when 'greater_than'
repository_rows.where("#{join_alias}.data > ?", Time.zone.parse(parameters['datetime']))
when 'greater_than_or_equal_to'
repository_rows.where("#{join_alias}.data >= ?", Time.zone.parse(parameters['datetime']))
when 'less_than'
repository_rows.where("#{join_alias}.data < ?", Time.zone.parse(parameters['datetime']))
when 'less_than_or_equal_to'
repository_rows.where("#{join_alias}.data <= ?", Time.zone.parse(parameters['datetime']))
when 'between'
repository_rows.where("#{join_alias}.data > ? AND #{join_alias}.data < ?",
Time.zone.parse(parameters['start_datetime']), Time.zone.parse(parameters['end_datetime']))

View file

@ -1,9 +1,12 @@
# frozen_string_literal: true
class RepositoryDateValue < RepositoryDateTimeValueBase
def data_changed?(new_data)
new_time = Time.zone.parse(new_data)
new_time.to_date != data.to_date
self.skip_time_zone_conversion_for_attributes = [:data]
before_save -> { self.data = data.to_date }, if: :data_changed?
def data_different?(new_data)
Date.parse(new_data).to_date != data.to_date
end
def formatted
@ -14,35 +17,30 @@ class RepositoryDateValue < RepositoryDateTimeValueBase
parameters = filter_element.parameters
case filter_element.operator
when 'today'
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.now.beginning_of_day)
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date)
when 'yesterday'
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
Time.zone.now.beginning_of_day - 1.day, Time.zone.now.beginning_of_day)
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date - 1.day)
when 'last_week'
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
Time.zone.now.beginning_of_week - 1.week, Time.zone.now.beginning_of_week)
Time.zone.now.beginning_of_week.to_date - 1.week, Time.zone.now.beginning_of_week.to_date)
when 'this_month'
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.now.beginning_of_month)
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.now.beginning_of_month.to_date)
when 'last_year'
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
Time.zone.now.beginning_of_year - 1.year, Time.zone.now.beginning_of_year)
Time.zone.now.beginning_of_year.to_date - 1.year, Time.zone.now.beginning_of_year.to_date)
when 'this_year'
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.now.beginning_of_year)
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.now.beginning_of_year.to_date)
when 'equal_to'
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.parse(parameters['date']))
repository_rows.where("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
when 'unequal_to'
repository_rows.where.not("#{join_alias}.data::date = ?", Time.zone.parse(parameters['date']))
when 'greater_than'
repository_rows.where("#{join_alias}.data::date > ?", Time.zone.parse(parameters['date']))
repository_rows.where.not("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
when 'greater_than_or_equal_to'
repository_rows.where("#{join_alias}.data::date >= ?", Time.zone.parse(parameters['date']))
repository_rows.where("#{join_alias}.data::date >= ?", Date.parse(parameters['date']))
when 'less_than'
repository_rows.where("#{join_alias}.data::date < ?", Time.zone.parse(parameters['date']))
when 'less_than_or_equal_to'
repository_rows.where("#{join_alias}.data::date <= ?", Time.zone.parse(parameters['date']))
repository_rows.where("#{join_alias}.data::date < ?", Date.parse(parameters['date']))
when 'between'
repository_rows.where("#{join_alias}.data::date > ? AND #{join_alias}.data::date < ?",
Time.zone.parse(parameters['start_date']), Time.zone.parse(parameters['end_date']))
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
else
raise ArgumentError, 'Wrong operator for RepositoryDateValue!'
end
@ -50,10 +48,16 @@ class RepositoryDateValue < RepositoryDateTimeValueBase
def self.new_with_payload(payload, attributes)
value = new(attributes)
value.data = Time.zone.parse(payload)
value.data = Date.parse(payload)
value
end
def update_data!(new_data, user)
self.data = Date.parse(new_data)
self.last_modified_by = user
save!
end
def self.import_from_text(text, attributes, options = {})
date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%')
new(attributes.merge(data: DateTime.strptime(text, date_format)))

View file

@ -53,7 +53,7 @@ class RepositoryListValue < ApplicationRecord
repository_list_item.data
end
def data_changed?(new_data)
def data_different?(new_data)
new_data.to_i != repository_list_item_id
end

View file

@ -45,7 +45,7 @@ class RepositoryNumberValue < ApplicationRecord
end
end
def data_changed?(new_data)
def data_different?(new_data)
BigDecimal(new_data.to_s) != data
end

View file

@ -40,7 +40,7 @@ class RepositoryStatusValue < ApplicationRecord
end
end
def data_changed?(new_data)
def data_different?(new_data)
new_data.to_i != repository_status_item_id
end

View file

@ -32,7 +32,7 @@ class RepositoryTextValue < ApplicationRecord
end
end
def data_changed?(new_data)
def data_different?(new_data)
new_data != data
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class RepositoryTimeRangeValue < RepositoryDateTimeRangeValueBase
def data_changed?(new_data)
def data_different?(new_data)
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
st = Time.zone.parse(data[:start_time])

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class RepositoryTimeValue < RepositoryDateTimeValueBase
def data_changed?(new_data)
def data_different?(new_data)
new_time = Time.zone.parse(new_data)
new_time.min != data.min || new_time.hour != data.hour
end

View file

@ -37,7 +37,7 @@ module RepositoryRows
next
end
if cell.value.data_changed?(value)
if cell.value.data_different?(value)
cell.value.update_data!(value, @user)
@record_updated = true
end

View file

@ -0,0 +1,51 @@
# frozen_string_literal: true
class FixRepositoryDateValues < ActiveRecord::Migration[6.1]
def up
add_column :repository_date_time_values, :data_dup, :datetime
change_table :repository_date_time_range_values, bulk: true do |t|
t.datetime :start_time_dup
t.datetime :end_time_dup
end
ActiveRecord::Base.connection.execute('UPDATE repository_date_time_values SET data_dup = data;')
ActiveRecord::Base.connection.execute('UPDATE repository_date_time_range_values SET start_time_dup = start_time;')
ActiveRecord::Base.connection.execute('UPDATE repository_date_time_range_values SET end_time_dup = end_time;')
ActiveRecord::Base.connection.execute(
"UPDATE repository_date_time_values SET data =
(CASE WHEN EXTRACT(HOUR FROM data) > 12 THEN
data::date + INTERVAL '1' DAY
ELSE
data::date
END)
WHERE type='RepositoryDateValue';"
)
ActiveRecord::Base.connection.execute(
"UPDATE repository_date_time_range_values SET start_time =
(CASE WHEN EXTRACT(HOUR FROM start_time) > 12 THEN
start_time::date + INTERVAL '1' DAY
ELSE
start_time::date
END)
WHERE type='RepositoryDateRangeValue';"
)
ActiveRecord::Base.connection.execute(
"UPDATE repository_date_time_range_values SET end_time =
(CASE WHEN EXTRACT(HOUR FROM end_time) > 12 THEN
end_time::date + INTERVAL '1' DAY
ELSE
end_time::date
END)
WHERE type='RepositoryDateRangeValue';"
)
end
def down
change_table :repository_date_time_range_values, bulk: true do |t|
t.remove :end_time_dup
t.remove :start_time_dup
end
remove_column :repository_date_time_values, :data_dup
end
end

View file

@ -43,9 +43,9 @@ describe RepositoryAssetValue, type: :model do
end
end
describe 'data_changed?' do
describe 'data_different?' do
it do
expect(repository_asset_value.data_changed?(anything)).to be_truthy
expect(repository_asset_value.data_different?(anything)).to be_truthy
end
end

View file

@ -14,7 +14,7 @@ describe RepositoryDateRangeValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different date value' do
let(:new_values) do
{
@ -24,7 +24,7 @@ describe RepositoryDateRangeValue, type: :model do
end
it do
expect(date_range_value.data_changed?(new_values)).to be_truthy
expect(date_range_value.data_different?(new_values)).to be_truthy
end
end
@ -37,7 +37,7 @@ describe RepositoryDateRangeValue, type: :model do
end
it do
expect(date_range_value.data_changed?(new_values)).to be_falsey
expect(date_range_value.data_different?(new_values)).to be_falsey
end
end
end

View file

@ -14,7 +14,7 @@ describe RepositoryDateTimeRangeValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different datetime value' do
let(:new_values) do
{
@ -24,7 +24,7 @@ describe RepositoryDateTimeRangeValue, type: :model do
end
it do
expect(date_time_range_value.data_changed?(new_values)).to be_truthy
expect(date_time_range_value.data_different?(new_values)).to be_truthy
end
end
@ -37,7 +37,7 @@ describe RepositoryDateTimeRangeValue, type: :model do
end
it do
expect(date_time_range_value.data_changed?(new_values)).to be_falsey
expect(date_time_range_value.data_different?(new_values)).to be_falsey
end
end
end

View file

@ -14,12 +14,12 @@ describe RepositoryDateTimeValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different datetime value' do
let(:new_values) { Time.utc(2000, 12, 11, 4, 14).to_s }
it do
expect(date_time_value.data_changed?(new_values)).to be_truthy
expect(date_time_value.data_different?(new_values)).to be_truthy
end
end
@ -27,7 +27,7 @@ describe RepositoryDateTimeValue, type: :model do
let(:new_values) { Time.utc(2000, 10, 11, 1, 4).to_s }
it do
expect(date_time_value.data_changed?(new_values)).to be_falsey
expect(date_time_value.data_different?(new_values)).to be_falsey
end
end
end

View file

@ -14,12 +14,12 @@ describe RepositoryDateValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different date value' do
let(:new_values) { Time.utc(2000, 12, 11, 4, 14).to_s }
it do
expect(date_value.data_changed?(new_values)).to be_truthy
expect(date_value.data_different?(new_values)).to be_truthy
end
end
@ -27,7 +27,7 @@ describe RepositoryDateValue, type: :model do
let(:new_values) { Time.utc(2000, 10, 11, 4, 11).to_s }
it do
expect(date_value.data_changed?(new_values)).to be_falsey
expect(date_value.data_different?(new_values)).to be_falsey
end
end
end

View file

@ -53,10 +53,10 @@ RSpec.describe RepositoryListValue, type: :model do
end
end
describe 'data_changed?' do
describe 'data_different?' do
context 'when has new data' do
it do
expect(repository_list_value.data_changed?('-1')).to be_truthy
expect(repository_list_value.data_different?('-1')).to be_truthy
end
end
@ -65,7 +65,7 @@ RSpec.describe RepositoryListValue, type: :model do
repository_list_value.save
id = repository_list_value.repository_list_item.id
expect(repository_list_value.data_changed?(id)).to be_falsey
expect(repository_list_value.data_different?(id)).to be_falsey
end
end
end

View file

@ -21,10 +21,10 @@ describe RepositoryStatusValue do
it { is_expected.to belong_to(:last_modified_by).optional }
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has new data' do
it do
expect(repository_status_value.data_changed?('-1')).to be_truthy
expect(repository_status_value.data_different?('-1')).to be_truthy
end
end
@ -33,7 +33,7 @@ describe RepositoryStatusValue do
repository_status_value.save
id = repository_status_value.repository_status_item.id
expect(repository_status_value.data_changed?(id)).to be_falsey
expect(repository_status_value.data_different?(id)).to be_falsey
end
end
end

View file

@ -35,17 +35,17 @@ describe RepositoryTextValue, type: :model do
end
end
describe 'data_changed?' do
describe 'data_different?' do
context 'when has new data' do
it do
expect(repository_text_value.data_changed?('newData')).to be_truthy
expect(repository_text_value.data_different?('newData')).to be_truthy
end
end
context 'when has same data' do
it do
data = repository_text_value.data
expect(repository_text_value.data_changed?(data)).to be_falsey
expect(repository_text_value.data_different?(data)).to be_falsey
end
end
end

View file

@ -14,7 +14,7 @@ describe RepositoryTimeRangeValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different time value' do
let(:new_values) do
{
@ -24,7 +24,7 @@ describe RepositoryTimeRangeValue, type: :model do
end
it do
expect(time_range_value.data_changed?(new_values)).to be_truthy
expect(time_range_value.data_different?(new_values)).to be_truthy
end
end
@ -37,7 +37,7 @@ describe RepositoryTimeRangeValue, type: :model do
end
it do
expect(time_range_value.data_changed?(new_values)).to be_falsey
expect(time_range_value.data_different?(new_values)).to be_falsey
end
end
end

View file

@ -14,12 +14,12 @@ describe RepositoryTimeValue, type: :model do
end
end
describe '.data_changed?' do
describe '.data_different?' do
context 'when has different time value' do
let(:new_values) { Time.utc(2000, 10, 11, 4, 14).to_s }
it do
expect(time_value.data_changed?(new_values)).to be_truthy
expect(time_value.data_different?(new_values)).to be_truthy
end
end
@ -27,7 +27,7 @@ describe RepositoryTimeValue, type: :model do
let(:new_values) { Time.utc(1999, 10, 14, 4, 11).to_s }
it do
expect(time_value.data_changed?(new_values)).to be_falsey
expect(time_value.data_different?(new_values)).to be_falsey
end
end
end