This commit is contained in:
Anantshree Chandola 2025-03-06 14:02:18 +05:30 committed by GitHub
parent 1bb19e13c0
commit 1b81a2e272
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 95 additions and 41 deletions

View file

@ -15,7 +15,7 @@ export const orgEnvironmentConstantService = {
function getAll(type = null) {
const requestOptions = { method: 'GET', headers: authHeader(), credentials: 'include' };
const queryParams = type ? `?type=${type}` : '';
return fetch(`${config.apiUrl}/organization-constants${queryParams}`, requestOptions).then(handleResponse);
return fetch(`${config.apiUrl}/organization-constants/decrypted${queryParams}`, requestOptions).then(handleResponse);
}
function create(name, value, type, environments) {

View file

@ -22,30 +22,51 @@ export class OrganizationConstantController implements IOrganizationConstantCont
@InitFeature(FEATURE_KEY.GET)
@Get()
async get(@User() user, @Query('type') type: OrganizationConstantType) {
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId);
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, false, type);
return { constants: result };
}
@UseGuards(JwtAuthGuard, FeatureAbilityGuard)
@InitFeature(FEATURE_KEY.GET_DECRYPTED_CONSTANTS)
@Get('decrypted')
async getDecryptedConstants(@User() user, @Query('type') type: OrganizationConstantType) {
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, true, type);
return { constants: result };
}
@UseGuards(JwtAuthGuard, FeatureAbilityGuard)
@InitFeature(FEATURE_KEY.GET_SECRETS)
@Get('secrets')
async getAllSecrets(@User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(
user.organizationId,
false,
OrganizationConstantType.SECRET
);
return { constants: result };
}
//by default, this api fetches only global constants (for public apps, need to fetch app to get orgId in the public guard)
@UseGuards(AppAuthGuard)
@Get('public/:app_slug')
@InitFeature(FEATURE_KEY.GET_PUBLIC)
async getConstantsFromPublicApp(@App() app, @Query('environmentId') environmentId) {
const result = await this.organizationConstantsService.getConstantsForEnvironment(
@Get('public/:slug')
async getConstantsFromPublicApp(@App() app) {
const result = await this.organizationConstantsService.allEnvironmentConstants(
app.organizationId,
environmentId,
false,
OrganizationConstantType.GLOBAL
);
return { constants: result };
}
//by default, this api fetches only global constants
@UseGuards(JwtAuthGuard, FeatureAbilityGuard)
@Get(':app_slug')
@UseGuards(JwtAuthGuard)
@InitFeature(FEATURE_KEY.GET_FROM_APP)
async getConstantsFromApp(@User() user, @Query('environmentId') environmentId) {
const result = await this.organizationConstantsService.getConstantsForEnvironment(
@Get(':app_slug')
async getConstantsFromApp(@User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(
user.organizationId,
environmentId,
false,
OrganizationConstantType.GLOBAL
);
return { constants: result };

View file

@ -10,9 +10,16 @@ export class OrganizationConstantRepository extends Repository<OrganizationConst
}
// Updated function to find all organization constants by organizationId
async findAllByOrganizationId(organizationId: string) {
async findAllByOrganizationId(organizationId: string, type?: OrganizationConstantType) {
const whereCondition: any = {
organizationId,
};
// Add type filter if provided
if (type) {
whereCondition.type = type;
}
return this.find({
where: { organizationId },
where: whereCondition,
relations: ['orgEnvironmentConstantValues'],
});
}
@ -31,14 +38,21 @@ export class OrganizationConstantRepository extends Repository<OrganizationConst
});
}
async findByEnvironment(organizationId: string, environmentId: string) {
return this.find({
where: {
organizationId,
orgEnvironmentConstantValues: {
environmentId,
},
async findByEnvironment(organizationId: string, environmentId: string, type?: OrganizationConstantType) {
const whereCondition: any = {
organizationId,
orgEnvironmentConstantValues: {
environmentId,
},
};
// Add type filter if provided
if (type) {
whereCondition.type = type;
}
return this.find({
where: whereCondition,
relations: ['orgEnvironmentConstantValues'],
});
}

View file

@ -8,7 +8,7 @@ import { IOrganizationConstantsService } from './interfaces/IService';
import { OrganizationConstantsUtilService } from './util.service';
import { OrganizationConstantType } from './constants';
import { OrganizationConstantRepository } from './repository';
const secretValue = '**********';
@Injectable()
export class OrganizationConstantsService implements IOrganizationConstantsService {
constructor(
@ -23,22 +23,37 @@ export class OrganizationConstantsService implements IOrganizationConstantsServi
type?: OrganizationConstantType
): Promise<OrganizationConstant[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const result = await this.organizationConstantRepository.findAllByOrganizationId(organizationId);
const result = await this.organizationConstantRepository.findAllByOrganizationId(organizationId, type);
const appEnvironments = await this.appEnvironmentUtilService.getAll(organizationId);
const constantsWithValues = await Promise.all(
result.map(async (constant) => {
// Skip processing values if type is SECRET and decryptSecretValue is false
if (constant.type === OrganizationConstantType.SECRET && !decryptSecretValue) {
return {
name: constant.constantName,
};
}
const values = await Promise.all(
appEnvironments.map(async (env) => {
const value = constant.orgEnvironmentConstantValues.find((value) => value.environmentId === env.id);
let resolvedValue = '';
if (value) {
if (constant.type === OrganizationConstantType.SECRET) {
resolvedValue = decryptSecretValue
? await this.organizationConstantsUtilService.decryptSecret(organizationId, value.value)
: secretValue;
} else {
resolvedValue = await this.organizationConstantsUtilService.decryptSecret(
organizationId,
value.value
); // Constant type values are always decrypted
}
}
return {
environmentName: env.name,
value:
value && value.value.length > 0
? await this.organizationConstantsUtilService.decryptSecret(organizationId, value.value)
: '',
id: value.environmentId,
value: resolvedValue,
};
})
);
@ -62,22 +77,26 @@ export class OrganizationConstantsService implements IOrganizationConstantsServi
environmentId: string,
type?: OrganizationConstantType
): Promise<any[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const result = await this.organizationConstantRepository.findByEnvironment(organizationId, environmentId);
return dbTransactionWrap(async (manager: EntityManager) => {
const result = await this.organizationConstantRepository.findByEnvironment(organizationId, environmentId, type);
const constantsWithValues = result.map(async (constant) => {
const decryptedValue = await this.organizationConstantsUtilService.decryptSecret(
organizationId,
constant.orgEnvironmentConstantValues[0].value
);
return {
id: constant.id,
name: constant.constantName,
value: decryptedValue,
};
});
return await Promise.all(
result.map(async (constant) => {
const resolvedValue = !(constant.type === OrganizationConstantType.SECRET)
? await this.organizationConstantsUtilService.decryptSecret(
organizationId,
constant.orgEnvironmentConstantValues[0].value
)
: secretValue;
return Promise.all(constantsWithValues);
return {
id: constant.id,
name: constant.constantName,
type: constant.type,
value: resolvedValue,
};
})
);
});
}