diff --git a/metadataScripts/README.md b/metadataScripts/README.md index 625e7c4..3db020a 100644 --- a/metadataScripts/README.md +++ b/metadataScripts/README.md @@ -25,4 +25,21 @@ To use this script: POSTGRES_DB="clinlims" \ POSTGRES_USER="clinlims" \ ./copy-result-range.sh - ``` \ No newline at end of file + ``` + +## 📦 [set_saleable_attribute.sh](./openmrs/set_saleable_attribute.sh) + +This script allows to set the `saleable` attribute to true for the concepts matched by UUIDS read from a CSV file. This is specifically useful when concepts in bulk needs to be updated with the attribute so that it gets synced with Odoo as products. An example could be `Procedure` concepts. + +The script leverages the concept attribute APIs `/openmrs/ws/rest/v1/concept/$uuid/attribute` and `/openmrs/ws/rest/v1/conceptattributetype?q=saleable&limit=1` + +Usage of this script: +1. Prepare a CSV which contains the UUIDs of the concepts which needs to be updated in the first column. + +2. Run the script by passing the required parameters + + ```./set_saleable_attribute.sh path/to/uuids.csv domain username password``` + +3. Example: + + ```./set_saleable_attribute.sh procedures.csv localhost superman Admin123``` diff --git a/metadataScripts/openmrs/set_saleable_attribute.sh b/metadataScripts/openmrs/set_saleable_attribute.sh new file mode 100755 index 0000000..91e0437 --- /dev/null +++ b/metadataScripts/openmrs/set_saleable_attribute.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +# Check if the CSV file and credentials are provided as arguments +if [ "$#" -ne 4 ]; then + echo "Usage: $0 path/to/uuids.csv domain username password" + exit 1 +fi + +CSV_FILE="$1" +DOMAIN="$2" +USERNAME="$3" +PASSWORD="$4" + +BASE_URL="https://$DOMAIN/openmrs/ws/rest/v1" + +# Encode the username and password in base64 +AUTH=$(echo -n "$USERNAME:$PASSWORD" | base64) + +# Function to fetch the attributeType UUID +fetch_attribute_type_uuid() { + local url="$BASE_URL/conceptattributetype?q=saleable&limit=1" + local response + local uuid + + response=$(curl -s -X GET "$url" \ + -H "Authorization: Basic $AUTH" \ + -H "Accept: application/json") + + # Parse JSON response to extract UUID + uuid=$(echo "$response" | awk -F '[,:}]' '{for(i=1;i<=NF;i++){if($i~/uuid/){print $(i+1)}}}' | tr -d '"') + + echo "$uuid" +} + +# Function to check if the attribute exists +check_attribute() { + local uuid="$1" + local url="$BASE_URL/concept/$uuid/attribute" + local response + + response=$(curl -s -X GET "$url" \ + -H "Authorization: Basic $AUTH" \ + -H "Accept: application/json") + + # Check if the attribute already exists + if echo "$response" | grep -q "$ATTRIBUTETYPE_UUID"; then + return 0 # Attribute exists + else + return 1 # Attribute does not exist + fi +} + +# Function to make the POST request +make_post_request() { + local uuid="$1" + local url="$BASE_URL/concept/$uuid/attribute" + local payload + + payload=$(cat <