66
77import {
88 OrganizationSettings ,
9+ OrgEnvVar ,
10+ OrgEnvVarWithValue ,
911 Team ,
1012 TeamMemberInfo ,
1113 TeamMemberRole ,
@@ -25,21 +27,32 @@ import { DBOrgSettings } from "./entity/db-team-settings";
2527import { DBUser } from "./entity/db-user" ;
2628import { TransactionalDBImpl } from "./transactional-db-impl" ;
2729import { TypeORM } from "./typeorm" ;
30+ import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service" ;
31+ import { DBOrgEnvVar } from "./entity/db-org-env-var" ;
32+ import { filter } from "../utils" ;
2833
2934@injectable ( )
3035export class TeamDBImpl extends TransactionalDBImpl < TeamDB > implements TeamDB {
31- constructor ( @inject ( TypeORM ) typeorm : TypeORM , @optional ( ) transactionalEM ?: EntityManager ) {
36+ constructor (
37+ @inject ( TypeORM ) typeorm : TypeORM ,
38+ @inject ( EncryptionService ) private readonly encryptionService : EncryptionService ,
39+ @optional ( ) transactionalEM ?: EntityManager ,
40+ ) {
3241 super ( typeorm , transactionalEM ) ;
3342 }
3443
3544 protected createTransactionalDB ( transactionalEM : EntityManager ) : TeamDB {
36- return new TeamDBImpl ( this . typeorm , transactionalEM ) ;
45+ return new TeamDBImpl ( this . typeorm , this . encryptionService , transactionalEM ) ;
3746 }
3847
3948 private async getTeamRepo ( ) : Promise < Repository < DBTeam > > {
4049 return ( await this . getEntityManager ( ) ) . getRepository < DBTeam > ( DBTeam ) ;
4150 }
4251
52+ private async getOrgEnvVarRepo ( ) : Promise < Repository < DBOrgEnvVar > > {
53+ return ( await this . getEntityManager ( ) ) . getRepository < DBOrgEnvVar > ( DBOrgEnvVar ) ;
54+ }
55+
4356 private async getMembershipRepo ( ) : Promise < Repository < DBTeamMembership > > {
4457 return ( await this . getEntityManager ( ) ) . getRepository < DBTeamMembership > ( DBTeamMembership ) ;
4558 }
@@ -408,4 +421,83 @@ export class TeamDBImpl extends TransactionalDBImpl<TeamDB> implements TeamDB {
408421 ) ;
409422 return result . length === 1 ;
410423 }
424+
425+ public async addOrgEnvironmentVariable ( orgId : string , envVar : OrgEnvVarWithValue ) : Promise < OrgEnvVar > {
426+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
427+ const insertedEnvVar = await envVarRepo . save ( {
428+ id : uuidv4 ( ) ,
429+ orgId,
430+ name : envVar . name ,
431+ value : envVar . value ,
432+ creationTime : new Date ( ) . toISOString ( ) ,
433+ } ) ;
434+ return toOrgEnvVar ( insertedEnvVar ) ;
435+ }
436+
437+ public async updateOrgEnvironmentVariable (
438+ orgId : string ,
439+ envVar : Partial < OrgEnvVarWithValue > ,
440+ ) : Promise < OrgEnvVar | undefined > {
441+ if ( ! envVar . id ) {
442+ throw new ApplicationError ( ErrorCodes . NOT_FOUND , "An environment variable with this ID could not be found" ) ;
443+ }
444+
445+ return await this . transaction ( async ( _ , ctx ) => {
446+ const envVarRepo = ctx . entityManager . getRepository < DBOrgEnvVar > ( DBOrgEnvVar ) ;
447+
448+ await envVarRepo . update (
449+ { id : envVar . id , orgId } ,
450+ filter ( envVar , ( _ , v ) => v !== null && v !== undefined ) ,
451+ ) ;
452+
453+ const found = await envVarRepo . findOne ( { id : envVar . id , orgId } ) ;
454+ if ( ! found ) {
455+ return ;
456+ }
457+ return toOrgEnvVar ( found ) ;
458+ } ) ;
459+ }
460+
461+ public async getOrgEnvironmentVariableById ( id : string ) : Promise < OrgEnvVar | undefined > {
462+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
463+ const envVarWithValue = await envVarRepo . findOne ( { id } ) ;
464+ if ( ! envVarWithValue ) {
465+ return undefined ;
466+ }
467+ const envVar = toOrgEnvVar ( envVarWithValue ) ;
468+ return envVar ;
469+ }
470+
471+ public async findOrgEnvironmentVariableByName ( orgId : string , name : string ) : Promise < OrgEnvVar | undefined > {
472+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
473+ return envVarRepo . findOne ( { orgId, name } ) ;
474+ }
475+
476+ public async getOrgEnvironmentVariables ( orgId : string ) : Promise < OrgEnvVar [ ] > {
477+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
478+ const envVarsWithValue = await envVarRepo . find ( { orgId } ) ;
479+ const envVars = envVarsWithValue . map ( toOrgEnvVar ) ;
480+ return envVars ;
481+ }
482+
483+ public async getOrgEnvironmentVariableValues ( envVars : OrgEnvVar [ ] ) : Promise < OrgEnvVarWithValue [ ] > {
484+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
485+ const envVarsWithValues = await envVarRepo . findByIds ( envVars ) ;
486+ return envVarsWithValues ;
487+ }
488+
489+ public async deleteOrgEnvironmentVariable ( id : string ) : Promise < void > {
490+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
491+ await envVarRepo . delete ( { id } ) ;
492+ }
493+ }
494+
495+ /**
496+ * @param envVarWithValue
497+ * @returns DBOrgEnvVar shape turned into an OrgEnvVar by dropping the "value" property
498+ */
499+ function toOrgEnvVar ( envVarWithValue : DBOrgEnvVar ) : OrgEnvVar {
500+ const envVar = { ...envVarWithValue } ;
501+ delete ( envVar as any ) [ "value" ] ;
502+ return envVar ;
411503}
0 commit comments