From 9dd808c547b6a344a4ccdf8d6b07abd58453b674 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Jul 2017 14:33:11 +0100 Subject: [PATCH] Fix issue with approving organisations. --- lib/Pear/LocalLoop.pm | 2 +- .../Controller/Admin/Organisations.pm | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 59ced68..2c92757 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -204,8 +204,8 @@ sub startup { submitted_at => $from_org_transaction->submitted_at, } ); + $from_org_transaction->delete; } - $from_org->delete; }); } diff --git a/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm index d1b39b3..55f90f5 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm @@ -1,6 +1,8 @@ package Pear::LocalLoop::Controller::Admin::Organisations; use Mojo::Base 'Mojolicious::Controller'; +use Try::Tiny; + sub list { my $c = shift; @@ -28,15 +30,27 @@ sub pending_read { 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 ); + + my $valid_org; + try { + $c->schema->storage->txn_do( sub { + $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 ); + } ); + } finally { + if ( @_ ) { + $c->flash( error => 'Something went wrong Validating the Organisation' ); + $c->redirect_to( '/admin/organisations/pending/' . $pending_org->id ); + } else { + $c->flash( success => 'Validated Organisation' ); + $c->redirect_to( '/admin/organisations/valid/' . $valid_org->id ); + } + } } 1;