Merge pull request #110 from Pear-Trading/finn/recurring

added recurring transaction editing and deletion
This commit is contained in:
Finn 2018-03-20 19:19:15 +00:00 committed by GitHub
commit 047ec888fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 180 additions and 16 deletions

View file

@ -154,6 +154,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');

View file

@ -43,7 +43,7 @@ sub post_category_list {
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') || undef;
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} = [] unless exists $data->{categories}->{$days};
push @{ $data->{categories}->{$days} }, { push @{ $data->{categories}->{$days} }, {

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,21 @@ sub post_category {
my $c = shift; my $c = shift;
my $self = $c; my $self = $c;
my $categories = { ids => [], names => [] };
my $category_rs = $c->schema->resultset('Category'); my $category_rs = $c->schema->resultset('Category');
for ( $category_rs->all ) { # for ( $category_rs->all ) {
push @{ $categories->{ ids } }, $_->get_column('id'); # push @{ $categories->{ ids } }, $_->get_column('id');
push @{ $categories->{ names } }, $_->get_column('name'); # push @{ $categories->{ names } }, $_->get_column('name');
} # }
my %category_list = (
map {
$_->id => $_->name,
} $category_rs->all
);
return $self->render( json => { return $self->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
categories => $categories, categories => \%category_list,
}); });
} }

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

@ -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;
} }

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

@ -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>