/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
apps/api/src/app/environment-variables/usecases/create-environment-variable/create-environment-variable.usecase.ts
73 строки
3 KB
George Djabarov
feat(dashboard): email agent whats-next guide and simplified setup fixes NV-8115 (#11706)
06 июл 2026, 17:03
Не верифицирован
06 июл 2026, 17:03
9ad94fb
Код
Авторство
О чём код?
import { BadRequestException, ConflictException, Injectable } from '@nestjs/common'; import { encryptSecret, ResourceValidatorService } from '@novu/application-generic'; import { EnvironmentRepository, EnvironmentVariableRepository, ErrorCodesEnum } from '@novu/dal'; import { EnvironmentVariableType, SECRET_MASK } from '@novu/shared'; import { EnvironmentVariableResponseDto } from '../../dtos/environment-variable-response.dto'; import { toEnvironmentVariableResponseDto } from '../get-environment-variables/get-environment-variables.usecase'; import { validateEnvironmentVariableValues } from '../validate-environment-variable-values'; import { CreateEnvironmentVariableCommand } from './create-environment-variable.command'; @Injectable() export class CreateEnvironmentVariable { constructor( private environmentVariableRepository: EnvironmentVariableRepository, private environmentRepository: EnvironmentRepository, private resourceValidatorService: ResourceValidatorService ) {} async execute(command: CreateEnvironmentVariableCommand): Promise<EnvironmentVariableResponseDto> { await this.resourceValidatorService.validateEnvironmentVariablesLimit(command.organizationId); const existing = await this.environmentVariableRepository.findOne( { _organizationId: command.organizationId, key: command.key }, ['_id'] ); if (existing) { throw new ConflictException(`Environment variable with key "${command.key}" already exists`); } const incomingValues = command.values ?? []; await validateEnvironmentVariableValues(this.environmentRepository, command.organizationId, incomingValues, { restrictToUserEnvironment: command.restrictToUserEnvironment, userEnvironmentId: command.environmentId, }); const maskedValue = incomingValues.find((v) => v.value === SECRET_MASK); if (maskedValue) { throw new BadRequestException( 'Submitted value matches the secret mask placeholder; provide the real value or omit the entry.' ); } const values = incomingValues.map((v) => ({ _environmentId: v._environmentId, value: encryptSecret(v.value), })); try { const created = await this.environmentVariableRepository.create({ _organizationId: command.organizationId, key: command.key, type: command.type ?? EnvironmentVariableType.STRING, isSecret: command.isSecret ?? false, values, _updatedBy: command.userId, }); return toEnvironmentVariableResponseDto(created); } catch (error: unknown) { if ( typeof error === 'object' && error !== null && 'code' in error && (error as { code: number }).code === ErrorCodesEnum.DUPLICATE_KEY ) { throw new ConflictException(`Environment variable with key "${command.key}" already exists`); } throw error; } } }