Optimized look and user experience; fixed errors

This commit is contained in:
Florian "flowdy" Heß
2017-01-28 14:00:20 +01:00
parent 5aaff598d1
commit 6f19437901
23 changed files with 1434 additions and 44 deletions

View File

@@ -11,14 +11,17 @@ __PACKAGE__->add_column("purpose");
__PACKAGE__->add_column("value" => { data_type => 'INTEGER' });
__PACKAGE__->add_column("paid" => { data_type => 'INTEGER', default => 0 });
__PACKAGE__->set_primary_key("billId");
__PACKAGE__->add_column("targetCredit" => { data_type => 'INTEGER' });
__PACKAGE__->add_column("targetCredit" => {
data_type => 'INTEGER',
is_nullable => 1
});
__PACKAGE__->belongs_to(
account => 'TrsrDB::Account',
{ 'foreign.ID' => 'self.debtor' }
);
__PACKAGE__->might_have(
__PACKAGE__->belongs_to(
target => 'TrsrDB::Credit',
{ 'foreign.credId' => 'self.targetCredit' }
);

View File

@@ -7,11 +7,22 @@ use Mojolicious::Sessions;
use Mojo::Base 'Mojolicious';
use POSIX qw(strftime);
has db => sub {
my $db;
eval q{use TrsrDB \$db} or $@ && die $@;
return $db;
};
{ my $sql_trace;
open my $dfh, '>', \$sql_trace;
sub get_trace () {
my $t = $sql_trace;
$sql_trace = q{};
seek $dfh, 0, 0;
return \$t;
}
has db => sub {
my $db;
eval q{use TrsrDB \$db} or $@ && die $@;
$db->storage->debugfh($dfh);
return $db;
};
}
# This method will run once at server start
sub startup {
@@ -43,6 +54,16 @@ sub startup {
pop =~ s{\n}{<br>}grms;
});
if ( $ENV{DBIC_TRACE} ) {
$self->helper(sql_trace => \&get_trace);
}
else {
$self->helper(sql_trace => sub {
return "No SQL trace shown here, because environment variable "
. "DBIC_TRACE is not set."
});
}
if ( my $l = $ENV{LOG} ) {
use Mojo::Log;
open my $fh, '>', $l or die "Could not open logfile $l to write: $!";
@@ -63,21 +84,25 @@ sub startup {
$auth->get( '/logout' )->to("user#logout");
my $check = $auth->under(sub { shift->stash('grade') })->get('/');
my $check = $auth->under(sub {
my $c = shift;
return $c->stash('grade') || undef;
})->get('/');
$check->get('/bankStatement' => sub {
my $c = shift;
$c->stash( records => $c->app->db->resultset("ReconstructedBankStatement") );
$c->render('bankStatement');
});
my $admin = $auth->under(sub { shift->stash('grade') > 1 });
my $admin = $auth->under(sub {
my $c = shift;
return $c->stash('grade') > 1 || undef;
});
$admin->any('/admin')->to('admin#dash');
$admin->any( [qw/GET POST/] => '/account/:account' => { account => undef })
->to('account#upsert');
$admin->post('/:account/in')->to('credit#upsert');
$admin->post('/:account/out')->to('debit#upsert');
$admin->get('/:account/credits')->to('credit#list');
$admin->get('/:account/debits')->to('debit#list');
$admin->any( [qw/GET POST/] => '/:account/in')->to('credit#upsert');
$admin->any( [qw/GET POST/] => '/:account/out')->to('debit#upsert');
$admin->post('/:account/transfer')->to('account#transfer');
$admin->any( [qw/GET POST PATCH/] => '/credit/:id' )->to('credit#upsert');
$admin->any( [qw/GET POST/] => '/credit')->to('credit#upsert');
@@ -90,6 +115,8 @@ sub startup {
my $account = $auth->get('/:account')->under(sub {
my $c = shift;
return 1 if $c->stash('grade');
my $account = $c->stash('account');
if ( my $acc = $c->app->db->resultset('Account')->find($account) ) {
$c->stash( account => $acc );
@@ -100,13 +127,20 @@ sub startup {
return;
}
return $account->type ? $c->stash('grade') : 1;
return 1 if !$account->type;
return $account->ID eq $c->stash("user")->user_id || undef;
});
$account->get('/in')->to("credit#upsert");
$account->get('/out')->to("debit#upsert");
$account->get('/credits')->to("credit#list");
$account->get('/debits')->to("debit#list");
$account->get('/:action')->to('account#');
$r->any('/*whatever' => {whatever => ''} => sub {
my $c = shift;
my $whatever = $c->param('whatever');
$c->render(text => "/$whatever did not match.", status => 404);
});
}
my $started_time;

View File

@@ -8,9 +8,14 @@ sub list {
my $self = shift;
my $accounts = $self->app->db->resultset("Account");
my %args = $self->stash("user")->grade ? () : ( type => undef );
$accounts = $accounts->search(\%args, { order_by => { -asc => [qw/type ID/] } });
my $user = $self->stash("user");
my %args = $user->grade ? ()
: $accounts->find( $user->user_id ) ? ( 'me.ID' => $user->user_id )
: ( type => q{} );
$accounts = $accounts->search(\%args, {
order_by => { -asc => [qw/type balance.even_until me.ID/] },
prefetch => 'balance'
});
$self->stash( accounts => $accounts );

View File

@@ -3,6 +3,7 @@ use strict;
package TrsrDB::HTTP::Credit;
use Mojo::Base 'Mojolicious::Controller';
use Carp qw(croak);
use POSIX qw(strftime);
sub list {
my $self = shift;
@@ -33,9 +34,11 @@ sub upsert {
my $db = $self->app->db;
my $id = $self->stash("id");
my $method = $id ? 'find_or_new' : 'new';
my $credit = $db->resultset("Credit")->$method(
{ $id ? (credId => $id) : (), account => $self->stash("account") }
);
my $credit = $db->resultset("Credit")->$method({
$id ? (credId => $id) : (),
account => $self->stash("account"),
date => strftime("%Y-%m-%d", localtime)
});
$self->stash( credit => $credit );
if ( $self->req->method eq 'GET' ) {

View File

@@ -3,6 +3,7 @@ use strict;
package TrsrDB::HTTP::Debit;
use Mojo::Base 'Mojolicious::Controller';
use Carp qw(croak);
use POSIX qw(strftime);
sub list {
my $self = shift;
@@ -42,6 +43,7 @@ sub upsert {
while ( my $m = $group_members->next ) {
my %props = map { $_ => $self->param($_) } @FIELDS;
$props{targetCredit} ||= undef;
for ( $props{billId} ) {
s{\%u}{ $m->ID }e or $_ .= "-" . $m->ID;
}
@@ -53,9 +55,11 @@ sub upsert {
}
my $method = $id ? 'find_or_new' : 'new';
my $debit = $db->resultset("Debit")->$method(
{ $id ? (billId => $id) : (), debtor => $debtor }
);
my $debit = $db->resultset("Debit")->$method({
$id ? (billId => $id) : (),
debtor => $debtor,
date => strftime("%Y-%m-%d", localtime)
});
$self->stash( debit => $debit );
@@ -76,6 +80,7 @@ sub upsert {
for my $field ( @FIELDS ) {
my $value = $self->param($field);
$value = undef if !length $value;
$debit->$field($value);
}
$debit->update_or_insert();
@@ -89,7 +94,15 @@ sub upsert {
my $to_pay_with = $self->every_param("payWith");
if ( @$to_pay_with ) {
$db->make_transfers( $to_pay_with => $self->param("billId") );
my $billId = $self->param("billId");
$db->make_transfers( $to_pay_with => $billId );
for my $param ( grep { /^note\[/ } @{ $self->req->params->names } ) {
my $note = $self->param($param) || next;
s{^note\[}{} && s{\]$}{} for $param;
$db->resultset("Transfer")->find({
billId => $self->param("billId"), credId => $param
})->update({ note => $note });
}
}
$self->redirect_to('home');

View File

@@ -18,6 +18,21 @@ sub login {
$self->render( retry_msg => 'authfailure' );
return;
}
elsif ( my $token = $self->param('token') ) {
my $pw = $self->param("password") // q{};
if ( ($user->password//q{}) ne $token ) {
$self->render( retry_msg => 'authfailure' );
return;
}
elsif ( $pw ne ($self->param("samepassword") // q{}) ) {
$self->render( retry_msg => "Passwords are different" );
return;
}
$self->session("user_id" => $user_id );
$user->salted_password($pw);
$user->update();
$self->redirect_to("home");
}
elsif ( $password && $user->password_equals($password) ) {
$self->session("user_id" => $user_id );
$self->redirect_to("home");
@@ -33,6 +48,7 @@ sub logout {
$self->session(expires => 1);
$self->redirect_to('home');
# $self->stash( retry_msg => 'loggedOut' );
}

View File

@@ -25,7 +25,7 @@ __PACKAGE__->set_primary_key('user_id');
sub salted_password {
my ($self, $password) = @_;
if ( exists $_[1] ) {
my $random_string = _randomstring(8);
my $random_string = randomstring(8);
return $self->password(
$random_string."//".hmac_sha256_hex($password, $random_string)
);
@@ -55,7 +55,7 @@ sub sqlt_deploy_hook {
}
my @chars = ( 0..9, "a".."z", "A".."Z" );
sub _randomstring {
sub randomstring {
my ($length) = @_;
return join q{}, map { $chars[ int rand(62) ] } 1 .. $length;
}