From 9c79e50eba702eaf4a39250794a5cc4dc5adcc79 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 9 Jul 2019 16:25:32 +0100 Subject: [PATCH] implemented API for transaction and supplier log --- lib/Pear/LocalLoop.pm | 2 + .../LocalLoop/Controller/Admin/ImportFrom.pm | 4 +- lib/Pear/LocalLoop/Controller/Api/External.pm | 96 +++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 lib/Pear/LocalLoop/Controller/Api/External.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index a6fd54f..6307e45 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -189,6 +189,8 @@ sub startup { $api_v1_org->post('/supplier/add')->to('api-organisation#post_supplier_add'); $api_v1_org->post('/employee')->to('api-organisation#post_employee_read'); $api_v1_org->post('/employee/add')->to('api-organisation#post_employee_add'); + $api_v1_org->post('/external/transactions')->to('api-external#post_lcc_transactions'); + $api_v1_org->post('/external/suppliers')->to('api-external#post_lcc_suppliers'); my $api_v1_cust = $api_v1->under('/customer')->to('api-v1-customer#auth'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index 3b8e923..aa54a81 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -32,7 +32,7 @@ sub post_suppliers { my $job_id = $c->minion->enqueue('csv_supplier_import' => [$filename] ); - my $job_url = $c->url_for("/admin/minionjobs?id=$job_id")->to_abs; + my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs; $c->flash(success => "CSV import started, see status of minion job at: $job_url"); return $c->redirect_to( '/admin/import_from' ); @@ -60,7 +60,7 @@ sub post_transactions { my $job_id = $c->minion->enqueue('csv_transaction_import' => [$filename] ); - my $job_url = $c->url_for("/admin/minionjobs?id=$job_id")->to_abs; + my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs; $c->flash(success => "CSV import started, see status of minion job at: $job_url"); return $c->redirect_to( '/admin/import_from' ); diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm new file mode 100644 index 0000000..138be8e --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -0,0 +1,96 @@ +package Pear::LocalLoop::Controller::Api::External; +use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON; + +sub post_lcc_transactions { + my $c = shift; + + use Devel::Dwarn; + Dwarn "hello"; + + my $user = $c->stash->{api_user}; + + # TODO Check the user is lancaster city council + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->optional('page')->number; + + return $c->api_validation_error if $validation->has_error; + + my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); + + my $lcc_transactions = $lcc_import_ext_ref->search_related('transactions', + undef, + { + page => $validation->param('page') || 1, + rows => 10, + order_by => { -desc => 'purchase_time' }, + }, + ); + + # purchase_time needs timezone attached to it + my @transaction_list = ( + map {{ + transaction_external_id => $_->external_id, + seller => $_->transaction->seller->name, + net_value => $_->transaction->value, + gross_value => $_->transaction->meta->gross_value, + sales_tax_value => $_->transaction->meta->sales_tax_value, + purchase_time => $c->transaction->format_iso_datetime($_->purchase_time), + }} $transactions->all + ); + + return $c->render( json => { + success => Mojo::JSON->true, + transactions => \@transaction_list, + page_no => $lcc_transactions->pager->total_entries, + }); +} + +sub post_lcc_suppliers { + my $c = shift; + + my $user = $c->stash->{api_user}; + + # TODO give an error if user is not of Lancashire County Council + + my $is_lcc = $self->entity->organisation->count({ name => "Lancashire County Council" }); + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->optional('page')->number; + + return $c->api_validation_error if $validation->has_error; + + my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); + + my $lcc_suppliers = $lcc_import_ext_ref->search_related('organisations', + undef, + { + page => $validation->param('page') || 1, + rows => 10, + order_by => { -desc => 'organisation.name' }, + }, + ); + + # purchase_time needs timezone attached to it + my @supplier_list = ( + map {{ + supplier_external_id => $_->external_id, + name => $_->organisation->name, + street => $_->organisation->street_name, + town => $_->organisation->town, + postcode => $_->organisation->post_code, + country => $_->organisation->country, + }} $lcc_suppliers->all + ); + + return $c->render( json => { + success => Mojo::JSON->true, + suppliers => \@supplier_list, + page_no => $lcc_suppliers->pager->total_entries, + }); +} + +1;