diff --git a/lib/NGCP/Panel/Controller/API/HeaderRules.pm b/lib/NGCP/Panel/Controller/API/HeaderRules.pm index d0772e8621..9bc3546aba 100644 --- a/lib/NGCP/Panel/Controller/API/HeaderRules.pm +++ b/lib/NGCP/Panel/Controller/API/HeaderRules.pm @@ -45,7 +45,33 @@ sub create_item { my $item; my $schema = $c->model('DB'); try { + my $header_actions = delete $resource->{actions}; + my $header_conditions = delete $resource->{conditions}; $item = $schema->resultset('voip_header_rules')->create($resource); + if ($header_actions) { + foreach my $action (@$header_actions) { + $action->{rule_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $action, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ActionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleActions->check_resource($c, undef, undef, $action, undef, undef); + my $action_result = $schema->resultset('voip_header_rule_actions')->create($action); + } + } + if ($header_conditions) { + foreach my $condition (@$header_conditions) { + $condition->{rule_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $condition, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ConditionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleConditions->check_resource($c, undef, undef, $condition, undef, undef); + my $condition_result = $schema->resultset('voip_header_rule_conditions')->create($condition); + } + } NGCP::Panel::Utils::HeaderManipulations::invalidate_ruleset( c => $c, set_id => $item->ruleset->id ); diff --git a/lib/NGCP/Panel/Form/Header/RuleAPI.pm b/lib/NGCP/Panel/Form/Header/RuleAPI.pm index 228bf18827..1e98fa82b6 100644 --- a/lib/NGCP/Panel/Form/Header/RuleAPI.pm +++ b/lib/NGCP/Panel/Form/Header/RuleAPI.pm @@ -12,6 +12,24 @@ has_field 'set_id' => ( }, ); +has_field 'conditions' => ( + type => 'Compound', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The list of conditions in the rule.'], + }, +); + +has_field 'actions' => ( + type => 'Compound', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The list of actions in the rule.'], + }, +); + has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], diff --git a/lib/NGCP/Panel/Role/API/HeaderRules.pm b/lib/NGCP/Panel/Role/API/HeaderRules.pm index 1ef5186bb1..7ae3b6390a 100644 --- a/lib/NGCP/Panel/Role/API/HeaderRules.pm +++ b/lib/NGCP/Panel/Role/API/HeaderRules.pm @@ -79,7 +79,35 @@ sub check_resource { sub update_item_model { my ($self, $c, $item, $old_resource, $resource, $form) = @_; - $item = $self->SUPER::update_item_model($c, $item, $old_resource, $resource, $form); + my $header_actions = delete $resource->{actions}; + my $header_conditions = delete $resource->{conditions}; + $item->update($resource); + if ($header_actions) { + $item->actions->delete; + foreach my $action (@$header_actions) { + $action->{rule_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $action, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ActionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleActions->check_resource($c, undef, undef, $action, undef, undef); + my $action_result = $item->actions->create($action); + } + } + if ($header_conditions) { + $item->conditions->delete; + foreach my $condition (@$header_conditions) { + $condition->{rule_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $condition, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ConditionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleConditions->check_resource($c, undef, undef, $condition, undef, undef); + my $condition_result = $item->conditions->create($condition); + } + } NGCP::Panel::Utils::HeaderManipulations::invalidate_ruleset( c => $c, set_id => $item->ruleset->id @@ -88,5 +116,19 @@ sub update_item_model { return $item; } +sub resource_from_item { + my ($self, $c, $item, $form) = @_; + + my %resource = $item->get_inflated_columns; + + my @actions = map { {$_->get_inflated_columns} } $item->actions->all; + my @conditions = map { {$_->get_inflated_columns} } $item->conditions->all; + + $resource{actions} = \@actions; + $resource{conditions} = \@conditions; + + return \%resource; +} + 1; # vim: set tabstop=4 expandtab: