From 04144c68cd5c681398b09d7660535f9bd1bd0579 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Wed, 19 Mar 2025 14:00:50 +0100
Subject: [PATCH] FIX: deal with borrowing bad entities

---
 .../build.properties.d/51_box_loan.properties |  1 -
 .../loan-forms/borrow_all_bookmarked.html     |  6 +++--
 .../forms/loan-forms/borrow_checkout.html     |  3 ++-
 .../caosdb-webui/src/ext/js/box_loan.js       |  3 ++-
 loanpy/src/loan/request_loan.py               | 24 +++++++++++++++++++
 5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/loan-custom/caosdb-server/caosdb-webui/build.properties.d/51_box_loan.properties b/loan-custom/caosdb-server/caosdb-webui/build.properties.d/51_box_loan.properties
index b2bfa0e..f133e17 100644
--- a/loan-custom/caosdb-server/caosdb-webui/build.properties.d/51_box_loan.properties
+++ b/loan-custom/caosdb-server/caosdb-webui/build.properties.d/51_box_loan.properties
@@ -1,5 +1,4 @@
 BUILD_MODULE_BOX_LOAN=ENABLED
-BUILD_MODULE_EXT_LOAN_BORROW_BOOKMARKED=ENABLED
 MODULE_DEPENDENCIES+=(
   box_loan_config.js
   box_loan.js
diff --git a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_all_bookmarked.html b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_all_bookmarked.html
index 71fd0cc..13371f4 100644
--- a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_all_bookmarked.html
+++ b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_all_bookmarked.html
@@ -72,7 +72,9 @@
                 <div class="row caosdb-form-row">
                   <div class="col-sm-12">
                     <div class="form-check">
-                      <label class="form-check-label">Are you going to completely exhaust the contents of this box?
+                      <label class="form-check-label">Are you going to
+                        completely exhaust the contents of at lease one
+                        {lentType}?
                         <input type="checkbox" class="form-check-input" id="exhaust-contents" value="no" />
                       </label>
                     </div>
@@ -92,4 +94,4 @@
   </div>
 </body>
 
-</html>
\ No newline at end of file
+</html>
diff --git a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
index f183fb0..f688982 100644
--- a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
+++ b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
@@ -61,7 +61,8 @@
           <div class="row caosdb-form-row">
             <div class="col-sm-12">
               <div class="form-check">
-                <label class="form-check-label">Are you going to completely exhaust the contents of this box?
+                <label class="form-check-label">Are you going to completely
+                  exhaust the contents of this {lentType}?
                   <input type="checkbox" class="form-check-input" id="exhaust-contents" value="no" />
                 </label>
               </div>
diff --git a/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js b/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
index fdeb511..e3ba18f 100644
--- a/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
+++ b/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
@@ -740,7 +740,8 @@ var box_loan = function(logger, box_loan_config) {
             email: email,
             destination: destination,
             expected_return_date: exp_return,
-            mindate: get_formatted_date("T")
+            mindate: get_formatted_date("T"),
+            lentType: datamodel.box
         });
     }
 
diff --git a/loanpy/src/loan/request_loan.py b/loanpy/src/loan/request_loan.py
index e582089..8a98589 100755
--- a/loanpy/src/loan/request_loan.py
+++ b/loanpy/src/loan/request_loan.py
@@ -23,6 +23,7 @@ Creates a loan request using information provided by a web formular.
 from __future__ import absolute_import
 
 import linkahead as db
+import logging
 from caosadvancedtools.serverside.helper import get_timestamp, print_success
 from linkahead.common.models import get_id_from_versionid, value_matches_versionid
 
@@ -32,6 +33,8 @@ from .box_loan import (BORROWER, BOX, COMMENT, DESTINATION, EXHAUST_CONTENTS, EX
                        LOAN, LOAN_REQUESTED, assert_date_in_future, assert_key_in_data,
                        insert_or_update_person, main, send_loan_request_mail)
 
+LOGGER_NAME = "box_loan"
+LOGGER = logging.getLogger(LOGGER_NAME)
 
 def create_loan(box, borrower, expected_return, exhaust_contents, comment, destination):
     """ Create a new loan record. """
@@ -92,10 +95,29 @@ def _check_data(data):
             data[F_EXPECTED_RETURN_DATE]))
 
 
+def _check_items(items):
+    """ Check whether the items have the correct RecordTypes """
+    if not isinstance(items, list):
+        items = [items]
+    bad_type_ids = []
+    for item in items:
+        if db.execute_query(f"COUNT {BOX.name} WITH ID='{item}'") != 1:
+            bad_type_ids.append(item)
+
+    if len(bad_type_ids)>0:
+        LOGGER.error(f"The entities with the following IDs do not have the appropriate RecordType "
+                     f"in order to create a loan:<br>{', '.join(bad_type_ids)}<br>")
+        return False
+    return True
+
+
+
 def _issue_loan_request(data):
     """ Insert a loan record a insert/update a person record. """
     data = _set_defaults(data)
     _check_data(data)
+    if not _check_items(data[F_BOX]):
+        return None, None
     borrower = insert_or_update_person(firstname=data[F_FIRST_NAME],
                                        lastname=data[F_LAST_NAME],
                                        email=data[F_EMAIL])
@@ -126,6 +148,8 @@ def issue_loan_request(data):
     identified by either email, oder firstName+lastName.
     """
     borrower, loan = _issue_loan_request(data)
+    if borrower is None:
+        return 1
 
     fn = borrower.get_property(FIRST_NAME.name).value
     ln = borrower.get_property(LAST_NAME.name).value
-- 
GitLab