From 606d5bffc2a994dbbdb8d0c462158aa00e99acbb Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 12 Jun 2017 23:22:34 +0100 Subject: [PATCH] added dev data and transaction creation scripts --- lib/Pear/LocalLoop/Command/dev_data.pm | 103 ++++++++++++++++++ .../LocalLoop/Command/dev_transactions.pm | 90 +++++++++++++++ lib/Pear/LocalLoop/Command/leaderboard.pm | 1 + 3 files changed, 194 insertions(+) create mode 100644 lib/Pear/LocalLoop/Command/dev_data.pm create mode 100644 lib/Pear/LocalLoop/Command/dev_transactions.pm diff --git a/lib/Pear/LocalLoop/Command/dev_data.pm b/lib/Pear/LocalLoop/Command/dev_data.pm new file mode 100644 index 0000000..1b5053d --- /dev/null +++ b/lib/Pear/LocalLoop/Command/dev_data.pm @@ -0,0 +1,103 @@ +package Pear::LocalLoop::Command::dev_data; +use Mojo::Base 'Mojolicious::Command'; + +use Mojo::Util 'getopt'; + +has description => 'Input Dev Data'; + +has usage => sub { shift->extract_usage }; + +sub run { + my ( $self, @args ) = @_; + + getopt \@args, + 'f|force' => \my $force; + + unless ( defined $force ) { + say "Will not do anything without force option"; + return; + } + + if ( $ENV{MOJO_MODE} eq 'production' || $self->app->mode eq 'production' ) { + say "Will not run dev data fixtures in production!"; + return; + } + + my $schema = $self->app->schema; + + $schema->resultset('AgeRange')->populate([ + [ qw/ string / ], + [ '20-35' ], + [ '35-50' ], + [ '50+' ], + ]); + + $schema->resultset('Leaderboard')->populate([ + [ qw/ name type / ], + [ 'Daily Total', 'daily_total' ], + [ 'Daily Count', 'daily_count' ], + [ 'Weekly Total', 'weekly_total' ], + [ 'Weekly Count', 'weekly_count' ], + [ 'Monthly Total', 'monthly_total' ], + [ 'Monthly Count', 'monthly_count' ], + [ 'All Time Total', 'all_time_total' ], + [ 'All Time Count', 'all_time_count' ], + ]); + + $schema->resultset('User')->create({ + email => 'test@example.com', + password => 'abc123', + customer => { + full_name => 'Test User', + display_name => 'Test User', + age_range_id => 1, + postcode => 'LA1 1AA', + }, + administrator => {}, + }); + + $schema->resultset('User')->create({ + email => 'test2@example.com', + password => 'abc123', + customer => { + full_name => 'Test User 2', + display_name => 'Test User 2', + age_range_id => 1, + postcode => 'LA1 1AA', + }, + }); + + $schema->resultset('User')->create({ + email => 'test3@example.com', + password => 'abc123', + customer => { + full_name => 'Test User 3', + display_name => 'Test User 3', + age_range_id => 1, + postcode => 'LA1 1AA', + }, + }); + + $schema->resultset('User')->create({ + email => 'testorg@example.com', + password => 'abc123', + organisation => { + name => 'Test Org', + street_name => 'Test Street', + town => 'Lancaster', + postcode => 'LA1 1AA', + }, + }); +} + +=head1 SYNOPSIS + + Usage: APPLICATION dev_data [OPTIONS] + + Options: + + -f, --force Actually insert the data + +=cut + +1; diff --git a/lib/Pear/LocalLoop/Command/dev_transactions.pm b/lib/Pear/LocalLoop/Command/dev_transactions.pm new file mode 100644 index 0000000..17a31c8 --- /dev/null +++ b/lib/Pear/LocalLoop/Command/dev_transactions.pm @@ -0,0 +1,90 @@ +package Pear::LocalLoop::Command::dev_transactions; +use Mojo::Base 'Mojolicious::Command'; + +use Mojo::Util 'getopt'; + +use DateTime; +use DateTime::Format::Strptime; + +has description => 'Input Dev Transaction'; + +has usage => sub { shift->extract_usage }; + +sub run { + my ( $self, @args ) = @_; + + getopt \@args, + 'f|force' => \my $force, + 'd|date=s' => \my $date, + 'n|number=i' => \my $number, + 'c|count=i' => \my $count; + + unless ( defined $force ) { + say "Will not do anything without force option"; + return; + } + + if ( $ENV{MOJO_MODE} eq 'production' || $self->app->mode eq 'production' ) { + say "Will not run dev data fixtures in production!"; + return; + } + + my $date_formatter = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%d' + ); + + my $datetime; + + if ( defined $date ) { + + $datetime = $date_formatter->parse_datetime($date); + + unless ( defined $datetime ) { + say "Unrecognised date format, please use 'YYYY-MM-DD' Format"; + return; + } + } else { + $datetime = DateTime->today; + } + + my $schema = $self->app->schema; + + my $user_rs = $schema->resultset('User'); + + my $organisation_rs = $user_rs->search({ customer_id => undef }); + + my $dtf = $schema->storage->datetime_parser; + + my @organisations = $organisation_rs->all; + + unless ( defined $number ) { + $number = 1; + } + + for my $day_sub ( 0 .. $count ) { + $datetime->subtract( days => $day_sub ); + for ( 1 .. $number ) { + for my $user_result ( $user_rs->all ) { + $user_result->create_related( 'transactions', { + seller_id => $organisations[int(rand($#organisations))]->id, + value => int(rand(9999)) / 100, + proof_image => 'a', + submitted_at => $dtf->format_datetime($datetime->clone->add( minutes => int(rand(1440)) )), + }); + } + } + } +} + +=head1 SYNOPSIS + + Usage: APPLICATION dev_transactions [OPTIONS] + + Options: + + -f, --force Actually insert the data + -d, --date Date to create the transactions on + +=cut + +1; diff --git a/lib/Pear/LocalLoop/Command/leaderboard.pm b/lib/Pear/LocalLoop/Command/leaderboard.pm index ba8f1c7..840efcb 100644 --- a/lib/Pear/LocalLoop/Command/leaderboard.pm +++ b/lib/Pear/LocalLoop/Command/leaderboard.pm @@ -44,6 +44,7 @@ sub run { unless ( defined $datetime ) { say "Unrecognised date format, please use 'YYYY-MM-DD' Format"; + return; } $leaderboard->create_new($datetime);