MEGAINFRA-WEBAPP/scripts/pb-add-dn-doc-type.py

54 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Adds doc_type / doc_number / doc_number_alts to dn_batch_notes, and
document_type to delivery_notes.
Five of fourteen blocked batches are tax invoices or collection notes.
They have no delivery note number, so the pipeline detected no notes and
orphaned the page. The identity of an invoice is its invoice number; of a
collection note, its document number. Requiring a delivery note number
was the wrong question.
Idempotent.
"""
import sys
from pb_env import PB, http, superuser_token
WANTED = {
"dn_batch_notes": [
{"name": "doc_type", "type": "select", "required": False, "maxSelect": 1,
"values": ["delivery_note", "invoice", "collection_note", "unknown"]},
{"name": "doc_number", "type": "text", "required": False},
{"name": "doc_number_alts", "type": "json", "required": False, "maxSize": 100000},
],
"delivery_notes": [
{"name": "document_type", "type": "select", "required": False, "maxSelect": 1,
"values": ["delivery_note", "invoice", "collection_note", "unknown"]},
],
}
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}")
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']}")
if __name__ == "__main__":
main()
print("\nDone.")