Skip to content

Commit

Permalink
Allowing manually setting created_at when finalizing documents
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Aug 16, 2024
1 parent a8fa295 commit e809888
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] -

### Changed
- Allowing manually setting `created_at` when finalizing documents

## [0.18.0] - 2024-07-27

### Added
Expand Down
19 changes: 19 additions & 0 deletions spec/bill/operations/mixins/set_finalized_created_at_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "../../../spec_helper"
private class SaveInvoice < Invoice::SaveOperation
permit_columns :user_id,
:business_details,
:created_at,
:description,
:due_at,
:reference,
Expand All @@ -29,6 +30,24 @@ describe Bill::SetFinalizedCreatedAt do
end
end

it "does not override date if explicitly updated" do
new_created_at = 20.days.ago.to_utc.at_beginning_of_second

user = UserFactory.create

invoice = InvoiceFactory.create &.user_id(user.id)
.created_at(10.days.ago.to_utc)
.status(:draft)

SaveInvoice.update(
invoice,
params(status: :open, created_at: new_created_at)
) do |operation, updated_invoice|
operation.saved?.should be_true
updated_invoice.created_at.should eq(new_created_at)
end
end

it "does not update date for already finalized documents" do
created_at = 10.days.ago.to_utc.at_beginning_of_second

Expand Down
6 changes: 4 additions & 2 deletions src/bill/operations/mixins/set_finalized_created_at.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ module Bill::SetFinalizedCreatedAt
end

private def set_finalized_created_at
return if created_at.value.nil?
created_at.value = Time.utc if {{ T }}Status.now_finalized?(status)
return if created_at.changed?
return unless {{ T }}Status.now_finalized?(status)

created_at.value = Time.utc
end
end
end

0 comments on commit e809888

Please sign in to comment.