From 98f15a67afb2f8a58043801bcf49624a4abd629f Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 24 Apr 2017 12:49:18 +0100 Subject: [PATCH] Added organisations page to admin interface --- lib/Pear/LocalLoop.pm | 27 ++++++++++ .../Controller/Admin/Organisations.pm | 42 +++++++++++++++ lib/Pear/LocalLoop/Controller/Api/Admin.pm | 19 ------- lib/Pear/LocalLoop/Schema/Result/User.pm | 12 +++++ templates/admin/organisations/list.html.ep | 52 +++++++++++++++++++ .../admin/organisations/pending_read.html.ep | 42 +++++++++++++++ .../admin/organisations/valid_read.html.ep | 41 +++++++++++++++ templates/layouts/admin.html.ep | 1 + 8 files changed, 217 insertions(+), 19 deletions(-) create mode 100644 lib/Pear/LocalLoop/Controller/Admin/Organisations.pm create mode 100644 templates/admin/organisations/list.html.ep create mode 100644 templates/admin/organisations/pending_read.html.ep create mode 100644 templates/admin/organisations/valid_read.html.ep diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index a43e3cb..bfd43ce 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -143,16 +143,23 @@ sub startup { my $admin_routes = $r->under('/admin')->to('admin#under'); $admin_routes->get('/')->to('admin#home'); + $admin_routes->get('/tokens')->to('admin-tokens#index'); $admin_routes->post('/tokens')->to('admin-tokens#create'); $admin_routes->get('/tokens/:id')->to('admin-tokens#read'); $admin_routes->post('/tokens/:id')->to('admin-tokens#update'); $admin_routes->post('/tokens/:id/delete')->to('admin-tokens#delete'); + $admin_routes->get('/users')->to('admin-users#index'); $admin_routes->get('/users/:id')->to('admin-users#read'); $admin_routes->post('/users/:id')->to('admin-users#update'); $admin_routes->post('/users/:id/delete')->to('admin-users#delete'); + $admin_routes->get('/organisations')->to('admin-organisations#list'); + $admin_routes->get('/organisations/valid/:id')->to('admin-organisations#valid_read'); + $admin_routes->get('/organisations/pending/:id')->to('admin-organisations#pending_read'); + $admin_routes->get('/organisations/pending/:id/approve')->to('admin-organisations#pending_approve'); + my $user_routes = $r->under('/')->to('root#under'); $user_routes->get('/home')->to('root#home'); @@ -160,12 +167,32 @@ sub startup { my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under'); $portal_api->post('/upload')->to('api-upload#post_upload'); + $portal_api->post('/search')->to('api-upload#post_search'); $self->hook( before_dispatch => sub { my $self = shift; $self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development'; }); + + $self->helper( copy_transactions_and_delete => sub { + my ( $c, $from_org, $to_org ) = @_; + + my $from_org_transaction_rs = $from_org->transactions; + + while ( my $from_org_transaction = $from_org_transaction_rs->next ) { + $to_org->create_related( + 'transactions', { + buyer_id => $from_org_transaction->buyer_id, + value => $from_org_transaction->value, + proof_image => $from_org_transaction->proof_image, + submitted_at => $from_org_transaction->submitted_at, + } + ); + } + + $from_org->delete; + }); } 1; diff --git a/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm new file mode 100644 index 0000000..d1b39b3 --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm @@ -0,0 +1,42 @@ +package Pear::LocalLoop::Controller::Admin::Organisations; +use Mojo::Base 'Mojolicious::Controller'; + +sub list { + my $c = shift; + + my $valid_orgs_rs = $c->schema->resultset('Organisation'); + my $pending_orgs_rs = $c->schema->resultset('PendingOrganisation'); + + $c->stash( + valid_orgs_rs => $valid_orgs_rs, + pending_orgs_rs => $pending_orgs_rs, + ); +} + +sub valid_read { + my $c = shift; + my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') ); + $c->stash( valid_org => $valid_org ); +} + +sub pending_read { + my $c = shift; + my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') ); + $c->stash( pending_org => $pending_org ); +} + +sub pending_approve { + my $c = shift; + my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') ); + my $valid_org = $c->schema->resultset('Organisation')->create({ + name => $pending_org->name, + street_name => $pending_org->street_name, + town => $pending_org->town, + postcode => $pending_org->postcode, + }); + $c->copy_transactions_and_delete( $pending_org, $valid_org ); + $c->flash( success => 'Validated Organisation' ); + $c->redirect_to( '/admin/organisations/valid/' . $valid_org->id ); +} + +1; diff --git a/lib/Pear/LocalLoop/Controller/Api/Admin.pm b/lib/Pear/LocalLoop/Controller/Api/Admin.pm index df1440e..e5d629a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Admin.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Admin.pm @@ -102,24 +102,5 @@ sub post_admin_merge { ); } -sub copy_transactions_and_delete { - my ( $c, $from_org, $to_org ) = @_; - - my $from_org_transaction_rs = $from_org->transactions; - - while ( my $from_org_transaction = $from_org_transaction_rs->next ) { - $to_org->create_related( - 'transactions', { - buyer_id => $from_org_transaction->buyer_id, - value => $from_org_transaction->value, - proof_image => $from_org_transaction->proof_image, - submitted_at => $from_org_transaction->submitted_at, - } - ); - } - - $from_org->delete; -} - 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index cf82e40..6974cb4 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -134,4 +134,16 @@ sub generate_session { return $token; } +sub name { + my $self = shift; + + if ( defined $self->customer_id ) { + return $self->customer->name; + } elsif ( defined $self->organisation_id ) { + return $self->organisation->name; + } else { + return undef; + } +} + 1; diff --git a/templates/admin/organisations/list.html.ep b/templates/admin/organisations/list.html.ep new file mode 100644 index 0000000..75e7670 --- /dev/null +++ b/templates/admin/organisations/list.html.ep @@ -0,0 +1,52 @@ +% layout 'admin'; +% title 'Organisations'; +% content_for javascript => begin +% end +% if ( my $error = flash 'error' ) { + +% } elsif ( my $success = flash 'success' ) { + +% } +
+
+
+

+ Valid Organisations +

+
+ % for my $valid_org ($valid_orgs_rs->all) { + +
+ %= $valid_org->name +
+
+ % } +
+
+
+
+
+

+ Pending Organisations +

+
+ % if ( $pending_orgs_rs->count == 0 ) { +
+ No Pending Organisations! +
+ % } else { + % for my $pending_org ($pending_orgs_rs->all) { + +
+ %= $pending_org->name +
+
+ % }} +
+
+
+
diff --git a/templates/admin/organisations/pending_read.html.ep b/templates/admin/organisations/pending_read.html.ep new file mode 100644 index 0000000..6f1c8c4 --- /dev/null +++ b/templates/admin/organisations/pending_read.html.ep @@ -0,0 +1,42 @@ +% layout 'admin'; +% title 'Organisations'; +% content_for javascript => begin +% end +% if ( my $error = flash 'error' ) { + +% } elsif ( my $success = flash 'success' ) { + +% } +
+

+ %= $pending_org->name +

+ +
+
+

+ Transactions +

+ +
diff --git a/templates/admin/organisations/valid_read.html.ep b/templates/admin/organisations/valid_read.html.ep new file mode 100644 index 0000000..fccf361 --- /dev/null +++ b/templates/admin/organisations/valid_read.html.ep @@ -0,0 +1,41 @@ +% layout 'admin'; +% title 'Organisations'; +% content_for javascript => begin +% end +% if ( my $error = flash 'error' ) { + +% } elsif ( my $success = flash 'success' ) { + +% } +
+

+ %= $valid_org->name +

+ +
+
+

+ Transactions +

+ +
diff --git a/templates/layouts/admin.html.ep b/templates/layouts/admin.html.ep index a929f1f..213b6ef 100644 --- a/templates/layouts/admin.html.ep +++ b/templates/layouts/admin.html.ep @@ -31,6 +31,7 @@ User Section Tokens Users + Organisations Logout