2017-09-05 16:37:31 +01:00
package Pear::LocalLoop::Controller::Api::Transactions ;
use Mojo::Base 'Mojolicious::Controller' ;
use Mojo::JSON ;
has error_messages = > sub {
return {
email = > {
required = > { message = > 'No email sent.' , status = > 400 } ,
email = > { message = > 'Email is invalid.' , status = > 400 } ,
} ,
2018-03-20 18:43:00 +00:00
value = > {
required = > { message = > 'transaction amount is missing' , status = > 400 } ,
number = > { message = > 'transaction amount does not look like a number' , status = > 400 } ,
gt_num = > { message = > 'transaction amount cannot be equal to or less than zero' , status = > 400 } ,
} ,
apply_time = > {
required = > { message = > 'purchase time is missing' , status = > 400 } ,
is_full_iso_datetime = > { message = > 'time is in incorrect format' , status = > 400 } ,
} ,
id = > {
required = > { message = > 'Recurring Transaction not found' , status = > 400 } ,
} ,
category = > {
in_resultset = > { message = > 'Category is invalid' , status = > 400 } ,
} ,
2017-09-05 16:37:31 +01:00
} ;
} ;
sub post_transaction_list_purchases {
my $ c = shift ;
my $ user = $ c - > stash - > { api_user } ;
my $ validation = $ c - > validation ;
$ validation - > input ( $ c - > stash - > { api_json } ) ;
$ validation - > optional ( 'page' ) - > number ;
return $ c - > api_validation_error if $ validation - > has_error ;
my $ transactions = $ user - > entity - > purchases - > search (
undef , {
page = > $ validation - > param ( 'page' ) || 1 ,
rows = > 10 ,
order_by = > { - desc = > 'purchase_time' } ,
} ,
) ;
2017-09-05 17:24:04 +01:00
2018-03-13 12:55:13 +00:00
my $ recurring_transactions = $ c - > schema - > resultset ( 'TransactionRecurring' ) - > search ( {
buyer_id = > $ user - > id ,
} ) ;
# purchase_time needs timezone attached to it
2017-09-05 16:37:31 +01:00
my @ transaction_list = (
map { {
2017-09-05 17:24:04 +01:00
seller = > $ _ - > seller - > name ,
2017-09-13 16:07:23 +01:00
value = > $ _ - > value / 100000 ,
2017-09-19 17:40:17 +01:00
purchase_time = > $ c - > format_iso_datetime ( $ _ - > purchase_time ) ,
2019-07-14 16:05:35 +01:00
( $ _ - > meta ? (
net_value = > $ _ - > meta - > net_value / 100000 ,
sales_tax_value = > $ _ - > meta - > sales_tax_value / 100000 ,
gross_value = > $ _ - > meta - > gross_value / 100000 ,
) : (
net_value = > undef ,
sales_tax_value = > undef ,
gross_value = > undef ,
) ) ,
2017-09-05 16:37:31 +01:00
} } $ transactions - > all
2017-09-05 17:24:04 +01:00
) ;
2018-03-20 12:19:04 +00:00
2018-03-13 12:55:13 +00:00
my @ recurring_transaction_list = (
map { {
2018-03-14 17:55:24 +00:00
id = > $ _ - > id ,
2018-03-13 12:55:13 +00:00
seller = > $ _ - > seller - > name ,
value = > $ _ - > value / 100000 ,
start_time = > $ c - > format_iso_datetime ( $ _ - > start_time ) ,
2018-03-15 13:06:50 +00:00
last_updated = > $ c - > format_iso_datetime ( $ _ - > last_updated ) || undef ,
2018-03-13 12:55:13 +00:00
essential = > $ _ - > essential ,
2018-03-20 12:19:04 +00:00
category = > $ _ - > category_id || 0 ,
2018-03-13 12:55:13 +00:00
recurring_period = > $ _ - > recurring_period ,
} } $ recurring_transactions - > all
) ;
2017-09-05 16:37:31 +01:00
return $ c - > render ( json = > {
success = > Mojo::JSON - > true ,
transactions = > \ @ transaction_list ,
2018-03-13 12:55:13 +00:00
recurring_transactions = > \ @ recurring_transaction_list ,
2017-09-06 17:03:53 +01:00
page_no = > $ transactions - > pager - > total_entries ,
2017-09-05 16:37:31 +01:00
} ) ;
}
2018-03-14 17:55:24 +00:00
sub update_recurring {
my $ c = shift ;
my $ user = $ c - > stash - > { api_user } ;
my $ validation = $ c - > validation ;
2018-03-20 18:43:00 +00:00
$ validation - > input ( $ c - > stash - > { api_json } ) ;
$ validation - > required ( 'id' ) ;
return $ c - > api_validation_error if $ validation - > has_error ;
my $ id = $ validation - > param ( 'id' ) ;
my $ recur_transaction = $ c - > schema - > resultset ( 'TransactionRecurring' ) - > find ( $ id ) ;
unless ( $ recur_transaction ) {
return $ c - > render (
json = > {
success = > Mojo::JSON - > false ,
message = > 'Error Finding Recurring Transaction' ,
error = > 'recurring_error' ,
} ,
status = > 400 ,
) ;
}
$ validation - > required ( 'recurring_period' ) ;
$ validation - > required ( 'apply_time' ) - > is_full_iso_datetime ;
$ validation - > optional ( 'category' ) - > in_resultset ( 'id' , $ c - > schema - > resultset ( 'Category' ) ) ;
$ validation - > optional ( 'essential' ) ;
$ validation - > required ( 'value' ) ;
return $ c - > api_validation_error if $ validation - > has_error ;
my $ apply_time = $ c - > parse_iso_datetime ( $ validation - > param ( 'apply_time' ) ) ;
$ c - > schema - > storage - > txn_do ( sub {
$ recur_transaction - > update ( {
start_time = > $ c - > format_db_datetime ( $ apply_time ) ,
last_updated = > undef ,
category_id = > $ validation - > param ( 'category' ) ,
essential = > $ validation - > param ( 'essential' ) ,
value = > $ validation - > param ( 'value' ) * 100000 ,
recurring_period = > $ validation - > param ( 'recurring_period' ) ,
} ) ;
} ) ;
return $ c - > render ( json = > {
success = > Mojo::JSON - > true ,
message = > 'Recurring Transaction Updated Successfully' ,
} ) ;
2018-03-14 17:55:24 +00:00
2018-03-20 18:43:00 +00:00
}
sub delete_recurring {
my $ c = shift ;
my $ user = $ c - > stash - > { api_user } ;
my $ validation = $ c - > validation ;
2018-03-14 17:55:24 +00:00
$ validation - > input ( $ c - > stash - > { api_json } ) ;
2018-03-20 18:43:00 +00:00
$ validation - > required ( 'id' ) ;
return $ c - > api_validation_error if $ validation - > has_error ;
my $ id = $ validation - > param ( 'id' ) ;
my $ recur_transaction = $ c - > schema - > resultset ( 'TransactionRecurring' ) - > find ( $ id ) ;
unless ( $ recur_transaction ) {
return $ c - > render (
json = > {
success = > Mojo::JSON - > false ,
message = > 'Error Finding Recurring Transaction' ,
error = > 'recurring_error' ,
} ,
status = > 400 ,
) ;
}
$ recur_transaction - > delete ;
return $ c - > render ( json = > {
success = > Mojo::JSON - > true ,
2018-03-20 18:54:55 +00:00
message = > 'Recurring Transaction Deleted Successfully' ,
2018-03-20 18:43:00 +00:00
} ) ;
2018-03-14 17:55:24 +00:00
}
2017-09-05 16:37:31 +01:00
1 ;