#!/bin/bash
echo "Example for calling the pdms rest-interface"

HOSTNAME=detect.vm.local
REALM_NAME=detect
USERNAME=pdms_user
CLIENT_ID=frontend
PASSWORD=password
INSECURE=--insecure
KEYCLOAK_URL=https://$HOSTNAME/auth/realms/$REALM_NAME/protocol/openid-connect/token

KEYCLOAK_CALL=$(curl -X POST "$KEYCLOAK_URL" "$INSECURE" -s\
 -H "Content-Type: application/x-www-form-urlencoded" \
 -d "username=$USERNAME" \
 -d "password=$PASSWORD" \
 -d "grant_type=password" \
 -d "scop=openid" \
 -d "client_id=$CLIENT_ID")

echo "> Keycloak Response:"
echo $KEYCLOAK_CALL
TOKEN=$(echo ${KEYCLOAK_CALL} | jq -r '.access_token')

curl -X 'POST' -k \
  'https://detect.vm.local/backend/pdms/wards/aneits/cases' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer '${TOKEN}'' \
  -d '[
  {
    "patient": {
      "pdmsID": "1234",
      "kisID": "1234",
      "surname": "Surname",
      "givenname": "Givenname",
      "position": "Bett 1"
    },
    "measurements": {
      "gcs": [
        {
          "value": 3,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "rass": [
        {
          "value": -5,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "pupilReaction": [
        {
          "left": false,
          "right": false,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "icp": [
        {
          "value": 0,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "cpp": [
        {
          "value": 0,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "natrium": [
        {
          "value": 100,
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "reanimation": [
        {
          "value": "no reanimation",
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ],
      "ventilation": [
        {
          "value": "ventilation",
          "timestamp": "2022-05-03T11:24:46.367Z"
        }
      ]
    }
  }
]'