ToolJet/server/src/controllers/apps.controller.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-07-22 14:24:18 +00:00
import { Controller, ForbiddenException, Get, Param, Post, Put, Query, Request, UnauthorizedException, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
2021-07-11 05:41:45 +00:00
import { AppsService } from '../services/apps.service';
2021-07-10 13:54:32 +00:00
import { decamelizeKeys } from 'humps';
2021-07-22 14:24:18 +00:00
import { AppsAbilityFactory } from 'src/modules/casl/abilities/apps-ability.factory';
2021-07-10 13:54:32 +00:00
@Controller('apps')
export class AppsController {
constructor(
2021-07-22 14:24:18 +00:00
private appsService: AppsService,
private appsAbilityFactory: AppsAbilityFactory
2021-07-10 13:54:32 +00:00
) { }
2021-07-10 14:07:47 +00:00
@UseGuards(JwtAuthGuard)
@Post()
async create(@Request() req) {
const params = req.body;
const app = await this.appsService.create(req.user);
return decamelizeKeys(app);
}
2021-07-11 08:22:05 +00:00
@UseGuards(JwtAuthGuard)
@Get(':id')
async show(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
let response = decamelizeKeys(app);
response['definition'] = app['definition'];
return response;
}
2021-07-22 14:24:18 +00:00
@UseGuards(JwtAuthGuard)
@Put(':id')
async update(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if(!ability.can('updateParams', app)) {
throw new ForbiddenException('you do not have permissions to perform this action');
}
const result = await this.appsService.update(req.user, params.id, req.body.app);
let response = decamelizeKeys(result);
return response;
}
2021-07-10 13:54:32 +00:00
@UseGuards(JwtAuthGuard)
@Get()
async index(@Request() req, @Query() query) {
const page = req.query.page;
const apps = await this.appsService.all(req.user, page);
const totalCount = await this.appsService.count(req.user);
const meta = {
total_pages: Math.round(totalCount/10),
total_count: totalCount,
current_page: parseInt(page || 0)
2021-07-10 13:54:32 +00:00
}
const response = {
meta,
apps
}
return decamelizeKeys(response);
}
2021-07-23 05:32:49 +00:00
@UseGuards(JwtAuthGuard)
@Get(':id/users')
async fetchUsers(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if(!ability.can('fetchUsers', app)) {
throw new ForbiddenException('you do not have permissions to perform this action');
}
const result = await this.appsService.fetchUsers(req.user, params.id);
return decamelizeKeys({ users: result });
}
2021-07-10 13:54:32 +00:00
}