← back to Fineartamerica Price Sync

scripts/monthly-sync.sh

54 lines

#!/bin/bash
# Monthly FAA price sync — refresh live FAA costs, detect drift, and (by default)
# DRAFT the reprice to pending-approval so Steve approves before any live write.
# FAA raises base/material prices over time; this catches it monthly so DW never
# silently sells a print below fulfillment cost again.
#
# Propose-only by default (money = gated). To make it fully automatic (re-apply
# on drift with no human step), set AUTO_APPLY=1 in the launchd plist env.
set -euo pipefail
cd "$(dirname "$0")/.."
LOG="data/monthly-$(date +%Y%m%d).log"
QUEUE="$HOME/.claude/yolo-queue/pending-approval"
mkdir -p "$QUEUE"

{
  echo "== FAA monthly sync $(date) =="
  # 1. refresh costs (free HTTP) — standard sizes then odd-aspect gap fill
  node bin/faa-fetch-all.js || true
  node bin/faa-fill-gaps.js || true
  # purge any invalid (<$50) costs so a bad fetch never masquerades as cost
  node -e "const fs=require('fs'),p='data/cost-cache.json',c=require('./'+p);for(const k of Object.keys(c))if(!(c[k]>=50))delete c[k];fs.writeFileSync(p,JSON.stringify(c,null,2));"

  # 2. dry-run to see what drifted
  node sync.js --all --round dollar | tee "$LOG.dry"
  N=$(grep -c "REPRICE" "$LOG.dry" || true)
  echo "drifted/needs-reprice: $N"

  if [ "${AUTO_APPLY:-0}" = "1" ]; then
    echo "AUTO_APPLY=1 → applying live"
    APPLY_CONFIRM=YES node sync.js --all --apply --round dollar
  elif [ "$N" -gt 0 ]; then
    # propose-only: draft an approval memo
    MEMO="$QUEUE/fineartamerica-monthly-reprice-$(date +%Y%m%d).md"
    {
      echo "# APPROVAL — FAA monthly reprice ($N variants drifted)"
      echo ""
      echo "Date: $(date). FAA costs refreshed; $N variants now off-target (below cost or margin)."
      echo "Full table: ~/Projects/fineartamerica-price-sync/data/dry-run.csv"
      echo ""
      echo "## To apply on APPROVE"
      echo '```'
      echo "cd ~/Projects/fineartamerica-price-sync"
      echo "APPLY_CONFIRM=YES node sync.js --all --apply --round dollar"
      echo '```'
      echo ""
      echo "APPROVE / REVISE / BLOCK: ______"
    } > "$MEMO"
    echo "drafted approval memo: $MEMO"
  else
    echo "no drift — nothing to do."
  fi
  echo "== done $(date) =="
} >> "$LOG" 2>&1