gmarche/scripts/check_sold.sh

64 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
[ "$EUID" -ne 0 ] && sudo="sudo" || sudo=""
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
start=`date +%s`
date=$(date +'%d-%m-%y à %H:%M')
echo "### $date ###"
## Initialisation
DUNITER=$(grep g1_node $SCRIPTPATH/../config/config.php | awk '{ print $3}' | tr -d ';' | tr -d "'")
TXBLOCKS=$(curl -s $DUNITER/blockchain/with/tx | jq '.result.blocks[]')
TXBLOCKS=$(echo "$TXBLOCKS" | tail -n 2 | tr '\n' ' ')
keyword="\[Achat GMarche] : "
[ ! -e $SCRIPTPATH/.loc_hash ] && touch $SCRIPTPATH/.loc_hash
## Récupération des transactions avec commentaire GMarche
for i in $TXBLOCKS; do
result=$(curl -s $DUNITER/blockchain/block/$i | jq '.transactions[].hash, .transactions[].comment' | grep -v '""' | tr -d '"' | grep -B1 "$keyword")
rhash=$(echo "$result" | head -n1)
rcom=$(echo "$result" | tail -n1)
if [[ ! -z $result && -z $(grep $rhash $SCRIPTPATH/.loc_hash) ]]; then
isSell+=$(echo -e "$rcom;")
echo "$rhash" >> $SCRIPTPATH/.loc_hash
fi
sleep 1
done
## Ajout des transactions en piscine pour les users GMarche
req="SELECT pubkey FROM users;"
usersPubkeys=$($sudo mysql --database gmarche -e "$req" | grep .)
for i in $usersPubkeys; do
result=$(curl -s $DUNITER/tx/history/$i/pending | jq '. | select(.history.sending == [])' | jq '.history.pending[].hash,.history.pending[].comment' | tr -d '"')
rhash=$(echo "$result" | head -n1)
rcom=$(echo "$result" | tail -n1)
if [[ ! -z $result && -z $(grep $rhash $SCRIPTPATH/.loc_hash) ]]; then
isSell+=$(echo -e "$rcom;")
echo "$rhash" >> $SCRIPTPATH/.loc_hash
fi
done
## Retrait de l'identifiant gmarche des commentaires
isSell=$(echo $isSell | sed "s/$keyword//g")
[[ ! -z $isSell ]] && echo "$isSell est vendu"
## Retrait d'un quantité pour les articles vendu
IFS=';'; ADDR=($isSell); unset IFS;
for i in "${ADDR[@]}"; do
#req="UPDATE products SET status = 'solde' WHERE name = '$i';"
req="UPDATE products SET quantite = CASE WHEN quantite = 0 THEN quantite ELSE quantite - 1 END WHERE name = '$i';"
$sudo mysql --database gmarche -e "$req"
done
#$sudo mysql --database gmarche -e "SELECT name,status FROM products;"
end=`date +%s`
runtime=$((end-start))
echo "Temps d'execution: ${runtime}s"
exit 0