112
112
LAST_RUN_FOLDER = "folderName"
113
113
ERROR_COUNTER = "errorCounter"
114
114
115
+ # Types of filter
116
+ MODIFIED_FILTER = "modified-time"
117
+ RECEIVED_FILTER = "received-time"
118
+
115
119
# headers
116
120
ITEMS_RESULTS_HEADERS = [
117
121
"sender" ,
@@ -2214,7 +2218,7 @@ def parse_incident_from_item(item): # pragma: no cover
2214
2218
return incident
2215
2219
2216
2220
2217
- def fetch_emails_as_incidents (client : EWSClient , last_run ):
2221
+ def fetch_emails_as_incidents (client : EWSClient , last_run , incident_filter ):
2218
2222
"""
2219
2223
Fetch incidents
2220
2224
:param client: EWS Client
@@ -2230,18 +2234,32 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
2230
2234
client .folder_name ,
2231
2235
last_run .get (LAST_RUN_TIME ),
2232
2236
excluded_ids ,
2237
+ incident_filter ,
2233
2238
)
2234
2239
2235
2240
incidents = []
2236
2241
incident : Dict [str , str ] = {}
2237
2242
emails_ids = [] # Used for mark emails as read
2238
2243
demisto .debug (f'{ APP_NAME } - Started fetch with { len (last_emails )} at { last_run .get (LAST_RUN_TIME )} ' )
2239
2244
current_fetch_ids = set ()
2245
+
2246
+ last_fetch_time = last_run .get (LAST_RUN_TIME )
2247
+
2248
+ last_modification_time = last_fetch_time
2249
+ if isinstance (last_modification_time , EWSDateTime ):
2250
+ last_modification_time = last_modification_time .ewsformat ()
2251
+
2240
2252
for item in last_emails :
2241
2253
if item .message_id :
2242
2254
current_fetch_ids .add (item .message_id )
2243
2255
incident = parse_incident_from_item (item )
2244
2256
incidents .append (incident )
2257
+
2258
+ if incident_filter == MODIFIED_FILTER :
2259
+ item_modified_time = item .last_modified_time .ewsformat ()
2260
+ if last_modification_time is None or last_modification_time < item_modified_time :
2261
+ last_modification_time = item_modified_time
2262
+
2245
2263
if item .id :
2246
2264
emails_ids .append (item .id )
2247
2265
@@ -2250,9 +2268,10 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
2250
2268
2251
2269
demisto .debug (f'{ APP_NAME } - ending fetch - got { len (incidents )} incidents.' )
2252
2270
2253
- last_fetch_time = last_run .get (LAST_RUN_TIME )
2254
-
2255
- last_incident_run_time = incident .get ("occurred" , last_fetch_time )
2271
+ if incident_filter == MODIFIED_FILTER :
2272
+ last_incident_run_time = last_modification_time
2273
+ else : # default case - using 'received' time
2274
+ last_incident_run_time = incident .get ("occurred" , last_fetch_time )
2256
2275
2257
2276
# making sure both last fetch time and the time of most recent incident are the same type for comparing.
2258
2277
if isinstance (last_incident_run_time , EWSDateTime ):
@@ -2299,7 +2318,7 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
2299
2318
2300
2319
2301
2320
def fetch_last_emails (
2302
- client : EWSClient , folder_name = "Inbox" , since_datetime = None , exclude_ids = None
2321
+ client : EWSClient , folder_name = "Inbox" , since_datetime = None , exclude_ids = None , incident_filter = RECEIVED_FILTER
2303
2322
):
2304
2323
"""
2305
2324
Fetches last emails
@@ -2313,7 +2332,10 @@ def fetch_last_emails(
2313
2332
demisto .debug (f"Finished getting the folder named { folder_name } by path" )
2314
2333
log_memory ()
2315
2334
if since_datetime :
2316
- qs = qs .filter (datetime_received__gte = since_datetime )
2335
+ if incident_filter == MODIFIED_FILTER :
2336
+ qs = qs .filter (last_modified_time__gte = since_datetime )
2337
+ else : # default to "received" time
2338
+ qs = qs .filter (datetime_received__gte = since_datetime )
2317
2339
else :
2318
2340
tz = EWSTimeZone ('UTC' )
2319
2341
first_fetch_datetime = dateparser .parse (FETCH_TIME )
@@ -2426,8 +2448,12 @@ def sub_main(): # pragma: no cover
2426
2448
demisto .results (test_module (client , params .get ('max_fetch' )))
2427
2449
elif command == "fetch-incidents" :
2428
2450
last_run = demisto .getLastRun ()
2429
- incidents = fetch_emails_as_incidents (client , last_run )
2451
+ incident_filter = params .get ('incidentFilter' , RECEIVED_FILTER )
2452
+ if incident_filter not in [RECEIVED_FILTER , MODIFIED_FILTER ]: # Ensure it's one of the allowed filter values
2453
+ incident_filter = RECEIVED_FILTER # or if not, force it to the default, RECEIVED_FILTER
2454
+ incidents = fetch_emails_as_incidents (client , last_run , incident_filter )
2430
2455
demisto .debug (f"Saving incidents with size { sys .getsizeof (incidents )} " )
2456
+
2431
2457
demisto .incidents (incidents )
2432
2458
2433
2459
# special outputs commands
0 commit comments