mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
prevent closing a period not in the past
This commit is contained in:
parent
9e39a0517c
commit
8a6ff0c093
@ -15,6 +15,7 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
|
||||
validates_with DateRangeValidator
|
||||
validates_with DurationValidator
|
||||
validates_with PastPeriodValidator
|
||||
validates_with PeriodOverlapValidator
|
||||
validates_with PeriodIntegrityValidator
|
||||
|
||||
|
@ -6,7 +6,8 @@ class DurationValidator < ActiveModel::Validator
|
||||
the_end = record.end_at
|
||||
the_start = record.start_at
|
||||
diff = (the_end - the_start).to_i
|
||||
return if diff.days >= 1.day && diff.days <= 1.year
|
||||
# 0.day means that (the_start == the_end), so it's a one day period
|
||||
return if diff.days >= 0.day && diff.days <= 1.year
|
||||
|
||||
record.errors[:end_at] << I18n.t('errors.messages.invalid_duration', DAYS: diff)
|
||||
end
|
||||
|
12
app/validators/past_period_validator.rb
Normal file
12
app/validators/past_period_validator.rb
Normal file
@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Validates the current period is strictly in the past
|
||||
class PastPeriodValidator < ActiveModel::Validator
|
||||
def validate(record)
|
||||
the_end = record.end_at
|
||||
|
||||
return if the_end.present? && the_end < Date.today
|
||||
|
||||
record.errors[:end_at] << I18n.t('errors.messages.must_be_in_the_past')
|
||||
end
|
||||
end
|
@ -42,6 +42,7 @@ en:
|
||||
invalid_footprint: "invoice's checksum is invalid"
|
||||
end_before_start: "The end date can't be before the start date. Pick a date after %{START}"
|
||||
invalid_duration: "The allowed duration must be between 1 day and 1 year. Your period is %{DAYS} days long."
|
||||
must_be_in_the_past: "The period must be strictly prior to today's date."
|
||||
|
||||
activemodel:
|
||||
errors:
|
||||
|
@ -42,6 +42,7 @@ es:
|
||||
invalid_footprint: "invoice's checksum is invalid" # missing translation
|
||||
end_before_start: "The end date can't be before the start date. Pick a date after %{START}" # missing translation
|
||||
invalid_duration: "The allowed duration must be between 1 day and 1 year. Your period is %{DAYS} days long." # missing translation
|
||||
must_be_in_the_past: "The period must be strictly prior to today's date." # missing translation
|
||||
|
||||
activemodel:
|
||||
errors:
|
||||
|
@ -42,6 +42,7 @@ fr:
|
||||
invalid_footprint: "la somme de contrôle de la facture est invalide"
|
||||
end_before_start: "La date de fin ne peut pas être antérieure à la date de début. Choisissez une date après le %{START}"
|
||||
invalid_duration: "La durée doit être comprise entre 1 jour et 1 an. Votre période dure %{DAYS} jours."
|
||||
must_be_in_the_past: "La période doit être strictement antérieure à la date du jour."
|
||||
|
||||
activemodel:
|
||||
errors:
|
||||
|
@ -42,6 +42,7 @@ pt:
|
||||
invalid_footprint: "invoice's checksum is invalid" # missing translation
|
||||
end_before_start: "The end date can't be before the start date. Pick a date after %{START}" # missing translation
|
||||
invalid_duration: "The allowed duration must be between 1 day and 1 year. Your period is %{DAYS} days long." # missing translation
|
||||
must_be_in_the_past: "The period must be strictly prior to today's date." # missing translation
|
||||
|
||||
activemodel:
|
||||
errors:
|
||||
|
@ -68,7 +68,7 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest
|
||||
FileUtils.rm_rf(accounting_period.archive_folder)
|
||||
end
|
||||
|
||||
test 'admin tries to closes a too long period' do
|
||||
test 'admin tries to close a too long period' do
|
||||
start_at = '2012-01-01T00:00:00.000Z'
|
||||
end_at = '2014-12-31T00:00:00.000Z'
|
||||
diff = (end_at.to_date - start_at.to_date).to_i
|
||||
@ -89,7 +89,7 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest
|
||||
assert_match(/#{I18n.t('errors.messages.invalid_duration', DAYS: diff)}/, response.body)
|
||||
end
|
||||
|
||||
test 'admin tries to closes an overlapping period' do
|
||||
test 'admin tries to close an overlapping period' do
|
||||
start_at = '2014-12-01T00:00:00.000Z'
|
||||
end_at = '2015-02-27T00:00:00.000Z'
|
||||
|
||||
@ -108,4 +108,24 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest
|
||||
# check the error
|
||||
assert_match(/#{I18n.t('errors.messages.cannot_overlap')}/, response.body)
|
||||
end
|
||||
|
||||
test 'admin tries to close today' do
|
||||
start_at = Date.today.beginning_of_day.iso8601
|
||||
end_at = Date.today.end_of_day.iso8601
|
||||
|
||||
post '/api/accounting_periods',
|
||||
{
|
||||
accounting_period: {
|
||||
start_at: start_at,
|
||||
end_at: end_at
|
||||
}
|
||||
}.to_json, default_headers
|
||||
|
||||
# Check response format & status
|
||||
assert_equal 422, response.status, response.body
|
||||
assert_equal Mime::JSON, response.content_type
|
||||
|
||||
# check the error
|
||||
assert_match(/#{I18n.t('errors.messages.must_be_in_the_past')}/, response.body)
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user