MEGAINFRA-WEBAPP/scripts/pb-add-dn-item-aliases.py
DEls 15b8bbdba4 feat(dn-pipeline): add the remembered product alias table
Co-Authored-By: MiniMax <noreply@minimax.io>
2026-09-17 21:34:37 +02:00

54 lines
No EOL
1.8 KiB
Python

#!/usr/bin/env python3
"""
Creates DN_ITEM_ALIASES: remembered mappings from a supplier's wording to
one of our material codes.
Keyed on material_code rather than a purchase_order_items id, because
those rows are per-PO — an alias pointing at one PO's row would not help
the next order from the same supplier. Material codes are shared across
POs (all 148 existing PO items have one), which is what makes the alias
durable.
Idempotent.
"""
import sys
from pb_env import PB, http, superuser_token
def main():
token = superuser_token()
print(f"\n→ {PB}")
code, existing = http("GET", "/api/collections/dn_item_aliases", token=token)
if code == 200:
print(" • DN_ITEM_ALIASES already exists")
return
payload = {
"name": "DN_ITEM_ALIASES",
"type": "base",
"fields": [
{"name": "supplier_key", "type": "text", "required": False},
{"name": "slip_description_raw", "type": "text", "required": False},
{"name": "slip_description_key", "type": "text", "required": True},
{"name": "material_code", "type": "text", "required": True},
{"name": "po_description", "type": "text", "required": False},
{"name": "created_by", "type": "text", "required": False},
{"name": "times_used", "type": "number", "required": False},
{"name": "last_used", "type": "date", "required": False},
],
"indexes": [
"CREATE UNIQUE INDEX idx_dn_alias_key ON DN_ITEM_ALIASES "
"(supplier_key, slip_description_key)"
],
}
code, resp = http("POST", "/api/collections", payload, token)
if code != 200:
sys.exit(f"CREATE failed: {code} {resp}")
print(" ✓ created DN_ITEM_ALIASES")
if __name__ == "__main__":
main()
print("\nDone.")