diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index c4ee95e..d874fa6 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -154,6 +154,7 @@ sub startup { $api->post('/user-history')->to('api-user#post_user_history'); $api->post('/stats')->to('api-stats#post_index'); $api->post('/stats/leaderboard')->to('api-stats#post_leaderboards'); + $api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases'); my $api_v1 = $api->under('/v1'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm new file mode 100644 index 0000000..d205b69 --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm @@ -0,0 +1,49 @@ +package Pear::LocalLoop::Controller::Api::Transactions; +use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON; + +has error_messages => sub { + return { + email => { + required => { message => 'No email sent.', status => 400 }, + email => { message => 'Email is invalid.', status => 400 }, + }, + }; +}; + +sub post_transaction_list_purchases { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->optional('page')->number; + + return $c->api_validation_error if $validation->has_error; + + my $transactions = $user->entity->purchases->search( + undef, { + page => $validation->param('page') || 1, + rows => 10, + order_by => { -desc => 'purchase_time' }, + }, + ); + +# purchase_time needs timezone attached to it + my @transaction_list = ( + map {{ + seller => $_->seller->name, + value => $_->value, + purchase_time => $_->purchase_time, + }} $transactions->all + ); + + return $c->render( json => { + success => Mojo::JSON->true, + transactions => \@transaction_list, + page_no => $transactions->pager->total_entries, + }); +} + +1; diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index b83f04b..dfe9bb3 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -64,7 +64,6 @@ sub post_account { if ( defined $user_result ) { my $email = $user_result->email; - #Needs elsif added for trader page for this similar relevant entry if ( $user_result->type eq 'customer' ) { my $full_name = $user_result->entity->customer->full_name; my $display_name = $user_result->entity->customer->display_name; diff --git a/t/api/transactions.t b/t/api/transactions.t new file mode 100644 index 0000000..849a2a8 --- /dev/null +++ b/t/api/transactions.t @@ -0,0 +1,69 @@ +use Mojo::Base -strict; + +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $start = DateTime->today->subtract( hours => 12 ); + +# create 30 days worth of data +for my $count ( 0 .. 29 ) { + my $trans_day = $start->clone->subtract( days => $count ); + + create_random_transaction( 'test1@example.com', $trans_day ); + if ( $count % 2 ) { + create_random_transaction( 'test2@example.com', $trans_day ); + } + if ( $count % 3 ) { + create_random_transaction( 'test3@example.com', $trans_day ); + } + if ( $count % 4 ) { + create_random_transaction( 'test4@example.com', $trans_day ); + } +} + +my $session_key = $framework->login({ + email => 'test1@example.com', + password => 'abc123', +}); + +use Data::Dumper; + +$t->post_ok('/api/outgoing-transactions' => json => { + session_key => $session_key, + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true) + ->json_has('/transactions') + ->json_has('/transactions/1/seller') + ->json_has('/transactions/1/value') + ->json_has('/transactions/1/purchase_time'); + + +sub create_random_transaction { + my $buyer = shift; + my $time = shift; + + my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity; + my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity; + $schema->resultset('Transaction')->create({ + buyer => $buyer_result, + seller => $seller_result, + value => 10, + proof_image => 'a', + purchase_time => $time, + }); +} + +done_testing;