From 662219fc6e4bc2429d92fb00b865ebe6eb415daf Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 8 Sep 2017 11:41:21 +0100 Subject: [PATCH] Initial API for submission added --- lib/Pear/LocalLoop.pm | 3 + .../LocalLoop/Controller/Api/Organisation.pm | 120 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 lib/Pear/LocalLoop/Controller/Api/Organisation.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 18e997b..fbc6d74 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -155,6 +155,9 @@ sub startup { $api->post('/stats')->to('api-stats#post_index'); $api->post('/stats/leaderboard')->to('api-stats#post_leaderboards'); $api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases'); + $api->post('/org/payroll')->to('api-organisation#post_payroll'); + $api->post('/org/supplier')->to('api-organisation#post_supplier'); + $api->post('/org/employee')->to('api-organisation#post_employee'); my $api_v1 = $api->under('/v1'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Organisation.pm b/lib/Pear/LocalLoop/Controller/Api/Organisation.pm new file mode 100644 index 0000000..90edbc2 --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/Organisation.pm @@ -0,0 +1,120 @@ +package Pear::LocalLoop::Controller::Api::Organisation; +use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON; + +has error_messages => sub { + return { + sector => { + required => { message => 'No sector sent.', status => 400 }, + }, + }; +}; + +sub post_payroll { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + + return $c->api_validation_error if $validation->has_error; + + my $user_rs = $c->schema->resultset('User')->search({ + id => { "!=" => $user->id }, + }); + + $validation->required('entryperiod'); + $validation->required('employeeamount'); + $validation->required('localemployeeamount'); + $validation->required('grosspayroll'); + $validation->optional('payrollincometax'); + $validation->optional('payrollemployeeni'); + $validation->optional('payrollemployerni'); + $validation->optional('payrolltotalpension'); + $validation->optional('payrollotherbenefit'); + + return $c->api_validation_error if $validation->has_error; + + $c->schema->txn_do( sub { + $user->entity->organisation->update({ + entry_period => $validation->param('entryperiod'), + }); + }); + + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Submitted Payroll Info Successfully', + }); +} + +sub post_supplier { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + + return $c->api_validation_error if $validation->has_error; + + my $user_rs = $c->schema->resultset('User')->search({ + id => { "!=" => $user->id }, + }); + + $validation->required('entryperiod'); + $validation->optional('postcode')->postcode; + $validation->optional('supplierbusinessname'); + $validation->optional('monthlyspend'); + + return $c->api_validation_error if $validation->has_error; + + $c->schema->txn_do( sub { + $user->entity->organisation->update({ + entry_period => $validation->param('entryperiod'), + }); + }); + + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Submitted Payroll Info Successfully', + }); +} + +sub post_employee { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + + return $c->api_validation_error if $validation->has_error; + + my $user_rs = $c->schema->resultset('User')->search({ + id => { "!=" => $user->id }, + }); + + $validation->required('entryperiod'); + $validation->optional('employeeno'); + $validation->optional('employeeincometax'); + $validation->optional('employeegrosswage'); + $validation->optional('employeeni'); + $validation->optional('employeepension'); + $validation->optional('employeeotherbenefit'); + + return $c->api_validation_error if $validation->has_error; + + $c->schema->txn_do( sub { + $user->entity->organisation->update({ + entry_period => $validation->param('entryperiod'), + }); + }); + + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Submitted Payroll Info Successfully', + }); +} + +1;