2017-04-06 21:43:27 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Admin;
|
2017-02-24 19:27:43 +00:00
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
2017-04-21 13:14:51 +00:00
|
|
|
|
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
2017-04-21 13:54:19 +00:00
|
|
|
target_organisation_id => {
|
|
|
|
required => { message => 'target_organisation_id is missing', status => 400 },
|
|
|
|
number => { message => 'target_organisation_id is not a number', status => 400 },
|
|
|
|
in_resultset => { message => 'target_organisation_id does not exist in the database', status => 400 },
|
|
|
|
},
|
2017-04-21 13:14:51 +00:00
|
|
|
pending_organisation_id => {
|
|
|
|
required => { message => 'pending_organisation_id is missing', status => 400 },
|
|
|
|
number => { message => 'pending_organisation_id is not a number', status => 400 },
|
|
|
|
in_resultset => { message => 'pending_organisation_id does not exist in the database', status => 400 },
|
|
|
|
},
|
|
|
|
postcode => {
|
|
|
|
postcode => { message => 'postcode is not a valid uk postcode', status => 400 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
sub auth {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
if ( defined $c->stash->{ api_user }->administrator ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Not Authorised',
|
|
|
|
},
|
|
|
|
status => 403,
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
sub post_admin_approve {
|
2017-04-20 00:27:18 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
2017-04-21 13:14:51 +00:00
|
|
|
my $validation = $c->validation;
|
|
|
|
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
|
|
|
|
|
|
|
my $pending_org_rs = $c->schema->resultset('PendingOrganisation');
|
|
|
|
$validation->required('pending_organisation_id')->number->in_resultset('id', $pending_org_rs);
|
|
|
|
$validation->optional('name');
|
|
|
|
$validation->optional('street_name');
|
|
|
|
$validation->optional('town');
|
|
|
|
$validation->optional('postcode')->postcode;
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
my $pending_org = $pending_org_rs->find( $validation->param('pending_organisation_id') );
|
|
|
|
|
|
|
|
my $valid_org = $c->schema->resultset('Organisation')->create({
|
|
|
|
name => $validation->param('name') || $pending_org->name,
|
|
|
|
street_name => $validation->param('street_name') || $pending_org->street_name,
|
|
|
|
town => $validation->param('town') || $pending_org->town,
|
|
|
|
postcode => $validation->param('postcode') || $pending_org->postcode,
|
|
|
|
});
|
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
$c->copy_transactions_and_delete( $pending_org, $valid_org );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
return $c->render(
|
2017-04-21 13:14:51 +00:00
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Successfully Approved Organisation',
|
2017-02-24 19:27:43 +00:00
|
|
|
},
|
2017-04-21 13:14:51 +00:00
|
|
|
status => 200,
|
|
|
|
);
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-06 03:35:49 +00:00
|
|
|
sub post_admin_merge {
|
2017-04-21 13:54:19 +00:00
|
|
|
my $c = shift;
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
my $validation = $c->validation;
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
$validation->input( $c->stash->{api_json} );
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
my $pending_org_rs = $c->schema->resultset('PendingOrganisation');
|
|
|
|
$validation->required('pending_organisation_id')->number->in_resultset('id', $pending_org_rs);
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
my $valid_org_rs = $c->schema->resultset('Organisation');
|
|
|
|
$validation->required('target_organisation_id')->number->in_resultset('id', $valid_org_rs);
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
return $c->api_validation_error if $validation->has_error;
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
my $pending_org = $pending_org_rs->find( $validation->param('pending_organisation_id') );
|
|
|
|
my $target_org = $valid_org_rs->find( $validation->param('target_organisation_id') );
|
|
|
|
|
|
|
|
$c->copy_transactions_and_delete( $pending_org, $target_org );
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Successfully Merged Organisations',
|
2017-03-06 03:35:49 +00:00
|
|
|
},
|
2017-04-21 13:54:19 +00:00
|
|
|
status => 200,
|
|
|
|
);
|
|
|
|
}
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
sub copy_transactions_and_delete {
|
|
|
|
my ( $c, $from_org, $to_org ) = @_;
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
my $from_org_transaction_rs = $from_org->transactions;
|
2017-03-06 03:35:49 +00:00
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
while ( my $from_org_transaction = $from_org_transaction_rs->next ) {
|
|
|
|
$to_org->create_related(
|
|
|
|
'transactions', {
|
2017-04-21 21:12:53 +00:00
|
|
|
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,
|
2017-04-21 13:54:19 +00:00
|
|
|
}
|
|
|
|
);
|
2017-03-06 03:35:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:54:19 +00:00
|
|
|
$from_org->delete;
|
2017-03-06 03:35:49 +00:00
|
|
|
}
|
2017-03-02 15:46:35 +00:00
|
|
|
|
2017-02-24 19:27:43 +00:00
|
|
|
1;
|
|
|
|
|