2021-07-22 14:24:18 +00:00
|
|
|
import { Controller, ForbiddenException, Get, Param, Post, Put, Query, Request, UnauthorizedException, UseGuards } from '@nestjs/common';
|
2021-07-14 14:14:35 +00:00
|
|
|
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-24 04:13:45 +00:00
|
|
|
import { AppAuthGuard } from 'src/modules/auth/app-auth.guard';
|
2021-07-26 12:45:10 +00:00
|
|
|
import { FoldersService } from '@services/folders.service';
|
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,
|
2021-07-26 12:45:10 +00:00
|
|
|
private foldersService: FoldersService,
|
2021-07-24 18:09:25 +00:00
|
|
|
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);
|
|
|
|
|
|
2021-08-10 09:04:27 +00:00
|
|
|
const seralizedQueries = [];
|
|
|
|
|
|
|
|
|
|
// serialize queries
|
|
|
|
|
for(const query of app.dataQueries) {
|
|
|
|
|
let decamelizedQuery = decamelizeKeys(query);
|
|
|
|
|
decamelizedQuery['options'] = query.options;
|
|
|
|
|
seralizedQueries.push(decamelizedQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response['data_queries'] = seralizedQueries;
|
2021-07-11 08:22:05 +00:00
|
|
|
response['definition'] = app['definition'];
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 04:13:45 +00:00
|
|
|
@UseGuards(AppAuthGuard) // This guard will allow access for unauthenticated user if the app is public
|
|
|
|
|
@Get('slugs/:slug')
|
|
|
|
|
async appFromSlug(@Request() req, @Param() params) {
|
|
|
|
|
|
|
|
|
|
if(req.user) {
|
|
|
|
|
const app = await this.appsService.findBySlug(params.slug);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if(!ability.can('viewApp', app)) {
|
|
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = await this.appsService.findBySlug(params.slug);
|
|
|
|
|
|
|
|
|
|
// serialize
|
|
|
|
|
return {
|
|
|
|
|
current_version_id: app['current_version_id'],
|
|
|
|
|
data_queries: app.dataQueries,
|
|
|
|
|
definition: app.currentVersion?.definition || {},
|
|
|
|
|
is_public: app.isPublic,
|
|
|
|
|
name: app.name,
|
|
|
|
|
slug: app.slug
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2021-07-26 12:45:10 +00:00
|
|
|
const folderId = req.query.folder;
|
|
|
|
|
|
|
|
|
|
let apps = [];
|
|
|
|
|
|
|
|
|
|
if(folderId) {
|
|
|
|
|
const folder = await this.foldersService.findOne(folderId);
|
|
|
|
|
apps = await this.foldersService.getAppsFor(req.user, folder, page);
|
|
|
|
|
} else {
|
|
|
|
|
apps = await this.appsService.all(req.user, page);
|
|
|
|
|
}
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
const totalCount = await this.appsService.count(req.user);
|
|
|
|
|
|
|
|
|
|
const meta = {
|
|
|
|
|
total_pages: Math.round(totalCount/10),
|
|
|
|
|
total_count: totalCount,
|
2021-07-11 05:53:05 +00:00
|
|
|
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-23 14:51:24 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get(':id/versions')
|
|
|
|
|
async fetchVersions(@Request() req, @Param() params) {
|
|
|
|
|
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if(!ability.can('fetchVersions', app)) {
|
|
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.appsService.fetchVersions(req.user, params.id);
|
|
|
|
|
return decamelizeKeys({ versions: result });
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 15:59:01 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post(':id/versions')
|
2021-07-27 18:20:54 +00:00
|
|
|
async createVersion(@Request() req, @Param() params) {
|
2021-07-23 15:59:01 +00:00
|
|
|
|
2021-07-27 18:20:54 +00:00
|
|
|
const versionName = req.body['versionName'];
|
2021-07-23 15:59:01 +00:00
|
|
|
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if(!ability.can('createVersions', app)) {
|
|
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const appUser = await this.appsService.createVersion(req.user, app, versionName);
|
|
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:57:59 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Put(':id/versions/:versionId')
|
2021-08-10 11:56:16 +00:00
|
|
|
async updateVersion(@Request() req, @Param() params) {
|
2021-07-23 16:57:59 +00:00
|
|
|
|
2021-08-10 11:56:16 +00:00
|
|
|
const definition = req.body['definition'];
|
2021-07-23 16:57:59 +00:00
|
|
|
|
2021-08-10 11:56:16 +00:00
|
|
|
const version = await this.appsService.findVersion(params.versionId);
|
2021-07-23 16:57:59 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if(!ability.can('updateVersions', version.app)) {
|
|
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const appUser = await this.appsService.updateVersion(req.user, version, definition);
|
|
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 13:54:32 +00:00
|
|
|
}
|