#!/usr/bin/env python3 """ Adds slip_product_code and po_description_key to DN_ITEM_ALIASES. The collection shipped keyed on material_code alone. Live data says that cannot work: 16 of 24 purchase orders carry a SINGLE material_code across every line (MEGCPO9239 is "513095" sixteen times) and code "580070" spans 24 different products, so such an alias resolves to every line on the order and is permanently ambiguous. Two extra keys fix it: slip_product_code - the supplier's article number or barcode from the slip. Never truncated by the printer, unlike the description, so it is the strongest lookup key. po_description_key - norm() of the chosen purchase order line's own description, which identifies a specific line. Safe to run before any aliases exist (there were none at the time this was written), and idempotent after. """ import sys from pb_env import PB, http, superuser_token WANTED = { "dn_item_aliases": [ {"name": "slip_product_code", "type": "text", "required": False}, {"name": "po_description_key", "type": "text", "required": False}, ], } def main(): token = superuser_token() print(f"\n→ {PB}") for name, wanted in WANTED.items(): code, coll = http("GET", f"/api/collections/{name}", token=token) if code != 200: sys.exit(f"GET {name} failed: {code} {coll}") # Read the live field list and APPEND. PocketBase replaces the # whole array on PATCH, so never build it from anything but a # fresh read. fields = list(coll.get("fields", [])) have = {f.get("name") for f in fields} add = [w for w in wanted if w["name"] not in have] if not add: print(f" • {name}: nothing to add") continue code, resp = http( "PATCH", f"/api/collections/{coll['id']}", {"fields": fields + add}, token ) if code != 200: sys.exit(f"PATCH {name} failed: {code} {resp}") for a in add: print(f" āœ“ {name}: added {a['name']} (kept {len(fields)} existing)") if __name__ == "__main__": main() print("\nDone.")