2017-04-18 09:44:07 +00:00
|
|
|
package Pear::LocalLoop::Controller::Admin::Users;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
use Try::Tiny;
|
2017-08-23 17:42:19 +00:00
|
|
|
use Data::Dumper;
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
has user_result_set => sub {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('User');
|
2017-04-18 09:44:07 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
has customer_result_set => sub {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Customer');
|
2017-08-24 11:37:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
has organisation_result_set => sub {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Organisation');
|
2017-08-24 11:37:53 +00:00
|
|
|
};
|
|
|
|
|
2021-03-20 19:03:59 +00:00
|
|
|
## no critic (Subroutines::ProhibitBuiltinHomonyms)
|
2017-04-18 09:44:07 +00:00
|
|
|
sub index {
|
2021-03-20 19:03:59 +00:00
|
|
|
## use critic
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user_rs = $c->user_result_set->search(
|
|
|
|
undef,
|
|
|
|
{
|
|
|
|
prefech => { entity => [qw/ customer organisation /] },
|
|
|
|
page => $c->param('page') || 1,
|
|
|
|
rows => 10,
|
|
|
|
order_by => { -asc => 'email' },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$c->stash( user_rs => $user_rs );
|
2021-03-20 23:26:52 +00:00
|
|
|
|
2021-03-20 15:02:00 +00:00
|
|
|
return 1;
|
2017-04-18 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:03:59 +00:00
|
|
|
## no critic (Subroutines::ProhibitBuiltinHomonyms)
|
2017-04-18 09:44:07 +00:00
|
|
|
sub read {
|
2021-03-20 19:03:59 +00:00
|
|
|
## use critic
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $id = $c->param('id');
|
|
|
|
|
|
|
|
if ( my $user = $c->user_result_set->find($id) ) {
|
|
|
|
my $transactions = $user->entity->purchases->search(
|
|
|
|
undef,
|
|
|
|
{
|
|
|
|
page => $c->param('page') || 1,
|
|
|
|
rows => 10,
|
|
|
|
order_by => { -desc => 'submitted_at' },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$c->stash(
|
|
|
|
user => $user,
|
|
|
|
transactions => $transactions,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$c->flash( error => 'No User found' );
|
|
|
|
$c->redirect_to('/admin/users');
|
|
|
|
}
|
2021-03-20 23:26:52 +00:00
|
|
|
|
2021-03-20 15:02:00 +00:00
|
|
|
return 1;
|
2021-03-20 12:09:50 +00:00
|
|
|
}
|
2017-04-18 09:44:07 +00:00
|
|
|
|
2021-03-20 19:03:59 +00:00
|
|
|
## no critic (Subroutines::ProhibitBuiltinHomonyms)
|
2021-03-20 12:09:50 +00:00
|
|
|
sub update {
|
2021-03-20 19:03:59 +00:00
|
|
|
## use critic
|
2021-03-20 23:26:52 +00:00
|
|
|
my ( $c, $error ) = @_;
|
2017-04-18 18:03:03 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $id = $c->param('id');
|
|
|
|
|
|
|
|
my $user;
|
|
|
|
|
|
|
|
unless ( $user = $c->user_result_set->find($id) ) {
|
|
|
|
$c->flash( error => 'No User found' );
|
|
|
|
return $c->redirect_to( '/admin/users/' . $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
|
|
|
|
my $not_myself_user_rs = $c->user_result_set->search(
|
|
|
|
{
|
|
|
|
id => { "!=" => $user->id },
|
|
|
|
}
|
2017-09-05 11:27:37 +00:00
|
|
|
);
|
2021-03-20 12:09:50 +00:00
|
|
|
$validation->required('email')
|
|
|
|
->email->not_in_resultset( 'email', $not_myself_user_rs );
|
|
|
|
$validation->required('postcode')->postcode;
|
|
|
|
$validation->optional('new_password');
|
|
|
|
|
|
|
|
if ( $user->type eq 'customer' ) {
|
|
|
|
$validation->required('display_name');
|
|
|
|
$validation->required('full_name');
|
|
|
|
}
|
|
|
|
elsif ( $user->type eq 'organisation' ) {
|
|
|
|
$validation->required('name');
|
|
|
|
$validation->required('street_name');
|
|
|
|
$validation->required('town');
|
|
|
|
$validation->optional('sector');
|
|
|
|
}
|
2017-04-18 09:44:07 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
if ( $validation->has_error ) {
|
|
|
|
$c->flash( error => 'The validation has failed' );
|
|
|
|
return $c->redirect_to( '/admin/users/' . $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
my $location =
|
|
|
|
$c->get_location_from_postcode( $validation->param('postcode'),
|
|
|
|
$user->type, );
|
|
|
|
|
|
|
|
if ( $user->type eq 'customer' ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
$c->schema->txn_do(
|
|
|
|
sub {
|
|
|
|
$user->entity->customer->update(
|
|
|
|
{
|
|
|
|
full_name => $validation->param('full_name'),
|
|
|
|
display_name => $validation->param('display_name'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
(
|
|
|
|
defined $location
|
|
|
|
? (%$location)
|
|
|
|
: ( latitude => undef, longitude => undef )
|
|
|
|
),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$user->update(
|
|
|
|
{
|
|
|
|
email => $validation->param('email'),
|
|
|
|
(
|
|
|
|
defined $validation->param('new_password')
|
|
|
|
? ( password =>
|
|
|
|
$validation->param('new_password') )
|
|
|
|
: ()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
finally {
|
2021-03-20 19:53:38 +00:00
|
|
|
if ($error) {
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->flash( error => 'Something went wrong Updating the User' );
|
2021-03-20 19:53:38 +00:00
|
|
|
$c->app->log->warn( Dumper $error );
|
2021-03-20 12:09:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$c->flash( success => 'Updated User' );
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 17:42:19 +00:00
|
|
|
}
|
2021-03-20 12:09:50 +00:00
|
|
|
elsif ( $user->type eq 'organisation' ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
$c->schema->txn_do(
|
|
|
|
sub {
|
|
|
|
$user->entity->organisation->update(
|
|
|
|
{
|
|
|
|
name => $validation->param('name'),
|
|
|
|
street_name => $validation->param('street_name'),
|
|
|
|
town => $validation->param('town'),
|
|
|
|
sector => $validation->param('sector'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
(
|
|
|
|
defined $location
|
|
|
|
? (%$location)
|
|
|
|
: ( latitude => undef, longitude => undef )
|
|
|
|
),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$user->update(
|
|
|
|
{
|
|
|
|
email => $validation->param('email'),
|
|
|
|
(
|
|
|
|
defined $validation->param('new_password')
|
|
|
|
? ( password =>
|
|
|
|
$validation->param('new_password') )
|
|
|
|
: ()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
finally {
|
2021-03-20 19:53:38 +00:00
|
|
|
if ($error) {
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->flash( error => 'Something went wrong Updating the User' );
|
2021-03-20 19:53:38 +00:00
|
|
|
$c->app->log->warn( Dumper $error );
|
2021-03-20 12:09:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$c->flash( success => 'Updated User' );
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 17:42:19 +00:00
|
|
|
}
|
2017-08-23 15:50:04 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->redirect_to( '/admin/users/' . $id );
|
2021-03-20 23:26:52 +00:00
|
|
|
|
2021-03-20 15:02:00 +00:00
|
|
|
return 1;
|
2017-08-23 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 09:44:07 +00:00
|
|
|
1;
|