From 00f0614ef9c0846f59e58c305c76c78dce06573e Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Fri, 8 Jan 2021 15:20:21 +0100 Subject: [PATCH] TT#107051 fix PATCH /billingprofiles/x - peaktimes Change-Id: I3d074976f15618d84cfd8ee4694a0fd4250fe091 --- .../Panel/Controller/API/BillingProfiles.pm | 7 ++-- .../Controller/API/BillingProfilesItem.pm | 20 +++++---- lib/NGCP/Panel/Role/API/BillingProfiles.pm | 42 +++++++++++-------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/BillingProfiles.pm b/lib/NGCP/Panel/Controller/API/BillingProfiles.pm index 21bc52eb03..d70c4c77ba 100644 --- a/lib/NGCP/Panel/Controller/API/BillingProfiles.pm +++ b/lib/NGCP/Panel/Controller/API/BillingProfiles.pm @@ -75,7 +75,7 @@ sub GET :Allow { my (@embedded, @links); my $form = $self->get_form($c); for my $profile (@$profiles_rows) { - push @embedded, $self->hal_from_profile($c, $profile, $form); + push @embedded, $self->hal_from_item($c, $profile, $self->resource_from_item($c, $profile, $form), $form); push @links, Data::HAL::Link->new( relation => 'ngcp:'.$self->resource_name, href => sprintf('/%s%d', $c->request->path, $profile->id), @@ -164,7 +164,7 @@ sub POST :Allow { my $billing_profile; try { - $billing_profile= $schema->resultset('billing_profiles')->create($resource); + $billing_profile = $schema->resultset('billing_profiles')->create($resource); foreach my $weekday_peaktime (@$weekday_peaktimes_to_create) { $billing_profile->billing_peaktime_weekdays->create($weekday_peaktime); } @@ -181,7 +181,8 @@ sub POST :Allow { my $self = shift; my ($c) = @_; my $_billing_profile = $self->profile_by_id($c, $billing_profile->id); - return $self->hal_from_profile($c, $_billing_profile,$form); }); + return $self->hal_from_item($c, $_billing_profile, + $self->resource_from_item($c, $_billing_profile, $form), $form); }); $guard->commit; diff --git a/lib/NGCP/Panel/Controller/API/BillingProfilesItem.pm b/lib/NGCP/Panel/Controller/API/BillingProfilesItem.pm index c0faddfbcd..5b585db65e 100644 --- a/lib/NGCP/Panel/Controller/API/BillingProfilesItem.pm +++ b/lib/NGCP/Panel/Controller/API/BillingProfilesItem.pm @@ -5,6 +5,7 @@ use Sipwise::Base; use HTTP::Headers qw(); use HTTP::Status qw(:constants); +use Clone qw/clone/; use NGCP::Panel::Utils::ValidateJSON qw(); use NGCP::Panel::Utils::Billing qw /check_profile_update_item/; @@ -49,7 +50,8 @@ sub GET :Allow { my $profile = $self->profile_by_id($c, $id); last unless $self->resource_exists($c, billingprofile => $profile); - my $hal = $self->hal_from_profile($c, $profile); + my $form = $self->get_form($c); + my $hal = $self->hal_from_item($c, $profile, $self->resource_from_item($c, $profile, $form)); # TODO: we don't need reseller stuff here! my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( @@ -85,17 +87,19 @@ sub PATCH :Allow { last; } + my $form = $self->get_form($c); my $profile = $self->profile_by_id($c, $id); last unless $self->resource_exists($c, billingprofile => $profile); - my $old_resource = { $profile->get_inflated_columns }; + my $old_resource = $self->resource_from_item($c, $profile, $form); + $old_resource = clone($old_resource); my $resource = $self->apply_patch($c, $old_resource, $json); last unless $resource; - my $form = $self->get_form($c); $profile = $self->update_profile($c, $profile, $old_resource, $resource, $form); last unless $profile; - my $hal = $self->hal_from_profile($c, $profile, $form); + $resource = $self->resource_from_item($c, $profile, $form); + my $hal = $self->hal_from_item($c, $profile, $resource, $form); last unless $self->add_update_journal_item_hal($c,$hal); $guard->commit; @@ -126,13 +130,15 @@ sub PUT :Allow { media_type => 'application/json', ); last unless $resource; - my $old_resource = { $profile->get_inflated_columns }; - + my $form = $self->get_form($c); + my $old_resource = $self->resource_from_item($c, $profile, $form); + $profile = $self->update_profile($c, $profile, $old_resource, $resource, $form); last unless $profile; - my $hal = $self->hal_from_profile($c, $profile, $form); + $resource = $self->resource_from_item($c, $profile, $form); + my $hal = $self->hal_from_item($c, $profile, $resource, $form); last unless $self->add_update_journal_item_hal($c,$hal); $guard->commit; diff --git a/lib/NGCP/Panel/Role/API/BillingProfiles.pm b/lib/NGCP/Panel/Role/API/BillingProfiles.pm index 167649ad0f..726c5a061a 100644 --- a/lib/NGCP/Panel/Role/API/BillingProfiles.pm +++ b/lib/NGCP/Panel/Role/API/BillingProfiles.pm @@ -5,7 +5,6 @@ use Sipwise::Base; use parent 'NGCP::Panel::Role::API'; - use boolean qw(true); use Data::HAL qw(); use Data::HAL::Link qw(); @@ -44,7 +43,7 @@ sub get_form { return NGCP::Panel::Form::get("NGCP::Panel::Form::BillingProfile::PeaktimeAPI", $c); } -sub hal_from_profile { +sub resource_from_item { my ($self, $c, $profile, $form) = @_; my %resource = $profile->get_inflated_columns; @@ -56,6 +55,24 @@ sub hal_from_profile { # if the structure is returned for one single item # (make it a method flag) + $form //= $self->get_form($c); + return unless $self->validate_form( + c => $c, + form => $form, + resource => \%resource, + run => 0, + ); + + $resource{id} = int($profile->id); + $resource{peaktime_weekdays} = $weekday_peaktimes; + $resource{peaktime_special} = $special_peaktimes; + + return \%resource; +} + +sub hal_from_item { + my ($self, $c, $item, $resource, $form) = @_; + my $hal = Data::HAL->new( links => [ Data::HAL::Link->new( @@ -66,26 +83,15 @@ sub hal_from_profile { ), Data::HAL::Link->new(relation => 'collection', href => sprintf('/api/%s/', $self->resource_name)), Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $profile->id)), - Data::HAL::Link->new(relation => 'ngcp:billingfees', href => sprintf("/api/billingfees/?billing_profile_id=%d", $profile->id ) ), - Data::HAL::Link->new(relation => 'ngcp:billingzones', href => sprintf("/api/billingzones/?billing_profile_id=%d", $profile->id )), - $self->get_journal_relation_link($c, $profile->id), + Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), + Data::HAL::Link->new(relation => 'ngcp:billingfees', href => sprintf("/api/billingfees/?billing_profile_id=%d", $item->id ) ), + Data::HAL::Link->new(relation => 'ngcp:billingzones', href => sprintf("/api/billingzones/?billing_profile_id=%d", $item->id )), + $self->get_journal_relation_link($c, $item->id), ], relation => 'ngcp:'.$self->resource_name, ); - $form //= $self->get_form($c); - return unless $self->validate_form( - c => $c, - form => $form, - resource => \%resource, - run => 0, - ); - - $resource{id} = int($profile->id); - $resource{peaktime_weekdays} = $weekday_peaktimes; - $resource{peaktime_special} = $special_peaktimes; - $hal->resource({%resource}); + $hal->resource($resource); return $hal; }