Skip to content

Commit

Permalink
Add DirectReceipts actions
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Aug 17, 2024
1 parent d4053d9 commit e902333
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `Api::DirectReceipts::Edit` action
- Add `Api::DirectReceipts::New` action
- Add `Api::DirectReceipts::Update` action
- Add `DirectReceipts::Create` action
- Add `DirectReceipts::Edit` action
- Add `DirectReceipts::New` action
- Add `DirectReceipts::Update` action

### Changed
- Allow `require`ing individual presets
Expand Down
15 changes: 15 additions & 0 deletions spec/bill/actions/direct_receipts/create_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "../../../spec_helper"

describe Bill::DirectReceipts::Create do
it "creates direct receipt" do
response = ApiClient.exec(DirectReceipts::Create, transaction: {
user_id: UserFactory.create.id,
description: "New receipt",
amount: 90
})

# ameba:disable Performance/AnyInsteadOfEmpty
TransactionQuery.new.any?.should be_true
response.status.should eq(HTTP::Status::FOUND)
end
end
14 changes: 14 additions & 0 deletions spec/bill/actions/direct_receipts/edit_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "../../../spec_helper"

describe Bill::DirectReceipts::Edit do
it "renders edit page" do
user = UserFactory.create
transaction = TransactionFactory.create &.user_id(user.id)

response = ApiClient.exec(DirectReceipts::Edit.with(
transaction_id: transaction.id
))

response.body.should eq("DirectReceipts::EditPage")
end
end
9 changes: 9 additions & 0 deletions spec/bill/actions/direct_receipts/new_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "../../../spec_helper"

describe Bill::DirectReceipts::New do
it "renders new page" do
response = ApiClient.exec(DirectReceipts::New)

response.body.should eq("DirectReceipts::NewPage")
end
end
21 changes: 21 additions & 0 deletions spec/bill/actions/direct_receipts/update_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "../../../spec_helper"

describe Bill::DirectReceipts::Update do
it "updates direct receipt" do
new_description = "Another receipt"

user = UserFactory.create

transaction = TransactionFactory.create &.user_id(user.id)
.description("New receipt")
.status(:draft)

response = ApiClient.exec(
DirectReceipts::Update.with(transaction_id: transaction.id),
transaction: {description: new_description}
)

transaction.reload.description.should eq(new_description)
response.status.should eq(HTTP::Status::FOUND)
end
end
7 changes: 7 additions & 0 deletions spec/support/app/src/actions/direct_receipts/create.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DirectReceipts::Create < BrowserAction
include Bill::DirectReceipts::Create

post "/direct-receipts" do
run_operation
end
end
8 changes: 8 additions & 0 deletions spec/support/app/src/actions/direct_receipts/edit.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DirectReceipts::Edit < BrowserAction
include Bill::DirectReceipts::Edit

get "/direct-receipts/:transaction_id/edit" do
operation = UpdateDirectReceipt.new(transaction)
html EditPage, operation: operation
end
end
8 changes: 8 additions & 0 deletions spec/support/app/src/actions/direct_receipts/new.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DirectReceipts::New < BrowserAction
include Bill::DirectReceipts::New

get "/direct-receipts/new" do
operation = ReceiveDirectPayment.new
html NewPage, operation: operation
end
end
7 changes: 7 additions & 0 deletions spec/support/app/src/actions/direct_receipts/update.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DirectReceipts::Update < BrowserAction
include Bill::DirectReceipts::Update

patch "/direct-receipts/:transaction_id" do
run_operation
end
end
7 changes: 7 additions & 0 deletions spec/support/app/src/pages/direct_receipts/edit_page.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct DirectReceipts::EditPage < MainLayout
needs operation : UpdateDirectReceipt

def content
text "DirectReceipts::EditPage"
end
end
7 changes: 7 additions & 0 deletions spec/support/app/src/pages/direct_receipts/new_page.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct DirectReceipts::NewPage < MainLayout
needs operation : ReceiveDirectPayment

def content
text "DirectReceipts::NewPage"
end
end
28 changes: 28 additions & 0 deletions src/bill/actions/direct_receipts/create.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Bill::DirectReceipts::Create
macro included
# post "/receipts" do
# run_operation
# end

def run_operation
ReceiveDirectPayment.create(params) do |operation, transaction|
if operation.saved?
do_run_operation_succeeded(operation, transaction.not_nil!)
else
response.status_code = 400
do_run_operation_failed(operation)
end
end
end

def do_run_operation_succeeded(operation, transaction)
flash.success = Rex.t(:"action.receipt.create.success")
redirect to: Transactions::Show.with(transaction_id: transaction.id)
end

def do_run_operation_failed(operation)
flash.failure = Rex.t(:"action.receipt.create.failure")
html NewPage, operation: operation
end
end
end
12 changes: 12 additions & 0 deletions src/bill/actions/direct_receipts/edit.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Bill::DirectReceipts::Edit
macro included
# get "/receipts/:transaction_id/edit" do
# operation = UpdateDirectReceipt.new(transaction)
# html EditPage, operation: operation
# end

getter transaction : Transaction do
TransactionQuery.find(transaction_id)
end
end
end
8 changes: 8 additions & 0 deletions src/bill/actions/direct_receipts/new.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Bill::DirectReceipts::New
macro included
# get "/receipts/new" do
# operation = ReceiveDirectPayment.new
# html NewPage, operation: operation
# end
end
end
35 changes: 35 additions & 0 deletions src/bill/actions/direct_receipts/update.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Bill::DirectReceipts::Update
macro included
# patch "/receipts/:transaction_id" do
# run_operation
# end

def run_operation
UpdateDirectReceipt.update(
transaction,
params
) do |operation, updated_transaction|
if operation.saved?
do_run_operation_succeeded(operation, updated_transaction)
else
response.status_code = 400
do_run_operation_failed(operation)
end
end
end

getter transaction : Transaction do
TransactionQuery.find(transaction_id)
end

def do_run_operation_succeeded(operation, transaction)
flash.success = Rex.t(:"action.receipt.update.success")
redirect to: Transactions::Show.with(transaction_id: transaction.id)
end

def do_run_operation_failed(operation)
flash.failure = Rex.t(:"action.receipt.update.failure")
html EditPage, operation: operation
end
end
end

0 comments on commit e902333

Please sign in to comment.