#!/usr/bin/env bash # The supplier order list: a code per size, needs built from reorder levels and split by supplier, # raised as one placed order per supplier (and one per person for a counter "order in"), the A4 # sheet with the codes, the supplier email door, and the forecast arithmetic behind "Suggested". # # Needs a server in dev mode, like the other suites (`npm run dev -- -p 3111`). Nothing sends mail. set -u B=${BASE:-http://127.0.0.1:3111} . "$(dirname "$0")/e2e-preflight.sh"; e2e_preflight "$B" T=${TMP:-/tmp}; J="$T/tc-ord-cj.txt"; rm -f "$J" PASS=0; FAIL=0 ok() { PASS=$((PASS+1)); echo " ✓ $1"; } fail() { FAIL=$((FAIL+1)); echo " ✗ $1 :: $(echo "$2" | head -c 300)"; } check(){ local name=$1 out=$2 pat=$3; if echo "$out" | grep -q "$pat"; then ok "$name"; else fail "$name" "$out"; fi; } no() { local name=$1 out=$2 pat=$3; if echo "$out" | grep -q "$pat"; then fail "$name" "$out"; else ok "$name"; fi; } mut() { curl -s -b "$J" -c "$J" -X POST "$B/api/mutate" -H 'content-type: application/json' -d "{\"op\":\"$1\",\"payload\":$2}"; } py() { python3 -c "import sys,json; d=json.load(sys.stdin); $1"; } bk() { curl -s -b "$J" "$B/api/backup"; } res() { py "print(d['result']$1)"; } page() { curl -s -b "$J" "$B$1"; } code() { curl -s -o /dev/null -w '%{http_code}' -b "$J" "$B$1"; } TS=$(date +%s) echo "== a facility with two suppliers" check "signup" "$(curl -s -c "$J" -X POST "$B/api/auth/signup" -H 'content-type: application/json' -H "x-forwarded-for: 10.12.$((RANDOM%250)).$((RANDOM%250))" -d "{\"first\":\"Ord\",\"last\":\"Admin\",\"facility\":\"Order Hospital $TS\",\"email\":\"ord$TS@example.com\",\"password\":\"password123\"}")" '"ok":true' check " groups" "$(e2e_groups "$B" "$J")" '"ok":true' R=$(mut catalog.add '{"item":"Scrub Top","type":"Top","supplier":"Northline Workwear","sku":"ST","cost":22.5,"sizes":["S","M","L"]}'); ST=$(echo "$R" | res "['id']"); check "garment A (Northline)" "$R" '"id"' R=$(mut catalog.add '{"item":"Fleece Jacket","type":"Jacket","supplier":"Harbour Embroidery","sku":"FJ","cost":48,"sizes":["M","L"]}'); FJ=$(echo "$R" | res "['id']"); check "garment B (Harbour)" "$R" '"id"' check "both suppliers exist" "$(bk | py "print(sorted(s['name'] for s in d['suppliers']))")" "\['Harbour Embroidery', 'Northline Workwear'\]" SUPS=$(bk | py "print(json.dumps({s['name']: s['id'] for s in d['suppliers']}))") WWG=$(echo "$SUPS" | py "print(d['Northline Workwear'])"); QUA=$(echo "$SUPS" | py "print(d['Harbour Embroidery'])") echo "== supplier codes, one per size" check "code on Scrub Top M" "$(mut stock.supplierCode "{\"itemId\":\"$ST\",\"si\":1,\"code\":\"NW-10422-M\"}")" '"ok":true' check "code on Scrub Top L" "$(mut stock.supplierCode "{\"itemId\":\"$ST\",\"si\":2,\"code\":\"NW-10422-L\"}")" '"ok":true' check "code on Fleece M" "$(mut stock.supplierCode "{\"itemId\":\"$FJ\",\"si\":0,\"code\":\"HE-7781-M\"}")" '"ok":true' check "a bad size is refused" "$(mut stock.supplierCode "{\"itemId\":\"$ST\",\"si\":9,\"code\":\"X\"}")" 'Invalid size' check "the code lands in the backup" "$(bk | py "print([x['supplierCode'] for x in d['stock'] if x['itemId']=='$ST' and x['sizeIndex']==1][0])")" 'NW-10422-M' check "the garment page shows it" "$(page "/app/stock/$ST")" 'NW-10422-M' echo "== supplier email" check "an email is accepted" "$(mut supplier.update "{\"id\":\"$WWG\",\"email\":\"orders@example.com\"}")" '"ok":true' check "a bad one is refused" "$(mut supplier.update "{\"id\":\"$WWG\",\"email\":\"not-an-address\"}")" "doesn't look like an email" echo "== stock below its reorder level" check "opening stock" "$(mut stock.moves "{\"mode\":\"Opening\",\"lines\":[{\"itemId\":\"$ST\",\"si\":1,\"qty\":2},{\"itemId\":\"$ST\",\"si\":2,\"qty\":40},{\"itemId\":\"$FJ\",\"si\":0,\"qty\":1}]}")" '"ok":true' check "reorder level 6 on Scrub Top M" "$(mut stock.reorder "{\"itemId\":\"$ST\",\"si\":1,\"reorder\":6}")" '"ok":true' check "reorder level 4 on Fleece M" "$(mut stock.reorder "{\"itemId\":\"$FJ\",\"si\":0,\"reorder\":4}")" '"ok":true' LIST=$(page /app/orders/list) check "the order list renders" "$(code /app/orders/list)" '200' check " with the Northline code as a line" "$LIST" '>NW-10422-MHE-7781-MNW-10422-L/dev/null; done check "issues recorded" "$(bk | py "print(sum(i['qty'] for i in d['issues'] if i['itemId']=='$ST' and i['sizeIndex']==2))")" '^26$' G=$(page "/app/stock/$ST") check "suggested 8 for size L" "$G" 'Suggested 8' check " 7.0 wk cover · ~2/wk" "$G" '7.0 wk cover · ~2/wk' no " and it does not run out before delivery" "$G" 'runs out before delivery' echo; echo "passed $PASS, failed $FAIL"; [ "$FAIL" -eq 0 ]