2021-08-30 11:43:27 +00:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
Get,
|
|
|
|
|
Param,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
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-08-30 11:43:27 +00:00
|
|
|
) {}
|
2021-07-10 13:54:32 +00:00
|
|
|
|
2021-07-10 14:07:47 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post()
|
|
|
|
|
async create(@Request() req) {
|
|
|
|
|
const app = await this.appsService.create(req.user);
|
2021-09-15 15:42:04 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if (!ability.can('createApp', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-09-15 15:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
await this.appsService.update(req.user, app.id, {
|
|
|
|
|
slug: app.id,
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-10 14:07:47 +00:00
|
|
|
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);
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = decamelizeKeys(app);
|
2021-07-11 08:22:05 +00:00
|
|
|
|
2021-08-10 09:04:27 +00:00
|
|
|
const seralizedQueries = [];
|
|
|
|
|
|
|
|
|
|
// serialize queries
|
2021-08-30 11:43:27 +00:00
|
|
|
for (const query of app.dataQueries) {
|
2021-09-21 13:48:28 +00:00
|
|
|
const decamelizedQuery = decamelizeKeys(query);
|
2021-08-10 09:04:27 +00:00
|
|
|
decamelizedQuery['options'] = query.options;
|
|
|
|
|
seralizedQueries.push(decamelizedQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response['data_queries'] = seralizedQueries;
|
2021-08-30 11:43:27 +00:00
|
|
|
response['definition'] = app.editingVersion?.definition;
|
2021-07-11 08:22:05 +00:00
|
|
|
|
|
|
|
|
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) {
|
2021-08-30 11:43:27 +00:00
|
|
|
if (req.user) {
|
2021-07-24 04:13:45 +00:00
|
|
|
const app = await this.appsService.findBySlug(params.slug);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('viewApp', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-24 04:13:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = await this.appsService.findBySlug(params.slug);
|
|
|
|
|
|
|
|
|
|
// serialize
|
|
|
|
|
return {
|
|
|
|
|
current_version_id: app['current_version_id'],
|
|
|
|
|
data_queries: app.dataQueries,
|
2021-08-30 11:43:27 +00:00
|
|
|
definition: app.editingVersion?.definition,
|
2021-07-24 04:13:45 +00:00
|
|
|
is_public: app.isPublic,
|
|
|
|
|
name: app.name,
|
2021-08-30 11:43:27 +00:00
|
|
|
slug: app.slug,
|
|
|
|
|
};
|
2021-07-24 04:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
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, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('updateParams', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-22 14:24:18 +00:00
|
|
|
}
|
2021-08-24 05:44:16 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const result = await this.appsService.update(req.user, params.id, req.body.app);
|
|
|
|
|
const response = decamelizeKeys(result);
|
2021-07-22 14:24:18 +00:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:47:44 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post(':id/clone')
|
|
|
|
|
async clone(@Request() req, @Param() params) {
|
|
|
|
|
const existingApp = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
|
|
|
|
if (!ability.can('cloneApp', existingApp)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-09-15 15:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const result = await this.appsService.clone(existingApp, req.user);
|
|
|
|
|
const response = decamelizeKeys(result);
|
2021-09-15 15:47:44 +00:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 14:06:37 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Delete(':id')
|
|
|
|
|
async delete(@Request() req, @Param() params) {
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('deleteApp', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('Only administrators are allowed to delete apps.');
|
2021-08-10 14:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.appsService.delete(params.id);
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = decamelizeKeys(result);
|
2021-08-10 14:06:37 +00:00
|
|
|
|
|
|
|
|
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 = [];
|
2021-08-24 05:44:16 +00:00
|
|
|
let folderCount = 0;
|
2021-07-26 12:45:10 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (folderId) {
|
2021-07-26 12:45:10 +00:00
|
|
|
const folder = await this.foldersService.findOne(folderId);
|
|
|
|
|
apps = await this.foldersService.getAppsFor(req.user, folder, page);
|
2021-08-24 05:44:16 +00:00
|
|
|
folderCount = await this.foldersService.userAppCount(req.user, folder);
|
2021-07-26 12:45:10 +00:00
|
|
|
} else {
|
|
|
|
|
apps = await this.appsService.all(req.user, page);
|
2021-08-30 11:43:27 +00:00
|
|
|
}
|
2021-08-24 05:44:16 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const totalCount = await this.appsService.count(req.user);
|
2021-07-10 13:54:32 +00:00
|
|
|
|
2021-08-24 05:44:16 +00:00
|
|
|
const totalPageCount = folderId ? folderCount : totalCount;
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
const meta = {
|
2021-08-30 11:43:27 +00:00
|
|
|
total_pages: Math.ceil(totalPageCount / 10),
|
2021-07-10 13:54:32 +00:00
|
|
|
total_count: totalCount,
|
2021-08-24 05:44:16 +00:00
|
|
|
folder_count: folderCount,
|
2021-08-30 11:43:27 +00:00
|
|
|
current_page: parseInt(page || 1),
|
|
|
|
|
};
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
meta,
|
2021-08-30 11:43:27 +00:00
|
|
|
apps,
|
|
|
|
|
};
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
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, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('fetchUsers', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-23 05:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('fetchVersions', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-23 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.appsService.fetchVersions(req.user, params.id);
|
2021-08-30 11:43:27 +00:00
|
|
|
return { versions: result };
|
2021-07-23 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
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, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('createVersions', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-23 15:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const appUser = await this.appsService.createVersion(req.user, app, versionName);
|
2021-07-23 15:59:01 +00:00
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get(':id/versions/:versionId')
|
|
|
|
|
async version(@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)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-08-30 11:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const appVersion = await this.appsService.findVersion(params.versionId);
|
|
|
|
|
|
|
|
|
|
return { ...appVersion, data_queries: app.dataQueries };
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
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, {});
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('updateVersions', version.app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('you do not have permissions to perform this action');
|
2021-07-23 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const appUser = await this.appsService.updateVersion(req.user, version, definition);
|
2021-07-23 16:57:59 +00:00
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
2021-07-10 13:54:32 +00:00
|
|
|
}
|