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 22:31:08 +01:00
use DateTime ;
2017-04-21 19:54:28 +01:00
use Mojo::Asset::File ;
use Mojo::File qw/ path tempdir / ;
2017-02-24 19:27:43 +00:00
2017-04-05 22:43:08 +01: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-07-25 14:14:30 +01:00
{ quote_names = > 1 } ,
2017-04-05 22:43:08 +01:00
) ;
} ;
2017-02-24 19:27:43 +00:00
sub startup {
my $ self = shift ;
2017-09-29 16:21:24 +01:00
my $ version = `git describe --tags` ;
2017-04-05 23:20:42 +01:00
$ self - > plugin ( 'Config' , {
default = > {
2017-04-21 19:54:28 +01:00
storage_path = > tempdir ,
2019-07-08 18:12:35 +01:00
upload_path = > $ self - > home - > child ( 'upload' ) ,
2017-04-05 23:20:42 +01:00
sessionTimeSeconds = > 60 * 60 * 24 * 7 ,
2017-04-18 22:31:08 +01:00
sessionTokenJsonName = > 'session_key' ,
2017-04-05 23:20:42 +01:00
sessionExpiresJsonName = > 'sessionExpires' ,
2017-09-29 16:21:24 +01:00
version = > $ version ,
2017-04-05 23:20:42 +01:00
} ,
} ) ;
my $ config = $ self - > config ;
2018-03-21 17:24:13 +00:00
if ( defined $ config - > { secret } ) {
$ self - > secrets ( [ $ config - > { secret } ] ) ;
} elsif ( $ self - > mode eq 'production' ) {
# Just incase we end up in production and it hasnt been set!
$ self - > secrets ( [ Data::UUID - > new - > create ( ) ] ) ;
}
2017-06-12 22:41:17 +01:00
push @ { $ self - > commands - > namespaces } , __PACKAGE__ . '::Command' ;
2017-07-25 15:44:16 +01:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::BootstrapPagination' , { bootstrap4 = > 1 } ) ;
2017-04-08 18:25:34 +01:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::Validators' ) ;
2017-08-14 12:45:54 +01:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::Datetime' ) ;
2017-11-15 18:22:49 +00:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::Currency' ) ;
2017-10-03 15:47:05 +01:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::Postcodes' ) ;
2017-09-28 16:39:03 +01:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::TemplateHelpers' ) ;
2018-03-20 19:24:48 +00:00
$ self - > plugin ( 'Pear::LocalLoop::Plugin::Minion' ) ;
2017-04-08 18:25:34 +01:00
2017-04-06 23:12:28 +01: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 22:44:59 +01:00
if ( defined $ user ) {
if ( $ user - > check_password ( $ password ) ) {
2017-04-21 20:52:14 +01:00
return $ user - > id ;
2017-04-18 22:44:59 +01:00
}
}
2017-08-25 14:44:36 +01:00
return ;
2017-04-06 23:12:28 +01:00
} ,
} ) ;
2017-04-05 23:20:42 +01:00
# shortcut for use in template
2017-04-21 20:03:51 +01:00
$ self - > helper ( db = > sub { warn "DEPRECATED db helper" ; return $ self - > app - > schema - > storage - > dbh } ) ;
2017-04-05 23:20:42 +01:00
$ self - > helper ( schema = > sub { $ self - > app - > schema } ) ;
2017-04-20 01:27:18 +01: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-09-18 13:31:30 +01:00
error = > $ c - > error_messages - > { $ val } - > { $ check } - > { error } || $ check ,
2017-04-20 01:27:18 +01:00
} ,
status = > $ c - > error_messages - > { $ val } - > { $ check } - > { status } ,
) ;
}
} ) ;
2017-04-05 23:20:42 +01:00
2017-04-21 19:54:28 +01: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 23:20:42 +01:00
my $ r = $ self - > routes ;
2017-04-08 14:25:06 +01:00
$ r - > get ( '/' ) - > to ( 'root#index' ) ;
2017-04-24 17:42:07 +01: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 23:12:28 +01:00
2017-04-25 20:13:34 +01:00
my $ api_public_get = $ r - > under ( '/api' = > sub {
2017-04-25 20:19:41 +01: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 20:13:34 +01:00
} ) ;
2017-04-25 19:47:50 +01:00
2017-04-25 20:13:34 +01:00
$ api_public_get - > options ( '*' = > sub {
2017-04-25 20:19:41 +01:00
my $ c = shift ;
$ c - > respond_to ( any = > { data = > '' , status = > 200 } ) ;
2017-04-25 19:47:50 +01:00
} ) ;
2017-04-23 14:02:10 +01:00
2017-04-18 22:31:08 +01:00
# Always available api routes
2017-04-25 20:13:34 +01:00
my $ api_public = $ api_public_get - > under ( '/' ) - > to ( 'api-auth#check_json' ) ;
2017-04-20 01:27:18 +01: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-07-24 13:20:22 +01:00
$ api_public - > post ( '/feedback' ) - > to ( 'api-feedback#post_feedback' ) ;
2017-04-06 23:12:28 +01:00
2017-04-20 01:27:18 +01:00
# Private, must be authenticated api routes
my $ api = $ api_public - > under ( '/' ) - > to ( 'api-auth#auth' ) ;
2017-04-06 23:12:28 +01:00
2017-04-18 22:31:08 +01:00
$ api - > post ( '/' = > sub {
return shift - > render ( json = > {
success = > Mojo::JSON - > true ,
message = > 'Successful Auth' ,
} ) ;
2017-04-05 23:20:42 +01:00
} ) ;
2017-04-18 22:31:08 +01:00
$ api - > post ( '/upload' ) - > to ( 'api-upload#post_upload' ) ;
$ api - > post ( '/search' ) - > to ( 'api-upload#post_search' ) ;
2018-01-15 14:18:27 +00:00
$ api - > post ( '/search/category' ) - > to ( 'api-upload#post_category' ) ;
2017-07-26 18:28:14 +01:00
$ api - > post ( '/user' ) - > to ( 'api-user#post_account' ) ;
$ api - > post ( '/user/account' ) - > to ( 'api-user#post_account_update' ) ;
2017-04-18 22:31:08 +01:00
$ api - > post ( '/user-history' ) - > to ( 'api-user#post_user_history' ) ;
2017-05-16 22:45:49 +01:00
$ api - > post ( '/stats' ) - > to ( 'api-stats#post_index' ) ;
2018-01-17 16:47:05 +00:00
$ api - > post ( '/stats/category' ) - > to ( 'api-categories#post_category_list' ) ;
2017-12-18 12:56:45 +00:00
$ api - > post ( '/stats/customer' ) - > to ( 'api-stats#post_customer' ) ;
2019-07-12 13:39:37 +01:00
$ api - > post ( '/stats/organisation' ) - > to ( 'api-stats#post_organisation' ) ;
2017-06-06 22:30:02 +01:00
$ api - > post ( '/stats/leaderboard' ) - > to ( 'api-stats#post_leaderboards' ) ;
2017-11-10 17:07:41 +00:00
$ api - > post ( '/stats/leaderboard/paged' ) - > to ( 'api-stats#post_leaderboards_paged' ) ;
2017-09-05 16:37:31 +01:00
$ api - > post ( '/outgoing-transactions' ) - > to ( 'api-transactions#post_transaction_list_purchases' ) ;
2018-03-20 18:43:00 +00:00
$ api - > post ( '/recurring-transactions' ) - > to ( 'api-transactions#update_recurring' ) ;
$ api - > post ( '/recurring-transactions/delete' ) - > to ( 'api-transactions#delete_recurring' ) ;
2017-09-18 17:31:29 +01:00
2017-02-24 19:27:43 +00:00
2017-08-29 12:42:27 +01:00
my $ api_v1 = $ api - > under ( '/v1' ) ;
2018-01-31 12:52:52 +00:00
my $ api_v1_user = $ api_v1 - > under ( '/user' ) ;
$ api_v1_user - > post ( '/medals' ) - > to ( 'api-v1-user-medals#index' ) ;
2018-02-05 14:58:55 +00:00
$ api_v1_user - > post ( '/points' ) - > to ( 'api-v1-user-points#index' ) ;
2018-01-31 12:52:52 +00:00
2017-09-27 18:01:06 +01:00
my $ api_v1_supplier = $ api_v1 - > under ( '/supplier' ) ;
$ api_v1_supplier - > post ( '/location' ) - > to ( 'api-v1-supplier-location#index' ) ;
2017-12-08 13:19:22 +00:00
$ api_v1_supplier - > post ( '/location/trail' ) - > to ( 'api-v1-supplier-location#trail_load' ) ;
2017-09-27 18:01:06 +01:00
2017-08-29 12:42:27 +01:00
my $ api_v1_org = $ api_v1 - > under ( '/organisation' ) - > to ( 'api-v1-organisation#auth' ) ;
$ api_v1_org - > post ( '/graphs' ) - > to ( 'api-v1-organisation-graphs#index' ) ;
2017-09-07 16:03:01 +01:00
$ api_v1_org - > post ( '/snippets' ) - > to ( 'api-v1-organisation-snippets#index' ) ;
2017-09-19 14:41:11 +01:00
$ api_v1_org - > post ( '/payroll' ) - > to ( 'api-organisation#post_payroll_read' ) ;
2017-09-18 17:31:29 +01:00
$ api_v1_org - > post ( '/payroll/add' ) - > to ( 'api-organisation#post_payroll_add' ) ;
2017-09-19 14:41:11 +01:00
$ api_v1_org - > post ( '/supplier' ) - > to ( 'api-organisation#post_supplier_read' ) ;
2017-09-18 17:31:29 +01:00
$ api_v1_org - > post ( '/supplier/add' ) - > to ( 'api-organisation#post_supplier_add' ) ;
2017-09-19 14:41:11 +01:00
$ api_v1_org - > post ( '/employee' ) - > to ( 'api-organisation#post_employee_read' ) ;
2017-09-18 17:31:29 +01:00
$ api_v1_org - > post ( '/employee/add' ) - > to ( 'api-organisation#post_employee_add' ) ;
2019-07-15 01:33:07 +01:00
2019-07-09 16:25:32 +01:00
$ api_v1_org - > post ( '/external/transactions' ) - > to ( 'api-external#post_lcc_transactions' ) ;
$ api_v1_org - > post ( '/external/suppliers' ) - > to ( 'api-external#post_lcc_suppliers' ) ;
2019-07-15 01:33:07 +01:00
$ api_v1_org - > post ( '/external/year_spend' ) - > to ( 'api-external#post_year_spend' ) ;
2019-07-15 02:41:23 +01:00
$ api_v1_org - > post ( '/external/supplier_count' ) - > to ( 'api-external#post_supplier_count' ) ;
2019-07-15 04:45:57 +01:00
$ api_v1_org - > post ( '/external/supplier_history' ) - > to ( 'api-external#post_supplier_history' ) ;
2019-09-06 17:31:09 +01:00
$ api_v1_org - > post ( '/external/lcc_tables' ) - > to ( 'api-external#post_lcc_table_summary' ) ;
2019-07-15 01:33:07 +01:00
2019-07-12 13:39:37 +01:00
$ api_v1_org - > post ( '/pies' ) - > to ( 'api-v1-organisation-pies#index' ) ;
2017-08-29 12:42:27 +01:00
2017-12-12 13:32:52 +00:00
my $ api_v1_cust = $ api_v1 - > under ( '/customer' ) - > to ( 'api-v1-customer#auth' ) ;
2017-12-12 17:21:32 +00:00
$ api_v1_cust - > post ( '/graphs' ) - > to ( 'api-v1-customer-graphs#index' ) ;
$ api_v1_cust - > post ( '/snippets' ) - > to ( 'api-v1-customer-snippets#index' ) ;
2017-12-14 17:20:06 +00:00
$ api_v1_cust - > post ( '/pies' ) - > to ( 'api-v1-customer-pies#index' ) ;
2017-12-12 13:32:52 +00:00
2017-04-06 23:12:28 +01:00
my $ admin_routes = $ r - > under ( '/admin' ) - > to ( 'admin#under' ) ;
2018-03-20 19:24:48 +00:00
if ( defined $ config - > { minion } ) {
$ self - > plugin ( 'Minion::Admin' = > {
return_to = > '/admin/home' ,
route = > $ admin_routes - > any ( '/minion' ) ,
} ) ;
}
2017-04-24 17:42:07 +01:00
$ admin_routes - > get ( '/home' ) - > to ( 'admin#home' ) ;
2017-04-24 12:49:18 +01:00
2017-04-08 02:32:13 +01: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 12:49:18 +01:00
2018-01-11 16:23:42 +00:00
$ admin_routes - > get ( '/categories' ) - > to ( 'admin-categories#index' ) ;
$ admin_routes - > post ( '/categories' ) - > to ( 'admin-categories#create' ) ;
$ admin_routes - > get ( '/categories/:id' ) - > to ( 'admin-categories#read' ) ;
$ admin_routes - > post ( '/categories/:id' ) - > to ( 'admin-categories#update' ) ;
$ admin_routes - > post ( '/categories/:id/delete' ) - > to ( 'admin-categories#delete' ) ;
2017-04-18 10:44:07 +01: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-07 00:33:50 +01:00
2017-04-24 12:49:18 +01:00
$ admin_routes - > get ( '/organisations' ) - > to ( 'admin-organisations#list' ) ;
2017-08-21 15:10:33 +01:00
$ admin_routes - > get ( '/organisations/add' ) - > to ( 'admin-organisations#add_org' ) ;
2017-08-31 14:52:45 +01:00
$ admin_routes - > post ( '/organisations/add' ) - > to ( 'admin-organisations#add_org_submit' ) ;
$ admin_routes - > get ( '/organisations/:id' ) - > to ( 'admin-organisations#valid_read' ) ;
$ admin_routes - > post ( '/organisations/:id' ) - > to ( 'admin-organisations#valid_edit' ) ;
2017-11-17 18:10:16 +00:00
$ admin_routes - > get ( '/organisations/:id/merge' ) - > to ( 'admin-organisations#merge_list' ) ;
$ admin_routes - > get ( '/organisations/:id/merge/:target_id' ) - > to ( 'admin-organisations#merge_detail' ) ;
$ admin_routes - > post ( '/organisations/:id/merge/:target_id' ) - > to ( 'admin-organisations#merge_confirm' ) ;
2017-04-24 12:49:18 +01:00
2017-07-24 15:21:10 +01:00
$ admin_routes - > get ( '/feedback' ) - > to ( 'admin-feedback#index' ) ;
$ admin_routes - > get ( '/feedback/:id' ) - > to ( 'admin-feedback#read' ) ;
2017-10-23 16:09:20 +01:00
$ admin_routes - > get ( '/feedback/:id/actioned' ) - > to ( 'admin-feedback#actioned' ) ;
2017-07-24 15:21:10 +01:00
2017-09-05 13:44:58 +01:00
$ admin_routes - > get ( '/transactions' ) - > to ( 'admin-transactions#index' ) ;
$ admin_routes - > get ( '/transactions/:id' ) - > to ( 'admin-transactions#read' ) ;
$ admin_routes - > get ( '/transactions/:id/image' ) - > to ( 'admin-transactions#image' ) ;
2017-09-07 13:20:53 +01:00
$ admin_routes - > post ( '/transactions/:id/delete' ) - > to ( 'admin-transactions#delete' ) ;
2017-09-05 13:44:58 +01:00
2017-09-29 14:45:44 +01:00
$ admin_routes - > get ( '/reports/transactions' ) - > to ( 'admin-reports#transaction_data' ) ;
2017-11-13 13:30:33 +00:00
$ admin_routes - > get ( '/import' ) - > to ( 'admin-import#index' ) ;
$ admin_routes - > get ( '/import/add' ) - > to ( 'admin-import#get_add' ) ;
$ admin_routes - > post ( '/import/add' ) - > to ( 'admin-import#post_add' ) ;
$ admin_routes - > get ( '/import/:set_id' ) - > to ( 'admin-import#list' ) ;
2017-11-13 19:00:34 +00:00
$ admin_routes - > get ( '/import/:set_id/user' ) - > to ( 'admin-import#get_user' ) ;
$ admin_routes - > get ( '/import/:set_id/org' ) - > to ( 'admin-import#get_org' ) ;
2017-11-14 18:41:54 +00:00
$ admin_routes - > get ( '/import/:set_id/ignore/:value_id' ) - > to ( 'admin-import#ignore_value' ) ;
2017-11-15 18:22:49 +00:00
$ admin_routes - > get ( '/import/:set_id/import' ) - > to ( 'admin-import#run_import' ) ;
2019-07-02 15:21:01 +01:00
$ admin_routes - > get ( '/import_from' ) - > to ( 'admin-import_from#index' ) ;
2019-07-04 14:16:49 +01:00
$ admin_routes - > post ( '/import_from/suppliers' ) - > to ( 'admin-import_from#post_suppliers' ) ;
$ admin_routes - > post ( '/import_from/transactions' ) - > to ( 'admin-import_from#post_transactions' ) ;
2019-08-29 16:37:55 +01:00
$ admin_routes - > post ( '/import_from/postcodes' ) - > to ( 'admin-import_from#post_postcodes' ) ;
2019-09-09 15:37:26 +01:00
$ admin_routes - > get ( '/import_from/org_search' ) - > to ( 'admin-import_from#org_search' ) ;
2019-07-02 15:21:01 +01:00
2017-04-24 17:42:07 +01:00
# my $user_routes = $r->under('/')->to('root#under');
2017-04-08 14:25:06 +01:00
2017-04-24 17:42:07 +01:00
# $user_routes->get('/home')->to('root#home');
2017-04-08 14:25:06 +01:00
2017-04-24 17:42:07 +01:00
# my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under');
2017-04-21 23:38:12 +01:00
2017-04-24 17:42:07 +01:00
# $portal_api->post('/upload')->to('api-upload#post_upload');
# $portal_api->post('/search')->to('api-upload#post_search');
2017-04-20 17:33:59 +01:00
2017-04-18 22:44:59 +01:00
$ self - > hook ( before_dispatch = > sub {
2018-03-21 17:24:13 +00:00
my $ c = shift ;
2017-02-24 19:27:43 +00:00
2018-03-21 17:24:13 +00:00
$ c - > res - > headers - > header ( 'Access-Control-Allow-Origin' = > '*' ) if $ c - > app - > mode eq 'development' ;
2017-04-18 22:44:59 +01:00
} ) ;
2017-04-24 12:49:18 +01: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' , {
2017-08-14 16:10:14 +01:00
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 ,
purchase_time = > $ from_org_transaction - > purchase_time ,
2017-04-24 12:49:18 +01:00
}
) ;
2017-07-25 14:33:11 +01:00
$ from_org_transaction - > delete ;
2017-04-24 12:49:18 +01:00
}
$ from_org - > delete ;
} ) ;
2017-02-24 19:27:43 +00:00
}
1 ;