Skip to content

Commit 3e4b2af

Browse files
authored
Merge branch 'main' into mg/OPS-3003
2 parents 01eb56b + 38c5293 commit 3e4b2af

13 files changed

Lines changed: 73 additions & 47 deletions

File tree

packages/server/api/src/app/authentication/new-user/organization-assignment.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AppSystemProp, system } from '@openops/server-shared';
33
import { ApplicationError, ErrorCode, isNil, User } from '@openops/shared';
44
import { openopsTables } from '../../openops-tables';
55
import { organizationService } from '../../organization/organization.service';
6+
import { projectService } from '../../project/project-service';
67
import { userService } from '../../user/user-service';
78

89
export async function assignDefaultOrganization(user: User): Promise<void> {
@@ -30,9 +31,21 @@ export async function assignDefaultOrganization(user: User): Promise<void> {
3031
organizationId: organization.id,
3132
});
3233

34+
const updatedUser = await userService.getOneOrFail({ id: user.id });
35+
const project = await projectService.getOneForUser(updatedUser);
36+
37+
if (isNil(project)) {
38+
throw new ApplicationError({
39+
code: ErrorCode.ENTITY_NOT_FOUND,
40+
params: {
41+
message: 'No project found for user',
42+
},
43+
});
44+
}
45+
3346
await addUserToDefaultWorkspace({
3447
email: user.email,
35-
workspaceId: organization.tablesWorkspaceId,
48+
workspaceId: project.tablesWorkspaceId,
3649
});
3750
}
3851

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class MoveTablesWorkspaceIdFromOrganizationToProject1760500000000
4+
implements MigrationInterface
5+
{
6+
name = 'MoveTablesWorkspaceIdFromOrganizationToProject1760500000000';
7+
8+
public async up(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`
10+
ALTER TABLE "project"
11+
ADD COLUMN IF NOT EXISTS "tablesWorkspaceId" integer;
12+
`);
13+
14+
// Migrate tablesWorkspaceId data from organization to project
15+
await queryRunner.query(`
16+
UPDATE "project"
17+
SET "tablesWorkspaceId" = "organization"."tablesWorkspaceId"
18+
FROM "organization"
19+
WHERE "project"."organizationId" = "organization"."id";
20+
`);
21+
22+
await queryRunner.query(`
23+
ALTER TABLE "project"
24+
ALTER COLUMN "tablesWorkspaceId" SET NOT NULL;
25+
`);
26+
27+
await queryRunner.query(`
28+
ALTER TABLE "organization"
29+
DROP COLUMN IF EXISTS "tablesWorkspaceId";
30+
`);
31+
}
32+
33+
public async down(_: QueryRunner): Promise<void> {
34+
throw new Error('Rollback not implemented');
35+
}
36+
}

packages/server/api/src/app/database/postgres-connection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { RenameIsWorkflowToIsInternal1756377588949 } from './migrations/17563775
3636
import { AddContentTypeToFolder1757331587268 } from './migrations/1757331587268-AddContentTypeToFolder';
3737
import { MigrateAiConfigToAppConnection1759242268873 } from './migrations/1759242268873-MigrateAiConfigToAppConnection';
3838
import { AddTestRunActionLimitsToFlowVersion1760429290001 } from './migrations/1760429290001-AddTestRunActionLimitsToFlowVersion';
39+
import { MoveTablesWorkspaceIdFromOrganizationToProject1760500000000 } from './migrations/1760500000000-MoveTablesWorkspaceIdFromOrganizationToProject';
3940

4041
const getSslConfig = (): boolean | TlsOptions => {
4142
const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL);
@@ -80,6 +81,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
8081
AddContentTypeToFolder1757331587268,
8182
MigrateAiConfigToAppConnection1759242268873,
8283
AddTestRunActionLimitsToFlowVersion1760429290001,
84+
MoveTablesWorkspaceIdFromOrganizationToProject1760500000000,
8385
];
8486
};
8587

packages/server/api/src/app/database/seeds/seed-admin.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export const upsertAdminUser = async (): Promise<void> => {
2323
const { workspaceId, databaseId } =
2424
await ensureOpenOpsTablesWorkspaceAndDatabaseExist();
2525

26-
await ensureOrganizationExists(user, workspaceId);
26+
await ensureOrganizationExists(user);
2727

28-
await ensureProjectExists(user, databaseId);
28+
await ensureProjectExists(user, databaseId, workspaceId);
2929
}
3030
};
3131

@@ -97,10 +97,7 @@ async function ensureOpenOpsTablesWorkspaceAndDatabaseExist(): Promise<{
9797
return { workspaceId, databaseId };
9898
}
9999

100-
async function ensureOrganizationExists(
101-
user: User,
102-
tablesWorkspaceId: number,
103-
): Promise<void> {
100+
async function ensureOrganizationExists(user: User): Promise<void> {
104101
if (user.organizationId) {
105102
const existingOrganization = await organizationService.getOne(
106103
user.organizationId,
@@ -112,19 +109,12 @@ async function ensureOrganizationExists(
112109
);
113110
}
114111

115-
if (existingOrganization.tablesWorkspaceId !== tablesWorkspaceId) {
116-
throw new Error(
117-
'User organization exists but with different tablesWorkspaceId',
118-
);
119-
}
120-
121112
return;
122113
}
123114

124115
const organization = await organizationService.create({
125116
ownerId: user.id,
126117
name: DEFAULT_ORGANIZATION_NAME,
127-
tablesWorkspaceId,
128118
});
129119

130120
user.organizationId = organization.id;
@@ -133,6 +123,7 @@ async function ensureOrganizationExists(
133123
async function ensureProjectExists(
134124
user: User,
135125
databaseId: number,
126+
workspaceId: number,
136127
): Promise<void> {
137128
const project = await projectService.getOneForUser(user);
138129
if (project) {
@@ -142,6 +133,12 @@ async function ensureProjectExists(
142133
);
143134
}
144135

136+
if (project.tablesWorkspaceId !== workspaceId) {
137+
throw new Error(
138+
'User project exists but with different tablesWorkspaceId',
139+
);
140+
}
141+
145142
return;
146143
}
147144

@@ -150,6 +147,7 @@ async function ensureProjectExists(
150147
ownerId: user.id,
151148
organizationId: user.organizationId!,
152149
tablesDatabaseId: databaseId,
150+
tablesWorkspaceId: workspaceId,
153151
});
154152
}
155153

packages/server/api/src/app/organization/organization.entity.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ export const OrganizationEntity = new EntitySchema<OrganizationSchema>({
2121
type: String,
2222
nullable: false,
2323
},
24-
tablesWorkspaceId: {
25-
type: Number,
26-
nullable: false,
27-
},
2824
},
2925
indices: [],
3026
relations: {

packages/server/api/src/app/organization/organization.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ export const organizationService = {
2121
return count > 0;
2222
},
2323
async create(params: AddParams): Promise<Organization> {
24-
const { ownerId, name, tablesWorkspaceId } = params;
24+
const { ownerId, name } = params;
2525

2626
const newOrganization: NewOrganization = {
2727
id: openOpsId(),
2828
ownerId,
2929
name,
30-
tablesWorkspaceId,
3130
};
3231

3332
const savedOrganization = await repo().save(newOrganization);
@@ -54,7 +53,6 @@ export const organizationService = {
5453
...organization,
5554
...spreadIfDefined('name', params.name),
5655
...spreadIfDefined('ownerId', params.ownerId),
57-
...spreadIfDefined('tablesWorkspaceId', params.tablesWorkspaceId),
5856
};
5957

6058
return repo().save(updatedOrganization);
@@ -91,7 +89,6 @@ export const organizationService = {
9189
type AddParams = {
9290
ownerId: UserId;
9391
name: string;
94-
tablesWorkspaceId: number;
9592
};
9693

9794
type NewOrganization = Omit<Organization, 'created' | 'updated'>;

packages/server/api/src/app/project/project-entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export const ProjectEntity = new EntitySchema<ProjectSchema>({
4242
type: Number,
4343
nullable: false,
4444
},
45+
tablesWorkspaceId: {
46+
type: Number,
47+
nullable: false,
48+
},
4549
},
4650
indices: [
4751
{

packages/server/api/src/app/project/project-service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const projectService = {
5252
...spreadIfDefined('displayName', request.displayName),
5353
...spreadIfDefined('ownerId', request.ownerId),
5454
...spreadIfDefined('tablesDatabaseId', request.tablesDatabaseId),
55+
...spreadIfDefined('tablesWorkspaceId', request.tablesWorkspaceId),
5556
},
5657
);
5758
return this.getOneOrThrow(projectId);
@@ -148,6 +149,7 @@ type UpdateParams = {
148149
displayName?: string;
149150
ownerId?: UserId;
150151
tablesDatabaseId?: number;
152+
tablesWorkspaceId?: number;
151153
};
152154

153155
type CreateParams = {
@@ -156,6 +158,7 @@ type CreateParams = {
156158
organizationId: string;
157159
externalId?: string;
158160
tablesDatabaseId: number;
161+
tablesWorkspaceId: number;
159162
};
160163

161164
type AddProjectToOrganizationParams = {

packages/server/api/test/helpers/mocks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export const createMockProject = (project?: Partial<Project>): Project => {
105105
displayName: project?.displayName ?? faker.lorem.word(),
106106
organizationId: project?.organizationId ?? openOpsId(),
107107
tablesDatabaseId: project?.tablesDatabaseId ?? faker.number.int(),
108+
tablesWorkspaceId: project?.tablesWorkspaceId ?? faker.number.int(),
108109
};
109110
};
110111

@@ -128,7 +129,6 @@ export const createMockOrganization = (
128129
updated: organization?.updated ?? faker.date.recent().toISOString(),
129130
ownerId: organization?.ownerId ?? openOpsId(),
130131
name: organization?.name ?? faker.lorem.word(),
131-
tablesWorkspaceId: organization?.tablesWorkspaceId ?? faker.number.int(),
132132
};
133133
};
134134

packages/server/api/test/integration/cloud/organization/organization.service.test.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ describe('Organization Service', () => {
6161
const result = await organizationService.create({
6262
ownerId: mockOrganization.ownerId,
6363
name: mockOrganization.name,
64-
tablesWorkspaceId: 123,
6564
});
6665

6766
const savedOrganization = await organizationService.getOneOrThrow(
6867
result.id,
6968
);
7069

7170
expect(savedOrganization.id).toBe(result.id);
72-
expect(savedOrganization.tablesWorkspaceId).toBe(123);
7371

7472
const savedUser = await userService.getOneOrFail({
7573
id: mockOrganization.ownerId,
@@ -99,26 +97,6 @@ describe('Organization Service', () => {
9997
expect(savedOrganization.name).toBe('Test Org 2');
10098
});
10199

102-
it('should update the organization tablesWorkspaceId', async () => {
103-
const mockOrganization = await createOrganizationInDB();
104-
let savedOrganization = await organizationService.getOneOrThrow(
105-
mockOrganization.id,
106-
);
107-
expect(savedOrganization.tablesWorkspaceId).toBe(
108-
mockOrganization.tablesWorkspaceId,
109-
);
110-
111-
await organizationService.update({
112-
id: mockOrganization.id,
113-
tablesWorkspaceId: 4564,
114-
});
115-
116-
savedOrganization = await organizationService.getOneOrThrow(
117-
mockOrganization.id,
118-
);
119-
expect(savedOrganization.tablesWorkspaceId).toBe(4564);
120-
});
121-
122100
it('should update the organization ownerId', async () => {
123101
const user = createMockUser();
124102
await databaseConnection().getRepository('user').save(user);

0 commit comments

Comments
 (0)