Add code formatter, format all code

This commit is contained in:
Rumperuu 2021-03-20 12:09:50 +00:00
parent 602a59f1c3
commit 47a55f6322
120 changed files with 8061 additions and 6967 deletions

View file

@ -2,20 +2,20 @@ package Pear::LocalLoop::Controller::Api::V1::Customer;
use Mojo::Base 'Mojolicious::Controller';
sub auth {
my $c = shift;
my $c = shift;
return 1 if $c->stash->{api_user}->type eq 'customer';
return 1 if $c->stash->{api_user}->type eq 'customer';
$c->render(
json => {
success => Mojo::JSON->false,
message => 'Not an Customer',
error => 'user_not_cust',
},
status => 403,
);
$c->render(
json => {
success => Mojo::JSON->false,
message => 'Not an Customer',
error => 'user_not_cust',
},
status => 403,
);
return 0;
return 0;
}
1;

View file

@ -2,166 +2,174 @@ package Pear::LocalLoop::Controller::Api::V1::Customer::Graphs;
use Mojo::Base 'Mojolicious::Controller';
has error_messages => sub {
return {
graph => {
required => { message => 'Must request graph type', status => 400 },
in => { message => 'Unrecognised graph type', status => 400 },
},
};
return {
graph => {
required => { message => 'Must request graph type', status => 400 },
in => { message => 'Unrecognised graph type', status => 400 },
},
};
};
sub index {
my $c = shift;
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('graph')->in( qw/
total_last_week
avg_spend_last_week
total_last_month
avg_spend_last_month
/ );
return $c->api_validation_error if $validation->has_error;
my $graph_sub = "graph_" . $validation->param('graph');
unless ( $c->can($graph_sub) ) {
# Secondary catch in case a mistake has been made
return $c->render(
json => {
success => Mojo::JSON->false,
message => $c->error_messages->{graph}->{in}->{message},
error => 'in',
},
status => $c->error_messages->{graph}->{in}->{status},
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('graph')->in(
qw/
total_last_week
avg_spend_last_week
total_last_month
avg_spend_last_month
/
);
}
return $c->$graph_sub;
return $c->api_validation_error if $validation->has_error;
my $graph_sub = "graph_" . $validation->param('graph');
unless ( $c->can($graph_sub) ) {
# Secondary catch in case a mistake has been made
return $c->render(
json => {
success => Mojo::JSON->false,
message => $c->error_messages->{graph}->{in}->{message},
error => 'in',
},
status => $c->error_messages->{graph}->{in}->{status},
);
}
return $c->$graph_sub;
}
sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) }
sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) }
sub graph_total_last_week { return shift->_purchases_total_duration(7) }
sub graph_total_last_month { return shift->_purchases_total_duration(30) }
sub _purchases_total_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration($duration);
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
$data->{bounds} = {
min => $c->format_iso_datetime($start),
max => $c->format_iso_datetime($end),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->purchases
->search_between( $start, $next_end )
->get_column('value')
->sum || 0 * 1;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions / 100000;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions =
$entity->purchases->search_between( $start, $next_end )
->get_column('value')->sum || 0 * 1;
push @{ $data->{labels} }, $c->format_iso_datetime($start);
push @{ $data->{data} }, $transactions / 100000;
$start->add( days => 1 );
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub graph_avg_spend_last_week { return shift->_purchases_avg_spend_duration( 7 ) }
sub graph_avg_spend_last_month { return shift->_purchases_avg_spend_duration( 30 ) }
sub graph_avg_spend_last_week { return shift->_purchases_avg_spend_duration(7) }
sub graph_avg_spend_last_month {
return shift->_purchases_avg_spend_duration(30);
}
sub _purchases_avg_spend_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration($duration);
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
$data->{bounds} = {
min => $c->format_iso_datetime($start),
max => $c->format_iso_datetime($end),
};
my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
{
purchase_time => {
-between => [
$dtf->format_datetime($start),
$dtf->format_datetime($end),
],
},
buyer_id => $entity->id,
},
{
columns => [
my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs =
$c->schema->resultset( 'ViewQuantisedTransaction' . $driver )->search(
{
quantised => 'quantised_days',
count => \"COUNT(*)",
sum_value => $c->pg_or_sqlite(
'SUM("me"."value")',
'SUM("me"."value")',
),
average_value => $c->pg_or_sqlite(
'AVG("me"."value")',
'AVG("me"."value")',
),
purchase_time => {
-between => [
$dtf->format_datetime($start),
$dtf->format_datetime($end),
],
},
buyer_id => $entity->id,
},
{
columns => [
{
quantised => 'quantised_days',
count => \"COUNT(*)",
sum_value => $c->pg_or_sqlite(
'SUM("me"."value")', 'SUM("me"."value")',
),
average_value => $c->pg_or_sqlite(
'AVG("me"."value")', 'AVG("me"."value")',
),
}
],
group_by => 'quantised_days',
order_by => { '-asc' => 'quantised_days' },
}
],
group_by => 'quantised_days',
order_by => { '-asc' => 'quantised_days' },
}
);
);
for ( $transaction_rs->all ) {
my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised'));
push @{ $data->{ labels } }, $c->format_iso_datetime( $quantised );
push @{ $data->{ data } }, ($_->get_column('average_value') || 0) / 100000;
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
for ( $transaction_rs->all ) {
my $quantised =
$c->db_datetime_parser->parse_datetime( $_->get_column('quantised') );
push @{ $data->{labels} }, $c->format_iso_datetime($quantised);
push @{ $data->{data} },
( $_->get_column('average_value') || 0 ) / 100000;
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub _get_start_end_duration {
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration );
return ( $start, $end );
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration($duration);
return ( $start, $end );
}
sub pg_or_sqlite {
my ( $c, $pg_sql, $sqlite_sql ) = @_;
my ( $c, $pg_sql, $sqlite_sql ) = @_;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
if ( $driver eq 'Pg' ) {
return \$pg_sql;
} elsif ( $driver eq 'SQLite' ) {
return \$sqlite_sql;
} else {
$c->app->log->warn('Unknown Driver Used');
return;
}
if ( $driver eq 'Pg' ) {
return \$pg_sql;
}
elsif ( $driver eq 'SQLite' ) {
return \$sqlite_sql;
}
else {
$c->app->log->warn('Unknown Driver Used');
return;
}
}
1;

View file

@ -2,61 +2,68 @@ package Pear::LocalLoop::Controller::Api::V1::Customer::Pies;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $c = shift;
my $c = shift;
my $entity = $c->stash->{api_user}->entity;
my $entity = $c->stash->{api_user}->entity;
my $purchase_rs = $entity->purchases;
my $purchase_rs = $entity->purchases;
my $local_org_local_purchase = $purchase_rs->search({
"me.distance" => { '<', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_local_purchase = $purchase_rs->search(
{
"me.distance" => { '<', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_non_local_purchase = $purchase_rs->search({
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_non_local_purchase = $purchase_rs->search(
{
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_local_purchase = $purchase_rs->search({
"me.distance" => { '<', 20000 },
'organisation.is_local' => 0,
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_local_purchase = $purchase_rs->search(
{
"me.distance" => { '<', 20000 },
'organisation.is_local' => 0,
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_non_local_purchase = $purchase_rs->search({
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 0,
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_non_local_purchase = $purchase_rs->search(
{
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 0,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_all = {
'Local shop local purchaser' => $local_org_local_purchase->count,
'Local shop non-local purchaser' => $local_org_non_local_purchase->count,
'Non-local shop local purchaser' => $non_local_org_local_purchase->count,
'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count,
};
my $local_all = {
'Local shop local purchaser' => $local_org_local_purchase->count,
'Local shop non-local purchaser' =>
$local_org_non_local_purchase->count,
'Non-local shop local purchaser' =>
$non_local_org_local_purchase->count,
'Non-local shop non-local purchaser' =>
$non_local_org_non_local_purchase->count,
};
return $c->render(
json => {
success => Mojo::JSON->true,
local_all => $local_all,
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
local_all => $local_all,
}
);
}

View file

@ -2,30 +2,33 @@ package Pear::LocalLoop::Controller::Api::V1::Customer::Snippets;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $c = shift;
my $c = shift;
my $entity = $c->stash->{api_user}->entity;
my $data = {
user_sum => 0,
user_position => 0,
};
my $entity = $c->stash->{api_user}->entity;
my $data = {
user_sum => 0,
user_position => 0,
};
my $user_rs = $entity->purchases;
$data->{ user_sum } = $user_rs->get_column('value')->sum || 0;
$data->{ user_sum } /= 100000;
my $user_rs = $entity->purchases;
$data->{user_sum} = $user_rs->get_column('value')->sum || 0;
$data->{user_sum} /= 100000;
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
my $monthly_board = $leaderboard_rs->get_latest( 'monthly_total' );
if (defined $monthly_board) {
my $monthly_values = $monthly_board->values;
$data->{ user_position } = $monthly_values ? $monthly_values->find({ entity_id => $entity->id })->position : 0;
}
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $data,
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
my $monthly_board = $leaderboard_rs->get_latest('monthly_total');
if ( defined $monthly_board ) {
my $monthly_values = $monthly_board->values;
$data->{user_position} =
$monthly_values
? $monthly_values->find( { entity_id => $entity->id } )->position
: 0;
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $data,
}
);
}

View file

@ -2,20 +2,20 @@ package Pear::LocalLoop::Controller::Api::V1::Organisation;
use Mojo::Base 'Mojolicious::Controller';
sub auth {
my $c = shift;
my $c = shift;
return 1 if $c->stash->{api_user}->type eq 'organisation';
return 1 if $c->stash->{api_user}->type eq 'organisation';
$c->render(
json => {
success => Mojo::JSON->false,
message => 'Not an Organisation',
error => 'user_not_org',
},
status => 403,
);
$c->render(
json => {
success => Mojo::JSON->false,
message => 'Not an Organisation',
error => 'user_not_org',
},
status => 403,
);
return 0;
return 0;
}
1;

View file

@ -2,197 +2,195 @@ package Pear::LocalLoop::Controller::Api::V1::Organisation::Graphs;
use Mojo::Base 'Mojolicious::Controller';
has error_messages => sub {
return {
graph => {
required => { message => 'Must request graph type', status => 400 },
in => { message => 'Unrecognised graph type', status => 400 },
},
};
return {
graph => {
required => { message => 'Must request graph type', status => 400 },
in => { message => 'Unrecognised graph type', status => 400 },
},
};
};
sub index {
my $c = shift;
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('graph')->in( qw/
customers_last_7_days
customers_last_30_days
sales_last_7_days
sales_last_30_days
purchases_last_7_days
purchases_last_30_days
customers_range
/ );
return $c->api_validation_error if $validation->has_error;
my $graph_sub = "graph_" . $validation->param('graph');
unless ( $c->can($graph_sub) ) {
# Secondary catch in case a mistake has been made
return $c->render(
json => {
success => Mojo::JSON->false,
message => $c->error_messages->{graph}->{in}->{message},
error => 'in',
},
status => $c->error_messages->{graph}->{in}->{status},
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('graph')->in(
qw/
customers_last_7_days
customers_last_30_days
sales_last_7_days
sales_last_30_days
purchases_last_7_days
purchases_last_30_days
customers_range
/
);
}
return $c->$graph_sub;
return $c->api_validation_error if $validation->has_error;
my $graph_sub = "graph_" . $validation->param('graph');
unless ( $c->can($graph_sub) ) {
# Secondary catch in case a mistake has been made
return $c->render(
json => {
success => Mojo::JSON->false,
message => $c->error_messages->{graph}->{in}->{message},
error => 'in',
},
status => $c->error_messages->{graph}->{in}->{status},
);
}
return $c->$graph_sub;
}
sub graph_customers_range {
my $c = shift;
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('start')->is_iso_date;
$validation->required('end')->is_iso_date;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('start')->is_iso_date;
$validation->required('end')->is_iso_date;
return $c->api_validation_error if $validation->has_error;
return $c->api_validation_error if $validation->has_error;
my $entity = $c->stash->{api_user}->entity;
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $start = $c->parse_iso_date( $validation->param('start') );
my $end = $c->parse_iso_date( $validation->param('end') );
my $data = { labels => [], data => [] };
my $start = $c->parse_iso_date( $validation->param('start') );
my $end = $c->parse_iso_date( $validation->param('end') );
while ( $start <= $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->count;
push @{ $data->{ labels } }, $c->format_iso_date( $start );
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
while ( $start <= $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions =
$entity->sales->search_between( $start, $next_end )->count;
push @{ $data->{labels} }, $c->format_iso_date($start);
push @{ $data->{data} }, $transactions;
$start->add( days => 1 );
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub graph_customers_last_7_days { return shift->_customers_last_duration( 7 ) }
sub graph_customers_last_30_days { return shift->_customers_last_duration( 30 ) }
sub graph_customers_last_7_days { return shift->_customers_last_duration(7) }
sub graph_customers_last_30_days { return shift->_customers_last_duration(30) }
sub _customers_last_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration($duration);
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
$data->{bounds} = {
min => $c->format_iso_datetime($start),
max => $c->format_iso_datetime($end),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->count;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions =
$entity->sales->search_between( $start, $next_end )->count;
push @{ $data->{labels} }, $c->format_iso_datetime($start);
push @{ $data->{data} }, $transactions;
$start->add( days => 1 );
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub graph_sales_last_7_days { return shift->_sales_last_duration( 7 ) }
sub graph_sales_last_30_days { return shift->_sales_last_duration( 30 ) }
sub graph_sales_last_7_days { return shift->_sales_last_duration(7) }
sub graph_sales_last_30_days { return shift->_sales_last_duration(30) }
sub _sales_last_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration($duration);
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
$data->{bounds} = {
min => $c->format_iso_datetime($start),
max => $c->format_iso_datetime($end),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->get_column('value')
->sum || 0 + 0;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions / 100000;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales->search_between( $start, $next_end )
->get_column('value')->sum || 0 + 0;
push @{ $data->{labels} }, $c->format_iso_datetime($start);
push @{ $data->{data} }, $transactions / 100000;
$start->add( days => 1 );
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub graph_purchases_last_7_days { return shift->_purchases_last_duration( 7 ) }
sub graph_purchases_last_30_days { return shift->_purchases_last_duration( 30 ) }
sub graph_purchases_last_7_days { return shift->_purchases_last_duration(7) }
sub graph_purchases_last_30_days { return shift->_purchases_last_duration(30) }
sub _purchases_last_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration($duration);
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
$data->{bounds} = {
min => $c->format_iso_datetime($start),
max => $c->format_iso_datetime($end),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->purchases
->search_between( $start, $next_end )
->get_column('value')
->sum || 0 + 0;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions / 100000;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions =
$entity->purchases->search_between( $start, $next_end )
->get_column('value')->sum || 0 + 0;
push @{ $data->{labels} }, $c->format_iso_datetime($start);
push @{ $data->{data} }, $transactions / 100000;
$start->add( days => 1 );
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub _get_start_end_duration {
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration );
return ( $start, $end );
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration($duration);
return ( $start, $end );
}
1;

View file

@ -2,61 +2,68 @@ package Pear::LocalLoop::Controller::Api::V1::Organisation::Pies;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $c = shift;
my $c = shift;
my $entity = $c->stash->{api_user}->entity;
my $entity = $c->stash->{api_user}->entity;
my $purchase_rs = $entity->purchases;
my $purchase_rs = $entity->purchases;
my $local_org_local_purchase = $purchase_rs->search({
"me.distance" => { '<', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_local_purchase = $purchase_rs->search(
{
"me.distance" => { '<', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_non_local_purchase = $purchase_rs->search({
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_org_non_local_purchase = $purchase_rs->search(
{
"me.distance" => { '>=', 20000 },
'organisation.is_local' => 1,
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_local_purchase = $purchase_rs->search({
"me.distance" => { '<', 20000 },
'organisation.is_local' => [0, undef],
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_local_purchase = $purchase_rs->search(
{
"me.distance" => { '<', 20000 },
'organisation.is_local' => [ 0, undef ],
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_non_local_purchase = $purchase_rs->search({
"me.distance" => { '>=', 20000 },
'organisation.is_local' => [0, undef],
},
{
join => { 'seller' => 'organisation' },
}
);
my $non_local_org_non_local_purchase = $purchase_rs->search(
{
"me.distance" => { '>=', 20000 },
'organisation.is_local' => [ 0, undef ],
},
{
join => { 'seller' => 'organisation' },
}
);
my $local_all = {
'Local shop local purchaser' => $local_org_local_purchase->count,
'Local shop non-local purchaser' => $local_org_non_local_purchase->count,
'Non-local shop local purchaser' => $non_local_org_local_purchase->count,
'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count,
};
my $local_all = {
'Local shop local purchaser' => $local_org_local_purchase->count,
'Local shop non-local purchaser' =>
$local_org_non_local_purchase->count,
'Non-local shop local purchaser' =>
$non_local_org_local_purchase->count,
'Non-local shop non-local purchaser' =>
$non_local_org_non_local_purchase->count,
};
return $c->render(
json => {
success => Mojo::JSON->true,
local_all => $local_all,
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
local_all => $local_all,
}
);
}

View file

@ -2,80 +2,87 @@ package Pear::LocalLoop::Controller::Api::V1::Organisation::Snippets;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $c = shift;
my $c = shift;
my $entity = $c->stash->{api_user}->entity;
my $data = {
all_sales_count => 0,
all_sales_total => 0,
all_purchases_count => 0,
all_purchases_total => 0,
this_month_sales_count => 0,
this_month_sales_total => 0,
this_month_purchases_count => 0,
this_month_purchases_total => 0,
this_week_sales_count => 0,
this_week_sales_total => 0,
this_week_purchases_count => 0,
this_week_purchases_total => 0,
today_sales_count => 0,
today_sales_total => 0,
today_purchases_count => 0,
today_purchases_total => 0,
};
my $entity = $c->stash->{api_user}->entity;
my $data = {
all_sales_count => 0,
all_sales_total => 0,
all_purchases_count => 0,
all_purchases_total => 0,
this_month_sales_count => 0,
this_month_sales_total => 0,
this_month_purchases_count => 0,
this_month_purchases_total => 0,
this_week_sales_count => 0,
this_week_sales_total => 0,
this_week_purchases_count => 0,
this_week_purchases_total => 0,
today_sales_count => 0,
today_sales_total => 0,
today_purchases_count => 0,
today_purchases_total => 0,
};
my $now = DateTime->now;
my $today = DateTime->today;
my $week_ago = $today->clone->subtract( days => 7 );
my $month_ago = $today->clone->subtract( days => 30 );
my $now = DateTime->now;
my $today = DateTime->today;
my $week_ago = $today->clone->subtract( days => 7 );
my $month_ago = $today->clone->subtract( days => 30 );
# TODO check that sales is doing the right thing here
my $all_sales = $entity->sales;
$data->{ all_sales_count } = $all_sales->count;
$data->{ all_sales_total } = $all_sales->get_column('value')->sum || 0;
$data->{ all_sales_total } /= 100000;
# TODO check that sales is doing the right thing here
my $all_sales = $entity->sales;
$data->{all_sales_count} = $all_sales->count;
$data->{all_sales_total} = $all_sales->get_column('value')->sum || 0;
$data->{all_sales_total} /= 100000;
my $today_sales = $entity->sales->search_between( $today, $now );
$data->{ today_sales_count } = $today_sales->count;
$data->{ today_sales_total } = $today_sales->get_column('value')->sum || 0;
$data->{ today_sales_total } /= 100000;
my $today_sales = $entity->sales->search_between( $today, $now );
$data->{today_sales_count} = $today_sales->count;
$data->{today_sales_total} = $today_sales->get_column('value')->sum || 0;
$data->{today_sales_total} /= 100000;
my $week_sales = $entity->sales->search_between( $week_ago, $today );
$data->{ this_week_sales_count } = $week_sales->count;
$data->{ this_week_sales_total } = $week_sales->get_column('value')->sum || 0;
$data->{ this_week_sales_total } /= 100000;
my $week_sales = $entity->sales->search_between( $week_ago, $today );
$data->{this_week_sales_count} = $week_sales->count;
$data->{this_week_sales_total} = $week_sales->get_column('value')->sum || 0;
$data->{this_week_sales_total} /= 100000;
my $month_sales = $entity->sales->search_between( $month_ago, $today );
$data->{ this_month_sales_count } = $month_sales->count;
$data->{ this_month_sales_total } = $month_sales->get_column('value')->sum || 0;
$data->{ this_month_sales_total } /= 100000;
my $month_sales = $entity->sales->search_between( $month_ago, $today );
$data->{this_month_sales_count} = $month_sales->count;
$data->{this_month_sales_total} =
$month_sales->get_column('value')->sum || 0;
$data->{this_month_sales_total} /= 100000;
my $all_purchases = $entity->purchases;
$data->{ all_purchases_count } = $all_purchases->count;
$data->{ all_purchases_total } = $all_purchases->get_column('value')->sum || 0;
$data->{ all_purchases_total } /= 100000;
my $all_purchases = $entity->purchases;
$data->{all_purchases_count} = $all_purchases->count;
$data->{all_purchases_total} =
$all_purchases->get_column('value')->sum || 0;
$data->{all_purchases_total} /= 100000;
my $today_purchases = $entity->purchases->search_between( $today, $now );
$data->{ today_purchases_count } = $today_purchases->count;
$data->{ today_purchases_total } = $today_purchases->get_column('value')->sum || 0;
$data->{ today_purchases_total } /= 100000;
my $today_purchases = $entity->purchases->search_between( $today, $now );
$data->{today_purchases_count} = $today_purchases->count;
$data->{today_purchases_total} =
$today_purchases->get_column('value')->sum || 0;
$data->{today_purchases_total} /= 100000;
my $week_purchases = $entity->purchases->search_between( $week_ago, $today );
$data->{ this_week_purchases_count } = $week_purchases->count;
$data->{ this_week_purchases_total } = $week_purchases->get_column('value')->sum || 0;
$data->{ this_week_purchases_total } /= 100000;
my $week_purchases =
$entity->purchases->search_between( $week_ago, $today );
$data->{this_week_purchases_count} = $week_purchases->count;
$data->{this_week_purchases_total} =
$week_purchases->get_column('value')->sum || 0;
$data->{this_week_purchases_total} /= 100000;
my $month_purchases = $entity->purchases->search_between( $month_ago, $today );
$data->{ this_month_purchases_count } = $month_purchases->count;
$data->{ this_month_purchases_total } = $month_purchases->get_column('value')->sum || 0;
$data->{ this_month_purchases_total } /= 100000;
my $month_purchases =
$entity->purchases->search_between( $month_ago, $today );
$data->{this_month_purchases_count} = $month_purchases->count;
$data->{this_month_purchases_total} =
$month_purchases->get_column('value')->sum || 0;
$data->{this_month_purchases_total} /= 100000;
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $data,
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $data,
}
);
}

View file

@ -2,192 +2,208 @@ package Pear::LocalLoop::Controller::Api::V1::Supplier::Location;
use Mojo::Base 'Mojolicious::Controller';
has validation_data => sub {
my $children_errors = {
latitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -90, 90 ], error_prefix => 'outside_range' } },
],
},
longitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -180, 180 ], error_prefix => 'outside_range' } },
],
},
};
my $children_errors = {
latitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{
in_range =>
{ args => [ -90, 90 ], error_prefix => 'outside_range' }
},
],
},
longitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{
in_range => {
args => [ -180, 180 ],
error_prefix => 'outside_range'
}
},
],
},
};
return {
index => {
north_east => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
south_west => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
}
}
return {
index => {
north_east => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
south_west => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
}
};
};
sub index {
my $c = shift;
my $c = shift;
return if $c->validation_error('index');
return if $c->validation_error('index');
my $json = $c->stash->{api_json};
my $json = $c->stash->{api_json};
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => [ 'upside_down' ],
},
status => 400,
);
}
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
# need: organisations only, with name, latitude, and longitude
my $org_rs = $entity->purchases->search_related('seller',
{
'seller.type' => 'organisation',
'organisation.latitude' => { -between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
] },
'organisation.longitude' => { -between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
] },
},
{
join => [ qw/ organisation / ],
columns => [
'organisation.name',
'organisation.latitude',
'organisation.longitude',
'organisation.street_name',
'organisation.town',
'organisation.postcode',
],
group_by => [ qw/ organisation.id / ],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $suppliers = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => ['upside_down'],
},
status => 400,
);
}
} $org_rs->all ];
$c->render(
json => {
success => Mojo::JSON->true,
suppliers => $suppliers,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
# need: organisations only, with name, latitude, and longitude
my $org_rs = $entity->purchases->search_related(
'seller',
{
'seller.type' => 'organisation',
'organisation.latitude' => {
-between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
]
},
'organisation.longitude' => {
-between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
]
},
},
{
join => [qw/ organisation /],
columns => [
'organisation.name', 'organisation.latitude',
'organisation.longitude', 'organisation.street_name',
'organisation.town', 'organisation.postcode',
],
group_by => [qw/ organisation.id /],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $suppliers = [
map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
}
} $org_rs->all
];
$c->render(
json => {
success => Mojo::JSON->true,
suppliers => $suppliers,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
}
sub trail_load {
my $c = shift;
my $c = shift;
return if $c->validation_error('index');
return if $c->validation_error('index');
my $json = $c->stash->{api_json};
my $json = $c->stash->{api_json};
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => [ 'upside_down' ],
},
status => 400,
);
}
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
my $orgs_lis = $c->schema->resultset('EntityAssociation')->search(
{
$json->{association} => 1,
},
);
# need: organisations only, with name, latitude, and longitude
my $org_rs = $orgs_lis->search_related('entity',
{
'entity.type' => 'organisation',
'organisation.latitude' => { -between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
] },
'organisation.longitude' => { -between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
] },
},
{
join => [ qw/ organisation / ],
columns => [
'organisation.name',
'organisation.latitude',
'organisation.longitude',
'organisation.street_name',
'organisation.town',
'organisation.postcode',
],
group_by => [ qw/ organisation.id / ],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $locations = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => ['upside_down'],
},
status => 400,
);
}
} $org_rs->all ];
$c->render(
json => {
success => Mojo::JSON->true,
locations => $locations,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
my $orgs_lis = $c->schema->resultset('EntityAssociation')->search(
{
$json->{association} => 1,
},
);
# need: organisations only, with name, latitude, and longitude
my $org_rs = $orgs_lis->search_related(
'entity',
{
'entity.type' => 'organisation',
'organisation.latitude' => {
-between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
]
},
'organisation.longitude' => {
-between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
]
},
},
{
join => [qw/ organisation /],
columns => [
'organisation.name', 'organisation.latitude',
'organisation.longitude', 'organisation.street_name',
'organisation.town', 'organisation.postcode',
],
group_by => [qw/ organisation.id /],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $locations = [
map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
}
} $org_rs->all
];
$c->render(
json => {
success => Mojo::JSON->true,
locations => $locations,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
}
1;

View file

@ -3,46 +3,46 @@ use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw/true false/;
sub index {
my $c = shift;
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
# Placeholder data
my $global_placeholder = {
group_name => {
threshold => {
awarded => true,
awarded_at => '2017-01-02T01:00:00Z',
threshold => 1,
points => 1,
},
total => 1,
},
};
my $organisation_placeholder = {
org_id => {
group_name => {
threshold => {
awarded => true,
awarded_at => '2017-01-02T01:00:00Z',
threshold => 1,
points => 1,
multiplier => 1,
# Placeholder data
my $global_placeholder = {
group_name => {
threshold => {
awarded => true,
awarded_at => '2017-01-02T01:00:00Z',
threshold => 1,
points => 1,
},
total => 1,
},
total => 1,
},
name => 'Placeholder',
},
};
};
my $organisation_placeholder = {
org_id => {
group_name => {
threshold => {
awarded => true,
awarded_at => '2017-01-02T01:00:00Z',
threshold => 1,
points => 1,
multiplier => 1,
},
total => 1,
},
name => 'Placeholder',
},
};
return $c->render(
json => {
success => Mojo::JSON->true,
global => $global_placeholder,
organisation => $organisation_placeholder,
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
global => $global_placeholder,
organisation => $organisation_placeholder,
}
);
}
1;

View file

@ -3,37 +3,37 @@ use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw/true false/;
sub index {
my $c = shift;
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
# Placeholder data
my $snippets_placeholder = {
points_total => 1,
point_last => 1,
trans_count => 1,
avg_multi => 1,
};
# Placeholder data
my $snippets_placeholder = {
points_total => 1,
point_last => 1,
trans_count => 1,
avg_multi => 1,
};
my $widget_line_placeholder = { labels => [], data => [] };
my $widget_line_placeholder = { labels => [], data => [] };
my $widget_progress_placeholder = {
this_week => 1,
last_week => 1,
max => 1,
sum => 1,
count => 1,
};
my $widget_progress_placeholder = {
this_week => 1,
last_week => 1,
max => 1,
sum => 1,
count => 1,
};
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $snippets_placeholder,
widget_line => $widget_line_placeholder,
widget_progress => $widget_progress_placeholder,
}
);
return $c->render(
json => {
success => Mojo::JSON->true,
snippets => $snippets_placeholder,
widget_line => $widget_line_placeholder,
widget_progress => $widget_progress_placeholder,
}
);
}
1;