#!/usr/bin/env bash # ===================================================================== # rename-consignees.sh — re-anonymise existing demo consignees IN PLACE: partner i of OLD.json # (matched by name in the merchant org) gets name/e-mail/address of entry i of NEW.json, so orders, # shipments, invoices and returns that reference the partner show the new identity everywhere. # Direct iDempiere REST (c_bpartner, c_bpartner_location, c_location) with a user token. # # ADMIN_USER=demo ADMIN_PASS=… MERCHANT_SHORT=kazaar ./rename-consignees.sh OLD.json NEW.json [count] # ===================================================================== set -euo pipefail OLD="${1:?old json}"; NEW="${2:?new json}"; CNT="${3:-$(jq length "$OLD")}" ADMIN_USER="${ADMIN_USER:?}"; ADMIN_PASS="${ADMIN_PASS:?}"; MERCHANT_SHORT="${MERCHANT_SHORT:?}" R="${REST:-https://127.0.0.1:8443/api/v1}"; CLIENT_ID="${CLIENT_ID:-1000000}"; ROLE_ID="${ROLE_ID:-1000000}" T="$(curl -sk -X POST "$R/auth/tokens" -H 'Content-Type: application/json' -d "$(jq -n --arg u "$ADMIN_USER" --arg p "$ADMIN_PASS" --argjson c "$CLIENT_ID" --argjson r "$ROLE_ID" '{userName:$u,password:$p,parameters:{clientId:$c,roleId:$r,organizationId:0}}')" | jq -r '.token // empty')" [ -n "$T" ] || { echo "token failed" >&2; exit 1; } get() { curl -sk "$R/$1" -H "Authorization: Bearer $T"; } put() { curl -sk -X PUT "$R/$1" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d "$2"; } enc() { jq -rn --arg s "$1" '$s|@uri'; } MORG="$(get "models/ad_org?\$filter=$(enc "Value eq '$MERCHANT_SHORT'")&\$top=1" | jq -r '.records[0].id')" done_=0; miss=0 for i in $(seq 0 $((CNT-1))); do oname="$(jq -r ".[$i].name" "$OLD")"; new="$(jq -c ".[$i]" "$NEW")"; nname="$(echo "$new" | jq -r .name)" [ "$oname" = "$nname" ] && continue bp="$(get "models/c_bpartner?\$filter=$(enc "AD_Org_ID eq $MORG and Name eq '${oname//\'/\'\'}'")&\$top=1" | jq -r '.records[0].id // empty')" [ -n "$bp" ] || { miss=$((miss+1)); continue; } put "models/c_bpartner/$bp" "$(echo "$new" | jq -c '{name:.name, EMail:.email, tableName:"C_BPartner"}')" >/dev/null for bl in $(get "models/c_bpartner_location?\$filter=$(enc "C_BPartner_ID eq $bp")&\$top=5" | jq -r '.records[] | "\(.id)|\(.C_Location_ID.id)"'); do put "models/c_bpartner_location/${bl%%|*}" "$(echo "$new" | jq -c '{name:.name, tableName:"C_BPartner_Location"}')" >/dev/null put "models/c_location/${bl##*|}" "$(echo "$new" | jq -c '{address1:.street, postal:.postal, city:.city, C_Country_ID:{id:.countryId, tableName:"C_Country"}, tableName:"C_Location"}')" >/dev/null done done_=$((done_+1)); [ $((done_ % 20)) = 0 ] && echo " $done_ renamed" done echo "renamed $done_ partners, $miss not found"