|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Reject ALL pending (pre-issuance) CERTInext orders — to reclaim credits / declutter the |
| 3 | +# sandbox. Targets certificateStatusId in {2,24} ("Pending for Approver"). NEVER touches |
| 4 | +# issued certs (9 "Certificate Downloaded") or already-rejected orders (13). |
| 5 | +# |
| 6 | +# Safety: dry-run by default (lists what it WOULD reject). Set REJECT_ALL_PENDING=1 to fire. |
| 7 | +# Optional: PAGE_SIZE (default 100), REMARKS. |
| 8 | +set -euo pipefail |
| 9 | +. ~/.env_certinext |
| 10 | +. "$(dirname "$0")/lib/certinext-auth.sh" |
| 11 | + |
| 12 | +DRY=1; [ "${REJECT_ALL_PENDING:-}" = "1" ] && DRY=0 |
| 13 | +PAGE_SIZE="${PAGE_SIZE:-100}" |
| 14 | +REMARKS="${REMARKS:-Cancelled pending order to reclaim sandbox credits.}" |
| 15 | + |
| 16 | +report_page() { # $1 = page number |
| 17 | + read -r ts txn authKey <<< "$(certinext_meta)" |
| 18 | + curl -s -X POST "$CERTINEXT_API_URL/GetOrderReport" -H "Content-Type: application/json" \ |
| 19 | + -d "{\"meta\":{\"ver\":\"1.0\",\"ts\":\"$ts\",\"txn\":\"$txn\",\"accountNumber\":\"$CERTINEXT_ACCOUNT_NUMBER\",\"authKey\":\"$authKey\"},\"searchCriteria\":{\"groupNumber\":\"$CERTINEXT_GROUP_NUMBER\",\"pageNumber\":\"$1\",\"pageSize\":\"$PAGE_SIZE\"}}" |
| 20 | +} |
| 21 | + |
| 22 | +# --- Snapshot all pending order numbers up front (before rejecting anything) --- |
| 23 | +first=$(report_page 1) |
| 24 | +pages=$(echo "$first" | jq -r '.orderDetails.noOfPages // 1') |
| 25 | +pending=$(echo "$first" | jq -r '.orderDetails.ordersArray[] | select(.certificateStatusId=="24" or .certificateStatusId=="2") | .orderNumber') |
| 26 | +p=2 |
| 27 | +while [ "$p" -le "$pages" ]; do |
| 28 | + more=$(report_page "$p" | jq -r '.orderDetails.ordersArray[] | select(.certificateStatusId=="24" or .certificateStatusId=="2") | .orderNumber') |
| 29 | + [ -n "$more" ] && pending="$pending"$'\n'"$more" |
| 30 | + p=$((p+1)) |
| 31 | +done |
| 32 | +pending=$(echo "$pending" | sed '/^$/d') |
| 33 | + |
| 34 | +count=$(echo "$pending" | grep -c . || true) |
| 35 | +echo "Found $count pending order(s) (certificateStatusId 2/24) across $pages page(s)." |
| 36 | + |
| 37 | +if [ "$DRY" = "1" ]; then |
| 38 | + echo "DRY RUN — set REJECT_ALL_PENDING=1 to reject. First 10:" |
| 39 | + echo "$pending" | head -10 | sed 's/^/ /' |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | + |
| 43 | +ok=0; fail=0 |
| 44 | +while IFS= read -r n; do |
| 45 | + [ -z "$n" ] && continue |
| 46 | + read -r ts txn authKey <<< "$(certinext_meta)" |
| 47 | + st=$(curl -s -X POST "$CERTINEXT_API_URL/RejectOrder" -H "Content-Type: application/json" \ |
| 48 | + -d "{\"meta\":{\"ver\":\"1.0\",\"ts\":\"$ts\",\"txn\":\"$txn\",\"accountNumber\":\"$CERTINEXT_ACCOUNT_NUMBER\",\"authKey\":\"$authKey\"},\"orderDetails\":{\"orderNumber\":\"$n\",\"rejectRemarks\":\"$REMARKS\"}}" \ |
| 49 | + | jq -r '.meta.status // "?"') |
| 50 | + if [ "$st" = "1" ]; then ok=$((ok+1)); else fail=$((fail+1)); echo " FAIL $n (status=$st)"; fi |
| 51 | +done <<< "$pending" |
| 52 | + |
| 53 | +echo "Done. Rejected ok=$ok fail=$fail (of $count)." |
0 commit comments