bulk_create_admins returns 500 for new emails or new orgs and leaves partial writes #37

Open
opened 2026-10-03 19:44:37 +00:00 by jalenluorion · 0 comments
Member

What's wrong
The app session is created with autoflush=False (api/app/services/db.py), and create_placeholder_user(), create_organization() and create_category() only call db.add(). So in bulk_create_admins:

  • an email with no user row yet gets Admin(user_id=None). The final commit fails with a NOT NULL/primary key error: 500, and nothing is saved. This is the "add a teammate who has never logged in" case in the Manager Dashboard.
  • a new org (site admin) gets Category(org_id=None): 500.
  • the per-email except Exception (L589-591) never rolls back, so after one failed flush every later email fails too.
  • the only commit inside the loop (L568, existing admin without a category) commits everything pending so far, so a later failure leaves partial writes.

How to reproduce
As an org1 admin (bearer(**BOB) with the world fixture):

POST /api/organizations/bulk_create_admins
{"user_emails": "newperson@andrew.cmu.edu", "organization_name": "Org One"}

Returns 500 and no user row is created.

Expected
201: placeholder users and admin rows are created. A failing email is reported in errors without blocking the others, and nothing half-written is committed.

Where to look

  • api/app/api/organizations.py bulk_create_admins() (L494-607).
  • Flush after creating the org, category and user (or in the helpers). Wrap each email in db.begin_nested() so a bad row rolls back alone. Remove the in-loop commit and commit once.
  • Line numbers are from main at 2c5f2e9.

Done when

  • new email, new org and mixed lists work
  • tests in api/tests/api/test_bulk_create_admins.py, including one email forced to fail (mock create_admin) while the others succeed
**What's wrong** The app session is created with `autoflush=False` (`api/app/services/db.py`), and `create_placeholder_user()`, `create_organization()` and `create_category()` only call `db.add()`. So in `bulk_create_admins`: - an email with no user row yet gets `Admin(user_id=None)`. The final commit fails with a NOT NULL/primary key error: 500, and nothing is saved. This is the "add a teammate who has never logged in" case in the Manager Dashboard. - a new org (site admin) gets `Category(org_id=None)`: 500. - the per-email `except Exception` (L589-591) never rolls back, so after one failed flush every later email fails too. - the only commit inside the loop (L568, existing admin without a category) commits everything pending so far, so a later failure leaves partial writes. **How to reproduce** As an org1 admin (`bearer(**BOB)` with the `world` fixture): ``` POST /api/organizations/bulk_create_admins {"user_emails": "newperson@andrew.cmu.edu", "organization_name": "Org One"} ``` Returns 500 and no user row is created. **Expected** 201: placeholder users and admin rows are created. A failing email is reported in `errors` without blocking the others, and nothing half-written is committed. **Where to look** - `api/app/api/organizations.py` `bulk_create_admins()` (L494-607). - Flush after creating the org, category and user (or in the helpers). Wrap each email in `db.begin_nested()` so a bad row rolls back alone. Remove the in-loop commit and commit once. - Line numbers are from main at 2c5f2e9. **Done when** - [ ] new email, new org and mixed lists work - [ ] tests in `api/tests/api/test_bulk_create_admins.py`, including one email forced to fail (mock `create_admin`) while the others succeed
jalenluorion added this to the Phase 1 milestone 2026-10-03 19:44:37 +00:00
jalenluorion added this to the CMUCal project 2026-10-03 19:45:09 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ScottyLabs/cal#37
No description provided.