#!/usr/bin/env bash
# Build: low-rating service recovery
# When a survey comes back low, a task lands on a manager immediately rather than being found in a report next month. Service recovery has a short half-life.
#
# Category : Operations
# Safety   : WRITES DATA, read before running
# Uses     : workflows create, task-types list, agents list
# Returns  : A workflow on survey.rating_low that opens a task the same day.
#
# Requires the SendSquared CLI and jq:
#   curl -fsSL https://cli.sendsquared.com/install.sh | bash
#   sendsquared auth login
#
# Free to use and adapt. https://sendsquared.com/cli/scripts/#build-low-rating-rescue

# Build low-rating service recovery. Run once.
# Usage: ./build-low-rating-rescue.sh <taskTypeId> [followerUserIds]
set -euo pipefail
TASK_TYPE=${1:?pass a task type id, see: sendsquared task-types list}
FOLLOWERS=${2:-}

STEPS=$(cat <<JSON
[
  { "type": "Task",
    "value": $TASK_TYPE,
    "label": "Low rating: call this guest",
    "dueIn": "4h",
    "priority": 1${FOLLOWERS:+,
    "followers": [$FOLLOWERS]}
  }
]
JSON
)

sendsquared workflows create \
  --name "Low rating rescue" \
  --description "Opens an urgent callback task when a survey rating comes back low." \
  --trigger-type survey \
  --trigger-value survey.rating_low \
  --steps "$STEPS"
