1- import {
2- authenticateDefaultUserInOpenOpsTables ,
3- getDefaultDatabaseId ,
4- } from '@openops/common' ;
5- import { logger } from '@openops/server-shared' ;
1+ import { authenticateDefaultUserInOpenOpsTables } from '@openops/common' ;
2+ import { AppSystemProp , logger , system } from '@openops/server-shared' ;
3+ import { projectService } from '../../project/project-service' ;
4+ import { userService } from '../../user/user-service' ;
65import { createAggregatedCostsTable } from './create-aggregated-costs-table' ;
76import { createAutoInstancesShutdownTable } from './create-auto-instances-shutdown-table' ;
87import { createBusinessUnitsTable } from './create-business-units-table' ;
@@ -12,10 +11,30 @@ import { createOpportunitiesTable } from './create-opportunities-table';
1211import { createResourceBuTagAssignmentTable } from './create-resource-bu-tag-assignment-table' ;
1312import { createTagOwnerMappingTable } from './create-tag-owner-mapping-table' ;
1413
14+ const getProjectTablesDatabaseId = async ( ) : Promise < number > => {
15+ const email = system . getOrThrow ( AppSystemProp . OPENOPS_ADMIN_EMAIL ) ;
16+ const user = await userService . getByOrganizationAndEmail ( {
17+ organizationId : null ,
18+ email,
19+ } ) ;
20+
21+ if ( ! user ) {
22+ throw new Error ( `Admin user not found for email: ${ email } ` ) ;
23+ }
24+
25+ const project = await projectService . getOneForUser ( user ) ;
26+
27+ if ( ! project ) {
28+ throw new Error ( `No project found for user: ${ email } ` ) ;
29+ }
30+
31+ return project . tablesDatabaseId ;
32+ } ;
33+
1534export const seedTemplateTablesService = {
16- async createBaseTemplateTables ( ) {
35+ async createBaseTemplateTables ( ) : Promise < void > {
1736 const { token } = await authenticateDefaultUserInOpenOpsTables ( ) ;
18- const databaseId = await getDefaultDatabaseId ( token ) ;
37+ const databaseId = await getProjectTablesDatabaseId ( ) ;
1938
2039 const buTable = await createBusinessUnitsTable ( databaseId , token ) ;
2140 await createTagOwnerMappingTable ( databaseId , token , buTable . tableId ) ;
@@ -29,25 +48,25 @@ export const seedTemplateTablesService = {
2948 logger . info ( '[Seeding template tables] Done' ) ;
3049 } ,
3150
32- async createOpportunityTemplateTable ( ) {
51+ async createOpportunityTemplateTable ( ) : Promise < void > {
3352 const { token } = await authenticateDefaultUserInOpenOpsTables ( ) ;
34- const databaseId = await getDefaultDatabaseId ( token ) ;
53+ const databaseId = await getProjectTablesDatabaseId ( ) ;
3554
3655 await createOpportunitiesTable ( token , databaseId ) ;
3756
3857 logger . info ( '[Seeding opportunity template table] Done' ) ;
3958 } ,
4059
41- async createAggregatedCostsTable ( ) {
60+ async createAggregatedCostsTable ( ) : Promise < void > {
4261 const { token } = await authenticateDefaultUserInOpenOpsTables ( ) ;
43- const databaseId = await getDefaultDatabaseId ( token ) ;
62+ const databaseId = await getProjectTablesDatabaseId ( ) ;
4463
4564 await createAggregatedCostsTable ( databaseId , token ) ;
4665 } ,
4766
48- async createKnownCostTypesByApplicationTable ( ) {
67+ async createKnownCostTypesByApplicationTable ( ) : Promise < void > {
4968 const { token } = await authenticateDefaultUserInOpenOpsTables ( ) ;
50- const databaseId = await getDefaultDatabaseId ( token ) ;
69+ const databaseId = await getProjectTablesDatabaseId ( ) ;
5170
5271 await createKnownCostTypesByApplicationTable ( token , databaseId ) ;
5372
@@ -56,7 +75,7 @@ export const seedTemplateTablesService = {
5675
5776 async createAutoInstancesShutdownTable ( ) : Promise < void > {
5877 const { token } = await authenticateDefaultUserInOpenOpsTables ( ) ;
59- const databaseId = await getDefaultDatabaseId ( token ) ;
78+ const databaseId = await getProjectTablesDatabaseId ( ) ;
6079
6180 await createAutoInstancesShutdownTable ( token , databaseId ) ;
6281
0 commit comments