← back to Dw Vendor Microsites

lib/resolve-vendor.sh

108 lines

#!/usr/bin/env bash
# resolve-vendor.sh <vendor_code>
#
# Step-0 private-label / exclusion gate. Resolves the customer-facing house_name +
# subdomain from vendor_registry, decides samples-only policy, and emits the set of
# BANNED upstream names that must NEVER appear in the JSONL/branding/subdomain.
#
# Reads dw_unified READ-ONLY over `ssh my-server 'sudo -u postgres psql ...'`.
# Prints a JSON object to stdout. Exits 3 + status=EXCLUDED for Nicolette Mayer.
#
# Output JSON keys:
#   vendor_code, vendor_name, catalog_table, is_private_label, house_name,
#   subdomain, samples_only, banned_names[], status (OK|EXCLUDED)
set -euo pipefail

VENDOR="${1:?usage: resolve-vendor.sh <vendor_code>}"

# Run remote psql by piping SQL over stdin to a SINGLE-string ssh command, so the
# remote shell receives the full `psql -tA` invocation intact and the SQL never
# gets re-split by ssh's arg-concatenation (parens/quotes survive). Standing memo:
# over `ssh my-server` the OS user is root -> must `sudo -u postgres psql`.
remote_psql() { ssh my-server "sudo -u postgres psql -d dw_unified -tA" ; }

# pull the registry row as JSON (lower-case match on vendor_code)
SQL="select row_to_json(t) from (select vendor_code, vendor_name, catalog_table, is_private_label, private_label_name, parent_brand, display_prices, do_not_price, sample_price from vendor_registry where lower(vendor_code)=lower('${VENDOR}') limit 1) t;"
ROW="$(printf '%s\n' "$SQL" | remote_psql 2>/dev/null || true)"

if [ -z "$ROW" ] || [ "$ROW" = "" ]; then
  echo "{\"vendor_code\":\"${VENDOR}\",\"status\":\"NOT_FOUND\"}"
  exit 4
fi

# resolve with python for safe JSON handling
python3 - "$ROW" "$VENDOR" <<'PY'
import sys, json, re
row = json.loads(sys.argv[1]); vcode = sys.argv[2]

vname    = (row.get('vendor_name') or '').strip()
ctable   = (row.get('catalog_table') or (vcode + '_catalog')).strip()
is_pl    = bool(row.get('is_private_label'))
pl_name  = (row.get('private_label_name') or '').strip()
parent   = (row.get('parent_brand') or '').strip()
disp     = row.get('display_prices')
do_not_p = bool(row.get('do_not_price'))

# --- EXCLUSION: Nicolette Mayer must stay fully off ---
if re.sub(r'[^a-z]', '', vcode.lower()) == 'nicolettemayer' or 'nicolette mayer' in vname.lower():
    print(json.dumps({"vendor_code": vcode, "vendor_name": vname,
                      "catalog_table": ctable, "status": "EXCLUDED",
                      "reason": "Nicolette Mayer — archived, must stay off"}))
    sys.exit(3)

# --- house name: private_label_name wins, else real vendor name ---
house = pl_name if (is_pl and pl_name) else vname

# --- subdomain: slug of house name ---
sub = re.sub(r'[^a-z0-9]+', '-', house.lower()).strip('-')

# --- banned upstream names (only when private-label) ---
banned = []
if is_pl:
    # the upstream/real names that must never leak to a customer
    for nm in [vname, parent]:
        nm = (nm or '').strip()
        if nm and nm.lower() != house.lower():
            banned.append(nm)
    # vendor_code as a slug-word is also a leak vector
    # known multi-brand parents per private-label
    EXTRA = {
        'malibu wallpaper': ['Brewster', 'York', 'Chesapeake', 'Seabrook', 'NextWall', 'WallQuest'],
        'phillipe romano': ['Relativity Textiles', 'Justin David', 'TWIL', 'Command54', 'York', 'Brewster'],
        'hollywood': ['Command54'],
    }
    for k, v in EXTRA.items():
        if house.lower() == k:
            banned += v
    # dedupe, preserve order, drop empties + the house itself
    seen=set(); out=[]
    for b in banned:
        bl=b.strip()
        if bl and bl.lower()!=house.lower() and bl.lower() not in seen:
            seen.add(bl.lower()); out.append(bl)
    banned = out

# --- samples-only policy ---
# do_not_price always forces samples-only. display_prices=false also implies
# samples-only. Default for this off-Shopify fleet is samples-only=TRUE.
samples_only = True
if do_not_p:
    samples_only = True
elif disp is True:
    # display_prices True means prices MAY show, but the off-Shopify model is
    # still samples-only (no checkout). Keep samples-only True by policy.
    samples_only = True

print(json.dumps({
    "vendor_code": vcode,
    "vendor_name": vname,
    "catalog_table": ctable,
    "is_private_label": is_pl,
    "house_name": house,
    "subdomain": sub,
    "samples_only": samples_only,
    "banned_names": banned,
    "status": "OK",
}))
PY