@@ -15,10 +15,32 @@ export default {
1515      label : "Folder IDs to Monitor" , 
1616      description : "Specify the folder IDs or names in Outlook that you want to monitor for new emails. Leave empty to monitor all folders." , 
1717      optional : true , 
18+       async  options ( )  { 
19+         const  {  value : folders  }  =  await  this . listFolders ( ) ; 
20+         return  folders ?. map ( ( { 
21+           id : value ,  displayName : label , 
22+         } )  =>  ( { 
23+           value, 
24+           label, 
25+         } ) )  ||  [ ] ; 
26+       } , 
1827    } , 
1928  } , 
2029  hooks : { 
2130    ...common . hooks , 
31+     async  deploy ( )  { 
32+       this . db . set ( "sentItemFolderId" ,  await  this . getSentItemFolderId ( ) ) ; 
33+ 
34+       const  events  =  await  this . getSampleEvents ( { 
35+         pageSize : 25 , 
36+       } ) ;  console . log ( events ) ; 
37+       if  ( ! events  ||  events . length  ==  0 )  { 
38+         return ; 
39+       } 
40+       for  ( const  item  of  events )  { 
41+         this . emitEvent ( item ) ; 
42+       } 
43+     } , 
2244    async  activate ( )  { 
2345      await  this . activate ( { 
2446        changeType : "created" , 
@@ -31,6 +53,16 @@ export default {
3153  } , 
3254  methods : { 
3355    ...common . methods , 
56+     listFolders ( )  { 
57+       return  this . microsoftOutlook . _makeRequest ( { 
58+         path : "/me/mailFolders" , 
59+       } ) ; 
60+     } , 
61+     async  getSentItemFolderId ( )  { 
62+       const  {  value : folders  }  =  await  this . listFolders ( ) ; 
63+       const  {  id }  =  folders . find ( ( {  displayName } )  =>  displayName  ===  "Sent Items" ) ; 
64+       return  id ; 
65+     } , 
3466    async  getSampleEvents ( {  pageSize } )  { 
3567      const  folders  =  this . folderIds ?. length 
3668        ? this . folderIds . map ( ( id )  =>  `/me/mailFolders/${ id }  /messages` ) 
@@ -51,13 +83,23 @@ export default {
5183      } 
5284      return  results ; 
5385    } , 
86+     isRelevant ( item )  { 
87+       if  ( this . folderIds ?. length )  { 
88+         return  true ; 
89+       } 
90+       // if no folderIds are specified, filter out items in Sent Items 
91+       const  sentItemFolderId  =  this . db . get ( "sentItemFolderId" ) ; 
92+       return  item . parentFolderId  !==  sentItemFolderId ; 
93+     } , 
5494    emitEvent ( item )  { 
55-       this . $emit ( 
56-         { 
57-           email : item , 
58-         } , 
59-         this . generateMeta ( item ) , 
60-       ) ; 
95+       if  ( this . isRelevant ( item ) )  { 
96+         this . $emit ( 
97+           { 
98+             email : item , 
99+           } , 
100+           this . generateMeta ( item ) , 
101+         ) ; 
102+       } 
61103    } , 
62104    generateMeta ( item )  { 
63105      return  { 
@@ -78,11 +120,15 @@ export default {
78120      await  this . run ( { 
79121        event, 
80122        emitFn : async  ( {  resourceId }  =  { } )  =>  { 
81-           const  item  =  await  this . microsoftOutlook . getMessage ( { 
82-             resource : folder , 
83-             messageId : resourceId , 
84-           } ) ; 
85-           this . emitEvent ( item ) ; 
123+           try  { 
124+             const  item  =  await  this . microsoftOutlook . getMessage ( { 
125+               resource : folder , 
126+               messageId : resourceId , 
127+             } ) ; 
128+             this . emitEvent ( item ) ; 
129+           }  catch  { 
130+             console . log ( `Could not fetch message with ID: ${ resourceId }  ` ) ; 
131+           } 
86132        } , 
87133      } ) ; 
88134    } 
0 commit comments