Correct subject formatting

Change-Id: I5e744d48f3c971c99def9594da3a80d9625e856b
Related-bug: PROD-24843 (PROD:24843)
diff --git a/sf_notifier/helpers.py b/sf_notifier/helpers.py
index 8a5bf8d..de02814 100644
--- a/sf_notifier/helpers.py
+++ b/sf_notifier/helpers.py
@@ -15,10 +15,19 @@
 
 
 RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
-SUBJECT_FMT = '[{}][{}] {}'
 
 
-def alert_fields_and_action(alert):
+def _format_subject(alert, env):
+    subject = '[{}]'.format(alert['labels'].get('environment', env))
+
+    host = alert['labels'].get('host')
+    if host is not None:
+        subject = '{}[{}]'.format(subject, host)
+
+    return '{} {}'.format(subject, alert['annotations']['summary'])
+
+
+def alert_fields_and_action(alert, env):
     fields = []
 
     if alert['status'] in RESOLVED_STATUSES:
@@ -26,11 +35,8 @@
         fields.append(alert['labels'])
     else:
         action = 'create_case'
-        # Order matters
-        subject = SUBJECT_FMT.format(alert['labels']['environment'],
-                                     alert['labels'].get('host', 'none'),
-                                     alert['annotations']['summary'])
-        fields.append(subject)
+        # Order of keys matters
+        fields.append(_format_subject(alert, env))
         fields.append(alert['annotations']['description'])
         fields.append(alert['status'])
         fields.append(alert['labels'])
diff --git a/sf_notifier/salesforce/client.py b/sf_notifier/salesforce/client.py
index 79a6467..5adc691 100644
--- a/sf_notifier/salesforce/client.py
+++ b/sf_notifier/salesforce/client.py
@@ -188,6 +188,9 @@
         try:
             return self.sf.Case.get_by_custom_id('Alert_ID__c', alert_id)
         except sf_exceptions.SalesforceResourceNotFound:
+            if self._registered_alerts.get(alert_id):
+                del self._registered_alerts[alert_id]
+
             logger.warning('Alert ID: {} was already solved.'.format(alert_id))
 
     def create_case(self, subject, body, status, labels):
diff --git a/sf_notifier/server.py b/sf_notifier/server.py
index e9414bc..dab1aae 100644
--- a/sf_notifier/server.py
+++ b/sf_notifier/server.py
@@ -62,7 +62,7 @@
     cases = []
     for alert in data['alerts']:
         try:
-            fields, action = alert_fields_and_action(alert)
+            fields, action = alert_fields_and_action(alert, sf_cli.environment)
         except KeyError as key:
             msg = 'Alert misses {} key.'.format(key)
             app.logger.error(msg)