2017-04-18 10:44:07 +01:00
package Pear::LocalLoop::Controller::Admin::Users ;
use Mojo::Base 'Mojolicious::Controller' ;
2017-08-24 12:37:53 +01:00
use Try::Tiny ;
2017-08-23 18:42:19 +01:00
use Data::Dumper ;
2017-08-24 12:37:53 +01:00
has user_result_set = > sub {
2017-04-18 10:44:07 +01:00
my $ c = shift ;
return $ c - > schema - > resultset ( 'User' ) ;
} ;
2017-08-24 12:37:53 +01: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 10:44:07 +01:00
sub index {
my $ c = shift ;
2017-11-20 13:26:52 +00:00
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 ) ;
2017-04-18 10:44:07 +01:00
}
sub read {
my $ c = shift ;
2017-04-18 19:03:03 +01:00
my $ id = $ c - > param ( 'id' ) ;
2017-08-24 12:37:53 +01:00
if ( my $ user = $ c - > user_result_set - > find ( $ id ) ) {
2017-09-05 12:27:37 +01:00
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 ,
) ;
2017-04-18 19:03:03 +01:00
} else {
$ c - > flash ( error = > 'No User found' ) ;
$ c - > redirect_to ( '/admin/users' ) ;
}
2017-04-18 10:44:07 +01:00
}
2017-08-31 16:38:03 +01:00
sub update {
2017-08-23 16:50:04 +01:00
my $ c = shift ;
my $ id = $ c - > param ( 'id' ) ;
2017-08-23 18:42:19 +01:00
my $ user ;
2017-08-24 12:37:53 +01:00
unless ( $ user = $ c - > user_result_set - > find ( $ id ) ) {
2017-08-23 16:50:04 +01:00
$ c - > flash ( error = > 'No User found' ) ;
2017-08-23 18:42:19 +01:00
return $ c - > redirect_to ( '/admin/users/' . $ id ) ;
2017-08-23 16:50:04 +01:00
}
my $ validation = $ c - > validation ;
2017-08-24 12:37:53 +01:00
my $ not_myself_user_rs = $ c - > user_result_set - > search ( {
2017-08-23 18:42:19 +01:00
id = > { "!=" = > $ user - > id } ,
} ) ;
$ validation - > required ( 'email' ) - > email - > not_in_resultset ( 'email' , $ not_myself_user_rs ) ;
2017-08-23 16:50:04 +01:00
$ validation - > required ( 'postcode' ) - > postcode ;
$ validation - > optional ( 'new_password' ) ;
2017-08-31 16:38:03 +01:00
if ( $ user - > type eq 'customer' ) {
2017-08-23 16:50:04 +01:00
$ validation - > required ( 'display_name' ) ;
$ validation - > required ( 'full_name' ) ;
2017-08-31 16:38:03 +01:00
} elsif ( $ user - > type eq 'organisation' ) {
2017-08-23 16:50:04 +01:00
$ validation - > required ( 'name' ) ;
$ validation - > required ( 'street_name' ) ;
$ validation - > required ( 'town' ) ;
2017-08-25 15:25:52 +01:00
$ validation - > optional ( 'sector' ) ;
2017-08-23 16:50:04 +01:00
}
if ( $ validation - > has_error ) {
$ c - > flash ( error = > 'The validation has failed' ) ;
return $ c - > redirect_to ( '/admin/users/' . $ id ) ;
}
2017-12-15 14:59:38 +00:00
my $ location = $ c - > get_location_from_postcode (
$ validation - > param ( 'postcode' ) ,
$ user - > type ,
) ;
2017-08-31 16:38:03 +01:00
if ( $ user - > type eq 'customer' ) {
2017-08-23 16:50:04 +01:00
2017-08-23 18:42:19 +01:00
try {
$ c - > schema - > txn_do ( sub {
2017-08-31 16:38:03 +01:00
$ user - > entity - > customer - > update ( {
2017-08-23 18:42:19 +01:00
full_name = > $ validation - > param ( 'full_name' ) ,
display_name = > $ validation - > param ( 'display_name' ) ,
postcode = > $ validation - > param ( 'postcode' ) ,
2017-12-15 14:59:38 +00:00
( defined $ location ? ( %$ location ) : ( latitude = > undef , longitude = > undef ) ) ,
2017-08-23 18:42:19 +01:00
} ) ;
$ user - > update ( {
email = > $ validation - > param ( 'email' ) ,
( defined $ validation - > param ( 'new_password' ) ? ( password = > $ validation - > param ( 'new_password' ) ) : ( ) ) ,
} ) ;
2017-08-23 16:50:04 +01:00
} ) ;
2017-08-23 18:42:19 +01: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 16:50:04 +01:00
}
2017-08-31 16:38:03 +01:00
elsif ( $ user - > type eq 'organisation' ) {
2017-08-23 16:50:04 +01:00
2017-08-23 18:42:19 +01:00
try {
$ c - > schema - > txn_do ( sub {
2017-08-31 16:38:03 +01:00
$ user - > entity - > organisation - > update ( {
2017-08-23 18:42:19 +01:00
name = > $ validation - > param ( 'name' ) ,
street_name = > $ validation - > param ( 'street_name' ) ,
town = > $ validation - > param ( 'town' ) ,
2017-08-25 15:25:52 +01:00
sector = > $ validation - > param ( 'sector' ) ,
2017-08-23 18:42:19 +01:00
postcode = > $ validation - > param ( 'postcode' ) ,
2017-12-15 14:59:38 +00:00
( defined $ location ? ( %$ location ) : ( latitude = > undef , longitude = > undef ) ) ,
2017-08-23 18:42:19 +01:00
} ) ;
$ user - > update ( {
email = > $ validation - > param ( 'email' ) ,
( defined $ validation - > param ( 'new_password' ) ? ( password = > $ validation - > param ( 'new_password' ) ) : ( ) ) ,
} ) ;
2017-08-23 16:50:04 +01:00
} ) ;
2017-08-23 18:42:19 +01: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 16:50:04 +01:00
$ c - > redirect_to ( '/admin/users/' . $ id ) ;
}
2017-04-18 10:44:07 +01:00
1 ;