2017-04-06 21:43:27 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::User;
|
2017-03-08 18:50:25 +00:00
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
2017-04-23 15:59:35 +00:00
|
|
|
use Mojo::JSON;
|
|
|
|
|
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
|
|
|
day => {
|
|
|
|
is_iso_datetime => { message => 'Invalid ISO8601 Datetime', status => 400 },
|
|
|
|
},
|
2017-07-26 18:05:35 +00:00
|
|
|
name => {
|
|
|
|
required => { message => 'No name sent or was blank.', status => 400 },
|
|
|
|
},
|
|
|
|
display_name => {
|
2017-07-27 13:18:22 +00:00
|
|
|
required => { message => 'No display name sent or was blank.', status => 400 },
|
2017-07-26 18:05:35 +00:00
|
|
|
},
|
|
|
|
full_name => {
|
2017-07-27 13:18:22 +00:00
|
|
|
required => { message => 'No full name sent or was blank.', status => 400 },
|
2017-07-26 18:05:35 +00:00
|
|
|
},
|
|
|
|
email => {
|
|
|
|
required => { message => 'No email sent.', status => 400 },
|
|
|
|
email => { message => 'Email is invalid.', status => 400 },
|
|
|
|
},
|
|
|
|
postcode => {
|
|
|
|
required => { message => 'No postcode sent.', status => 400 },
|
|
|
|
postcode => { message => 'Postcode is invalid', status => 400 },
|
|
|
|
},
|
|
|
|
password => {
|
|
|
|
required => { message => 'No password sent.', status => 400 },
|
|
|
|
},
|
|
|
|
street_name => {
|
2017-09-18 10:34:22 +00:00
|
|
|
required => { message => 'No street name sent.', status => 400 },
|
2017-07-26 18:05:35 +00:00
|
|
|
},
|
|
|
|
town => {
|
2017-09-18 10:34:22 +00:00
|
|
|
required => { message => 'No town/city sent.', status => 400 },
|
2017-07-26 18:05:35 +00:00
|
|
|
},
|
2017-09-04 16:05:10 +00:00
|
|
|
sector => {
|
|
|
|
required => { message => 'No sector sent.', status => 400 },
|
|
|
|
},
|
2017-04-23 15:59:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-26 17:28:14 +00:00
|
|
|
sub post_account {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
2017-07-27 13:18:22 +00:00
|
|
|
my $user_result = $c->schema->resultset('User')->find({ id => $c->stash->{api_user}->id });
|
2017-07-26 17:28:14 +00:00
|
|
|
|
|
|
|
if ( defined $user_result ) {
|
|
|
|
my $email = $user_result->email;
|
|
|
|
|
2017-08-31 18:08:22 +00:00
|
|
|
if ( $user_result->type eq 'customer' ) {
|
2017-10-03 14:33:43 +00:00
|
|
|
my $customer = $user_result->entity->customer;
|
|
|
|
my $full_name = $customer->full_name;
|
|
|
|
my $display_name = $customer->display_name;
|
|
|
|
my $postcode = $customer->postcode;
|
2017-09-04 15:12:03 +00:00
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
full_name => $full_name,
|
|
|
|
display_name => $display_name,
|
|
|
|
email => $email,
|
|
|
|
postcode => $postcode,
|
2017-10-03 14:33:43 +00:00
|
|
|
location => {
|
|
|
|
latitude => (defined $customer->latitude ? $customer->latitude * 1 : undef),
|
|
|
|
longitude => (defined $customer->longitude ? $customer->longitude * 1 : undef),
|
|
|
|
},
|
2017-09-04 15:12:03 +00:00
|
|
|
});
|
2017-08-31 18:08:22 +00:00
|
|
|
} elsif ( $user_result->type eq 'organisation' ) {
|
2017-10-03 14:33:43 +00:00
|
|
|
my $organisation = $user_result->entity->organisation;
|
|
|
|
my $name = $organisation->name;
|
|
|
|
my $postcode = $organisation->postcode;
|
|
|
|
my $street_name = $organisation->street_name;
|
|
|
|
my $town = $organisation->town;
|
|
|
|
my $sector = $organisation->sector;
|
2017-09-04 15:12:03 +00:00
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
town => $town,
|
|
|
|
name => $name,
|
2017-09-04 16:05:10 +00:00
|
|
|
sector => $sector,
|
2017-09-04 15:12:03 +00:00
|
|
|
street_name => $street_name,
|
|
|
|
email => $email,
|
|
|
|
postcode => $postcode,
|
2017-10-03 14:33:43 +00:00
|
|
|
location => {
|
|
|
|
latitude => (defined $organisation->latitude ? $organisation->latitude * 1 : undef),
|
|
|
|
longitude => (defined $organisation->longitude ? $organisation->longitude * 1 : undef),
|
|
|
|
},
|
2017-09-04 15:12:03 +00:00
|
|
|
});
|
2017-07-26 17:28:14 +00:00
|
|
|
} else {
|
2017-09-04 15:12:03 +00:00
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Invalid Server Error.',
|
|
|
|
},
|
|
|
|
status => 500
|
|
|
|
);
|
2017-07-26 17:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Email or password is invalid.',
|
|
|
|
},
|
|
|
|
status => 401
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub post_account_update {
|
2017-07-26 17:57:13 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
2017-07-27 13:18:22 +00:00
|
|
|
|
2017-07-26 17:57:13 +00:00
|
|
|
my $validation = $c->validation;
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
2017-07-27 13:18:22 +00:00
|
|
|
$validation->required('password');
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
if ( ! $user->check_password($validation->param('password')) ) {
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'password is invalid.',
|
|
|
|
},
|
|
|
|
status => 401
|
|
|
|
);
|
|
|
|
}
|
2017-07-26 17:57:13 +00:00
|
|
|
|
2017-07-27 13:18:22 +00:00
|
|
|
my $user_rs = $c->schema->resultset('User')->search({
|
|
|
|
id => { "!=" => $user->id },
|
|
|
|
});
|
2017-07-26 17:57:13 +00:00
|
|
|
|
2017-07-27 13:18:22 +00:00
|
|
|
$validation->required('email')->not_in_resultset( 'email', $user_rs );
|
2017-07-26 17:57:13 +00:00
|
|
|
$validation->required('postcode')->postcode;
|
2017-07-27 13:18:22 +00:00
|
|
|
$validation->optional('new_password');
|
2017-07-26 17:57:13 +00:00
|
|
|
|
2017-08-31 18:08:22 +00:00
|
|
|
if ( $user->type eq 'customer' ) {
|
2017-07-26 17:57:13 +00:00
|
|
|
$validation->required('display_name');
|
|
|
|
$validation->required('full_name');
|
2017-08-31 18:08:22 +00:00
|
|
|
} elsif ( $user->type eq 'organisation' ) {
|
2017-07-26 17:57:13 +00:00
|
|
|
$validation->required('name');
|
|
|
|
$validation->required('street_name');
|
|
|
|
$validation->required('town');
|
2017-08-25 14:25:52 +00:00
|
|
|
$validation->required('sector');
|
2017-07-26 17:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
2017-10-03 14:47:05 +00:00
|
|
|
my $location = $c->get_location_from_postcode(
|
|
|
|
$validation->param('postcode'),
|
|
|
|
$user->type,
|
|
|
|
);
|
|
|
|
|
2017-08-31 18:08:22 +00:00
|
|
|
if ( $user->type eq 'customer' ){
|
2017-07-26 17:28:14 +00:00
|
|
|
|
2017-07-26 17:57:13 +00:00
|
|
|
$c->schema->txn_do( sub {
|
2017-08-31 18:08:22 +00:00
|
|
|
$user->entity->customer->update({
|
2017-07-26 17:57:13 +00:00
|
|
|
full_name => $validation->param('full_name'),
|
|
|
|
display_name => $validation->param('display_name'),
|
|
|
|
postcode => $validation->param('postcode'),
|
2017-10-03 14:47:05 +00:00
|
|
|
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
|
2017-07-26 17:57:13 +00:00
|
|
|
});
|
2017-07-27 13:18:22 +00:00
|
|
|
$user->update({
|
|
|
|
email => $validation->param('email'),
|
|
|
|
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
2017-07-26 17:57:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2017-08-31 18:08:22 +00:00
|
|
|
elsif ( $user->type eq 'organisation' ) {
|
2017-07-26 17:57:13 +00:00
|
|
|
|
|
|
|
$c->schema->txn_do( sub {
|
2017-08-31 18:08:22 +00:00
|
|
|
$user->entity->organisation->update({
|
2017-07-26 17:57:13 +00:00
|
|
|
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-07-26 17:57:13 +00:00
|
|
|
postcode => $validation->param('postcode'),
|
2017-10-03 14:47:05 +00:00
|
|
|
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
|
2017-07-26 17:57:13 +00:00
|
|
|
});
|
2017-07-27 13:18:22 +00:00
|
|
|
$user->update({
|
2017-07-26 17:57:13 +00:00
|
|
|
email => $validation->param('email'),
|
2017-07-27 13:18:22 +00:00
|
|
|
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
2017-07-26 17:57:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Edited Account Successfully',
|
|
|
|
});
|
2017-07-26 17:28:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 15:59:35 +00:00
|
|
|
1;
|