From fb62f19ab6676b582cd68e97538a550af06bdc9f Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Fri, 1 May 2020 12:45:26 +0000
Subject: [PATCH] added logging of duplicate email in OTRS customer-user export

---
 inventory_provider/db/ims_data.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/inventory_provider/db/ims_data.py b/inventory_provider/db/ims_data.py
index 869241d0..b648a9c7 100644
--- a/inventory_provider/db/ims_data.py
+++ b/inventory_provider/db/ims_data.py
@@ -1,9 +1,14 @@
+import logging
 import re
 from collections import OrderedDict
 
+from inventory_provider import environment
 from inventory_provider.db import ims
 from inventory_provider.db.ims import InventoryStatus
 
+environment.setup_logging()
+logger = logging.getLogger(__name__)
+
 INTERNAL_POP_NAMES = {
     'Cambridge OC',
     'DANTE Lab',
@@ -228,5 +233,17 @@ def otrs_get_customer_users_rows(ds):
                 yield c
 
     sorted_cus = sorted(get_all_cus_user_rows(), key=lambda x: x['email'])
+    duplicate_emails = set()
+    previous_email = None
+    for cu in sorted_cus:
+        if cu['email'] == previous_email:
+            duplicate_emails.add(previous_email)
+        previous_email = cu['email']
+    if duplicate_emails:
+        logger.error('Duplicate emails found in OTRS customer-user export: '
+                       f'{duplicate_emails}')
+        # raise KeyError('Duplicate emails found in OTRS customer-user export: '  # noqa
+        #                f'{duplicate_emails}')
+
     for cu in sorted_cus:
         yield list(cu.values())
-- 
GitLab