]> git.plutz.net Git - lobster/commitdiff
introduce transaction id for therapy updates
authorPaul Hänsch <paul@plutz.net>
Tue, 25 Aug 2020 00:23:42 +0000 (02:23 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 25 Aug 2020 00:23:42 +0000 (02:23 +0200)
therapies/autosave.js
therapies/page.sh
therapies/update_therapy.sh

index b4020ddce94ca1a4db7cfbcccfb81af67ea3849e..665a6011463ff7de12fa5e064847910805be58c1 100644 (file)
@@ -4,6 +4,7 @@ var formdata = '';
 
 function postsubmit(){
   if ( this.status == 200 ) {
+    document.querySelector('#report input[name="tid"]').setAttribute('value', this.response);
     console.log('successful auto submit of form data');
     button.setAttribute('style', 'display: none;');
   } else {
@@ -18,7 +19,7 @@ function failsubmit(){
 
 function formencode(fd){
     var send;
-    send='autosubmit=false';
+    send='autosubmit=true';
     for (var tup of fd.entries()){
       send += '&' + encodeURIComponent(tup[0]) + '=' + encodeURIComponent(tup[1]);
     }
index a38372f9e3443ebc5caec497cc391369d8aaa432..fb86050e1399d0d9030f62a0e3156c5cf0df620c 100755 (executable)
@@ -152,7 +152,7 @@ fi
 
 cat <<EOF
 [form #report method=POST action="/therapies/update_therapy.sh"
-  [hidden "id" "$id"]
+  [hidden "id" "$id"][hidden "tid" "$(transid "$tpyfile")"]
 
   [input .stickynote type=checkbox name=c_stickynote #show_stickynote]
   [fieldset .stickynote
index dafa1b20692d474484eb3f5b24ff5d69496272dc..bb3901fbe21f66d3cbf6ecafb87d39e9ff1afc68 100755 (executable)
 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
 
 . "$_EXEC/pdiread.sh"
-tpy=$(POST id)
+tpy="$(POST id)"
 
 tpyfile="$_DATA/therapies/${tpy}.tpy"
 tempfile="$_DATA/temp/${tpy}.tpy"
 
+if [ "$(POST tid)" != "$(transid "$tpyfile")" ]; then
+  if [ "$(POST autosubmit)" = "true" ]; then
+    printf 'Status: 409 Conflict\r\nContent-Length: 0\r\n\r\n'
+    exit 0
+  else
+    SET_COOKIE session message="TRANSACTION CONFLICT"
+    REDIRECT "/therapies/${tpy%.*}/${tpy#*.}"
+  fi
+fi
+
 # serialize POST array into file
 for key in $(POST_KEYS); do
   case "$key" in
-    imagedata) : ;;
+    imagedata|tid) : ;;
     session*_date)
       value="$(POST "$key")"
       y=0 mon=0 dom=0
@@ -96,11 +106,15 @@ elif [ "$(POST imagedata)" ]; then
   sync
 fi
 
-mv "$tempfile" "$tpyfile"
-rm -f "${_DATA}/cache/${tpy%%.*}.vcf.cache"
+if ! diff -q "$tempfile" "$tpyfile" >/dev/null; then
+  mv "$tempfile" "$tpyfile"
+  rm -f -- "${_DATA}/cache/${tpy%%.*}.vcf.cache"
+fi
 
 if [ "$(POST autosubmit)" = "true" ]; then
-  printf 'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'
+  msg="$(transid "$tpyfile")"
+  printf 'HTTP/1.1 200 OK\r\nContent-Length: %i\r\n\r\n%s' \
+         "${#msg}" "${msg}"
 else
   REDIRECT "/therapies/${tpy%.*}/${tpy#*.}"
 fi