diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 41f15f0..5e59953 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -28,15 +28,15 @@ sub post_index { return $c->render( json => { success => Mojo::JSON->true, - today_sum => $today_sum, + today_sum => $today_sum || 0, today_count => $today_count, - week_sum => $week_sum, + week_sum => $week_sum || 0, week_count => $week_count, - month_sum => $month_sum, + month_sum => $month_sum || 0, month_count => $month_count, - user_sum => $user_sum, + user_sum => $user_sum || 0, user_count => $user_count, - global_sum => $global_sum, + global_sum => $global_sum || 0, global_count => $global_count, }); } diff --git a/t/api/stats.t b/t/api/stats.t index 01d6d21..180a91f 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -39,6 +39,25 @@ $framework->register_organisation($org); my $org_result = $schema->resultset('Organisation')->find({ name => $org->{name} }); my $user_result = $schema->resultset('User')->find({ email => $user->{email} }); +my $session_key = $framework->login({ + email => $user->{email}, + password => $user->{password}, +}); + +$t->post_ok('/api/stats' => json => { session_key => $session_key } ) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true) + ->json_is('/today_sum', 0) + ->json_is('/today_count', 0) + ->json_is('/week_sum', 0) + ->json_is('/week_count', 0) + ->json_is('/month_sum', 0) + ->json_is('/month_count', 0) + ->json_is('/user_sum', 0) + ->json_is('/user_count', 0) + ->json_is('/global_sum', 0) + ->json_is('/global_count', 0); + for ( 1 .. 10 ) { $user_result->create_related( 'transactions', { seller_id => $org_result->id, @@ -93,11 +112,6 @@ is $user_result->transactions->search({ })->get_column('value')->sum, 55, 'Got correct sum'; is $user_result->transactions->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs'; -my $session_key = $framework->login({ - email => $user->{email}, - password => $user->{password}, -}); - $t->post_ok('/api/stats' => json => { session_key => $session_key } ) ->status_is(200) ->json_is('/success', Mojo::JSON->true)