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 {
|
2017-04-18 09:44:07 +00:00
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('User');
|
|
|
|
};
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
has customer_result_set => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Customer');
|
|
|
|
};
|
|
|
|
|
|
|
|
has organisation_result_set => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Organisation');
|
|
|
|
};
|
|
|
|
|
2017-04-18 09:44:07 +00:00
|
|
|
sub index {
|
|
|
|
my $c = shift;
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
my $user_rs = $c->user_result_set;
|
2017-04-18 09:44:07 +00:00
|
|
|
$user_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
|
|
|
$c->stash( users => [ $user_rs->all ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub read {
|
|
|
|
my $c = shift;
|
|
|
|
|
2017-04-18 18:03:03 +00:00
|
|
|
my $id = $c->param('id');
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
if ( my $user = $c->user_result_set->find($id) ) {
|
2017-04-18 18:03:03 +00:00
|
|
|
$c->stash( user => $user );
|
|
|
|
} else {
|
|
|
|
$c->flash( error => 'No User found' );
|
|
|
|
$c->redirect_to( '/admin/users' );
|
|
|
|
}
|
2017-04-18 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 15:50:04 +00:00
|
|
|
sub edit {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $id = $c->param('id');
|
|
|
|
|
2017-08-23 17:42:19 +00:00
|
|
|
my $user;
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
unless ( $user = $c->user_result_set->find($id) ) {
|
2017-08-23 15:50:04 +00:00
|
|
|
$c->flash( error => 'No User found' );
|
2017-08-23 17:42:19 +00:00
|
|
|
return $c->redirect_to( '/admin/users/' . $id );
|
2017-08-23 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
|
2017-08-24 11:37:53 +00:00
|
|
|
my $not_myself_user_rs = $c->user_result_set->search({
|
2017-08-23 17:42:19 +00:00
|
|
|
id => { "!=" => $user->id },
|
|
|
|
});
|
|
|
|
$validation->required('email')->email->not_in_resultset( 'email', $not_myself_user_rs );
|
2017-08-23 15:50:04 +00:00
|
|
|
$validation->required('postcode')->postcode;
|
|
|
|
$validation->optional('new_password');
|
|
|
|
|
|
|
|
if ( defined $user->customer_id ) {
|
|
|
|
$validation->required('display_name');
|
|
|
|
$validation->required('full_name');
|
|
|
|
} elsif ( defined $user->organisation_id ) {
|
|
|
|
$validation->required('name');
|
|
|
|
$validation->required('street_name');
|
|
|
|
$validation->required('town');
|
2017-08-25 14:25:52 +00:00
|
|
|
$validation->optional('sector');
|
2017-08-23 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
$c->flash( error => 'The validation has failed' );
|
|
|
|
$c->app->log->warn(Dumper $validation);
|
|
|
|
return $c->redirect_to( '/admin/users/' . $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( defined $user->customer_id ){
|
|
|
|
|
2017-08-23 17:42:19 +00:00
|
|
|
try {
|
|
|
|
$c->schema->txn_do( sub {
|
|
|
|
$user->customer->update({
|
|
|
|
full_name => $validation->param('full_name'),
|
|
|
|
display_name => $validation->param('display_name'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
});
|
|
|
|
$user->update({
|
|
|
|
email => $validation->param('email'),
|
|
|
|
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
|
|
|
});
|
2017-08-23 15:50:04 +00:00
|
|
|
});
|
2017-08-23 17:42:19 +00:00
|
|
|
} finally {
|
|
|
|
if ( @_ ) {
|
|
|
|
$c->flash( error => 'Something went wrong Updating the User' );
|
|
|
|
$c->app->log->warn(Dumper @_);
|
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Updated User' );
|
|
|
|
};
|
|
|
|
}
|
2017-08-23 15:50:04 +00:00
|
|
|
}
|
|
|
|
elsif ( defined $user->organisation_id ) {
|
|
|
|
|
2017-08-23 17:42:19 +00:00
|
|
|
try {
|
|
|
|
$c->schema->txn_do( sub {
|
|
|
|
$user->organisation->update({
|
|
|
|
name => $validation->param('name'),
|
|
|
|
street_name => $validation->param('street_name'),
|
|
|
|
town => $validation->param('town'),
|
2017-08-25 14:25:52 +00:00
|
|
|
sector => $validation->param('sector'),
|
2017-08-23 17:42:19 +00:00
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
});
|
|
|
|
$user->update({
|
|
|
|
email => $validation->param('email'),
|
|
|
|
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
|
|
|
});
|
2017-08-23 15:50:04 +00:00
|
|
|
});
|
2017-08-23 17:42:19 +00:00
|
|
|
} finally {
|
|
|
|
if ( @_ ) {
|
|
|
|
$c->flash( error => 'Something went wrong Updating the User' );
|
|
|
|
$c->app->log->warn(Dumper @_);
|
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Updated User' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-08-23 15:50:04 +00:00
|
|
|
|
|
|
|
$c->redirect_to( '/admin/users/' . $id );
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:03:03 +00:00
|
|
|
sub update {
|
2017-04-18 09:44:07 +00:00
|
|
|
my $c = shift;
|
2017-04-18 18:03:03 +00:00
|
|
|
$c->redirect_to( '/admin/users' );
|
2017-04-18 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|