11use crate :: bulk:: Bulk ;
22use crate :: settings:: DinoParkSettings ;
3+ use crate :: updater:: send_profile;
34use crate :: updater:: UpdaterClient ;
45use actix_cors:: Cors ;
5- use actix_web:: client:: Client ;
66use actix_web:: dev:: HttpServiceFactory ;
77use actix_web:: error;
88use actix_web:: error:: Error ;
@@ -13,61 +13,37 @@ use actix_web::web::Json;
1313use actix_web:: HttpResponse ;
1414use actix_web:: Result ;
1515use cis_profile:: schema:: Profile ;
16- use futures:: Future ;
16+ use futures:: future :: TryFutureExt ;
1717use serde_json:: json;
1818use serde_json:: Value ;
1919
20- pub fn internal_update (
21- dp : & DinoParkSettings ,
22- profile : & Profile ,
23- ) -> impl Future < Item = Value , Error = Error > {
24- let id = profile
25- . user_id
26- . value
27- . clone ( )
28- . unwrap_or_else ( || String :: from ( "unknown" ) ) ;
29- info ! ( "internally updating profile for: {}" , & id) ;
30- let id_c = id. clone ( ) ;
31- let orgchart_update = Client :: default ( )
32- . post ( & dp. orgchart_update_endpoint )
33- . send_json ( profile)
34- . map ( move |_| info ! ( "internally updated orgchart for: {}" , id) ) ;
35-
36- orgchart_update
37- . join (
38- Client :: default ( )
39- . post ( & dp. search_update_endpoint )
40- . send_json ( profile)
41- . map ( move |_| info ! ( "internally updated search for: {}" , id_c) ) ,
42- )
43- . map ( |_| json ! ( { } ) )
44- . map_err ( Into :: into)
20+ pub async fn internal_update ( dp : & DinoParkSettings , profile : Profile ) -> Result < Value , Error > {
21+ send_profile ( dp, profile) . map_err ( Into :: into) . await
4522}
4623
47- fn internal_update_event (
24+ async fn internal_update_event (
4825 dino_park_settings : Data < DinoParkSettings > ,
4926 profile : Json < Profile > ,
50- ) -> impl Future < Item = HttpResponse , Error = Error > {
27+ ) -> Result < HttpResponse , Error > {
5128 let id = profile
5229 . user_id
5330 . value
5431 . clone ( )
5532 . unwrap_or_else ( || String :: from ( "unknown" ) ) ;
5633 info ! ( "internally updating profile for: {}" , & id) ;
5734 let id_c = id. clone ( ) ;
58- internal_update ( & dino_park_settings, & profile)
59- . map ( move |res| {
60- info ! ( "internally updated profile for {}" , id) ;
61- HttpResponse :: Ok ( ) . json ( res)
62- } )
63- . map_err ( move |e| {
35+ let res = internal_update ( & dino_park_settings, profile. into_inner ( ) ) . await ;
36+ info ! ( "internally updated profile for {}" , id) ;
37+ match res {
38+ Ok ( j) => Ok ( HttpResponse :: Ok ( ) . json ( j) ) ,
39+ Err ( e) => {
6440 error ! ( "failed to internally update profile for {}: {}" , id_c, e) ;
65- error:: ErrorInternalServerError ( e)
66- } )
67- . map_err ( Into :: into )
41+ Err ( error:: ErrorInternalServerError ( e) )
42+ }
43+ }
6844}
6945
70- fn bulk_update < U : UpdaterClient + Clone + ' static > (
46+ async fn bulk_update < U : UpdaterClient + Clone + ' static > (
7147 updater : Data < U > ,
7248 bulk : Json < Bulk > ,
7349) -> Result < HttpResponse > {
@@ -85,11 +61,12 @@ pub fn internal_app<U: UpdaterClient + Clone + Send + 'static>(
8561 . allowed_methods ( vec ! [ "POST" ] )
8662 . allowed_headers ( vec ! [ http:: header:: AUTHORIZATION , http:: header:: ACCEPT ] )
8763 . allowed_header ( http:: header:: CONTENT_TYPE )
88- . max_age ( 3600 ) ,
64+ . max_age ( 3600 )
65+ . finish ( ) ,
8966 )
9067 . data ( updater)
9168 . data ( dino_park_settings)
92- . data ( web:: JsonConfig :: default ( ) . limit ( 1_048_576 ) )
69+ . app_data ( web:: JsonConfig :: default ( ) . limit ( 1_048_576 ) )
9370 . service ( web:: resource ( "/bulk" ) . route ( web:: post ( ) . to ( bulk_update :: < U > ) ) )
94- . service ( web:: resource ( "/update" ) . route ( web:: post ( ) . to_async ( internal_update_event) ) )
71+ . service ( web:: resource ( "/update" ) . route ( web:: post ( ) . to ( internal_update_event) ) )
9572}
0 commit comments