diff --git a/src/form/form.controller.ts b/src/form/form.controller.ts index 335712a..c98e3ab 100644 --- a/src/form/form.controller.ts +++ b/src/form/form.controller.ts @@ -13,15 +13,16 @@ import { ApiOperation } from '@nestjs/swagger'; import { UpdateFieldDto } from './dto/update-field.dto'; import { QueryFormDto } from './dto/query-form.dto'; import { CreateFormDto } from './dto/create-form.dto'; +import { CreateFieldDto } from './dto/create-field.dto'; @Controller('form') export class FormController { constructor(private readonly formService: FormService) {} @Post('create-form') - @ApiOperation({ summary: 'Create a Form'}) - async createOne(@Body() createFormDto:CreateFormDto){ - return await this.formService.createForm(createFormDto) + @ApiOperation({ summary: 'Create a Form' }) + async createOne(@Body() createFormDto: CreateFormDto) { + return await this.formService.createForm(createFormDto); } @Get() @@ -36,11 +37,20 @@ export class FormController { return await this.formService.find(formId); } @Get(':formId/interface') - @ApiOperation({summary:'Get interface for the form provided by llm'}) - async getInterface(@Param('formId') formId:string){ + @ApiOperation({ summary: 'Get interface for the form provided by llm' }) + async getInterface(@Param('formId') formId: string) { return await this.formService.findInterface(formId); } + @Patch(':formId/field') + @ApiOperation({ summary: 'Add a field to a form' }) + async addField( + @Param('formId') formId: string, + @Body() createFieldDto: CreateFieldDto, + ) { + return await this.formService.addField(formId, createFieldDto); + } + @Patch(':id/fields/:fieldId') @ApiOperation({ summary: 'update a field in a form' }) async updateField( diff --git a/src/form/form.service.ts b/src/form/form.service.ts index c29ec70..72a0e8f 100644 --- a/src/form/form.service.ts +++ b/src/form/form.service.ts @@ -13,6 +13,8 @@ import { FormQueryBuilder } from './helpers/form-query.builder'; import { PaginatedResponse } from 'src/interfaces/paginated-response.interface'; import { LlmService } from 'src/common/services/llm.service'; import { CreateFormDto } from './dto/create-form.dto'; +import { CreateFieldDto } from './dto/create-field.dto'; +import { threadCpuUsage } from 'process'; // Reusable projections const LIST_PROJECTION = { @@ -40,6 +42,16 @@ export class FormService { return form; } + async addField(formId: string, fieldDto: CreateFieldDto): Promise
{ + const updatedForm = await this.formModel.findOneAndUpdate( + { id: formId }, + { $push: { fields: fieldDto } }, + { returnDocument: 'after' }, //as {new:true} will be deprecated + ); + if (!updatedForm) throw new NotFoundException(`Form ${formId} not found`); + return updatedForm; + } + async findAll(query: QueryFormDto): Promise> { const filter = FormQueryBuilder.buildFilter(query); const sort = FormQueryBuilder.buildSort(query); @@ -113,7 +125,7 @@ export class FormService { { id: formId }, { $set: setPayload }, { - new: true, + returnDocument: 'after', arrayFilters: [{ 'elem.id': fieldId }], projection: DETAIL_PROJECTION, }, @@ -126,7 +138,7 @@ export class FormService { const form = await this.formModel.findOneAndUpdate( { id: formId }, { $pull: { fields: { id: fieldId } } }, - { new: true, projection: DETAIL_PROJECTION }, + { returnDocument: 'after', projection: DETAIL_PROJECTION }, ); if (!form) throw new NotFoundException(`Form ${formId} not found`); return form; @@ -140,7 +152,7 @@ export class FormService { deletedAt: new Date(), }, }, - { new: true, projection: DETAIL_PROJECTION }, + { returnDocument: 'after', projection: DETAIL_PROJECTION }, ); if (!form) throw new NotFoundException(`Form ${FormId} not found`); @@ -155,7 +167,7 @@ export class FormService { deletedAt: null, }, }, - { new: true, projection: DETAIL_PROJECTION }, + { returnDocument: 'after', projection: DETAIL_PROJECTION }, ); if (!form) throw new NotFoundException(`Form ${FormId} not found`); return form;