From 616181def37e668a48aaf4367cd30a236c3a62a6 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Sep 2017 17:13:18 +0100 Subject: [PATCH] Payroll sumission API improved and test added --- doc/Fixtures/Users.md | 2 +- .../LocalLoop/Controller/Api/Organisation.pm | 45 ++-- .../LocalLoop/Schema/Result/Organisation.pm | 7 + .../Schema/Result/OrganisationPayroll.pm | 83 +++++++ t/api/organisation.t | 221 ++++++++++++++++++ 5 files changed, 342 insertions(+), 16 deletions(-) create mode 100644 lib/Pear/LocalLoop/Schema/Result/OrganisationPayroll.pm create mode 100644 t/api/organisation.t diff --git a/doc/Fixtures/Users.md b/doc/Fixtures/Users.md index 15db8d3..9545fe5 100644 --- a/doc/Fixtures/Users.md +++ b/doc/Fixtures/Users.md @@ -17,7 +17,7 @@ * email: test4@example.com * password: abc123 * Test Org - * email: test5@example.com + * email: org@example.com * password: abc123 * Test Admin * email: admin@example.com diff --git a/lib/Pear/LocalLoop/Controller/Api/Organisation.pm b/lib/Pear/LocalLoop/Controller/Api/Organisation.pm index ce04ce4..effd9ee 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Organisation.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Organisation.pm @@ -17,47 +17,47 @@ has error_messages => sub { required => { message => 'No gross payroll sent.', status => 400 }, }, payroll_income_tax => { - required => { message => 'no payroll income tax sent.', status => 400 }, + required => { message => 'No total income tax sent.', status => 400 }, }, payroll_employee_ni => { - required => { message => 'no payroll employee ni sent.', status => 400 }, + required => { message => 'No total employee NI sent.', status => 400 }, }, payroll_employer_ni => { - required => { message => 'no payroll employer ni sent.', status => 400 }, + required => { message => 'No total employer NI sent.', status => 400 }, }, payroll_total_pension => { - required => { message => 'no payroll total pension sent.', status => 400 }, + required => { message => 'No total total pension sent.', status => 400 }, }, payroll_other_benefit => { - required => { message => 'no payroll other benefit sent.', status => 400 }, + required => { message => 'No total other benefits total sent.', status => 400 }, }, supplier_business_name => { - required => { message => 'no supplier business name sent.', status => 400 }, + required => { message => 'No supplier business name sent.', status => 400 }, }, postcode => { - required => { message => 'no postcode sent.', status => 400 }, + required => { message => 'No postcode sent.', status => 400 }, postcode => { message => 'postcode must be valid', status => 400 }, }, monthly_spend => { - required => { message => 'no monthly spend sent.', status => 400 }, + required => { message => 'No monthly spend sent.', status => 400 }, }, employee_no => { - required => { message => 'no employee no sent.', status => 400 }, + required => { message => 'No employee no sent.', status => 400 }, }, employee_income_tax => { - required => { message => 'no employee income tax sent.', status => 400 }, + required => { message => 'No employee income tax sent.', status => 400 }, }, employee_gross_wage => { - required => { message => 'no employee gross wage sent.', status => 400 }, + required => { message => 'No employee gross wage sent.', status => 400 }, }, employee_ni => { - required => { message => 'no employee ni sent.', status => 400 }, + required => { message => 'No employee ni sent.', status => 400 }, }, employee_pension => { - required => { message => 'no employee pension sent.', status => 400 }, + required => { message => 'No employee pension sent.', status => 400 }, }, employee_other_benefit => { - required => { message => 'no employee other benefits sent.', status => 400 }, + required => { message => 'No employee other benefits sent.', status => 400 }, }, }; }; @@ -86,11 +86,26 @@ sub post_payroll { $validation->required('payroll_total_pension'); $validation->required('payroll_other_benefit'); + my $gross_payroll = $validation->param('gross_payroll'); + my $payroll_income_tax = $validation->param('payroll_income_tax'); + my $payroll_employee_ni = $validation->param('payroll_employee_ni'); + my $payroll_employer_ni = $validation->param('payroll_employer_ni'); + my $payroll_total_pension = $validation->param('payroll_total_pension'); + my $payroll_other_benefit = $validation->param('payroll_other_benefit'); + return $c->api_validation_error if $validation->has_error; $c->schema->txn_do( sub { - $user->entity->organisation->update({ + $user->entity->organisation->payroll->create({ entry_period => $validation->param('entry_period'), + employee_amount => $validation->param('employee_amount'), + local_employee_amount => $validation->param('local_employee_amount'), + gross_payroll => $gross_payroll * 100000, + payroll_income_tax => $payroll_income_tax * 100000, + payroll_employee_ni => $payroll_employee_ni * 100000, + payroll_employer_ni => $payroll_employer_ni * 100000, + payroll_total_pension => $payroll_total_pension * 100000, + payroll_other_benefit => $payroll_other_benefit * 100000, }); }); diff --git a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm index 7f6c4c8..9512c18 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm @@ -68,6 +68,13 @@ __PACKAGE__->belongs_to( "entity_id", ); +__PACKAGE__->has_many( + "payroll", + "Pear::LocalLoop::Schema::Result::OrganisationPayroll", + { "foreign.org_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + __PACKAGE__->filter_column( pending => { filter_to_storage => 'to_bool', }); diff --git a/lib/Pear/LocalLoop/Schema/Result/OrganisationPayroll.pm b/lib/Pear/LocalLoop/Schema/Result/OrganisationPayroll.pm new file mode 100644 index 0000000..c60d458 --- /dev/null +++ b/lib/Pear/LocalLoop/Schema/Result/OrganisationPayroll.pm @@ -0,0 +1,83 @@ +package Pear::LocalLoop::Schema::Result::OrganisationPayroll; + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->load_components(qw/ + InflateColumn::DateTime + TimeStamp +/); + +__PACKAGE__->table("organisation_payroll"); + +__PACKAGE__->add_columns( + "id" => { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + }, + "org_id" => { + data_type => 'integer', + is_nullable => 0, + is_foreign_key => 1, + }, + "submitted_at" => { + data_type => "datetime", + is_nullable => 0, + set_on_create => 1, + }, + "entry_period" => { + data_type => "datetime", + is_nullable => 0, + }, + "employee_amount" => { + data_type => "integer", + is_nullable => 0, + }, + "local_employee_amount" => { + data_type => "integer", + is_nullable => 0, + }, + "gross_payroll" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, + "payroll_income_tax" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, + "payroll_employee_ni" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, + "payroll_employer_ni" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, + "payroll_total_pension" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, + "payroll_other_benefit" => { + data_type => "numeric", + size => [ 100, 0 ], + is_nullable => 0, + }, +); + +__PACKAGE__->set_primary_key("id"); + +__PACKAGE__->belongs_to( + "organisation", + "Pear::LocalLoop::Schema::Result::Organisation", + "org_id", +); + +1; diff --git a/t/api/organisation.t b/t/api/organisation.t new file mode 100644 index 0000000..7f05b87 --- /dev/null +++ b/t/api/organisation.t @@ -0,0 +1,221 @@ +use Mojo::Base -strict; + +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $session_key = $framework->login({ + email => 'org@example.com', + password => 'abc123', +}); + +## Payroll Data Submission + +#No JSON sent +$t->post_ok('/api/org/payroll') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/org/payroll' => json => {}) + ->json_is('/success', Mojo::JSON->false); + +# no session key +$t->post_ok('/api/org/payroll' => json => { + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(401) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid Session/); + +# No entry_period +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No entry period/); + +# No employee_amount +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No employee amount/); + +# No local_employee_amount +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/local employee amount/); + +# No gross_payroll +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No gross payroll/); + +# No payroll_income_tax +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No total income tax/); + +# No payroll_employee_ni +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No total employee NI/); + +# No payroll_employer_ni +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/total employer NI/); + +# No payroll_total_pension +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_other_benefit => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No total total pension/); + +# No payroll_other_benefit +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + }) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/No total other benefits total/); + +# Valid payroll submission +$t->post_ok('/api/org/payroll' => json => { + session_key => $session_key, + entry_period => '2017-12', + employee_amount => '10', + local_employee_amount => '10', + gross_payroll => '10', + payroll_income_tax => '10', + payroll_employee_ni => '10', + payroll_employer_ni => '10', + payroll_total_pension => '10', + payroll_other_benefit => '10', + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +## Supplier Form submission + +#TODO make the test! + +## Employee Form submission + +#TODO make the test! + +$framework->logout( $session_key ); + +done_testing;