2017-04-06 21:43:27 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Register;
|
2017-02-24 19:27:43 +00:00
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
2017-04-18 16:36:37 +00:00
|
|
|
use DateTime;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 11:38:39 +00:00
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
|
|
|
token => {
|
|
|
|
required => { message => 'No token sent.', status => 400 },
|
|
|
|
in_resultset => { message => 'Token invalid or has been used.', status => 401 },
|
|
|
|
},
|
2017-04-22 18:35:19 +00:00
|
|
|
name => {
|
|
|
|
required => { message => 'No name sent or was blank.', status => 400 },
|
2017-04-18 11:38:39 +00:00
|
|
|
},
|
2017-04-25 20:44:13 +00:00
|
|
|
display_name => {
|
|
|
|
required => { message => 'No name sent or was blank.', status => 400 },
|
|
|
|
},
|
|
|
|
full_name => {
|
|
|
|
required => { message => 'No name sent or was blank.', status => 400 },
|
|
|
|
},
|
2017-04-18 11:38:39 +00:00
|
|
|
email => {
|
|
|
|
required => { message => 'No email sent.', status => 400 },
|
|
|
|
email => { message => 'Email is invalid.', status => 400 },
|
|
|
|
not_in_resultset => { message => 'Email exists.', status => 403 },
|
|
|
|
},
|
|
|
|
postcode => {
|
|
|
|
required => { message => 'No postcode sent.', status => 400 },
|
2017-04-22 18:35:19 +00:00
|
|
|
postcode => { message => 'Postcode is invalid', status => 400 },
|
2017-04-18 11:38:39 +00:00
|
|
|
},
|
|
|
|
password => {
|
|
|
|
required => { message => 'No password sent.', status => 400 },
|
|
|
|
},
|
|
|
|
usertype => {
|
|
|
|
required => { message => 'No usertype sent.', status => 400 },
|
|
|
|
in => { message => '"usertype" is invalid.', status => 400 },
|
|
|
|
},
|
2017-06-13 20:02:24 +00:00
|
|
|
year_of_birth => {
|
|
|
|
required => { message => 'No year_of_birth sent.', status => 400 },
|
|
|
|
number => { message => 'year_of_birth is invalid', status => 400 },
|
|
|
|
gt_num => { message => 'year_of_birth must be within last 150 years', status => 400 },
|
|
|
|
lt_num => { message => 'year_of_birth must be atleast 10 years ago', status => 400 },
|
2017-04-18 11:38:39 +00:00
|
|
|
},
|
2017-04-21 13:14:51 +00:00
|
|
|
street_name => {
|
|
|
|
required => { message => 'No street_name sent.', status => 400 },
|
2017-04-20 21:17:13 +00:00
|
|
|
},
|
|
|
|
town => {
|
|
|
|
required => { message => 'No town sent.', status => 400 },
|
2017-04-18 11:38:39 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-08-31 15:46:25 +00:00
|
|
|
sub post_register {
|
2017-04-18 11:38:39 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
2017-04-22 18:35:19 +00:00
|
|
|
$validation->input( $c->stash->{api_json} );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 11:38:39 +00:00
|
|
|
my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0});
|
2017-04-21 20:28:50 +00:00
|
|
|
$validation->required('token')->in_resultset('name', $token_rs);
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 11:38:39 +00:00
|
|
|
my $user_rs = $c->schema->resultset('User');
|
|
|
|
$validation->required('email')->email->not_in_resultset('email', $user_rs);
|
|
|
|
$validation->required('password');
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-22 18:35:19 +00:00
|
|
|
$validation->required('postcode')->postcode;
|
2017-04-18 11:38:39 +00:00
|
|
|
$validation->required('usertype')->in('customer', 'organisation');
|
|
|
|
|
|
|
|
my $usertype = $validation->param('usertype') || '';
|
|
|
|
|
|
|
|
if ( $usertype eq 'customer' ) {
|
2017-04-25 20:44:13 +00:00
|
|
|
$validation->required('display_name');
|
|
|
|
$validation->required('full_name');
|
2017-06-13 20:02:24 +00:00
|
|
|
my $year = DateTime->now->year;
|
|
|
|
$validation->required('year_of_birth')->number->gt_num($year - 150)->lt_num($year - 10);
|
2017-04-18 11:38:39 +00:00
|
|
|
} elsif ( $usertype eq 'organisation' ) {
|
2017-04-25 20:44:13 +00:00
|
|
|
$validation->required('name');
|
2017-04-21 13:14:51 +00:00
|
|
|
$validation->required('street_name');
|
2017-04-20 21:17:13 +00:00
|
|
|
$validation->required('town');
|
2017-08-25 14:25:52 +00:00
|
|
|
$validation->required('sector');
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 18:35:19 +00:00
|
|
|
return $c->api_validation_error if $validation->has_error;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 11:38:39 +00:00
|
|
|
if ($usertype eq 'customer'){
|
|
|
|
|
2017-04-18 16:36:37 +00:00
|
|
|
$c->schema->txn_do( sub {
|
|
|
|
$c->schema->resultset('AccountToken')->find({
|
2017-04-21 20:28:50 +00:00
|
|
|
name => $validation->param('token'),
|
2017-04-18 16:36:37 +00:00
|
|
|
used => 0,
|
|
|
|
})->update({ used => 1 });
|
2017-08-31 15:46:25 +00:00
|
|
|
|
|
|
|
$c->schema->resultset('Entity')->create({
|
|
|
|
customer => {
|
|
|
|
full_name => $validation->param('full_name'),
|
|
|
|
display_name => $validation->param('display_name'),
|
|
|
|
year_of_birth => $validation->param('year_of_birth'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
},
|
|
|
|
user => {
|
|
|
|
email => $validation->param('email'),
|
|
|
|
password => $validation->param('password'),
|
|
|
|
},
|
|
|
|
type => 'customer',
|
2017-04-18 16:36:37 +00:00
|
|
|
});
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
elsif ($usertype eq 'organisation') {
|
|
|
|
|
2017-04-18 16:36:37 +00:00
|
|
|
$c->schema->txn_do( sub {
|
|
|
|
$c->schema->resultset('AccountToken')->find({
|
2017-04-21 20:28:50 +00:00
|
|
|
name => $validation->param('token'),
|
2017-04-18 16:36:37 +00:00
|
|
|
used => 0,
|
|
|
|
})->update({ used => 1 });
|
2017-08-31 15:46:25 +00:00
|
|
|
$c->schema->resultset('Entity')->create({
|
|
|
|
organisation => {
|
|
|
|
name => $validation->param('name'),
|
|
|
|
street_name => $validation->param('street_name'),
|
|
|
|
town => $validation->param('town'),
|
|
|
|
sector => $validation->param('sector'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
},
|
|
|
|
user => {
|
|
|
|
email => $validation->param('email'),
|
|
|
|
password => $validation->param('password'),
|
|
|
|
},
|
|
|
|
type => 'organisation',
|
2017-04-18 16:36:37 +00:00
|
|
|
});
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
2017-04-18 16:36:37 +00:00
|
|
|
|
2017-04-20 21:17:13 +00:00
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Registered Successfully',
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|