ToolJet/server/src/controllers/organization_constants.controller.ts
Arpit ee79b2015a
Feature - Workspace environment constants (#6810)
* init--

* org constants: controllers, services, module and api for creating new constants

* read operation: all constants from all envs and all constants from a env

* update and delete operation

* updated delete msg

* encrypt constant value before saving and decrypt before sending back to controller

* adds group permission for org constants and required migrations

* adds ability to create constant

* adds group permissions and fixes migrations

* adds workspace constant permission to workspace settings UI

* ToolJetUI - componenst, ref: ee/5c92dc9c03b73d7006562d71d639fe4eb1971dee

* Org constants UI

* UI updates: display constant value as per enviroment

* Added client side pagination

* groups without create/edit/delete permission can only view the the constants

* group permission: Create and Edit - UI updates

* create/update/date flow - UI

* extending api support to create constant in multiple environments

* UI - add constant for multiple envs

* disable name input for update operation

* UI fixes and updates

* adds deprecating banner to variables dashboard

* removes update/create from UI --variables

* adding depricating soon alert in codehinter when workspace vars are used

* extra check to display the banner in preview of cdehinter

* resolves constants for admin in client-side

* resolves constants client-side and server side for queries

* include grp permission for create/delete for org constants

* resolves org constants in runJS and runPY

* cleanup

* updated the tab UI

* fixed: constant pagination footer

* fixes client side pagination bug

* adds docs link

* ui updates and fixes ui flickering on closing the drawer

* refactor updating table data

* adds error state to constant forms

* remove the alert from runJS and runPY after 5s

* added max length constraint for constant value

* refactor: component form

* delete only constant as per environment

* remove unwanted logs

* resolves constants in gds connection forms

* resolve gds source options for testConnections, preview and runQuery

* UI style fixes: banner

* remove alert banner for runJS and runPY

* input text color fixed

* style fixes for disabled inputs

* Edit constant- Until value is changed- CTA should be disabled

* adds tooltip for table name and value

* adds tooltip for disbaled input in edit drawer

* updated the tooltip position

* handle input height on type for large strings

* added empty state

* adds new badge for workspace constants

* substring should be different for viewers

* resolve constants in viewer

* add support to resolve constants for widgets and queires in public apps

* changed the api endpoint for org constant for env

* bugfix: handle org constants for encrypted value from source options

* add support to resolve app variables and constants in a single single

* fixes: multiple multiline and same line variables

* refactor

* cleanup

* remove encryption for ce

* remove group permission from permission dashboard

* text transform for badge

* added padding for each table row

* updated alert banner padding

* updated alert banner padding

* updates width for empty banner

* refactor

* adds tests for constant api

* added validations for constant name

* updates textare dynamic form

* updates the resolvers for constants

* removes decryptiong for source options in services

* refactor - frontend

* resolved conflicts

* fixes: typo-1 ~ workspace variables alert

* fixes: inconsistency in alert info icon

* fixes: naming convention consistency

* fixes: dark theme for confirm box

* disable create button for  add constant with same name,

* adds error state for updating constant with same value

* fixes: error message typo

* fixes: error message typo

* fixes position of tooltip

* fixes frontend lint issues

* update lock file

* UI updates and typos

* cleanup-refactor

* checking error state before creating

* should check all error states

* resolving multiple constants and app variables in a single line

* reafactore

* fixed: depricating icon : codehinter preview box

* remove log

* remove repeated logic for deleteOrgEnvironmentConstant

* only return status code for delete request for org constants

* Add data-cy for workspace constants elements (#7251)

* updates contants doc link

---------

Co-authored-by: Ajith KV <ajith.jaban@gmail.com>
2023-08-14 14:28:51 +05:30

99 lines
3.6 KiB
TypeScript

import {
Controller,
Post,
UseGuards,
Body,
Get,
Param,
Patch,
Delete,
ForbiddenException,
Query,
} from '@nestjs/common';
import { decamelizeKeys } from 'humps';
import { JwtAuthGuard } from '../modules/auth/jwt-auth.guard';
import { IsPublicGuard } from 'src/modules/org_environment_variables/is-public.guard';
import { User } from 'src/decorators/user.decorator';
import { OrganizationConstantsService } from '@services/organization_constants.service';
import { CreateOrganizationConstantDto, UpdateOrganizationConstantDto } from '@dto/organization-constant.dto';
import { OrganizationConstant } from '../entities/organization_constants.entity';
import { OrganizationConstantsAbilityFactory } from 'src/modules/casl/abilities/organization-constants-ability.factory';
import { AppDecorator as App } from 'src/decorators/app.decorator';
@Controller('organization-constants')
export class OrganizationConstantController {
constructor(
private organizationConstantsService: OrganizationConstantsService,
private organizationConstantsAbilityFactory: OrganizationConstantsAbilityFactory
) {}
@UseGuards(JwtAuthGuard)
@Get()
async get(@User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId);
return { constants: result };
}
@UseGuards(IsPublicGuard)
@Get(':app_slug')
async getConstantsFromApp(@App() app) {
const result = await this.organizationConstantsService.allEnvironmentConstants(app.organizationId);
return { constants: result };
}
@UseGuards(JwtAuthGuard)
@Get('/environment/:environmentId')
async getConstantsFromEnvironment(@User() user, @Param('environmentId') environmentId) {
const result = await this.organizationConstantsService.getConstantsForEnvironment(
user.organizationId,
environmentId
);
return { constants: result };
}
@UseGuards(JwtAuthGuard)
@Post()
async create(@User() user, @Body() createOrganizationConstantDto: CreateOrganizationConstantDto) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, {});
if (!ability.can('createOrganizationConstant', OrganizationConstant)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
const { organizationId } = user;
const result = await this.organizationConstantsService.create(createOrganizationConstantDto, organizationId);
return decamelizeKeys({ constant: result });
}
@UseGuards(JwtAuthGuard)
@Patch(':id')
async update(@Body() body: UpdateOrganizationConstantDto, @User() user, @Param('id') constantId) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, {});
if (!ability.can('createOrganizationConstant', OrganizationConstant)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
const { organizationId } = user;
const result = await this.organizationConstantsService.update(constantId, organizationId, body);
return decamelizeKeys({ constant: result });
}
@UseGuards(JwtAuthGuard)
@Delete(':id')
async delete(@User() user, @Param('id') constantId, @Query('environmentId') environmentId) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, {});
if (!ability.can('deleteOrganizationConstant', OrganizationConstant)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
const { organizationId } = user;
await this.organizationConstantsService.delete(constantId, organizationId, environmentId);
return { statusCode: 204 };
}
}