Skip to content

Commit

Permalink
[Northumberland] Send updates to assigned user to Alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
nephila-nacrea authored and dracos committed Nov 26, 2024
1 parent 32cff14 commit 305a485
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 25 deletions.
50 changes: 33 additions & 17 deletions perllib/Open311/Endpoint/Integration/AlloyV2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,39 @@ sub _find_contact {
return undef;
}

my $body = {
my $body = $self->find_item_body(
dodi_code => $self->config->{contact}->{code},
attribute_code => $attribute_code,
search_term => $search_term,
);

my $results = $self->alloy->search($body);

return undef unless @$results;
my $contact = $results->[0];

# Sanity check that the user we're returning actually has the correct email
# or phone, just in case Alloy returns something
my $a = $self->alloy->attributes_to_hash( $contact );
return undef unless $a->{$attribute_code} && $a->{$attribute_code} eq $search_term;

return $contact;
}

=head2 find_item_body
Builds query body for looking up a specific item in Alloy.
=cut

sub find_item_body {
my ( $self, %params ) = @_;

return {
properties => {
dodiCode => $self->config->{contact}->{code},
dodiCode => $params{dodi_code},
collectionCode => "Live",
attributes => [ $attribute_code ],
attributes => [ $params{attribute_code} ],
},
children => [
{
Expand All @@ -377,31 +405,19 @@ sub _find_contact {
{
type => "Attribute",
properties => {
attributeCode => $attribute_code,
attributeCode => $params{attribute_code},
},
},
{
type => "String",
properties => {
value => [ $search_term ]
value => [ $params{search_term} ]
}
}
]
}
]
};

my $results = $self->alloy->search($body);

return undef unless @$results;
my $contact = $results->[0];

# Sanity check that the user we're returning actually has the correct email
# or phone, just in case Alloy returns something
my $a = $self->alloy->attributes_to_hash( $contact );
return undef unless $a->{$attribute_code} && $a->{$attribute_code} eq $search_term;

return $contact;
}

=head2 _create_contact
Expand Down
44 changes: 42 additions & 2 deletions perllib/Open311/Endpoint/Integration/UK/NorthumberlandAlloy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ Adds an update for the status attribute given by C<update_status_attribute_id>,
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 [
my $attr = [
{ attributeCode => $self->config->{update_status_attribute_id},
value => [
$self->config->{update_status_mapping}
Expand All @@ -104,9 +106,47 @@ sub update_additional_attributes {
},
{ attributeCode =>
$self->config->{inspection_attribute_mapping}{extra_details},
value => $args->{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 $body = $self->SUPER::find_item_body(
dodi_code => $mapping->{design},
attribute_code => $mapping->{email_attribute},
search_term => $args->{attributes}{assigned_to_user_email},
);

my $res = $self->alloy->search($body);

# 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
1 change: 0 additions & 1 deletion perllib/Open311/Endpoint/Role/mySociety.pm
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ sub POST_Service_Request_Update_input_schema {
public_anonymity_required => Open311::Endpoint::Schema->enum('//str', 'TRUE', 'FALSE'),
email_alerts_requested => Open311::Endpoint::Schema->enum('//str', 'TRUE', 'FALSE'),
service_code => $self->get_identifier_type('service_code'),
extra_details => '//str',
}
};

Expand Down
69 changes: 64 additions & 5 deletions t/open311/endpoint/northumberland_alloy.t
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,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 @@ -432,10 +446,11 @@ HERE
service_code => 'Damaged_/_Missing_/_Facing_Wrong_Way',
description => 'update',
status => 'FIXED',
extra_details => $extra_details,
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 @@ -450,14 +465,58 @@ HERE
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";
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';

$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++;
}
}
is $check_count, 1, 'correct number of attributes tested';
};

done_testing;

0 comments on commit 305a485

Please sign in to comment.