Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
- Config, docs, scripts, and backup manifests - Submodule refs unchanged (m = modified content in submodules) Made-with: Cursor
118 lines
3.9 KiB
Bash
118 lines
3.9 KiB
Bash
#!/usr/bin/env bash
|
||
# OMNL Fineract — Create clients 9–15 (FIDIS, Alpha Omega Holdings, …) via POST /clients.
|
||
# Idempotent: skips if a client with the same externalId already exists.
|
||
# Usage: run from repo root; sources omnl-fineract/.env or .env.
|
||
# ENTITY_DATA=<path> JSON entity data (default: docs/04-configuration/mifos-omnl-central-bank/OMNL_ENTITY_MASTER_DATA.json)
|
||
# DRY_RUN=1 print payloads only, do not POST.
|
||
# Requires: curl, jq.
|
||
|
||
set -euo pipefail
|
||
REPO_ROOT="${REPO_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
|
||
DRY_RUN="${DRY_RUN:-0}"
|
||
ENTITY_DATA="${ENTITY_DATA:-${REPO_ROOT}/docs/04-configuration/mifos-omnl-central-bank/OMNL_ENTITY_MASTER_DATA.json}"
|
||
|
||
if [ ! -f "$ENTITY_DATA" ]; then
|
||
echo "Entity data file not found: $ENTITY_DATA" >&2
|
||
exit 1
|
||
fi
|
||
|
||
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:-}"
|
||
OFFICE_ID="${OFFICE_ID:-1}"
|
||
# legalFormId from GET /clients/template: 1=Person, 2=Entity
|
||
LEGAL_FORM_ID="${LEGAL_FORM_ID:-2}"
|
||
|
||
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}")
|
||
SUBMITTED_DATE="${SUBMITTED_DATE:-$(date +%Y-%m-%d)}"
|
||
|
||
# Return client id if a client with this externalId exists, else empty
|
||
get_client_id_by_external_id() {
|
||
local ext_id="$1"
|
||
local clients_json="$2"
|
||
if echo "$clients_json" | jq -e '.pageItems' >/dev/null 2>&1; then
|
||
echo "$clients_json" | jq -r --arg e "$ext_id" '.pageItems[] | select(.externalId == $e) | .id'
|
||
else
|
||
echo "$clients_json" | jq -r --arg e "$ext_id" '.[] | select(.externalId == $e) | .id'
|
||
fi
|
||
}
|
||
|
||
clients_json=$(curl "${CURL_OPTS[@]}" "${BASE_URL}/clients")
|
||
if ! echo "$clients_json" | jq -e '.pageItems // .' >/dev/null 2>&1; then
|
||
echo "Unexpected clients response." >&2
|
||
exit 1
|
||
fi
|
||
|
||
created=0
|
||
skipped=0
|
||
for client_num in 9 10 11 12 13 14 15; do
|
||
entity=$(jq -c ".entities[] | select(.clientNumber == $client_num)" "$ENTITY_DATA")
|
||
if [ -z "$entity" ]; then
|
||
echo "Skip: no entity with clientNumber=$client_num in $ENTITY_DATA" >&2
|
||
continue
|
||
fi
|
||
entity_name=$(echo "$entity" | jq -r '.entityName')
|
||
account_no=$(echo "$entity" | jq -r '.accountNo')
|
||
ext_id="OMNL-${client_num}"
|
||
|
||
existing=$(get_client_id_by_external_id "$ext_id" "$clients_json")
|
||
if [ -n "$existing" ] && [ "$existing" != "null" ]; then
|
||
echo "Skip client $client_num: already exists (externalId=$ext_id, id=$existing)" >&2
|
||
((skipped++)) || true
|
||
continue
|
||
fi
|
||
|
||
payload=$(jq -n \
|
||
--argjson officeId "$OFFICE_ID" \
|
||
--argjson legalFormId "$LEGAL_FORM_ID" \
|
||
--arg firstname "$entity_name" \
|
||
--arg externalId "$ext_id" \
|
||
--arg submittedOnDate "$SUBMITTED_DATE" \
|
||
'{
|
||
officeId: $officeId,
|
||
legalFormId: $legalFormId,
|
||
firstname: $firstname,
|
||
lastname: ".",
|
||
externalId: $externalId,
|
||
dateFormat: "yyyy-MM-dd",
|
||
locale: "en",
|
||
active: false,
|
||
submittedOnDate: $submittedOnDate
|
||
}')
|
||
|
||
echo "Create client $client_num: $entity_name (externalId=$ext_id)" >&2
|
||
|
||
if [ "$DRY_RUN" = "1" ]; then
|
||
echo " [DRY RUN] POST clients $payload" >&2
|
||
((created++)) || true
|
||
continue
|
||
fi
|
||
|
||
res=$(curl "${CURL_OPTS[@]}" -X POST -d "$payload" "${BASE_URL}/clients" 2>/dev/null) || true
|
||
if echo "$res" | jq -e '.resourceId // .clientId' >/dev/null 2>&1; then
|
||
new_id=$(echo "$res" | jq -r '.resourceId // .clientId')
|
||
echo " Created clientId=$new_id" >&2
|
||
((created++)) || true
|
||
else
|
||
echo " Failed: $res" >&2
|
||
fi
|
||
done
|
||
|
||
echo "Done: $created created, $skipped skipped (already existed)." >&2
|