2017-02-24 19:27:43 +00:00
|
|
|
package Pear::LocalLoop;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious';
|
|
|
|
use Data::UUID;
|
|
|
|
use Mojo::JSON;
|
|
|
|
use Pear::LocalLoop::Schema;
|
2017-04-18 21:31:08 +00:00
|
|
|
use DateTime;
|
2017-04-21 18:54:28 +00:00
|
|
|
use Mojo::Asset::File;
|
|
|
|
use Mojo::File qw/ path tempdir /;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 21:43:08 +00:00
|
|
|
has schema => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return Pear::LocalLoop::Schema->connect(
|
|
|
|
$c->app->config->{dsn},
|
|
|
|
$c->app->config->{user},
|
|
|
|
$c->app->config->{pass},
|
|
|
|
);
|
|
|
|
};
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
sub startup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
$self->plugin('Config', {
|
|
|
|
default => {
|
2017-04-21 18:54:28 +00:00
|
|
|
storage_path => tempdir,
|
2017-04-05 22:20:42 +00:00
|
|
|
sessionTimeSeconds => 60 * 60 * 24 * 7,
|
2017-04-18 21:31:08 +00:00
|
|
|
sessionTokenJsonName => 'session_key',
|
2017-04-05 22:20:42 +00:00
|
|
|
sessionExpiresJsonName => 'sessionExpires',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
my $config = $self->config;
|
|
|
|
|
2017-04-08 17:25:34 +00:00
|
|
|
$self->plugin('Pear::LocalLoop::Plugin::Validators');
|
|
|
|
|
2017-04-06 22:12:28 +00:00
|
|
|
$self->plugin('Authentication' => {
|
|
|
|
'load_user' => sub {
|
|
|
|
my ( $c, $user_id ) = @_;
|
|
|
|
return $c->schema->resultset('User')->find($user_id);
|
|
|
|
},
|
|
|
|
'validate_user' => sub {
|
|
|
|
my ( $c, $email, $password, $args) = @_;
|
|
|
|
my $user = $c->schema->resultset('User')->find({email => $email});
|
2017-04-18 21:44:59 +00:00
|
|
|
if ( defined $user ) {
|
|
|
|
if ( $user->check_password( $password ) ) {
|
2017-04-21 19:52:14 +00:00
|
|
|
return $user->id;
|
2017-04-18 21:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
2017-04-06 22:12:28 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
# shortcut for use in template
|
2017-04-21 19:03:51 +00:00
|
|
|
$self->helper( db => sub { warn "DEPRECATED db helper"; return $self->app->schema->storage->dbh });
|
2017-04-05 22:20:42 +00:00
|
|
|
$self->helper( schema => sub { $self->app->schema });
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
$self->helper( api_validation_error => sub {
|
|
|
|
my $c = shift;
|
|
|
|
my $failed_vals = $c->validation->failed;
|
|
|
|
for my $val ( @$failed_vals ) {
|
|
|
|
my $check = shift @{ $c->validation->error($val) };
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => $c->error_messages->{$val}->{$check}->{message},
|
2017-04-23 13:02:10 +00:00
|
|
|
error => $check,
|
2017-04-20 00:27:18 +00:00
|
|
|
},
|
|
|
|
status => $c->error_messages->{$val}->{$check}->{status},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2017-04-05 22:20:42 +00:00
|
|
|
|
2017-04-23 15:59:35 +00:00
|
|
|
$self->helper( datetime_formatter => sub {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
return DateTime::Format::Strptime->new(
|
|
|
|
pattern => '%FT%T%z',
|
|
|
|
strict => 1,
|
|
|
|
on_error => 'undef',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-04-21 18:54:28 +00:00
|
|
|
$self->helper( get_path_from_uuid => sub {
|
|
|
|
my $c = shift;
|
|
|
|
my $uuid = shift;
|
|
|
|
my ( $folder ) = $uuid =~ /(..)/;
|
|
|
|
return path($c->app->config->{storage_path}, $folder, $uuid);
|
|
|
|
});
|
|
|
|
|
|
|
|
$self->helper( store_file_from_upload => sub {
|
|
|
|
my $c = shift;
|
|
|
|
my $upload = shift;
|
|
|
|
my $uuid = Data::UUID->new->create_str;
|
|
|
|
my $path = $c->get_path_from_uuid( $uuid );
|
|
|
|
$path->dirname->make_path;
|
|
|
|
$upload->move_to( $path );
|
|
|
|
return $uuid;
|
|
|
|
});
|
|
|
|
|
|
|
|
$self->helper( get_file_from_uuid => sub {
|
|
|
|
my $c = shift;
|
|
|
|
my $uuid = shift;
|
|
|
|
return Mojo::Asset::File->new( path => $c->get_path_from_uuid( $uuid ) );
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
my $r = $self->routes;
|
2017-04-08 13:25:06 +00:00
|
|
|
$r->get('/')->to('root#index');
|
2017-04-24 16:42:07 +00:00
|
|
|
$r->get('/admin')->to('admin#index');
|
|
|
|
$r->post('/admin')->to('admin#auth_login');
|
|
|
|
# $r->get('/register')->to('register#index');
|
|
|
|
# $r->post('/register')->to('register#register');
|
|
|
|
$r->any('/admin/logout')->to('admin#auth_logout');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-25 19:13:34 +00:00
|
|
|
my $api_public_get = $r->under('/api' => sub {
|
2017-04-25 19:19:41 +00:00
|
|
|
my $c = shift;
|
|
|
|
$c->res->headers->header('Access-Control-Allow-Origin'=> '*');
|
|
|
|
$c->res->headers->header('Access-Control-Allow-Credentials' => 'true');
|
|
|
|
$c->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT');
|
|
|
|
$c->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token');
|
|
|
|
$c->res->headers->header('Access-Control-Max-Age' => '1728000');
|
2017-04-25 19:13:34 +00:00
|
|
|
});
|
2017-04-25 18:47:50 +00:00
|
|
|
|
2017-04-25 19:13:34 +00:00
|
|
|
$api_public_get->options('*' => sub {
|
2017-04-25 19:19:41 +00:00
|
|
|
my $c = shift;
|
|
|
|
$c->respond_to(any => { data => '', status => 200 });
|
2017-04-25 18:47:50 +00:00
|
|
|
});
|
2017-04-25 19:13:34 +00:00
|
|
|
$api_public_get->get('/info/ages')->to('api-info#get_ages');
|
2017-04-23 13:02:10 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
# Always available api routes
|
2017-04-25 19:13:34 +00:00
|
|
|
my $api_public = $api_public_get->under('/')->to('api-auth#check_json');
|
2017-04-20 00:27:18 +00:00
|
|
|
|
|
|
|
$api_public->post('/login')->to('api-auth#post_login');
|
|
|
|
$api_public->post('/register')->to('api-register#post_register');
|
|
|
|
$api_public->post('/logout')->to('api-auth#post_logout');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
# Private, must be authenticated api routes
|
|
|
|
my $api = $api_public->under('/')->to('api-auth#auth');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
$api->post('/' => sub {
|
|
|
|
return shift->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Successful Auth',
|
|
|
|
});
|
2017-04-05 22:20:42 +00:00
|
|
|
});
|
2017-04-18 21:31:08 +00:00
|
|
|
$api->post('/upload')->to('api-upload#post_upload');
|
|
|
|
$api->post('/search')->to('api-upload#post_search');
|
2017-04-23 15:59:35 +00:00
|
|
|
$api->post('/user/day')->to('api-user#post_day');
|
2017-04-18 21:31:08 +00:00
|
|
|
$api->post('/edit')->to('api-api#post_edit');
|
|
|
|
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
|
|
|
$api->post('/user-history')->to('api-user#post_user_history');
|
2017-05-16 20:30:38 +00:00
|
|
|
$api->post('/stats')->to('api-stats#post_today');
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 13:14:51 +00:00
|
|
|
my $api_admin = $api->under('/')->to('api-admin#auth');
|
|
|
|
|
|
|
|
$api_admin->post('/admin-approve')->to('api-admin#post_admin_approve');
|
|
|
|
$api_admin->post('/admin-merge')->to('api-admin#post_admin_merge');
|
|
|
|
|
2017-04-06 22:12:28 +00:00
|
|
|
my $admin_routes = $r->under('/admin')->to('admin#under');
|
|
|
|
|
2017-04-24 16:42:07 +00:00
|
|
|
$admin_routes->get('/home')->to('admin#home');
|
2017-04-24 11:49:18 +00:00
|
|
|
|
2017-04-08 01:32:13 +00:00
|
|
|
$admin_routes->get('/tokens')->to('admin-tokens#index');
|
|
|
|
$admin_routes->post('/tokens')->to('admin-tokens#create');
|
|
|
|
$admin_routes->get('/tokens/:id')->to('admin-tokens#read');
|
|
|
|
$admin_routes->post('/tokens/:id')->to('admin-tokens#update');
|
|
|
|
$admin_routes->post('/tokens/:id/delete')->to('admin-tokens#delete');
|
2017-04-24 11:49:18 +00:00
|
|
|
|
2017-04-18 09:44:07 +00:00
|
|
|
$admin_routes->get('/users')->to('admin-users#index');
|
|
|
|
$admin_routes->get('/users/:id')->to('admin-users#read');
|
|
|
|
$admin_routes->post('/users/:id')->to('admin-users#update');
|
|
|
|
$admin_routes->post('/users/:id/delete')->to('admin-users#delete');
|
2017-04-06 23:33:50 +00:00
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
$admin_routes->get('/organisations')->to('admin-organisations#list');
|
|
|
|
$admin_routes->get('/organisations/valid/:id')->to('admin-organisations#valid_read');
|
|
|
|
$admin_routes->get('/organisations/pending/:id')->to('admin-organisations#pending_read');
|
|
|
|
$admin_routes->get('/organisations/pending/:id/approve')->to('admin-organisations#pending_approve');
|
|
|
|
|
2017-04-24 16:42:07 +00:00
|
|
|
# my $user_routes = $r->under('/')->to('root#under');
|
2017-04-08 13:25:06 +00:00
|
|
|
|
2017-04-24 16:42:07 +00:00
|
|
|
# $user_routes->get('/home')->to('root#home');
|
2017-04-08 13:25:06 +00:00
|
|
|
|
2017-04-24 16:42:07 +00:00
|
|
|
# my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under');
|
2017-04-21 22:38:12 +00:00
|
|
|
|
2017-04-24 16:42:07 +00:00
|
|
|
# $portal_api->post('/upload')->to('api-upload#post_upload');
|
|
|
|
# $portal_api->post('/search')->to('api-upload#post_search');
|
2017-04-20 16:33:59 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->hook( before_dispatch => sub {
|
|
|
|
my $self = shift;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development';
|
|
|
|
});
|
2017-04-24 11:49:18 +00:00
|
|
|
|
|
|
|
$self->helper( copy_transactions_and_delete => sub {
|
|
|
|
my ( $c, $from_org, $to_org ) = @_;
|
|
|
|
|
|
|
|
my $from_org_transaction_rs = $from_org->transactions;
|
|
|
|
|
|
|
|
while ( my $from_org_transaction = $from_org_transaction_rs->next ) {
|
|
|
|
$to_org->create_related(
|
|
|
|
'transactions', {
|
|
|
|
buyer_id => $from_org_transaction->buyer_id,
|
|
|
|
value => $from_org_transaction->value,
|
|
|
|
proof_image => $from_org_transaction->proof_image,
|
|
|
|
submitted_at => $from_org_transaction->submitted_at,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$from_org->delete;
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|