mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
show wallet transactions of user
This commit is contained in:
parent
1dab903054
commit
d0956bb0aa
@ -260,8 +260,8 @@ Application.Controllers.controller "AdminMembersController", ["$scope", 'members
|
||||
##
|
||||
# Controller used in the member edition page
|
||||
##
|
||||
Application.Controllers.controller "EditMemberController", ["$scope", "$state", "$stateParams", "Member", 'Training', 'dialogs', 'growl', 'Group', 'Subscription', 'CSRF', 'memberPromise', 'tagsPromise', '$uibModal', 'Plan', '$filter', '_t', 'walletPromise'
|
||||
, ($scope, $state, $stateParams, Member, Training, dialogs, growl, Group, Subscription, CSRF, memberPromise, tagsPromise, $uibModal, Plan, $filter, _t, walletPromise) ->
|
||||
Application.Controllers.controller "EditMemberController", ["$scope", "$state", "$stateParams", "Member", 'Training', 'dialogs', 'growl', 'Group', 'Subscription', 'CSRF', 'memberPromise', 'tagsPromise', '$uibModal', 'Plan', '$filter', '_t', 'walletPromise', 'transactionsPromise'
|
||||
, ($scope, $state, $stateParams, Member, Training, dialogs, growl, Group, Subscription, CSRF, memberPromise, tagsPromise, $uibModal, Plan, $filter, _t, walletPromise, transactionsPromise) ->
|
||||
|
||||
|
||||
|
||||
@ -303,6 +303,9 @@ Application.Controllers.controller "EditMemberController", ["$scope", "$state",
|
||||
## the user wallet
|
||||
$scope.wallet = walletPromise
|
||||
|
||||
## user wallet transactions
|
||||
$scope.transactions = transactionsPromise
|
||||
|
||||
$scope.view = 'member_edit'
|
||||
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
Application.Controllers.controller "WalletController", ['$scope', 'walletPromise', ($scope, walletPromise)->
|
||||
Application.Controllers.controller "WalletController", ['$scope', 'walletPromise', 'transactionsPromise', ($scope, walletPromise, transactionsPromise)->
|
||||
|
||||
### PUBLIC SCOPE ###
|
||||
|
||||
## current user wallet
|
||||
$scope.wallet = walletPromise
|
||||
|
||||
## current wallet transactions
|
||||
$scope.transactions = transactionsPromise
|
||||
]
|
||||
|
@ -207,6 +207,9 @@ angular.module('application.router', ['ui.router']).
|
||||
walletPromise: ['Wallet', (Wallet)->
|
||||
Wallet.my().$promise
|
||||
]
|
||||
transactionsPromise: ['Wallet', 'walletPromise', (Wallet, walletPromise)->
|
||||
Wallet.transactions(id: walletPromise.id).$promise
|
||||
]
|
||||
translations: [ 'Translations', (Translations) ->
|
||||
Translations.query(['app.shared.wallet']).$promise
|
||||
]
|
||||
@ -875,6 +878,9 @@ angular.module('application.router', ['ui.router']).
|
||||
walletPromise: ['Wallet', '$stateParams', (Wallet, $stateParams)->
|
||||
Wallet.getWalletByUser(user_id: $stateParams.id).$promise
|
||||
]
|
||||
transactionsPromise: ['Wallet', 'walletPromise', (Wallet, walletPromise)->
|
||||
Wallet.transactions(id: walletPromise.id).$promise
|
||||
]
|
||||
tagsPromise: ['Tag', (Tag)->
|
||||
Tag.query().$promise
|
||||
]
|
||||
|
@ -11,4 +11,8 @@ Application.Services.factory 'Wallet', ["$resource", ($resource)->
|
||||
method: 'GET'
|
||||
url: '/api/wallet/by_user/:user_id'
|
||||
isArray: false
|
||||
transactions:
|
||||
method: 'GET'
|
||||
url: '/api/wallet/:id/transactions'
|
||||
isArray: true
|
||||
]
|
||||
|
@ -39,3 +39,4 @@
|
||||
.text-purple { color: $violet !important; }
|
||||
.text-japonica { color: $japonica !important; }
|
||||
.text-beige { color: $beige !important; }
|
||||
.text-green, .green { color: #79C84A !important; }
|
||||
|
@ -514,3 +514,28 @@ padding: 10px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
}
|
||||
|
||||
.wallet-amount-container {
|
||||
padding: 20px 0;
|
||||
border-top: 2px dotted $border-color;
|
||||
border-bottom: 2px dotted $border-color;
|
||||
margin-bottom: 20px;
|
||||
text-align: center;
|
||||
|
||||
.wallet-amount {
|
||||
font-size: rem-calc(40);
|
||||
font-weight: 700;
|
||||
font-style: italic;
|
||||
color: #616161;
|
||||
|
||||
span {
|
||||
font-weight: 500;
|
||||
font-size: .7em;
|
||||
}
|
||||
|
||||
&.cr-green {
|
||||
color: $green;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -220,6 +220,10 @@
|
||||
<div class="col-md-12 m m-t-lg">
|
||||
<ng-include src="'<%= asset_path 'wallet/show.html' %>'"></ng-include>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12 m m-t-lg">
|
||||
<ng-include src="'<%= asset_path 'wallet/transactions.html' %>'"></ng-include>
|
||||
</div>
|
||||
</uib-tab>
|
||||
</uib-tabset>
|
||||
|
||||
|
@ -12,5 +12,10 @@
|
||||
<div class="col-md-12 m m-t-lg">
|
||||
<ng-include src="'<%= asset_path 'wallet/show.html' %>'"></ng-include>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12 m m-t-lg">
|
||||
<ng-include src="'<%= asset_path 'wallet/transactions.html' %>'"></ng-include>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
27
app/assets/templates/wallet/transactions.html.erb
Normal file
27
app/assets/templates/wallet/transactions.html.erb
Normal file
@ -0,0 +1,27 @@
|
||||
<div>
|
||||
<table class="table" ng-if="transactions.length > 0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:25%" translate>{{ 'date' }}</th>
|
||||
<th style="width:25%" translate>{{ 'operation' }}</th>
|
||||
<th style="width:25%" translate>{{ 'operator' }}</th>
|
||||
<th style="width:25%" translate>{{ 'amount' }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr ng-repeat="t in transactions">
|
||||
<td>{{ t.created_at | amDateFormat:'L' }}</td>
|
||||
<td>
|
||||
<span ng-if="t.transaction_type == 'credit'" translate>{{ 'credit' }}</span>
|
||||
</td>
|
||||
<td>{{ t.user.full_name }}</td>
|
||||
<td ng-class="{'green':t.transaction_type == 'credit', 'red':t.transaction_type == 'debit'}">
|
||||
<span ng-if="t.transaction_type == 'credit'">+</span>
|
||||
<span ng-if="t.transaction_type == 'debit'">-</span>
|
||||
<strong>{{ t.amount | currency }}</strong>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p ng-if="transactions.length == 0" translate>{{ 'no_transactions_for_now' }}</p>
|
||||
</div>
|
@ -11,4 +11,10 @@ class API::WalletController < API::ApiController
|
||||
@wallet = Wallet.find_by(user_id: params[:user_id])
|
||||
render :show
|
||||
end
|
||||
|
||||
def transactions
|
||||
@wallet = Wallet.find(params[:id])
|
||||
authorize @wallet
|
||||
@wallet_transactions = @wallet.wallet_transactions.includes(:transactable, user: [:profile]).order(created_at: :desc)
|
||||
end
|
||||
end
|
||||
|
@ -2,4 +2,8 @@ class WalletPolicy < ApplicationPolicy
|
||||
def by_user?
|
||||
user.is_admin?
|
||||
end
|
||||
|
||||
def transactions?
|
||||
user.is_admin? or user == record.user
|
||||
end
|
||||
end
|
||||
|
13
app/services/wallet_service.rb
Normal file
13
app/services/wallet_service.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class WalletService
|
||||
def initialize(user: nil, wallet: nil)
|
||||
@user = user
|
||||
@wallet = wallet
|
||||
end
|
||||
|
||||
def credit(amount)
|
||||
if @wallet.credit(amount)
|
||||
WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'credit', amount: amount)
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
@ -1,2 +1 @@
|
||||
json.user_id @wallet.user_id
|
||||
json.amount amount_to_f(@wallet.amount)
|
||||
json.extract! @wallet, :id, :user_id, :amount
|
||||
|
7
app/views/api/wallet/transactions.json.jbuilder
Normal file
7
app/views/api/wallet/transactions.json.jbuilder
Normal file
@ -0,0 +1,7 @@
|
||||
json.array!(@wallet_transactions) do |t|
|
||||
json.extract! t, :id, :transaction_type, :created_at, :amount
|
||||
json.user do
|
||||
json.id t.user.id
|
||||
json.full_name t.user.profile.full_name
|
||||
end
|
||||
end
|
@ -300,3 +300,8 @@ en:
|
||||
wallet: 'Wallet'
|
||||
your_wallet_amount: 'Your amount available'
|
||||
wallet_amount: 'Amount available'
|
||||
no_transactions_for_now: 'No transactions for now'
|
||||
operation: 'Operation'
|
||||
operator: 'Operator'
|
||||
amount: 'Amount'
|
||||
credit: 'Crédit'
|
||||
|
@ -300,3 +300,8 @@ fr:
|
||||
wallet: 'Porte-monnaie'
|
||||
your_wallet_amount: 'Votre montant disponible'
|
||||
wallet_amount: 'Montant disponible'
|
||||
no_transactions_for_now: 'Aucune transaction pour le moment'
|
||||
operation: 'Opération'
|
||||
operator: 'Opérateur'
|
||||
amount: 'Montant'
|
||||
credit: 'Crédit'
|
||||
|
@ -51,6 +51,7 @@ Rails.application.routes.draw do
|
||||
resources :wallet do
|
||||
get :my, on: :collection
|
||||
get '/by_user/:user_id', action: 'by_user', on: :collection
|
||||
get :transactions, on: :member
|
||||
end
|
||||
|
||||
# for homepage
|
||||
|
16
test/fixtures/wallet_transactions.yml
vendored
16
test/fixtures/wallet_transactions.yml
vendored
@ -1,13 +1,7 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
user_id:
|
||||
wallet_id:
|
||||
transaction_type: MyString
|
||||
amount: 1
|
||||
|
||||
two:
|
||||
user_id:
|
||||
wallet_id:
|
||||
transaction_type: MyString
|
||||
amount: 1
|
||||
transaction1:
|
||||
user_id: 4
|
||||
wallet: wallet_4
|
||||
transaction_type: credit
|
||||
amount: 1000
|
||||
|
2
test/fixtures/wallets.yml
vendored
2
test/fixtures/wallets.yml
vendored
@ -4,7 +4,7 @@ wallet_2:
|
||||
|
||||
wallet_4:
|
||||
user_id: 4
|
||||
amount: 0
|
||||
amount: 1000
|
||||
|
||||
wallet_6:
|
||||
user_id: 6
|
||||
|
@ -3,8 +3,8 @@ class WalletsTest < ActionDispatch::IntegrationTest
|
||||
# Called before every test method runs. Can be used
|
||||
# to set up fixture information.
|
||||
def setup
|
||||
@jdupond = User.find_by_username('jdupond')
|
||||
login_as(@jdupond, scope: :user)
|
||||
@kdumas = User.find_by(username: 'kdumas')
|
||||
login_as(@kdumas, scope: :user)
|
||||
end
|
||||
|
||||
# Called after every test method runs. Can be used to tear
|
||||
@ -19,8 +19,8 @@ class WalletsTest < ActionDispatch::IntegrationTest
|
||||
assert_equal 200, response.status
|
||||
assert_equal Mime::JSON, response.content_type
|
||||
wallet = json_response(response.body)
|
||||
assert_equal @jdupond.wallet.user_id, wallet[:user_id]
|
||||
assert_equal @jdupond.wallet.amount, wallet[:amount]
|
||||
assert_equal @kdumas.wallet.user_id, wallet[:user_id]
|
||||
assert_equal @kdumas.wallet.amount, wallet[:amount]
|
||||
end
|
||||
|
||||
test 'admin can get wallet by user id' do
|
||||
@ -35,9 +35,25 @@ class WalletsTest < ActionDispatch::IntegrationTest
|
||||
assert_equal @user1.wallet.amount, wallet[:amount]
|
||||
end
|
||||
|
||||
test 'cant get wallet of user if not admin' do
|
||||
@user1 = User.first
|
||||
get "/api/wallet/by_user/#{@user1.id}"
|
||||
test 'cant get wallet of an user if not admin' do
|
||||
user5 = users(:user_5)
|
||||
get "/api/wallet/by_user/#{user5.id}"
|
||||
assert_equal 403, response.status
|
||||
end
|
||||
|
||||
test 'get all transactions of wallet' do
|
||||
w = @kdumas.wallet
|
||||
get "/api/wallet/#{w.id}/transactions"
|
||||
assert_equal 200, response.status
|
||||
assert_equal Mime::JSON, response.content_type
|
||||
transactions = json_response(response.body)
|
||||
assert_equal w.wallet_transactions.count, transactions.size
|
||||
assert_equal wallet_transactions(:transaction1).id, transactions.first[:id]
|
||||
end
|
||||
|
||||
test 'only admin and wallet owner can show their transactions' do
|
||||
user5 = users(:user_5)
|
||||
get "/api/wallet/#{user5.wallet.id}/transactions"
|
||||
assert_equal 403, response.status
|
||||
end
|
||||
end
|
||||
|
@ -21,7 +21,7 @@ class WalletTest < ActiveSupport::TestCase
|
||||
test 'can debit amount' do
|
||||
w = Wallet.first
|
||||
w.credit(5)
|
||||
expected_amount = 0
|
||||
expected_amount = w.amount - 5
|
||||
assert w.debit(5)
|
||||
assert_equal w.amount, expected_amount
|
||||
end
|
||||
|
27
test/services/wallet_service_test.rb
Normal file
27
test/services/wallet_service_test.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require 'test_helper'
|
||||
|
||||
class WalletServiceTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@admin = User.find_by(username: 'admin')
|
||||
@user = User.find_by(username: 'jdupond')
|
||||
@wallet = @user.wallet
|
||||
end
|
||||
|
||||
test 'admin can credit a wallet' do
|
||||
service = WalletService.new(user: @admin, wallet: @wallet)
|
||||
expected_amount = @wallet.amount + 5
|
||||
assert service.credit(5)
|
||||
assert_equal @wallet.amount, expected_amount
|
||||
end
|
||||
|
||||
test 'create a credit transaction after credit amount to wallet' do
|
||||
service = WalletService.new(user: @admin, wallet: @wallet)
|
||||
assert_equal 0, @wallet.wallet_transactions.count
|
||||
assert service.credit(10)
|
||||
transaction = @wallet.wallet_transactions.first
|
||||
assert_equal transaction.transaction_type, 'credit'
|
||||
assert_equal transaction.amount, 10
|
||||
assert_equal transaction.user, @admin
|
||||
assert_equal transaction.wallet, @wallet
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user