Skip to content

Commit fc66c9b

Browse files
committed
2 parents 6dc213f + 2a71e07 commit fc66c9b

13 files changed

Lines changed: 110 additions & 107 deletions

packages/server/api/src/app/database/seeds/auto-instances-shutdown-table-seed.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { getTableByName } from '@openops/common';
22
import { logger } from '@openops/server-shared';
33
import { FlagEntity } from '../../flags/flag.entity';
4-
import { SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME } from '../../openops-tables/template-tables/create-auto-instances-shutdown-table';
5-
import { seedTemplateTablesService } from '../../openops-tables/template-tables/seed-tables-for-templates';
4+
import {
5+
createAutoInstancesShutdownTable,
6+
SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME,
7+
} from '../../openops-tables/template-tables/create-auto-instances-shutdown-table';
68
import { databaseConnection } from '../database-connection';
79
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
10+
import { getAdminTokenAndDatabaseId } from './get-admin-token-and-database';
811

912
const AUTO_INSTANCES_SHUTDOWN_TABLE_SEED = 'AUTOINSTANCESSHUTDOWN';
1013

@@ -39,7 +42,8 @@ export const seedAutoInstancesShutdownTable = async (): Promise<void> => {
3942
);
4043

4144
if (!table) {
42-
await seedTemplateTablesService.createAutoInstancesShutdownTable();
45+
const tokenAndDatabaseId = await getAdminTokenAndDatabaseId();
46+
await createAutoInstancesShutdownTable(tokenAndDatabaseId);
4347
}
4448

4549
await setTableSeedFlag();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { AppSystemProp, system } from '@openops/server-shared';
2+
import { Project } from '@openops/shared';
3+
import { authenticateAdminUserInOpenOpsTables } from '../../openops-tables/auth-admin-tables';
4+
import { projectService } from '../../project/project-service';
5+
import { userService } from '../../user/user-service';
6+
7+
const getAdminUserProject = async (): Promise<Project> => {
8+
const email = system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL);
9+
const user = await userService.getByOrganizationAndEmail({
10+
organizationId: null,
11+
email,
12+
});
13+
14+
if (!user) {
15+
throw new Error(`Admin user not found for email: ${email}`);
16+
}
17+
18+
const project = await projectService.getOneForUser(user);
19+
20+
if (!project) {
21+
throw new Error(`No project found for user: ${email}`);
22+
}
23+
24+
return project;
25+
};
26+
27+
export const getAdminTokenAndDatabaseId = async (): Promise<{
28+
token: string;
29+
tablesDatabaseId: number;
30+
}> => {
31+
const { token } = await authenticateAdminUserInOpenOpsTables();
32+
const { tablesDatabaseId } = await getAdminUserProject();
33+
return { token, tablesDatabaseId };
34+
};

packages/server/api/src/app/database/seeds/openops-aggregated-costs-seed.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { getTableByName } from '@openops/common';
22
import { logger } from '@openops/server-shared';
33
import { FlagEntity } from '../../flags/flag.entity';
4-
import { SEED_TABLE_NAME } from '../../openops-tables/template-tables/create-aggregated-costs-table';
5-
import { seedTemplateTablesService } from '../../openops-tables/template-tables/seed-tables-for-templates';
4+
import {
5+
createAggregatedCostsTable,
6+
SEED_TABLE_NAME,
7+
} from '../../openops-tables/template-tables/create-aggregated-costs-table';
68
import { databaseConnection } from '../database-connection';
79
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
10+
import { getAdminTokenAndDatabaseId } from './get-admin-token-and-database';
811

912
const AGGREGATED_TABLE_SEED = 'AGGREGATEDCOSTS';
1013

@@ -38,7 +41,8 @@ export const seedFocusDataAggregationTemplateTable =
3841
);
3942

4043
if (!table) {
41-
await seedTemplateTablesService.createAggregatedCostsTable();
44+
const tokenAndDatabaseId = await getAdminTokenAndDatabaseId();
45+
await createAggregatedCostsTable(tokenAndDatabaseId);
4246
}
4347

4448
await setTableSeedFlag();

packages/server/api/src/app/database/seeds/openops-knonw-cost-types-by-application-seed.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { getTableByName } from '@openops/common';
22
import { logger } from '@openops/server-shared';
33
import { FlagEntity } from '../../flags/flag.entity';
4-
import { SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME } from '../../openops-tables/template-tables/create-known-cost-types-by-application-table';
5-
import { seedTemplateTablesService } from '../../openops-tables/template-tables/seed-tables-for-templates';
4+
import {
5+
createKnownCostTypesByApplicationTable,
6+
SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME,
7+
} from '../../openops-tables/template-tables/create-known-cost-types-by-application-table';
68
import { databaseConnection } from '../database-connection';
79
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
10+
import { getAdminTokenAndDatabaseId } from './get-admin-token-and-database';
811

912
const KNOWN_COST_TYPES_BY_APPLICATION = 'KNOWNCOSTTYPES';
1013

@@ -39,7 +42,8 @@ export const seedKnownCostTypesByApplicationTable = async (): Promise<void> => {
3942
);
4043

4144
if (!table) {
42-
await seedTemplateTablesService.createKnownCostTypesByApplicationTable();
45+
const tokenAndDatabaseId = await getAdminTokenAndDatabaseId();
46+
await createKnownCostTypesByApplicationTable(tokenAndDatabaseId);
4347
}
4448

4549
await setTableSeedFlag();

packages/server/api/src/app/database/seeds/openops-opportunities-table-seed.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { getTableByName } from '@openops/common';
22
import { logger } from '@openops/server-shared';
33
import { FlagEntity } from '../../flags/flag.entity';
4-
import { SEED_OPENOPS_TABLE_NAME } from '../../openops-tables/template-tables/create-opportunities-table';
5-
import { seedTemplateTablesService } from '../../openops-tables/template-tables/seed-tables-for-templates';
4+
import {
5+
createOpportunitiesTable,
6+
SEED_OPENOPS_TABLE_NAME,
7+
} from '../../openops-tables/template-tables/create-opportunities-table';
68
import { databaseConnection } from '../database-connection';
79
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
10+
import { getAdminTokenAndDatabaseId } from './get-admin-token-and-database';
811

912
const OPPORTUNITIES_TABLE_SEED = 'OPPORTUNITIESSEED';
1013

@@ -39,7 +42,8 @@ export const seedOpportunitesTemplateTable = async (): Promise<void> => {
3942
);
4043

4144
if (!table) {
42-
await seedTemplateTablesService.createOpportunityTemplateTable();
45+
const tokenAndDatabaseId = await getAdminTokenAndDatabaseId();
46+
await createOpportunitiesTable(tokenAndDatabaseId);
4347
}
4448

4549
await setTableSeedFlag();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { logger } from '@openops/server-shared';
22
import { FlagEntity } from '../../flags/flag.entity';
33
import { seedTemplateTablesService } from '../../openops-tables/template-tables/seed-tables-for-templates';
44
import { databaseConnection } from '../database-connection';
5+
import { getAdminTokenAndDatabaseId } from './get-admin-token-and-database';
56

67
const TEMPLATE_TABLES_SEED = 'TEMPLATE_TABLES_SEED';
78

@@ -30,7 +31,8 @@ export const seedTemplateTables = async (): Promise<void> => {
3031
return;
3132
}
3233

33-
await seedTemplateTablesService.createBaseTemplateTables();
34+
const tokenAndDatabaseId = await getAdminTokenAndDatabaseId();
35+
await seedTemplateTablesService.createBaseTemplateTables(tokenAndDatabaseId);
3436

3537
await setTableSeedFlag();
3638
};

packages/server/api/src/app/openops-tables/template-tables/create-aggregated-costs-table.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import {
77
} from '@openops/common';
88
import { logger } from '@openops/server-shared';
99
import { openopsTables } from '../index';
10+
import { TokenAndDatabaseId } from './types';
1011

1112
export const SEED_TABLE_NAME = 'Aggregated Costs';
1213
const SEED_LOG_HEADER = `[Seeding ${SEED_TABLE_NAME} table]`;
1314

14-
export async function createAggregatedCostsTable(
15-
databaseId: number,
16-
token: string,
17-
): Promise<{ tableId: number }> {
15+
export async function createAggregatedCostsTable({
16+
token,
17+
tablesDatabaseId,
18+
}: TokenAndDatabaseId): Promise<{ tableId: number }> {
1819
logger.debug(`${SEED_LOG_HEADER} Start`);
1920

2021
const table = await openopsTables.createTable(
21-
databaseId,
22+
tablesDatabaseId,
2223
SEED_TABLE_NAME,
2324
[['Group Key']],
2425
token,

packages/server/api/src/app/openops-tables/template-tables/create-auto-instances-shutdown-table.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import {
77
} from '@openops/common';
88
import { logger } from '@openops/server-shared';
99
import { createTable } from '../create-table';
10+
import { TokenAndDatabaseId } from './types';
1011

1112
export const SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME =
1213
'Auto instances shutdown';
1314

14-
export async function createAutoInstancesShutdownTable(
15-
token: string,
16-
databaseId: number,
17-
): Promise<void> {
15+
export async function createAutoInstancesShutdownTable({
16+
token,
17+
tablesDatabaseId,
18+
}: TokenAndDatabaseId): Promise<void> {
1819
logger.debug(
1920
`[Seeding ${SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME} table] Start`,
2021
);
2122

2223
const table = await createTable(
23-
databaseId,
24+
tablesDatabaseId,
2425
SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME,
2526
[['Resource ID']],
2627
token,

packages/server/api/src/app/openops-tables/template-tables/create-known-cost-types-by-application-table.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ import {
88
} from '@openops/common';
99
import { logger } from '@openops/server-shared';
1010
import { createTable } from '../create-table';
11+
import { TokenAndDatabaseId } from './types';
1112

1213
export const SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME =
1314
'Known cost types by application';
1415

15-
export async function createKnownCostTypesByApplicationTable(
16-
token: string,
17-
databaseId: number,
18-
) {
16+
export async function createKnownCostTypesByApplicationTable({
17+
token,
18+
tablesDatabaseId,
19+
}: TokenAndDatabaseId) {
1920
logger.debug(
2021
`[Seeding ${SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME} table] Start`,
2122
);
2223

2324
const table = await createTable(
24-
databaseId,
25+
tablesDatabaseId,
2526
SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME,
2627
[['ID']],
2728
token,

packages/server/api/src/app/openops-tables/template-tables/create-opportunities-table.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import {
88
} from '@openops/common';
99
import { logger } from '@openops/server-shared';
1010
import { createTable } from '../create-table';
11+
import { TokenAndDatabaseId } from './types';
1112

1213
export const SEED_OPENOPS_TABLE_NAME = 'Opportunities';
1314

14-
export async function createOpportunitiesTable(
15-
token: string,
16-
databaseId: number,
17-
) {
15+
export async function createOpportunitiesTable({
16+
token,
17+
tablesDatabaseId,
18+
}: TokenAndDatabaseId) {
1819
logger.debug(`[Seeding ${SEED_OPENOPS_TABLE_NAME} table] Start`);
1920

2021
const table = await createTable(
21-
databaseId,
22+
tablesDatabaseId,
2223
SEED_OPENOPS_TABLE_NAME,
2324
[['ID']],
2425
token,

0 commit comments

Comments
 (0)