166 lines
4.8 KiB
Bash
Executable file
166 lines
4.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Applies the admin-side changes between basic-sanity.yaml and
|
|
# basic-sanity-after-admin.yaml:
|
|
# - mark both new orders packaged
|
|
# - turn flash delivery off
|
|
# - suspend the combo
|
|
# - mark the 1kg SKU out of stock
|
|
# - rebuild caches
|
|
#
|
|
# Usage:
|
|
# ./tests/e2e/basic-sanity-admin-update.sh
|
|
#
|
|
# Reads tests/e2e/.basic-sanity-state.json.
|
|
|
|
set -euo pipefail
|
|
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
|
cd "$ROOT"
|
|
|
|
API="${API:-https://devbackend.freshyo.in/api/trpc}"
|
|
LOCAL_API="${LOCAL_API:-http://localhost:8787}"
|
|
STATE_FILE="$ROOT/tests/e2e/.basic-sanity-state.json"
|
|
TOKEN="${ADMIN_TOKEN:-$(cat /tmp/dev_admin_token 2>/dev/null || true)}"
|
|
DB="$(ls apps/backend/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/*.sqlite 2>/dev/null | head -n 1)"
|
|
|
|
if [[ ! -f "$STATE_FILE" ]]; then
|
|
echo "Missing $STATE_FILE. Run basic-sanity-setup.sh first." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -z "$TOKEN" ]]; then
|
|
echo "ADMIN_TOKEN is required (or put the token in /tmp/dev_admin_token)" >&2
|
|
exit 1
|
|
fi
|
|
|
|
export API LOCAL_API STATE_FILE TOKEN DB
|
|
|
|
python3 - <<'PY'
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
API = os.environ["API"].rstrip("/")
|
|
LOCAL_API = os.environ["LOCAL_API"].rstrip("/")
|
|
TOKEN = os.environ["TOKEN"]
|
|
STATE = json.loads(Path(os.environ["STATE_FILE"]).read_text())
|
|
DB = os.environ["DB"]
|
|
|
|
|
|
def trpc(procedure, payload):
|
|
req = urllib.request.Request(
|
|
f"{API}/{procedure}",
|
|
data=json.dumps(payload).encode(),
|
|
headers={
|
|
"content-type": "application/json",
|
|
"authorization": f"Bearer {TOKEN}",
|
|
"User-Agent": "freshyo-basic-sanity/1.0",
|
|
},
|
|
method="POST",
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=60) as resp:
|
|
parsed = json.loads(resp.read())
|
|
except urllib.error.HTTPError as exc:
|
|
detail = exc.read().decode(errors="replace")
|
|
raise SystemExit(f"{procedure} failed ({exc.code}): {detail}") from exc
|
|
if "error" in parsed:
|
|
raise SystemExit(f"{procedure} failed: {json.dumps(parsed['error'])}")
|
|
return parsed["result"]["data"]
|
|
|
|
|
|
def db_query(sql):
|
|
out = subprocess.check_output(["sqlite3", DB, sql], text=True)
|
|
return [line for line in out.splitlines() if line.strip()]
|
|
|
|
|
|
sku_ids = [STATE["sku500g"], STATE["sku1kg"], STATE["skuCombo"]]
|
|
sku_csv = ",".join(str(int(sku)) for sku in sku_ids)
|
|
order_ids = db_query(
|
|
"SELECT DISTINCT o.id FROM orders o "
|
|
"JOIN order_items oi ON oi.order_id = o.id "
|
|
f"WHERE oi.sku_id IN ({sku_csv}) "
|
|
"AND o.created_at >= datetime('now', '-6 hours') "
|
|
"ORDER BY o.id;"
|
|
)
|
|
|
|
if len(order_ids) != 2:
|
|
raise SystemExit(
|
|
f"Expected 2 new orders for the fixtures, found {len(order_ids)}: {order_ids}"
|
|
)
|
|
|
|
for order_id in order_ids:
|
|
trpc("admin.order.updatePackaged", {"orderId": str(order_id), "isPackaged": True})
|
|
print(f"packaged order {order_id}")
|
|
|
|
# Flash off.
|
|
trpc(
|
|
"admin.region.updateBoundSettings",
|
|
{
|
|
"boundId": 1,
|
|
"settings": [{"key": "isFlashDeliveryEnabled", "value": "false"}],
|
|
},
|
|
)
|
|
print("flash delivery disabled")
|
|
|
|
# Suspend the combo SKU.
|
|
trpc(
|
|
"admin.product.updateProduct",
|
|
{
|
|
"id": STATE["comboId"],
|
|
"name": STATE["comboName"],
|
|
"shortDescription": "Basic sanity combo",
|
|
"longDescription": "Combo of the two product A variants",
|
|
"storeId": 1,
|
|
"incrementStep": 1,
|
|
"productType": "combo",
|
|
"skus": [
|
|
{
|
|
"id": STATE["skuCombo"],
|
|
"price": 499,
|
|
"marketPrice": 549,
|
|
"images": [STATE["comboImageUrl"]],
|
|
"isFlashAvailable": True,
|
|
"flashPrice": 399,
|
|
"isOutOfStock": False,
|
|
"isOffer": False,
|
|
"isComboOnly": False,
|
|
"isDeleted": False,
|
|
"isSuspended": True,
|
|
"features": [{"featureName": "quantity", "featureValue": "1 combo"}],
|
|
"comboItems": [
|
|
{"skuId": STATE["sku500g"]},
|
|
{"skuId": STATE["sku1kg"]},
|
|
],
|
|
}
|
|
],
|
|
},
|
|
)
|
|
print(f"suspended combo SKU {STATE['skuCombo']}")
|
|
|
|
# 1kg out of stock; 500g stays in stock.
|
|
trpc(
|
|
"admin.product.toggleOutOfStock",
|
|
{
|
|
"id": STATE["productAId"],
|
|
"skuIds": [STATE["sku1kg"]],
|
|
"isOutOfStock": True,
|
|
},
|
|
)
|
|
print(f"marked SKU {STATE['sku1kg']} out of stock")
|
|
|
|
req = urllib.request.Request(
|
|
f"{LOCAL_API}/api/test/rebuild?regionId=1",
|
|
data=b"",
|
|
headers={
|
|
"authorization": f"Bearer {TOKEN}",
|
|
"User-Agent": "freshyo-basic-sanity/1.0",
|
|
},
|
|
method="POST",
|
|
)
|
|
with urllib.request.urlopen(req, timeout=120) as resp:
|
|
print("cache rebuild:", resp.read().decode(errors="replace"))
|
|
PY
|