Merge branch 'development' into TBSliver/Minion-Tasks

This commit is contained in:
Thomas Bloor 2018-04-16 12:49:59 +01:00
commit c3ac620a2d
No known key found for this signature in database
GPG key ID: 4657C7EBE42CC5CC
23 changed files with 408 additions and 97 deletions

View file

@ -2,6 +2,22 @@
# Next Release # Next Release
* Added `cron_daily` script for holding all daily cronjobs
* **Admin Fix** Parse currency without a currency symbol on import
* **Admin Fix** Fix large CSV issue on import
* Use custom secrets for encryption
# v0.10.6
* Fixed organisation submission
* Changed category listing code
* Made transaction upload code more lenient
* Added API ability to edit and delete transactions
* Added test for above
* Made test dumping more sane
* Fixed quantised transaction calcuations for weeks on sqlite
* Amended customer snippet, category list and customer stats tests
# v0.10.5 # v0.10.5
* **Admin Feature** Removed generic Transaction List, replaced with a new * **Admin Feature** Removed generic Transaction List, replaced with a new

View file

@ -34,6 +34,13 @@ sub startup {
}); });
my $config = $self->config; my $config = $self->config;
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() ]);
}
push @{ $self->commands->namespaces }, __PACKAGE__ . '::Command'; push @{ $self->commands->namespaces }, __PACKAGE__ . '::Command';
$self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } ); $self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } );
@ -155,6 +162,8 @@ sub startup {
$api->post('/stats/leaderboard')->to('api-stats#post_leaderboards'); $api->post('/stats/leaderboard')->to('api-stats#post_leaderboards');
$api->post('/stats/leaderboard/paged')->to('api-stats#post_leaderboards_paged'); $api->post('/stats/leaderboard/paged')->to('api-stats#post_leaderboards_paged');
$api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases'); $api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases');
$api->post('/recurring-transactions')->to('api-transactions#update_recurring');
$api->post('/recurring-transactions/delete')->to('api-transactions#delete_recurring');
my $api_v1 = $api->under('/v1'); my $api_v1 = $api->under('/v1');
@ -252,9 +261,9 @@ sub startup {
# $portal_api->post('/search')->to('api-upload#post_search'); # $portal_api->post('/search')->to('api-upload#post_search');
$self->hook( before_dispatch => sub { $self->hook( before_dispatch => sub {
my $self = shift; my $c = shift;
$self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development'; $c->res->headers->header('Access-Control-Allow-Origin' => '*') if $c->app->mode eq 'development';
}); });
$self->helper( copy_transactions_and_delete => sub { $self->helper( copy_transactions_and_delete => sub {

View file

@ -144,7 +144,8 @@ sub _csv_flash_error {
$c->flash( $c->flash(
error => $error, error => $error,
csv_data => $c->param('csv'), # If csv info is huge, this fails epically
#csv_data => $c->param('csv'),
date_format => $c->param('date_format'), date_format => $c->param('date_format'),
); );
} }

View file

@ -31,32 +31,33 @@ sub post_category_list {
value => { sum => 'value' }, value => { sum => 'value' },
category_id => 'category_id', category_id => 'category_id',
essential => 'essential', essential => 'essential',
} },
], ],
group_by => [ qw/ category_id quantised_weeks / ], group_by => [ qw/ category_id quantised_weeks essential / ],
order_by => { '-desc' => 'value' },
} }
); );
my $data = { categories => {}, essentials => {} }; my $data = { categories => {}, essentials => {} };
my $category_list = $c->schema->resultset('Category')->as_hash;
for my $cat_trans ( $month_transaction_category_rs->all ) { for my $cat_trans ( $month_transaction_category_rs->all ) {
my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised'));
my $days = $c->format_iso_date( $quantised ) || 0; my $days = $c->format_iso_date( $quantised ) || 0;
my $category = $cat_trans->get_column('category_id') || 0; my $category = $cat_trans->get_column('category_id') || 0;
my $value = ($cat_trans->get_column('value') || 0) / 100000; my $value = ($cat_trans->get_column('value') || 0) / 100000;
$data->{categories}->{$days} = [] unless exists $data->{categories}->{$days}; $data->{categories}->{$days}->{$category_list->{$category}} += $value;
push @{ $data->{categories}->{$days} }, {
days => $days,
value => $value,
category => $category,
};
next unless $cat_trans->get_column('essential'); next unless $cat_trans->get_column('essential');
$data->{essentials}->{$days}->{value} += $value; $data->{essentials}->{$days}->{value} += $value;
} }
for my $day ( keys %{ $data->{categories} } ) { for my $day ( keys %{ $data->{categories} } ) {
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @{ $data->{categories}->{$day} } ]; my @days = ( map{ {
days => $day,
value => $data->{categories}->{$day}->{$_},
category => $_,
} } keys %{ $data->{categories}->{$day} } );
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ];
} }
return $c->render( return $c->render(

View file

@ -63,9 +63,9 @@ sub post_customer {
my $entity = $c->stash->{api_user}->entity; my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( weeks => 7 ); my $duration_weeks = DateTime::Duration->new( weeks => 7 );
my $end = DateTime->today; my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration ); my $start_weeks = $end->clone->subtract_duration( $duration_weeks );
my $dtf = $c->schema->storage->datetime_parser; my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -73,7 +73,7 @@ sub post_customer {
{ {
purchase_time => { purchase_time => {
-between => [ -between => [
$dtf->format_datetime($start), $dtf->format_datetime($start_weeks),
$dtf->format_datetime($end), $dtf->format_datetime($end),
], ],
}, },
@ -92,7 +92,7 @@ sub post_customer {
); );
my @all_weeks = $week_transaction_rs->all; my @all_weeks = $week_transaction_rs->all;
my $first = $all_weeks[0]->get_column('count') || 0; my $first = defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0;
my $second = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0; my $second = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0;
my $max = max( map { $_->get_column('count') } @all_weeks ); my $max = max( map { $_->get_column('count') } @all_weeks );
my $sum = sum( map { $_->get_column('count') } @all_weeks ); my $sum = sum( map { $_->get_column('count') } @all_weeks );
@ -125,8 +125,69 @@ sub post_customer {
push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0); push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0);
} }
my $data = { cat_total => {}, categories => {}, essentials => {} };
my $purchase_rs = $entity->purchases;
my $purchase_no_essential_rs = $purchase_rs->search({
"me.essential" => 1,
});
$data->{essentials} = {
purchase_no_total => $purchase_rs->count,
purchase_no_essential_total => $purchase_no_essential_rs->count,
};
my $duration_month = DateTime::Duration->new( days => 28 );
my $start_month = $end->clone->subtract_duration( $duration_month );
my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search(
{
purchase_time => {
-between => [
$dtf->format_datetime($start_month),
$dtf->format_datetime($end),
],
},
buyer_id => $entity->id,
},
{
columns => [
{
quantised => 'quantised_weeks',
value => { sum => 'value' },
category_id => 'category_id',
essential => 'essential',
},
],
group_by => [ qw/ category_id quantised_weeks essential / ],
}
);
my $category_list = $c->schema->resultset('Category')->as_hash;
for my $cat_trans ( $month_transaction_category_rs->all ) {
my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised'));
my $days = $c->format_iso_date( $quantised ) || 0;
my $category = $cat_trans->get_column('category_id') || 0;
my $value = ($cat_trans->get_column('value') || 0) / 100000;
$data->{cat_total}->{$category_list->{$category}} += $value;
$data->{categories}->{$days}->{$category_list->{$category}} += $value;
next unless $cat_trans->get_column('essential');
$data->{essentials}->{$days}->{value} += $value;
}
for my $day ( keys %{ $data->{categories} } ) {
my @days = ( map{ {
days => $day,
value => $data->{categories}->{$day}->{$_},
category => $_,
} } keys %{ $data->{categories}->{$day} } );
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ];
}
return $c->render( json => { return $c->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
data => $data,
weeks => $weeks, weeks => $weeks,
sectors => $sectors, sectors => $sectors,
}); });

View file

@ -8,6 +8,21 @@ has error_messages => sub {
required => { message => 'No email sent.', status => 400 }, required => { message => 'No email sent.', status => 400 },
email => { message => 'Email is invalid.', status => 400 }, email => { message => 'Email is invalid.', status => 400 },
}, },
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 },
},
}; };
}; };
@ -30,7 +45,11 @@ sub post_transaction_list_purchases {
}, },
); );
# purchase_time needs timezone attached to it my $recurring_transactions = $c->schema->resultset('TransactionRecurring')->search({
buyer_id => $user->id,
});
# purchase_time needs timezone attached to it
my @transaction_list = ( my @transaction_list = (
map {{ map {{
seller => $_->seller->name, seller => $_->seller->name,
@ -39,11 +58,112 @@ sub post_transaction_list_purchases {
}} $transactions->all }} $transactions->all
); );
my @recurring_transaction_list = (
map {{
id => $_->id,
seller => $_->seller->name,
value => $_->value / 100000,
start_time => $c->format_iso_datetime($_->start_time),
last_updated => $c->format_iso_datetime($_->last_updated) || undef,
essential => $_->essential,
category => $_->category_id || 0,
recurring_period => $_->recurring_period,
}} $recurring_transactions->all
);
return $c->render( json => { return $c->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
transactions => \@transaction_list, transactions => \@transaction_list,
recurring_transactions => \@recurring_transaction_list,
page_no => $transactions->pager->total_entries, page_no => $transactions->pager->total_entries,
}); });
} }
sub update_recurring {
my $c = shift;
my $user = $c->stash->{api_user};
my $validation = $c->validation;
$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',
});
}
sub delete_recurring {
my $c = shift;
my $user = $c->stash->{api_user};
my $validation = $c->validation;
$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,
);
}
$recur_transaction->delete;
return $c->render( json => {
success => Mojo::JSON->true,
message => 'Recurring Transaction Deleted Successfully',
});
}
1; 1;

View file

@ -147,7 +147,7 @@ sub post_upload {
# Unknown Organisation # Unknown Organisation
$validation->required('organisation_name'); $validation->required('organisation_name');
$validation->optional('street_name'); $validation->optional('street_name');
$validation->required('town'); $validation->optional('town');
$validation->optional('postcode')->postcode; $validation->optional('postcode')->postcode;
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;
@ -243,18 +243,12 @@ sub post_category {
my $c = shift; my $c = shift;
my $self = $c; my $self = $c;
my $categories = { ids => [], names => [] }; my $category_list = $c->schema->resultset('Category')->as_hash;
delete $category_list->{0};
my $category_rs = $c->schema->resultset('Category');
for ( $category_rs->all ) {
push @{ $categories->{ ids } }, $_->get_column('id');
push @{ $categories->{ names } }, $_->get_column('name');
}
return $self->render( json => { return $self->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
categories => $categories, categories => $category_list,
}); });
} }

View file

@ -7,6 +7,7 @@ sub index {
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({ my $local_org_local_purchase = $purchase_rs->search({
"me.distance" => { '<', 20000 }, "me.distance" => { '<', 20000 },
'organisation.is_local' => 1, 'organisation.is_local' => 1,
@ -43,17 +44,17 @@ sub index {
} }
); );
my $data = { my $local_all = {
'Local shop local purchaser' => $local_org_local_purchase->count, 'Local shop local purchaser' => $local_org_local_purchase->count,
'Local shop non-local purchaser' => $local_org_non_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 local purchaser' => $non_local_org_local_purchase->count,
'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count,
}; };
return $c->render( return $c->render(
json => { json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
pie => $data, local_all => $local_all,
} }
); );

View file

@ -9,6 +9,8 @@ sub register {
my $value; my $value;
if ( $currency_string =~ /^£([\d.]+)/ ) { if ( $currency_string =~ /^£([\d.]+)/ ) {
$value = $1 * 1; $value = $1 * 1;
} elsif ( $currency_string =~ /^([\d.]+)/ ) {
$value = $1 * 1;
} }
return $value; return $value;
}); });

View file

@ -66,6 +66,7 @@ sub register {
$app->helper( format_iso_datetime => sub { $app->helper( format_iso_datetime => sub {
my ( $c, $datetime_obj ) = @_; my ( $c, $datetime_obj ) = @_;
return unless defined $datetime_obj;
return $c->iso_datetime_parser->format_datetime( return $c->iso_datetime_parser->format_datetime(
$datetime_obj, $datetime_obj,
); );

View file

@ -10,18 +10,18 @@ __PACKAGE__->table('view_quantised_transactions');
__PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition( qq/ __PACKAGE__->result_source_instance->view_definition( qq/
SELECT "transactions.value", SELECT "transactions"."value",
"transactions.distance", "transactions"."distance",
"transactions.purchase_time", "transactions"."purchase_time",
"transactions.buyer_id", "transactions"."buyer_id",
"transactions.seller_id", "transactions"."seller_id",
"transactions.essential", "transactions"."essential",
"transaction_category.category_id", "transaction_category"."category_id",
DATE_TRUNC('hour', "transactions.purchase_time") AS "quantised_hours", DATE_TRUNC('hour', "transactions"."purchase_time") AS "quantised_hours",
DATE_TRUNC('day', "transactions.purchase_time") AS "quantised_days", DATE_TRUNC('day', "transactions"."purchase_time") AS "quantised_days",
DATE_TRUNC('week', "transactions.purchase_time") AS "quantised_weeks" DATE_TRUNC('week', "transactions"."purchase_time") AS "quantised_weeks"
FROM "transactions" FROM "transactions"
LEFT JOIN "transaction_category" ON "transactions.id" = "transaction_category.transaction_id" LEFT JOIN "transaction_category" ON "transactions"."id" = "transaction_category"."transaction_id"
/); /);
1; 1;

View file

@ -19,7 +19,7 @@ SELECT "transactions"."value",
"transaction_category"."category_id", "transaction_category"."category_id",
DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions"."purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions"."purchase_time")) AS "quantised_hours",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time")) AS "quantised_days", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time")) AS "quantised_days",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time", 'weekday 1')) AS "quantised_weeks" DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time", 'weekday 0','-6 days')) AS "quantised_weeks"
FROM "transactions" FROM "transactions"
LEFT JOIN "transaction_category" ON "transactions"."id" = "transaction_category"."transaction_id" LEFT JOIN "transaction_category" ON "transactions"."id" = "transaction_category"."transaction_id"
/); /);

View file

@ -17,7 +17,7 @@ SELECT "value",
"seller_id", "seller_id",
DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time", 'weekday 1')) AS "quantised_weeks" DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time",'weekday 0','-6 days')) AS "quantised_weeks"
FROM "transactions" FROM "transactions"
/); /);

View file

@ -0,0 +1,22 @@
package Pear::LocalLoop::Schema::ResultSet::Category;
use strict;
use warnings;
use base 'DBIx::Class::ResultSet';
sub as_hash {
my ( $self ) = @_;
my %category_list = (
(
map {
$_->id => $_->name,
} $self->all
),
0 => 'Uncategorised',
);
return \%category_list;
}
1;

View file

@ -114,6 +114,8 @@ sub dump_error {
my $self = shift; my $self = shift;
if ( my $error = $self->tx->res->dom->at('pre[id="error"]') ) { if ( my $error = $self->tx->res->dom->at('pre[id="error"]') ) {
diag $error->text; diag $error->text;
} elsif ( my $route_error = $self->tx->res->dom->at('div[id="routes"] > p') ) {
diag $route_error->content;
} else { } else {
diag $self->tx->res->to_string; diag $self->tx->res->to_string;
} }

10
script/cron_daily Normal file
View file

@ -0,0 +1,10 @@
#! /bin/bash
# Scripts to run daily.
# This will be run sometime between 2 & 3AM every morning.
# If order matters, make sure they are in the right place.
eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
MOJO_MODE=production ./script/pear-local_loop recur_transactions --force
MOJO_MODE=production ./script/pear-local_loop recalc_leaderboards

View file

@ -1,5 +0,0 @@
#! /bin/bash
eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
MOJO_MODE=production ./script/pear-local_loop recalc_leaderboards

View file

@ -19,7 +19,12 @@ $framework->install_fixtures('users');
my $t = $framework->framework; my $t = $framework->framework;
my $schema = $t->app->schema; my $schema = $t->app->schema;
set_absolute_time('2017-01-02T00:00:00Z'); $schema->resultset('Category')->create({
id => 1,
name => 'test',
});
set_absolute_time('2018-01-08T00:00:00Z');
my $start = DateTime->today->subtract( hours => 12 ); my $start = DateTime->today->subtract( hours => 12 );
@ -50,48 +55,40 @@ $t->post_ok('/api/stats/category' => json => {
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/data', { ->json_is('/data', {
categories => { categories => {
"2016-12-05" => [{ "2017-12-11" => [{
days => "2016-12-05", days => "2017-12-11",
value => 30, value => 210,
category => 1, category => 'test',
}], }],
"2016-12-12" => [{ "2017-12-18" => [{
days => "2016-12-12", days => "2017-12-18",
value => 200, value => 200,
category => 1, category => 'test',
}], }],
"2016-12-19" => [{ "2017-12-25" => [{
days => "2016-12-19", days => "2017-12-25",
value => 220, value => 210,
category => 1, category => 'test',
}], }],
"2016-12-26" => [{ "2018-01-01" => [{
days => "2016-12-26", days => "2018-01-01",
value => 190, value => 190,
category => 1, category => 'test',
}],
"2017-01-02" => [{
days => "2017-01-02",
value => 170,
category => 1,
}], }],
}, },
essentials => { essentials => {
"2016-12-05" => { "2017-12-11" => {
value => 30, value => 210,
}, },
"2016-12-12" => { "2017-12-18" => {
value => 200, value => 200,
}, },
"2016-12-19" => { "2017-12-25" => {
value => 220, value => 210,
}, },
"2016-12-26" => { "2018-01-01" => {
value => 190, value => 190,
}, },
"2017-01-02" => {
value => 170,
},
} }
})->or($framework->dump_error); })->or($framework->dump_error);
@ -99,6 +96,7 @@ sub create_random_transaction {
my $buyer = shift; my $buyer = shift;
my $time = shift; my $time = shift;
my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity; my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity;
my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity; my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
my $test_transaction = $schema->resultset('Transaction')->create({ my $test_transaction = $schema->resultset('Transaction')->create({
@ -109,6 +107,7 @@ sub create_random_transaction {
purchase_time => $time, purchase_time => $time,
essential => 1, essential => 1,
}); });
$schema->resultset('TransactionCategory')->create({ $schema->resultset('TransactionCategory')->create({
category_id => 1, category_id => 1,
transaction_id => $test_transaction->id, transaction_id => $test_transaction->id,

View file

@ -19,7 +19,7 @@ $framework->install_fixtures('users');
my $t = $framework->framework; my $t = $framework->framework;
my $schema = $t->app->schema; my $schema = $t->app->schema;
set_absolute_time('2017-01-01T00:00:00Z'); set_absolute_time('2018-01-08T00:00:00Z');
my $start = DateTime->today->subtract( hours => 12 ); my $start = DateTime->today->subtract( hours => 12 );
@ -49,17 +49,50 @@ $t->post_ok('/api/stats/customer' => json => {
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/weeks', { ->json_is('/weeks', {
first => 2, first => 17,
second => 21, second => 20,
max => 22, max => 21,
sum => 118, sum => 118,
count => 7, count => 6,
}) })
->or($framework->dump_error)
->json_is('/sectors', { ->json_is('/sectors', {
sectors => ['A'], sectors => ['A'],
purchases => [118], purchases => [118],
}); })
->json_is('/data', {
cat_total => {
Uncategorised => 810,
},
categories => {
"2017-12-11" => [{
days => "2017-12-11",
value => 210,
category => 'Uncategorised',
}],
"2017-12-18" => [{
days => "2017-12-18",
value => 200,
category => 'Uncategorised',
}],
"2017-12-25" => [{
days => "2017-12-25",
value => 210,
category => 'Uncategorised',
}],
"2018-01-01" => [{
days => "2018-01-01",
value => 190,
category => 'Uncategorised',
}],
},
cat_total => {
Uncategorised => 810,
},
essentials => {
purchase_no_essential_total => 0,
purchase_no_total => 118,
},
})->or($framework->dump_error);
sub create_random_transaction { sub create_random_transaction {
my $buyer = shift; my $buyer = shift;

View file

@ -38,8 +38,6 @@ my $session_key = $framework->login({
password => 'abc123', password => 'abc123',
}); });
use Data::Dumper;
$t->post_ok('/api/outgoing-transactions' => json => { $t->post_ok('/api/outgoing-transactions' => json => {
session_key => $session_key, session_key => $session_key,
}) })
@ -50,6 +48,36 @@ $t->post_ok('/api/outgoing-transactions' => json => {
->json_has('/transactions/1/value') ->json_has('/transactions/1/value')
->json_has('/transactions/1/purchase_time'); ->json_has('/transactions/1/purchase_time');
my $test_purchase_time = "2017-08-14T11:29:07.965+01:00";
$t->post_ok('/api/recurring-transactions' => json => {
session_key => $session_key,
id => 1,
apply_time => $test_purchase_time,
essential => "false",
value => 5,
recurring_period => 'daily',
})
->status_is(200)->or($framework->dump_error)
->json_is({
success => Mojo::JSON->true,
message => 'Recurring Transaction Updated Successfully',
});
is $schema->resultset('TransactionRecurring')->count, 87;
$t->post_ok('/api/recurring-transactions/delete' => json => {
session_key => $session_key,
id => 1,
})
->status_is(200)->or($framework->dump_error)
->json_is({
success => Mojo::JSON->true,
message => 'Recurring Transaction Deleted Successfully',
});
is $schema->resultset('TransactionRecurring')->count, 86;
sub create_random_transaction { sub create_random_transaction {
my $buyer = shift; my $buyer = shift;
@ -64,6 +92,14 @@ sub create_random_transaction {
proof_image => 'a', proof_image => 'a',
purchase_time => $time, purchase_time => $time,
}); });
$schema->resultset('TransactionRecurring')->create({
buyer => $buyer_result,
seller => $seller_result,
value => 10,
start_time => $time,
essential => 1,
recurring_period => 'weekly',
});
} }
done_testing; done_testing;

View file

@ -42,7 +42,7 @@ $t->post_ok('/api/v1/customer/pies' => json => {
session_key => $session_key, session_key => $session_key,
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/pie', { ->json_is('/local_all', {
'Local shop local purchaser' => 0, 'Local shop local purchaser' => 0,
'Local shop non-local purchaser' => 0, 'Local shop non-local purchaser' => 0,
'Non-local shop local purchaser' => 0, 'Non-local shop local purchaser' => 0,

View file

@ -1,5 +1,9 @@
use Mojo::Base -strict; use Mojo::Base -strict;
BEGIN {
use Test::MockTime qw/ set_absolute_time /;
}
use FindBin qw/ $Bin /; use FindBin qw/ $Bin /;
use Test::More; use Test::More;
@ -15,7 +19,7 @@ $framework->install_fixtures('users');
my $t = $framework->framework; my $t = $framework->framework;
my $schema = $t->app->schema; my $schema = $t->app->schema;
$t->app->schema->resultset('Leaderboard')->create_new( 'monthly_total', DateTime->now->truncate(to => 'month' )->subtract( months => 1) ); set_absolute_time('2017-01-02T00:00:00Z');
my $start = DateTime->today->subtract( hours => 12 ); my $start = DateTime->today->subtract( hours => 12 );
@ -25,15 +29,19 @@ for my $count ( 0 .. 60 ) {
create_random_transaction( 'test1@example.com', $trans_day ); create_random_transaction( 'test1@example.com', $trans_day );
if ( $count % 2 ) { if ( $count % 2 ) {
create_random_transaction( 'test2@example.com', $trans_day ); create_random_transaction( 'test1@example.com', $trans_day );
} }
if ( $count % 3 ) { if ( $count % 3 ) {
create_random_transaction( 'test3@example.com', $trans_day ); create_random_transaction( 'test1@example.com', $trans_day );
} }
if ( $count % 4 ) { if ( $count % 4 ) {
create_random_transaction( 'test4@example.com', $trans_day ); create_random_transaction( 'test1@example.com', $trans_day );
} }
} }
my $lb_start = $start->clone->truncate( to => 'month' )->subtract( months => 1);
#use Devel::Dwarn; Dwarn({ $_->get_columns }) for $schema->resultset('Transaction')->all;
$schema->resultset('Leaderboard')->create_new( 'monthly_total', $lb_start );
my $session_key = $framework->login({ my $session_key = $framework->login({
email => 'test1@example.com', email => 'test1@example.com',
@ -45,7 +53,7 @@ $t->post_ok('/api/v1/customer/snippets' => json => {
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/snippets', { ->json_is('/snippets', {
user_sum => 610, user_sum => 1760,
user_position => 1, user_position => 1,
}); });

View file

@ -59,8 +59,8 @@
<label for="local" class="col-md-4 col-form-label">Is Local</label> <label for="local" class="col-md-4 col-form-label">Is Local</label>
<div class="col-md-8"> <div class="col-md-8">
<select id="local" class="form-control" name="is_local"> <select id="local" class="form-control" name="is_local">
<option value="0"<%= !$valid_org->is_local ? '' : ' selected' %>>Non Local</option> <option value="0" selected>Non Local</option>
<option value="1"<%= $valid_org->is_local ? ' selected' : '' %>>Local</option> <option value="1">Local</option>
</select> </select>
</div> </div>
</div> </div>
@ -68,8 +68,8 @@
<label for="local" class="col-md-4 col-form-label">Is Fairly Traded</label> <label for="local" class="col-md-4 col-form-label">Is Fairly Traded</label>
<div class="col-md-8"> <div class="col-md-8">
<select id="local" class="form-control" name="is_fair"> <select id="local" class="form-control" name="is_fair">
<option value="0"<%= !$valid_org->is_fair ? '' : ' selected' %>>Non Fairly Traded</option> <option value="0" selected>Non Fairly Traded</option>
<option value="1"<%= $valid_org->is_fair ? ' selected' : '' %>>Fairly Traded</option> <option value="1">Fairly Traded</option>
</select> </select>
</div> </div>
</div> </div>