Line # Revision Author
1 523 ahitrov <% $json %>
2 <%once>
3
4 use JSON::XS;
5
6 </%once>
7 <%args>
8
9 $id => undef
10 $phone => undef
11
12 </%args>
13 <%init>
14
15 my %result;
16 if ( $phone ) {
17 if ( $keeper->{users}->_phone_format( $phone ) ) {
18 my $phone_r = $keeper->{users}->_phone_reduction( $phone );
19 if ( $id && $id =~ /^\d+$/) {
20 my $object = $keeper->{users}->get_profile( id => $id );
21 if ( ref $object ) {
22 my ($cred) = $keeper->get_documents(
23 name => $phone_r,
24 class => 'users::Phone',
25 limit => 1,
26 );
27 if ( ref $cred && $cred->uid != $object->id ) {
28 $result{error} = 'Phone already registered for another user';
29 }
30 } else {
31 $result{error} = 'User not found';
32 }
33 } else {
34 $result{error} = 'User ID is not valid or not defined';
35 }
36 } else {
37 $result{error} = 'Phone is not valid';
38 }
39 } else {
40 $result{nothing} = 1;
41 }
42 $result{ok} = 1 unless exists $result{error} || exists $result{nothing};
43
44 my $json = encode_json \%result;
45
46 </%init>