Skip to content

Commit

Permalink
Add UpdateDirectReceipt operation
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Nov 16, 2023
1 parent 97aea73 commit 9aecd89
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Add support for Lucky v1.1
- Add `UpdateDirectReceipt` operation

### Fixed
- Send direct receipt emails **only** for finalized transactions
Expand Down
11 changes: 11 additions & 0 deletions docs/04-TRANSACTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ The ledger is immutable -- once transactions are recorded, they are never update

This operation allows you to receive payments directly as a recorded transaction, without setting up a `Receipt` model. It does not exist if `Receipt` model is set up.

---
```crystal
# ->>> src/operations/receive_direct_payment.cr
class UpdateDirectReceipt < Transaction::SaveOperation
# ...
include Bill::SendDirectReceiptEmail
# ...
end
```

---
```crystal
# ->>> src/operations/update_finalized_transaction.cr
Expand Down
41 changes: 41 additions & 0 deletions spec/bill/operations/update_direct_receipt_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "../../spec_helper"

private class SaveTransaction < Transaction::SaveOperation
permit_columns :reference

include Bill::UpdateDirectReceipt
end

describe Bill::UpdateDirectReceipt do
it "updates receipt transaction" do
user = UserFactory.create

transaction = TransactionFactory.create &.user_id(user.id)
.amount(-20)
.description("Awesome transaction")
.status(:draft)
.type(:receipt)

new_user = UserFactory.create &.email("some@one.now")
new_description = "Another transaction"
new_amount = 45
new_status = TransactionStatus.new(:open)

SaveTransaction.update(transaction, params(
user_id: new_user.id,
amount: new_amount,
credit: false,
description: new_description,
status: new_status,
type: :invoice
)) do |operation, updated_transaction|
operation.saved?.should be_true

updated_transaction.amount.should eq(-new_amount)
updated_transaction.description.should eq(new_description)
updated_transaction.status.should eq(new_status)
updated_transaction.type.should eq(TransactionType.new(:receipt))
updated_transaction.user_id.should eq(new_user.id)
end
end
end
19 changes: 19 additions & 0 deletions src/bill/operations/update_direct_receipt.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Receives payment without creating a `Receipt` record
module Bill::UpdateDirectReceipt
macro included
before_save do
set_type
set_credit
end

include Bill::UpdateTransaction

private def set_type
type.value = TransactionType.new(:receipt)
end

private def set_credit
credit.value = true
end
end
end
4 changes: 4 additions & 0 deletions src/presets/transaction.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ end
class ReceiveDirectPayment < Transaction::SaveOperation
include Bill::ReceiveDirectPayment
end

class UpdateDirectReceipt < Transaction::SaveOperation
include Bill::UpdateDirectReceipt
end
{% end %}

0 comments on commit 9aecd89

Please sign in to comment.