Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Northumberland] Fetch extra_details from Alloy #358

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions perllib/Open311/Endpoint/Integration/AlloyV2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ sub _get_inspection_updates {

$args{extras} = $assigned_to_user if $assigned_to_user;
}
if ( my $extra_details_code = $mapping->{extra_details} ) {
$args{extras}{detailed_information}
= $attributes->{$extra_details_code} // '';
}

push @updates, Open311::Endpoint::Service::Request::Update::mySociety->new( %args );
}
Expand Down
80 changes: 76 additions & 4 deletions perllib/Open311/Endpoint/Integration/UK/NorthumberlandAlloy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,87 @@ sub process_attributes {

Adds an update for the status attribute given by C<update_status_attribute_id>, using the mapping C<update_status_mapping>.

Adds an update for 'extra_details' field ('FMS Extra Details' on Alloy end).

Adds an update for the assigned user ('Assigned to' on Alloy end).

=cut

sub update_additional_attributes {
my ($self, $args) = @_;

return [{
attributeCode => $self->config->{update_status_attribute_id},
value => [ $self->config->{update_status_mapping}->{lc ($args->{status})} ]
}];
my $attr = [
{ attributeCode => $self->config->{update_status_attribute_id},
value => [
$self->config->{update_status_mapping}
->{ lc( $args->{status} ) }
],
},
{ attributeCode =>
$self->config->{inspection_attribute_mapping}{extra_details},
value => $args->{attributes}{extra_details},
},
];

if ( exists $args->{attributes}{assigned_to_user_email} ) {
my $email = $args->{attributes}{assigned_to_user_email};

if ($email) {
# TODO Handle failure

# Search for existing user
my $mapping = $self->config->{assigned_to_user_mapping};

my $res = $self->alloy->search(
dracos marked this conversation as resolved.
Show resolved Hide resolved
{ properties => {
dodiCode => $mapping->{design},
collectionCode => 'Live',
attributes => [
$mapping->{email_attribute},
],
},
children => [
{ type => "Equals",
children => [
{ type => 'Attribute',
properties => {
attributeCode =>
$mapping->{email_attribute}
},
},
{ type => 'String',
properties => {
value =>
[ $args->{attributes}{assigned_to_user_email} ]
},
}
],
},
],
},
);

# We don't update if user does not exist in Alloy
if (@$res) {
push @$attr, {
attributeCode =>
$self->config->{inspection_attribute_mapping}
{assigned_to_user},
value => [ $res->[0]{itemId} ],
};
}
} else {
# Unset user
push @$attr, {
attributeCode =>
$self->config->{inspection_attribute_mapping}
{assigned_to_user},
value => [],
};
}
}

return $attr;
}

=head2 get_assigned_to_users
Expand Down
8 changes: 3 additions & 5 deletions perllib/Open311/Endpoint/Role/mySociety.pm
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ sub POST_Service_Request_Update_input_schema {
delete $attributes->{required}{update_id};
}

# Allow attributes through for Oxfordshire XXX
if ($jurisdiction eq 'oxfordshire') {
for my $key (grep { /^attribute\[\w+\]$/ } keys %$args) {
$attributes->{optional}{$key} = '//str';
}
# Allow attributes through
for my $key (grep { /^attribute\[\w+\]$/ } keys %$args) {
$attributes->{optional}{$key} = '//str';
}

# Allow nsg_ref through for Bexley XXX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"attributeCode": "attributes_customerRequestEmail_63868f209e005f0396e519ca",
"value": "[email protected]"
},
{
"attributeCode": "attributes_customerRequestFMSExtraDetails_646e07533726d8036a7a4022",
"value": "Goodbye here"
},
{
"attributeCode": "attributes_customerRequestAssignedTo_653664b0557119eef53a97e1",
"value": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"attributeCode": "attributes_customerRequestEmail_63868f209e005f0396e519ca",
"value": "[email protected]"
},
{
"attributeCode": "attributes_customerRequestFMSExtraDetails_646e07533726d8036a7a4022",
"value": "Hello there"
},
{
"attributeCode": "attributes_customerRequestAssignedTo_653664b0557119eef53a97e1",
"value": [
Expand Down
93 changes: 82 additions & 11 deletions t/open311/endpoint/northumberland_alloy.t
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,29 @@ $integration->mock('api_call', sub {
$content = path(__FILE__)->sibling("json/alloyv2/northumberland/fixmystreet_users_query_response.json")->slurp;

# Only get the IDs that are provided in the body
my $id_type = $body->{aqs}{children}[0]{children}[0]{type};
my @body_ids
= @{
$body->{aqs}{children}[0]{children}[1]{properties}{value}
};

my @matching_items;
for my $item ( @{ decode_json($content)->{results} } ) {
for (@body_ids) {
push @matching_items, $item if $item->{itemId} == $_;
if ( $id_type eq 'ItemProperty' ) {
# Searching for multiple users
for my $item ( @{ decode_json($content)->{results} } ) {
for (@body_ids) {
push @matching_items, $item if $item->{itemId} eq $_;
}
}
} elsif ( $id_type eq 'Attribute' ) {
# Searching for a single user via email
for my $item ( @{ decode_json($content)->{results} } ) {
for ( @{ $item->{attributes} } ) {
push @matching_items, $item
if $_->{attributeCode} eq
'attributes_fixMyStreetUsersEmail_6532518cac7139477485ec38'
&& $_->{value} eq $body_ids[0];
}
}
}

Expand Down Expand Up @@ -403,8 +417,9 @@ subtest "check fetch updates" => sub {
update_id => '100001',
updated_datetime => '2023-11-13T11:05:00Z',
extras => {
assigned_user_name => 'FMS User 123',
assigned_user_email => '[email protected]',
assigned_user_name => 'FMS User 123',
assigned_user_email => '[email protected]',
detailed_information => 'Hello there',
},
},
{ description => '',
Expand All @@ -416,6 +431,7 @@ subtest "check fetch updates" => sub {
extras => {
assigned_user_name => 'FMS User 234',
assigned_user_email => '[email protected]',
detailed_information => '',
},
},
{ description => '',
Expand All @@ -427,12 +443,19 @@ subtest "check fetch updates" => sub {
extras => {
assigned_user_name => 'FMS User 345',
assigned_user_email => '[email protected]',
detailed_information => '',
},
},
], 'correct json returned';
};

subtest "update status on problem" => sub {
subtest "send updates for problem" => sub {
my $extra_details = <<HERE;
Red and
yellow and
pink
HERE

my $res = $endpoint->run_test_request(
POST => '/servicerequestupdates.json',
jurisdiction_id => 'dummy',
Expand All @@ -443,6 +466,8 @@ subtest "update status on problem" => sub {
service_request_id => '642062376be3a0036bbbb64b',
update_id => '1',
updated_datetime => '2023-05-15T14:55:55+00:00',
'attribute[extra_details]' => $extra_details,
'attribute[assigned_to_user_email]' => '[email protected]',
);
ok $res->is_success, 'valid request'
or diag $res->content;
Expand All @@ -454,15 +479,61 @@ subtest "update status on problem" => sub {
my $expected_status_attribute_code = 'attributes_customerRequestMainFMSStatus_63fcb297c9ec9c036ec35dfb';
my $expected_status_attribute_value = '63fcb00c753aed036a5e43a2';

my $status_match_found = 0;
my $expected_extra_details_attribute_code = 'attributes_customerRequestFMSExtraDetails_646e07533726d8036a7a4022';
my $expected_extra_details_attribute_value = $extra_details;

my $expected_assigned_user_attribute_code =
'attributes_customerRequestAssignedTo_653664b0557119eef53a97e1';
my $expected_assigned_user_attribute_value = '123';

my $check_count = 0;
foreach (@{ $attributes }) {
if ($_->{attributeCode} eq $expected_status_attribute_code) {
ok ref($_->{value}) eq 'ARRAY' && $_->{value}[0] eq $expected_status_attribute_value, "value sent in status attribute update is correct";
$status_match_found = 1;
last;
is_deeply $_->{value}, [$expected_status_attribute_value],
"value sent in status attribute update is correct";
$check_count++;
}
if ($_->{attributeCode} eq $expected_extra_details_attribute_code) {
ok $_->{value} eq $expected_extra_details_attribute_value, "value sent in extra_details attribute update is correct";
$check_count++;
}
if ( $_->{attributeCode} eq $expected_assigned_user_attribute_code ) {
is_deeply $_->{value}, [$expected_assigned_user_attribute_value],
"value sent in assigned_to_user attribute update is correct";
$check_count++;
}
}
is $check_count, 3, 'correct number of attributes tested';

note 'unset assigned user';

my $res = $endpoint->run_test_request(
POST => '/servicerequestupdates.json',
jurisdiction_id => 'dummy',
api_key => 'test',
service_code => 'Damaged_/_Missing_/_Facing_Wrong_Way',
description => 'update',
status => 'FIXED',
service_request_id => '642062376be3a0036bbbb64b',
update_id => '1',
updated_datetime => '2023-05-15T14:55:55+00:00',
'attribute[extra_details]' => $extra_details,
'attribute[assigned_to_user_email]' => '',
);
ok $res->is_success, 'valid request'
or diag $res->content;

$sent = pop @sent;
$attributes = $sent->{attributes};
$check_count = 0;
for (@$attributes) {
if ( $_->{attributeCode} eq $expected_assigned_user_attribute_code ) {
is_deeply $_->{value}, [],
"empty arrayref sent for assigned_to_user attribute";
$check_count++;
}
}
ok $status_match_found, "status attribute update was sent";
is $check_count, 1, 'correct number of attributes tested';
};

done_testing;
1 change: 1 addition & 0 deletions t/open311/endpoint/northumberland_alloy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"updates": "attributes_customerRequestFMSUpdates_6387cc9805cb250393e00e2f",
"status": "attributes_customerRequestMainFMSStatus_63fcb297c9ec9c036ec35dfb",
"inspector_comments": "attributes_customerRequestInspectorsComments_638629fcfb3d97038c4e5d5a",
"extra_details": "attributes_customerRequestFMSExtraDetails_646e07533726d8036a7a4022",
"assigned_to_user": "attributes_customerRequestAssignedTo_653664b0557119eef53a97e1",
},

Expand Down
Loading