This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Command/dev_transactions.pm

109 lines
2.5 KiB
Perl
Raw Normal View History

package Pear::LocalLoop::Command::dev_transactions;
2021-03-20 12:09:50 +00:00
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 {
2021-03-20 12:09:50 +00:00
my ( $self, @args ) = @_;
2021-03-20 12:09:50 +00:00
getopt \@args,
'f|force' => \my $force,
'd|date=s' => \my $date,
'n|number=i' => \my $number,
'c|count=i' => \my $count;
2021-03-20 12:09:50 +00:00
unless ( defined $force ) {
say "Will not do anything without force option";
return;
}
2021-03-20 12:09:50 +00:00
if ( ( defined( $ENV{MOJO_MODE} ) && $ENV{MOJO_MODE} eq 'production' )
|| $self->app->mode eq 'production' )
{
say "Will not run dev data fixtures in production!";
return;
}
2021-03-20 12:09:50 +00:00
my $date_formatter =
DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
2021-03-20 12:09:50 +00:00
my $datetime;
2021-03-20 12:09:50 +00:00
if ( defined $date ) {
2021-03-20 12:09:50 +00:00
$datetime = $date_formatter->parse_datetime($date);
2021-03-20 12:09:50 +00:00
unless ( defined $datetime ) {
say "Unrecognised date format, please use 'YYYY-MM-DD' Format";
return;
}
}
else {
$datetime = DateTime->today;
}
2021-03-20 12:09:50 +00:00
my $schema = $self->app->schema;
2021-03-20 12:09:50 +00:00
my $user_rs = $schema->resultset('User');
2021-03-20 12:09:50 +00:00
my $organisation_rs = $user_rs->search( { customer_id => undef } );
2021-03-20 12:09:50 +00:00
my $dtf = $schema->storage->datetime_parser;
2021-03-20 12:09:50 +00:00
my @organisations = $organisation_rs->all;
2021-03-20 12:09:50 +00:00
unless ( defined $number ) {
$number = 1;
}
2021-03-20 12:09:50 +00:00
unless ( defined $count ) {
$count = 0;
}
2021-03-20 12:09:50 +00:00
for my $day_sub ( 0 .. $count ) {
$datetime->subtract( days => 1 );
for ( 1 .. $number ) {
for my $user_result ( $user_rs->all ) {
$user_result->create_related(
'transactions',
{
seller_id =>
$organisations[ int( rand($#organisations) ) ]
->organisation_id,
value => int( rand(9999) ) / 100,
proof_image => 'a',
purchase_time => $dtf->format_datetime(
$datetime->clone->add(
minutes => int( rand(1440) )
)
),
}
);
}
}
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
=head1 SYNOPSIS
Usage: APPLICATION dev_transactions [OPTIONS]
Options:
-f, --force Actually insert the data
-d, --date Date to create the transactions on
=cut
1;