Verify zipped bag exists before "Copy Resource Bag to iRODS User Zone"
See @ChristinaB 's  error here https://github.com/hydroshare/hydroshare/issues/2662#issuecomment-384010685 with this resource https://www.hydroshare.org/resource/ec518c49772b420b97047f7482ae92aa/  when trying to "Copy Resource Bag to iRODS User Zone"

I checked and ec518c49772b420b97047f7482ae92aa.zip bag does not exist in the bags directory /hydroshareuserZone/home/localHydroProxy/bags 

Regardless, will likely need iRODS 4.2.3 installed first to fix the .zip bag creation bug associated with iRODS 4.2.2 here https://github.com/hydroshare/hydroshare/issues/2661 
diff --git a/hs_core/hydroshare/utils.py b/hs_core/hydroshare/utils.py
index c7d23b2d8..3a87c527d 100755
--- a/hs_core/hydroshare/utils.py
+++ b/hs_core/hydroshare/utils.py
@@ -370,23 +370,41 @@ def replicate_resource_bag_to_user_zone(user, res_id):
     res = get_resource_by_shortkey(res_id)
     res_coll = res.root_path
     istorage = res.get_irods_storage()
-    bag_modified = "false"
+    bag_modified_flag = True
     # needs to check whether res_id collection exists before getting/setting AVU on it to
     # accommodate the case where the very same resource gets deleted by another request when
     # it is getting downloaded
-    # TODO: why would we want to do anything at all if the resource does not exist???
     if istorage.exists(res_coll):
         bag_modified = istorage.getAVU(res_coll, 'bag_modified')
-        if bag_modified.lower() == "true":
+
+        # make sure bag_modified_flag is set to False only if bag exists and bag_modified AVU
+        # is False; otherwise, bag_modified_flag will take the default True value so that the
+        # bag will be created or recreated
+        if bag_modified:
+            if bag_modified.lower() == "false":
+                bag_file_name = res_id + '.zip'
+                if res.resource_federation_path:
+                    bag_full_path = os.path.join(res.resource_federation_path, 'bags',
+                                                 bag_file_name)
+                else:
+                    bag_full_path = os.path.join('bags', bag_file_name)
+
+                if istorage.exists(bag_full_path):
+                    bag_modified_flag = False
+
+        if bag_modified_flag:
             # import here to avoid circular import issue
             from hs_core.tasks import create_bag_by_irods
-            create_bag_by_irods(res_id)
+            status = create_bag_by_irods(res_id)
+            if not status:
+                # bag fails to be created successfully
+                raise SessionException(-1, '', 'The resource bag fails to be created '
+                                               'before bag replication')
 
         # do replication of the resource bag to irods user zone
         if not res.resource_federation_path:
             istorage.set_fed_zone_session()
         src_file = res.bag_path
-        # TODO: allow setting destination path
         tgt_file = '/{userzone}/home/{username}/{resid}.zip'.format(
             userzone=settings.HS_USER_IRODS_ZONE, username=user.username, resid=res_id)
         fsize = istorage.size(src_file)
diff --git a/hs_core/tests/api/native/test_irods_user_zone_federation.py b/hs_core/tests/api/native/test_irods_user_zone_federation.py
index ea7a2267f..7a0e5dc1e 100644
--- a/hs_core/tests/api/native/test_irods_user_zone_federation.py
+++ b/hs_core/tests/api/native/test_irods_user_zone_federation.py
@@ -264,7 +264,10 @@ class TestUserZoneIRODSFederation(TestCaseCommonUtilities, TransactionTestCase):
         # since 'copy' is used for fed_copy_or_move when adding the file to the resource
         self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
 
-        # test replication of this resource to user zone
+        # test replication of this resource to user zone even if the bag_modified AVU for this
+        # resource is wrongly set to False when the bag for this resource does not exist and
+        # need to be recreated
+        res.setAVU('bag_modified', 'false')
         hydroshare.replicate_resource_bag_to_user_zone(self.user, res.short_id)
         self.assertTrue(self.irods_storage.exists(user_path + res.short_id + '.zip'),
                         msg='replicated resource bag is not in the user zone')
diff --git a/hs_core/views/__init__.py b/hs_core/views/__init__.py
index f26dcc144..843e199aa 100644
--- a/hs_core/views/__init__.py
+++ b/hs_core/views/__init__.py
@@ -612,6 +612,12 @@ def rep_res_bag_to_irods_user_zone(request, shortkey, *args, **kwargs):
             json.dumps({"error": ex.message}),
             content_type="application/json"
         )
+    except ValidationError as ex:
+        return HttpResponse(
+            json.dumps({"error": ex.message}),
+            content_type="application/json"
+        )
+
 
 def copy_resource(request, shortkey, *args, **kwargs):
     res, authorized, user = authorize(request, shortkey,
diff --git a/irods_browser_app/static/js/irods.js b/irods_browser_app/static/js/irods.js
index e3ac0567e..32ae934a6 100644
--- a/irods_browser_app/static/js/irods.js
+++ b/irods_browser_app/static/js/irods.js
@@ -127,7 +127,7 @@ function click_folder_opr() {
     }
     else {
         var store = $(this).attr('name');
-        var parent = $(this)
+        var parent = $(this);
         get_store(store, parent, margin_left);
         $(this).addClass('isOpen');
         set_datastore($(this).attr('name'), true);
['hs_core/hydroshare/utils.py', 'hs_core/tests/api/native/test_irods_user_zone_federation.py', 'hs_core/views/__init__.py', 'irods_browser_app/static/js/irods.js']