Skip to content

Commit b27895a

Browse files
authored
[Marketplace Contribution] EWS - Content Pack Update (#24563)
1 parent 487743b commit b27895a

File tree

5 files changed

+123
-364
lines changed

5 files changed

+123
-364
lines changed

Packs/MicrosoftExchangeOnline/Integrations/EWSO365/EWSO365.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
LAST_RUN_FOLDER = "folderName"
113113
ERROR_COUNTER = "errorCounter"
114114

115+
# Types of filter
116+
MODIFIED_FILTER = "modified-time"
117+
RECEIVED_FILTER = "received-time"
118+
115119
# headers
116120
ITEMS_RESULTS_HEADERS = [
117121
"sender",
@@ -2214,7 +2218,7 @@ def parse_incident_from_item(item): # pragma: no cover
22142218
return incident
22152219

22162220

2217-
def fetch_emails_as_incidents(client: EWSClient, last_run):
2221+
def fetch_emails_as_incidents(client: EWSClient, last_run, incident_filter):
22182222
"""
22192223
Fetch incidents
22202224
:param client: EWS Client
@@ -2230,18 +2234,32 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
22302234
client.folder_name,
22312235
last_run.get(LAST_RUN_TIME),
22322236
excluded_ids,
2237+
incident_filter,
22332238
)
22342239

22352240
incidents = []
22362241
incident: Dict[str, str] = {}
22372242
emails_ids = [] # Used for mark emails as read
22382243
demisto.debug(f'{APP_NAME} - Started fetch with {len(last_emails)} at {last_run.get(LAST_RUN_TIME)}')
22392244
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+
22402252
for item in last_emails:
22412253
if item.message_id:
22422254
current_fetch_ids.add(item.message_id)
22432255
incident = parse_incident_from_item(item)
22442256
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+
22452263
if item.id:
22462264
emails_ids.append(item.id)
22472265

@@ -2250,9 +2268,10 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
22502268

22512269
demisto.debug(f'{APP_NAME} - ending fetch - got {len(incidents)} incidents.')
22522270

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)
22562275

22572276
# making sure both last fetch time and the time of most recent incident are the same type for comparing.
22582277
if isinstance(last_incident_run_time, EWSDateTime):
@@ -2299,7 +2318,7 @@ def fetch_emails_as_incidents(client: EWSClient, last_run):
22992318

23002319

23012320
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
23032322
):
23042323
"""
23052324
Fetches last emails
@@ -2313,7 +2332,10 @@ def fetch_last_emails(
23132332
demisto.debug(f"Finished getting the folder named {folder_name} by path")
23142333
log_memory()
23152334
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)
23172339
else:
23182340
tz = EWSTimeZone('UTC')
23192341
first_fetch_datetime = dateparser.parse(FETCH_TIME)
@@ -2426,8 +2448,12 @@ def sub_main(): # pragma: no cover
24262448
demisto.results(test_module(client, params.get('max_fetch')))
24272449
elif command == "fetch-incidents":
24282450
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)
24302455
demisto.debug(f"Saving incidents with size {sys.getsizeof(incidents)}")
2456+
24312457
demisto.incidents(incidents)
24322458

24332459
# special outputs commands

0 commit comments

Comments
 (0)