Files
proxmox/scripts/omnl/omnl-client-names-fix.sh

122 lines
3.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# OMNL Fineract — Set client firstname/lastname to canonical operating-entity names.
# Usage: run from repo root; sources omnl-fineract/.env or .env.
# DRY_RUN=1 print payloads only, do not PUT.
# Requires: curl, jq.
set -euo pipefail
REPO_ROOT="${REPO_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
DRY_RUN="${DRY_RUN:-0}"
# Canonical names: Client 1 = Head Office, Client 215 = operating entities (by account no 000000001000000015)
declare -A CLIENT_NAMES=(
[1]="OMNL Head Office (DBIS) Central Bank"
[2]="Shamrayan Enterprises"
[3]="HYBX"
[4]="TAJ Private Single Family Office"
[5]="Aseret Mortgage Bank"
[6]="Mann Li Family Offices"
[7]="Sovereign Order of Malta OSJ"
[8]="Alltra Mainnet"
[9]="FIDIS"
[10]="Alpha Omega Holdings"
[11]="SGI Capital"
[12]="Titan Financial"
[13]="Roy Walker PLLC"
[14]="SGI Partners LLC"
[15]="Tsunami Holdings AG"
)
if [ -f "${REPO_ROOT}/omnl-fineract/.env" ]; then
set +u
source "${REPO_ROOT}/omnl-fineract/.env" 2>/dev/null || true
set -u
elif [ -f "${REPO_ROOT}/.env" ]; then
set +u
source "${REPO_ROOT}/.env" 2>/dev/null || true
set -u
fi
BASE_URL="${OMNL_FINERACT_BASE_URL:-}"
TENANT="${OMNL_FINERACT_TENANT:-omnl}"
USER="${OMNL_FINERACT_USER:-app.omnl}"
PASS="${OMNL_FINERACT_PASSWORD:-}"
if [ -z "$BASE_URL" ] || [ -z "$PASS" ]; then
echo "Set OMNL_FINERACT_BASE_URL and OMNL_FINERACT_PASSWORD (e.g. in omnl-fineract/.env)" >&2
exit 1
fi
CURL_OPTS=(-s -S -H "Fineract-Platform-TenantId: ${TENANT}" -H "Content-Type: application/json" -u "${USER}:${PASS}")
clients_json=$(curl "${CURL_OPTS[@]}" "${BASE_URL}/clients")
if ! echo "$clients_json" | jq -e '.pageItems // .' >/dev/null 2>&1; then
echo "Unexpected clients response (no pageItems or array)." >&2
echo "$clients_json" | head -c 500
exit 1
fi
# Normalize: Fineract may return { pageItems: [...] } or direct array
if echo "$clients_json" | jq -e '.pageItems' >/dev/null 2>&1; then
items=$(echo "$clients_json" | jq -c '.pageItems[]')
else
items=$(echo "$clients_json" | jq -c '.[]')
fi
updated=0
skipped=0
while IFS= read -r row; do
[ -z "$row" ] && continue
id=$(echo "$row" | jq -r '.id')
account_no=$(echo "$row" | jq -r '.accountNo // ""')
ext_id=$(echo "$row" | jq -r '.externalId // ""')
first=$(echo "$row" | jq -r '.firstname // ""')
last=$(echo "$row" | jq -r '.lastname // ""')
display=$(echo "$row" | jq -r '.displayName // ""')
# Resolve client number from accountNo (000000001 -> 1, etc.)
client_num=""
if [ -n "$account_no" ]; then
client_num=$(echo "$account_no" | sed 's/^0*//')
[ -z "$client_num" ] && client_num="0"
fi
display_name="${CLIENT_NAMES[$client_num]:-}"
# Skip if no canonical name for this client number
if [ -z "$display_name" ]; then
echo "Skip clientId=$id accountNo=$account_no (no canonical name for Client $client_num)" >&2
((skipped++)) || true
continue
fi
# Idempotent: skip if already set to this exact name
if [ -n "$display" ] && [ "$display" = "$display_name" ]; then
echo "Skip clientId=$id (already set: $display)" >&2
((skipped++)) || true
continue
fi
# Set full name in firstname; lastname must be non-blank (tenant validation)
firstname="$display_name"
lastname="."
payload=$(jq -n --arg f "$firstname" --arg l "$lastname" '{ firstname: $f, lastname: $l }')
echo "Client id=$id accountNo=$account_no (Client $client_num) -> $display_name" >&2
if [ "$DRY_RUN" = "1" ]; then
echo " [DRY RUN] PUT ${BASE_URL}/clients/${id} $payload" >&2
((updated++)) || true
continue
fi
res=$(curl "${CURL_OPTS[@]}" -X PUT -d "$payload" "${BASE_URL}/clients/${id}" 2>/dev/null) || true
if echo "$res" | jq -e '.resourceId // .clientId' >/dev/null 2>&1; then
echo " Updated clientId=$id" >&2
((updated++)) || true
else
echo " Failed clientId=$id: $res" >&2
fi
done <<< "$items"
echo "Done: $updated updated, $skipped skipped (already had name)." >&2