#!/usr/bin/env bash
# Build: lead follow-up automation
# Replaces the overdue follow-up cron with a native automation on lead.followup_at, so the reminder fires on the date the rep set rather than whenever a script last ran.
#
# Category : Revenue
# Safety   : WRITES DATA, read before running
# Uses     : workflows create, task-types list
# Returns  : A workflow that opens a follow-up task on the lead follow-up date.
#
# 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-lead-followup

# Build the lead follow-up automation. Run once.
# Usage: ./build-lead-followup.sh <taskTypeId>
set -euo pipefail
TASK_TYPE=${1:?pass a task type id, see: sendsquared task-types list}

STEPS=$(cat <<JSON
[
  { "type": "Task", "value": $TASK_TYPE, "label": "Follow up on this lead", "dueIn": "2h", "priority": 2 }
]
JSON
)

sendsquared workflows create \
  --name "Lead follow-up due" \
  --description "Opens a task when a lead reaches its follow-up date." \
  --trigger-type time_based \
  --trigger-value lead.followup_at \
  --sync-interval 1h \
  --steps "$STEPS"
