Skip to content

Commit

Permalink
BAH-4020 | Add. Script for bulk update of saleable attributes for con…
Browse files Browse the repository at this point in the history
…cept (#31)
  • Loading branch information
mohan-13 authored Jul 19, 2024
1 parent 418fe08 commit 72669b6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
19 changes: 18 additions & 1 deletion metadataScripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ To use this script:
POSTGRES_DB="clinlims" \
POSTGRES_USER="clinlims" \
./copy-result-range.sh
```
```

## 📦 [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```
91 changes: 91 additions & 0 deletions metadataScripts/openmrs/set_saleable_attribute.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
{
"attributeType": "$ATTRIBUTETYPE_UUID",
"value": "true"
}
EOF
)

curl -X POST "$url" \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d "$payload"

echo "POST request sent for UUID: $uuid"
}

# Fetch the attributeType UUID
ATTRIBUTETYPE_UUID=$(fetch_attribute_type_uuid)
echo "Fetched attributeType UUID: $ATTRIBUTETYPE_UUID"

# Read the CSV file line by line, skipping the header row
tail -n +2 "$CSV_FILE" | while IFS=, read -r uuid _; do
echo "Processing UUID: $uuid"

if check_attribute "$uuid"; then
echo "Attribute with UUID $ATTRIBUTETYPE_UUID already exists for concept $uuid. Skipping POST request."
else
echo "Attribute with UUID $ATTRIBUTETYPE_UUID not found for concept $uuid. Making POST request."
make_post_request "$uuid"
fi
done

echo "All requests have been processed."

0 comments on commit 72669b6

Please sign in to comment.