2022-12-02 08:43:28 +00:00
|
|
|
import { Controller, ForbiddenException, Get, Param, Post, Query, UseGuards, Body } from '@nestjs/common';
|
|
|
|
|
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
|
|
|
|
|
import { AppsService } from '../services/apps.service';
|
|
|
|
|
import { decamelizeKeys } from 'humps';
|
|
|
|
|
import { AppsAbilityFactory } from 'src/modules/casl/abilities/apps-ability.factory';
|
|
|
|
|
import { App } from 'src/entities/app.entity';
|
|
|
|
|
import { AppImportExportService } from '@services/app_import_export.service';
|
|
|
|
|
import { User } from 'src/decorators/user.decorator';
|
2023-10-17 07:48:18 +00:00
|
|
|
import { AppImportDto } from '@dto/app-import.dto';
|
2022-12-02 08:43:28 +00:00
|
|
|
|
|
|
|
|
@Controller('apps')
|
|
|
|
|
export class AppsImportExportController {
|
|
|
|
|
constructor(
|
|
|
|
|
private appsService: AppsService,
|
|
|
|
|
private appImportExportService: AppImportExportService,
|
|
|
|
|
private appsAbilityFactory: AppsAbilityFactory
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post('/import')
|
2023-10-17 07:48:18 +00:00
|
|
|
async import(@User() user, @Body() appImportDto: AppImportDto) {
|
2022-12-02 08:43:28 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(user);
|
|
|
|
|
|
|
|
|
|
if (!ability.can('createApp', App)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
2023-10-17 07:48:18 +00:00
|
|
|
const { name: appName, app: appContent } = appImportDto;
|
|
|
|
|
const app = await this.appImportExportService.import(user, appContent, appName);
|
2022-12-02 08:43:28 +00:00
|
|
|
return decamelizeKeys(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get(':id/export')
|
|
|
|
|
async export(@User() user, @Param('id') id, @Query() query) {
|
|
|
|
|
const appToExport = await this.appsService.find(id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(user, id);
|
|
|
|
|
|
|
|
|
|
if (!ability.can('cloneApp', appToExport)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = await this.appImportExportService.export(user, id, query);
|
|
|
|
|
return {
|
|
|
|
|
...app,
|
|
|
|
|
tooljetVersion: globalThis.TOOLJET_VERSION,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|