Skip to content

fix(accounts): create managed users in the sandbox database for test keys - #378

Merged
roncodes merged 1 commit into
release/v0.6.72from
fix/sandbox-managed-accounts
Oct 9, 2026
Merged

roncodes merged 1 commit into
release/v0.6.72from
fix/sandbox-managed-accounts

Conversation

@roncodes

@roncodes roncodes commented Oct 9, 2026

Copy link
Copy Markdown
Member

Fixes #359

With a flb_test_ key, POST /v1/drivers and POST /v1/contacts failed with 1452 ... company_users_user_uuid_foreign.

core-api pins User to the live mysql connection, while CompanyUser follows the request onto sandbox. ProfileAccountManager::createManagedAccount() therefore saved the user in the live database, then inserted the membership into fleetbase_sandbox.company_users, whose foreign key couldn't find that user. Each attempt also left an orphan user in live users. A later retry could succeed after the hourly sandbox:sync, which made the failure look intermittent.

Changes

  • New ProfileAccountManager::copyAccountToSandbox(). On sandbox requests only, it upserts the user row, and the company row it belongs to, from the live connection into sandbox before the membership is written. This mirrors core-api's ApiCredentialController::upsertModelToSandbox() and is idempotent. It's called from attachToCompany() and from the contact account path in Contact.
  • Cleanup on failure. createManagedAccount() force-deletes the new user, and removes its sandbox copy, if attaching it to the company fails, then rethrows. No more orphan users.
  • Live mode is unchanged: both helpers return immediately when is_sandbox isn't set.

This is done in fleetops only, so it doesn't wait on a core-api release. The two copy helpers could later move into core-api and be shared with ApiCredentialController.

Tests

  • server/tests/Unit/Support/ProfileAccountManagerTest.php covers the sandbox copy, live-mode no-op, idempotency and cleanup on a failed attach.
  • DriverControllerCreateRecordTest.php is updated for the new path.

Both pass under pest-runner.

…keys

Users and companies are pinned to the live connection while company_users
follows a test key onto the sandbox connection, so POST /v1/drivers and
/v1/contacts failed on company_users_user_uuid_foreign and left an orphan
live user. Copy the account (and its company) into the sandbox before
adding the membership, and delete a newly created account if adding the
membership fails.

Fixes #359
@roncodes
roncodes merged commit b27a7b3 into release/v0.6.72 Oct 9, 2026
4 of 6 checks passed
@roncodes
roncodes deleted the fix/sandbox-managed-accounts branch October 9, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant