From 6fe95e9bde30ba3de5d0de75b52754e9da4d74c0 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 9 Apr 2023 00:17:31 +0530 Subject: [PATCH] feat: form view apis Signed-off-by: Pranav C --- packages/nocodb-nest/src/app.module.ts | 3 +- .../modules/forms/forms.controller.spec.ts | 20 +++++++ .../src/modules/forms/forms.controller.ts | 54 +++++++++++++++++++ .../src/modules/forms/forms.module.ts | 9 ++++ .../src/modules/forms/forms.service.spec.ts | 18 +++++++ .../src/modules/forms/forms.service.ts | 42 +++++++++++++++ 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 packages/nocodb-nest/src/modules/forms/forms.controller.spec.ts create mode 100644 packages/nocodb-nest/src/modules/forms/forms.controller.ts create mode 100644 packages/nocodb-nest/src/modules/forms/forms.module.ts create mode 100644 packages/nocodb-nest/src/modules/forms/forms.service.spec.ts create mode 100644 packages/nocodb-nest/src/modules/forms/forms.service.ts diff --git a/packages/nocodb-nest/src/app.module.ts b/packages/nocodb-nest/src/app.module.ts index f2fceb2bef..a6ad2973b4 100644 --- a/packages/nocodb-nest/src/app.module.ts +++ b/packages/nocodb-nest/src/app.module.ts @@ -18,9 +18,10 @@ import { ViewColumnsModule } from './modules/view-columns/view-columns.module'; import { BasesModule } from './modules/bases/bases.module'; import { HooksModule } from './modules/hooks/hooks.module'; import { SharedBasesModule } from './modules/shared-bases/shared-bases.module'; +import { FormsModule } from './modules/forms/forms.module'; @Module({ - imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule], + imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule], controllers: [], providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware], exports: [Connection, MetaService], diff --git a/packages/nocodb-nest/src/modules/forms/forms.controller.spec.ts b/packages/nocodb-nest/src/modules/forms/forms.controller.spec.ts new file mode 100644 index 0000000000..103e763fa3 --- /dev/null +++ b/packages/nocodb-nest/src/modules/forms/forms.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { FormsController } from './forms.controller'; +import { FormsService } from './forms.service'; + +describe('FormsController', () => { + let controller: FormsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [FormsController], + providers: [FormsService], + }).compile(); + + controller = module.get(FormsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/forms/forms.controller.ts b/packages/nocodb-nest/src/modules/forms/forms.controller.ts new file mode 100644 index 0000000000..6a650a81b6 --- /dev/null +++ b/packages/nocodb-nest/src/modules/forms/forms.controller.ts @@ -0,0 +1,54 @@ +import { + Body, + Controller, + Get, + Param, + Patch, + Post, + UseGuards, +} from '@nestjs/common'; +import { ViewCreateReqType } from 'nocodb-sdk'; +import { + Acl, + ExtractProjectIdMiddleware, +} from '../../middlewares/extract-project-id/extract-project-id.middleware'; +import { FormsService } from './forms.service'; +import { AuthGuard } from '@nestjs/passport'; + +@Controller('forms') +@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) +export class FormsController { + constructor(private readonly formsService: FormsService) {} + + @Get('/api/v1/db/meta/forms/:formViewId') + @Acl('formViewGet') + async formViewGet(@Param('formViewId') formViewId: string) { + const formViewData = await this.formsService.formViewGet({ + formViewId, + }); + return formViewData; + } + + @Post('/api/v1/db/meta/tables/:tableId/forms') + @Acl('formViewCreate') + async formViewCreate( + @Param('tableId') tableId: string, + @Body() body: ViewCreateReqType, + ) { + const view = await this.formsService.formViewCreate({ + body, + tableId, + }); + return view; + } + @Patch('/api/v1/db/meta/forms/:formViewId') + @Acl('formViewUpdate') + async formViewUpdate(req, res) { + res.json( + await this.formsService.formViewUpdate({ + formViewId: req.params.formViewId, + form: req.body, + }), + ); + } +} diff --git a/packages/nocodb-nest/src/modules/forms/forms.module.ts b/packages/nocodb-nest/src/modules/forms/forms.module.ts new file mode 100644 index 0000000000..62e048a620 --- /dev/null +++ b/packages/nocodb-nest/src/modules/forms/forms.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { FormsService } from './forms.service'; +import { FormsController } from './forms.controller'; + +@Module({ + controllers: [FormsController], + providers: [FormsService] +}) +export class FormsModule {} diff --git a/packages/nocodb-nest/src/modules/forms/forms.service.spec.ts b/packages/nocodb-nest/src/modules/forms/forms.service.spec.ts new file mode 100644 index 0000000000..9694c9ee42 --- /dev/null +++ b/packages/nocodb-nest/src/modules/forms/forms.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { FormsService } from './forms.service'; + +describe('FormsService', () => { + let service: FormsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [FormsService], + }).compile(); + + service = module.get(FormsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/forms/forms.service.ts b/packages/nocodb-nest/src/modules/forms/forms.service.ts new file mode 100644 index 0000000000..7cc64e8335 --- /dev/null +++ b/packages/nocodb-nest/src/modules/forms/forms.service.ts @@ -0,0 +1,42 @@ +import { Injectable } from '@nestjs/common'; + +import { T } from 'nc-help'; +import { FormUpdateReqType, ViewCreateReqType, ViewTypes } from 'nocodb-sdk'; +import { validatePayload } from '../../helpers'; +import { FormView, View } from '../../models'; + +@Injectable() +export class FormsService { + async formViewGet(param: { formViewId: string }) { + const formViewData = await FormView.getWithInfo(param.formViewId); + return formViewData; + } + + async formViewCreate(param: { tableId: string; body: ViewCreateReqType }) { + validatePayload( + 'swagger.json#/components/schemas/ViewCreateReq', + param.body, + ); + + const view = await View.insert({ + ...param.body, + // todo: sanitize + fk_model_id: param.tableId, + type: ViewTypes.FORM, + }); + + T.emit('evt', { evt_type: 'vtable:created', show_as: 'form' }); + + return view; + } + + async formViewUpdate(param: { formViewId: string; form: FormUpdateReqType }) { + validatePayload( + 'swagger.json#/components/schemas/FormUpdateReq', + param.form, + ); + + T.emit('evt', { evt_type: 'view:updated', type: 'form' }); + return await FormView.update(param.formViewId, param.form); + } +}