gigafibre-fsm/scripts/migration/update_opened_by_staff.py
louispaulb 101faa21f1 feat: inline editing, search, notifications + full repo cleanup
- InlineField component + useInlineEdit composable for Odoo-style dblclick editing
- Client search by name, account ID, and legacy_customer_id (or_filters)
- SMS/Email notification panel on ContactCard via n8n webhooks
- Ticket reply thread via Communication docs
- All migration scripts (51 files) now tracked
- Client portal and field tech app added to monorepo
- README rewritten with full feature list, migration summary, architecture
- CHANGELOG updated with all recent work
- ROADMAP updated with current completion status
- Removed hardcoded tokens from docs (use $ERP_SERVICE_TOKEN)
- .gitignore updated (docker/, .claude/, exports/, .quasar/)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 07:34:41 -04:00

79 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Update opened_by_staff on Issues from legacy ticket.open_by → staff name."""
import pymysql
import psycopg2
LEGACY = {"host": "10.100.80.100", "user": "facturation", "password": "VD67owoj",
"database": "gestionclient", "connect_timeout": 30, "read_timeout": 600}
PG = {"host": "db", "port": 5432, "user": "postgres", "password": "123",
"dbname": "_eb65bdc0c4b1b2d6"}
def main():
mc = pymysql.connect(**LEGACY)
cur = mc.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT id, first_name, last_name FROM staff ORDER BY id")
staff_map = {}
for s in cur.fetchall():
name = ((s["first_name"] or "") + " " + (s["last_name"] or "")).strip()
if name:
staff_map[s["id"]] = name
cur.execute("SELECT id, open_by FROM ticket WHERE open_by > 0")
openers = cur.fetchall()
mc.close()
print(f"{len(openers)} tickets with open_by, {len(staff_map)} staff names")
pg = psycopg2.connect(**PG)
pgc = pg.cursor()
# Add column if not exists
pgc.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'tabIssue' AND column_name = 'opened_by_staff'")
if not pgc.fetchone():
pgc.execute('ALTER TABLE "tabIssue" ADD COLUMN opened_by_staff varchar(140)')
pg.commit()
print("Column opened_by_staff added")
else:
print("Column opened_by_staff already exists")
batch = {}
for o in openers:
name = staff_map.get(o["open_by"])
if name:
batch[o["id"]] = name
print(f"{len(batch)} tickets to update")
pgc.execute("""
CREATE TEMP TABLE _staff_open (
legacy_ticket_id integer PRIMARY KEY,
staff_name varchar(140)
)
""")
items = list(batch.items())
for i in range(0, len(items), 10000):
chunk = items[i:i+10000]
args = ",".join(pgc.mogrify("(%s,%s)", (tid, name)).decode() for tid, name in chunk)
pgc.execute("INSERT INTO _staff_open VALUES " + args)
pg.commit()
pgc.execute("""
UPDATE "tabIssue" i
SET opened_by_staff = so.staff_name
FROM _staff_open so
WHERE i.legacy_ticket_id = so.legacy_ticket_id
AND (i.opened_by_staff IS NULL OR i.opened_by_staff = '')
""")
updated = pgc.rowcount
pg.commit()
pgc.execute("DROP TABLE _staff_open")
pg.commit()
pg.close()
print(f"Updated: {updated} issues with opened_by_staff")
if __name__ == "__main__":
main()