Skip to content

Conversation

@MarceloRGonc
Copy link
Contributor

@MarceloRGonc MarceloRGonc commented Jan 6, 2026

Fixes OPS-3340.

@linear
Copy link

linear bot commented Jan 6, 2026

import { projectService } from '../../project/project-service';

export async function getAdminProject(user: User): Promise<Project | null> {
return projectService.getOneForUser(user);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour will be different in the other repository.

@MarceloRGonc MarceloRGonc changed the title Mg/ops 3312 Protect the seed-admin to work with multiple projects Jan 6, 2026
import { userService } from '../user/user-service';
import { getAdminProject } from './seeds/get-admin-project';

// TODO: Change the place where this method is used to not rely on the admin user
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarceloRGonc MarceloRGonc marked this pull request as ready for review January 8, 2026 08:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the seed-admin functionality to support multiple projects by eliminating assumptions that users have only one workspace, database, or token in OpenOps Tables. The key changes enable reusing existing workspace/database/token context when seeding admin users.

Key Changes:

  • Modified createDefaultWorkspaceAndDatabase to accept optional existing workspace context and verify/reuse it rather than always creating new resources
  • Refactored seed-admin logic to check for existing project data and pass it to workspace/database creation
  • Extracted getAdminProject helper function for consistent project retrieval

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/server/api/src/app/openops-tables/default-workspace-database.ts Refactored workspace/database creation to support optional existing context and verification of existing resources
packages/server/api/src/app/database/seeds/seed-admin.ts Updated admin seeding to resolve existing organization/project context and pass it to workspace creation
packages/server/api/src/app/database/seeds/get-admin-project.ts Added new helper function to retrieve admin user's project
packages/server/api/src/app/database/get-default-user-db-token.ts Updated to use new getAdminProject helper and added TODO comment
packages/server/api/test/unit/openops-tables/default-workspace-database.test.ts Added test coverage for existing workspace/database scenario and updated existing tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

databaseId: number,
workspaceId: number,
databaseToken: string,
): Promise<Project> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these checks no longer necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm validating this directly on the Tables validation. Example:

async function ensureTablesWorkspaceExists(
  token: string,
  workspaceId?: number,
): Promise<number> {
  if (workspaceId) {
    return getWorkspaceId(token, workspaceId);
  }

  const workspace = await openopsTables.createWorkspace(
    OPENOPS_DEFAULT_WORKSPACE_NAME,
    token,
  );

  return workspace.id;
}

async function getWorkspaceId(
  token: string,
  workspaceId: number,
): Promise<number> {
  const workspaces = await openopsTables.listWorkspaces(token);

  const exists = workspaces.some((w) => w.id === workspaceId);
  if (!exists) {
    throw new Error(
      `Workspace ${workspaceId} was not found in OpenOps Tables.`,
    );
  }

  return workspaceId;
}

return {};
}

async function ensureOpenOpsTablesWorkspaceAndDatabaseExist(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what happens exactly when params is undefined? I can't quite understand the flow in that scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When params is undefined, is because we don't have a project on the DB so we will create a new workspace, and database


const user = await ensureUserExists(email, password);

const { organization, project } = await resolveUserOrganizationContext(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's room for some debug logs for transparency around the params and findings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some debug logs

);
});

it('should successfully fetch the existing workspace and database', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a few more tests for different failures?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 9, 2026

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.

3 participants