11import express , { Request , Response } from "express" ;
2- import { DirectoryService } from "../services" ;
2+ import { UnifiedDirectoryService } from "../services" ;
33import { db as knex } from "../data/db-client" ;
4- import e from "express" ;
54
65export const directoryRouter = express . Router ( ) ;
76
8- const directoryService = new DirectoryService ( ) ;
7+ const unifiedDirectory = new UnifiedDirectoryService ( ) ;
98
10- directoryRouter . post ( "/search-directory" , async ( req : Request , res : Response ) => {
11- let { terms } = req . body ;
9+ directoryRouter . post (
10+ "/search-directory" ,
11+ async ( req : Request , res : Response ) => {
12+ let { terms } = req . body ;
1213
13- await directoryService . connect ( ) ;
14- let results = await directoryService . search ( terms ) ;
14+ const results = await unifiedDirectory . search ( terms ) ;
1515
16- return res . json ( { data : [ ...results ] } ) ;
17- } ) ;
16+ return res . json ( { data : results } ) ;
17+ }
18+ ) ;
1819
19- directoryRouter . post ( "/search-action-directory" , async ( req : Request , res : Response ) => {
20- let { terms } = req . body ;
21- terms = terms . toLowerCase ( ) ;
20+ directoryRouter . post (
21+ "/search-action-directory" ,
22+ async ( req : Request , res : Response ) => {
23+ let { terms } = req . body ;
24+ terms = terms . toLowerCase ( ) ;
2225
23- await directoryService . connect ( ) ;
24- let data = await directoryService . search ( terms ) ;
26+ const results = await unifiedDirectory . search ( terms ) ;
27+ let data = results . slice ( ) ;
28+ let yesnetResults : any [ ] = [ ] ;
2529
26- const parts = terms . split ( " " ) ;
30+ const parts = terms . split ( " " ) ;
2731
28- let allUsersQuery = knex ( "users" ) ;
32+ let allUsersQuery = knex ( "users" ) ;
2933
30- for ( const part of parts ) {
31- allUsersQuery . whereRaw (
32- `(LOWER("email") LIKE '${ part } %' OR LOWER("first_name") LIKE '${ part } %' OR LOWER("last_name") LIKE '${ part } %')`
33- ) ;
34- }
34+ for ( const part of parts ) {
35+ allUsersQuery . whereRaw (
36+ `(LOWER("email") LIKE '${ part } %' OR LOWER("first_name") LIKE '${ part } %' OR LOWER("last_name") LIKE '${ part } %')`
37+ ) ;
38+ }
3539
36- let allUsers = await allUsersQuery ;
37- data . map ( ( d : any ) => ( d . user_id = allUsers . find ( ( u ) => u . email == d . email ) ?. id ) ) ;
38-
39- const foundEmails = data . map ( ( d : any ) => d . email ) ;
40- allUsers = allUsers . filter ( ( u ) => u . is_active == true ) ;
41- allUsers = allUsers . filter ( ( u ) => ! foundEmails . includes ( u . email ) ) ;
42-
43- for ( const user of allUsers ) {
44- data . push ( {
45- display_name : `${ user . first_name } ${ user . last_name } ` ,
46- first_name : user . first_name ,
47- last_name : user . last_name ,
48- ynet_id : user . ynet_id ,
49- email : user . email ,
50- long_name : `${ user . first_name } ${ user . last_name } (${ user . email } ) ${ user . department } : ${ user . title } ` ,
51- title : user . title ,
52- department : user . department ,
53- officeLocation : user . officeLocation ,
54- userPrincipalName : user . userPrincipalName ,
55- user_id : user . id ,
56- } ) ;
57- }
40+ let allUsers = await allUsersQuery ;
41+ data . map (
42+ ( d : any ) => ( d . user_id = allUsers . find ( ( u ) => u . email == d . email ) ?. id )
43+ ) ;
44+ yesnetResults . map (
45+ ( d : any ) => ( d . user_id = allUsers . find ( ( u ) => u . email == d . email ) ?. id )
46+ ) ;
5847
59- return res . json ( { data } ) ;
60- } ) ;
48+ const combinedData = [ ...data , ...yesnetResults ] ;
49+
50+ const foundEmails = combinedData . map ( ( d : any ) => d . email ) ;
51+ allUsers = allUsers . filter ( ( u ) => u . is_active == true ) ;
52+ allUsers = allUsers . filter ( ( u ) => ! foundEmails . includes ( u . email ) ) ;
53+
54+ for ( const user of allUsers ) {
55+ combinedData . push ( {
56+ display_name : `${ user . first_name } ${ user . last_name } ` ,
57+ first_name : user . first_name ,
58+ last_name : user . last_name ,
59+ ynet_id : user . ynet_id ,
60+ email : user . email ,
61+ long_name : `${ user . first_name } ${ user . last_name } (${ user . email } ) ${ user . department } : ${ user . title } ` ,
62+ title : user . title ,
63+ department : user . department ,
64+ officeLocation : user . officeLocation ,
65+ userPrincipalName : user . userPrincipalName ,
66+ user_id : user . id ,
67+ } ) ;
68+ }
6169
62- directoryRouter . post ( "/search-action-directory-email" , async ( req : Request , res : Response ) => {
63- let { terms } = req . body ;
70+ return res . json ( { data : combinedData } ) ;
71+ }
72+ ) ;
73+
74+ directoryRouter . post (
75+ "/search-action-directory-email" ,
76+ async ( req : Request , res : Response ) => {
77+ let { terms } = req . body ;
78+
79+ const allUsers = await knex ( "users" ) . where ( { email : terms } ) ;
80+
81+ if ( allUsers . length == 1 ) {
82+ const nonDirectoryUser = allUsers [ 0 ] ;
83+
84+ return res . json ( {
85+ data : [
86+ {
87+ display_name : `${ nonDirectoryUser . first_name } ${ nonDirectoryUser . last_name } ` ,
88+ first_name : nonDirectoryUser . first_name ,
89+ last_name : nonDirectoryUser . last_name ,
90+ ynet_id : nonDirectoryUser . ynet_id ,
91+ email : nonDirectoryUser . email ,
92+ long_name : `${ nonDirectoryUser . first_name } ${ nonDirectoryUser . last_name } (${ nonDirectoryUser . email } ) ${ nonDirectoryUser . department } : ${ nonDirectoryUser . title } ` ,
93+ title : nonDirectoryUser . title ,
94+ department : nonDirectoryUser . department ,
95+ officeLocation : nonDirectoryUser . officeLocation ,
96+ userPrincipalName : nonDirectoryUser . userPrincipalName ,
97+ user_id : nonDirectoryUser . id ,
98+ } ,
99+ ] ,
100+ } ) ;
101+ }
64102
65- await directoryService . connect ( ) ;
66- const allUsers = await knex ( "users" ) . where ( { email : terms } ) ;
103+ const data = await unifiedDirectory . searchByEmail ( terms ) ;
67104
68- if ( allUsers . length == 1 ) {
69- const nonDirectoryUser = allUsers [ 0 ] ;
105+ if ( data . length > 0 ) {
106+ data . map (
107+ ( d : any ) => ( d . user_id = allUsers . find ( ( u ) => u . email == d . email ) ?. id )
108+ ) ;
109+ } else {
110+ const nonDirectoryUser = allUsers . find ( ( u ) => u . email == terms ) ;
70111
71- return res . json ( {
72- data : [
73- {
112+ if ( nonDirectoryUser ) {
113+ data . push ( {
74114 display_name : `${ nonDirectoryUser . first_name } ${ nonDirectoryUser . last_name } ` ,
75115 first_name : nonDirectoryUser . first_name ,
76116 last_name : nonDirectoryUser . last_name ,
@@ -82,34 +122,10 @@ directoryRouter.post("/search-action-directory-email", async (req: Request, res:
82122 officeLocation : nonDirectoryUser . officeLocation ,
83123 userPrincipalName : nonDirectoryUser . userPrincipalName ,
84124 user_id : nonDirectoryUser . id ,
85- } ,
86- ] ,
87- } ) ;
88- }
89-
90- const data = await directoryService . searchByEmail ( terms ) ;
91-
92- if ( data . length > 0 ) {
93- data . map ( ( d : any ) => ( d . user_id = allUsers . find ( ( u ) => u . email == d . email ) ?. id ) ) ;
94- } else {
95- const nonDirectoryUser = allUsers . find ( ( u ) => u . email == terms ) ;
96-
97- if ( nonDirectoryUser ) {
98- data . push ( {
99- display_name : `${ nonDirectoryUser . first_name } ${ nonDirectoryUser . last_name } ` ,
100- first_name : nonDirectoryUser . first_name ,
101- last_name : nonDirectoryUser . last_name ,
102- ynet_id : nonDirectoryUser . ynet_id ,
103- email : nonDirectoryUser . email ,
104- long_name : `${ nonDirectoryUser . first_name } ${ nonDirectoryUser . last_name } (${ nonDirectoryUser . email } ) ${ nonDirectoryUser . department } : ${ nonDirectoryUser . title } ` ,
105- title : nonDirectoryUser . title ,
106- department : nonDirectoryUser . department ,
107- officeLocation : nonDirectoryUser . officeLocation ,
108- userPrincipalName : nonDirectoryUser . userPrincipalName ,
109- user_id : nonDirectoryUser . id ,
110- } ) ;
125+ } ) ;
126+ }
111127 }
112- }
113128
114- return res . json ( { data } ) ;
115- } ) ;
129+ return res . json ( { data } ) ;
130+ }
131+ ) ;
0 commit comments