diff --git a/scripts/bms-tickets.sh b/scripts/bms-tickets.sh index 1f0a4da..5202504 100644 --- a/scripts/bms-tickets.sh +++ b/scripts/bms-tickets.sh @@ -145,7 +145,7 @@ prompt_if_empty() { } cmd_create() { - local title="" details="" account_id="" location_id="" contact_id="" + local title="" details="" account_id="" account_name="" location_id="" location_name="" contact_id="" local status_id="" priority_id="" type_id="" source_id="" local assignee_id="" queue_id="" due_date="" open_date="" local template_id="" @@ -158,7 +158,9 @@ cmd_create() { --title) title="$2"; shift 2 ;; --details|--description) details="$2"; shift 2 ;; --account-id) account_id="$2"; shift 2 ;; + --account-name) account_name="$2"; shift 2 ;; --location-id) location_id="$2"; shift 2 ;; + --location-name) location_name="$2"; shift 2 ;; --contact-id) contact_id="$2"; shift 2 ;; --status-id) status_id="$2"; shift 2 ;; --priority-id) priority_id="$2"; shift 2 ;; @@ -224,6 +226,21 @@ cmd_create() { fi fi + # ── Resolve account name to ID ───────────────────────────────────────────── + if [[ -n "$account_name" && -z "$account_id" ]]; then + account_search=$(bms_curl POST "/v2/servicedesk/accounts/search" -d "{\"Filter\":{\"AccountName\":\"$account_name\"},\"PageNumber\":1,\"PageSize\":1}") + account_id=$(echo "$account_search" | jq -r '.Data[0].Id // .result[0].id // empty') + [[ -n "$account_id" ]] || die "Account not found: $account_name" + fi + + # ── Resolve location name to ID (scoped to account) ─────────────────────── + if [[ -n "$location_name" && -z "$location_id" ]]; then + [[ -n "$account_id" ]] || die "Must specify --account-id or --account-name before using --location-name" + loc_search=$(bms_curl GET "/v2/crm/accounts/${account_id}/locations/lookup") + location_id=$(echo "$loc_search" | jq -r '.result[]? | select(.Name=="$location_name") | .Id // empty') + [[ -n "$location_id" ]] || die "Location not found: $location_name (account: ${account_id:-unknown})" + fi + [[ -n "$title" ]] || die "Missing required field: --title" [[ -n "$details" ]] || die "Missing required field: --details" [[ -n "$account_id" ]] || die "Missing required field: --account-id" @@ -280,6 +297,8 @@ cmd_create() { args_json=$(jq -n \ --arg title "$title" \ --arg details "$details" \ + --arg account_name "${account_name:-}" \ + --arg location_name "${location_name:-}" \ --argjson account_id "$account_id" \ --argjson location_id "$location_id" \ --argjson status_id "$status_id" \ @@ -288,7 +307,7 @@ cmd_create() { --argjson source_id "$source_id" \ --argjson queue_id "${queue_id:-null}" \ --argjson assignee_id "${assignee_id:-null}" \ - '{title: $title, details: $details, account_id: $account_id, location_id: $location_id, status_id: $status_id, priority_id: $priority_id, type_id: $type_id, source_id: $source_id, queue_id: $queue_id, assignee_id: $assignee_id}') + '{title: $title, details: $details, account_name: $account_name, location_name: $location_name, account_id: $account_id, location_id: $location_id, status_id: $status_id, priority_id: $priority_id, type_id: $type_id, source_id: $source_id, queue_id: $queue_id, assignee_id: $assignee_id}') result_json=$(jq -n '{error: "creation_failed", response: ("$response" | fromjson? // "$response")}') log_action "tickets.create" "$args_json" "$result_json" "error" exit 1 @@ -300,6 +319,8 @@ cmd_create() { args_json=$(jq -n \ --arg title "$title" \ --arg details "$details" \ + --arg account_name "${account_name:-}" \ + --arg location_name "${location_name:-}" \ --argjson account_id "$account_id" \ --argjson location_id "$location_id" \ --argjson status_id "$status_id" \ @@ -308,7 +329,7 @@ cmd_create() { --argjson source_id "$source_id" \ --argjson queue_id "${queue_id:-null}" \ --argjson assignee_id "${assignee_id:-null}" \ - '{title: $title, details: $details, account_id: $account_id, location_id: $location_id, status_id: $status_id, priority_id: $priority_id, type_id: $type_id, source_id: $source_id, queue_id: $queue_id, assignee_id: $assignee_id}') + '{title: $title, details: $details, account_name: $account_name, location_name: $location_name, account_id: $account_id, location_id: $location_id, status_id: $status_id, priority_id: $priority_id, type_id: $type_id, source_id: $source_id, queue_id: $queue_id, assignee_id: $assignee_id}') result_json=$(jq -n --argjson tid "$ticket_id" --arg tn "${ticket_number:-}" '{ticket_id: $tid, ticket_number: $tn}') log_action "tickets.create" "$args_json" "$result_json" "success"