add field to form api added

This commit is contained in:
Suman991 2026-04-13 15:25:39 +05:30
parent 4c1ecafa77
commit 5f34171a4a
2 changed files with 31 additions and 9 deletions

View File

@ -13,6 +13,7 @@ import { ApiOperation } from '@nestjs/swagger';
import { UpdateFieldDto } from './dto/update-field.dto'; import { UpdateFieldDto } from './dto/update-field.dto';
import { QueryFormDto } from './dto/query-form.dto'; import { QueryFormDto } from './dto/query-form.dto';
import { CreateFormDto } from './dto/create-form.dto'; import { CreateFormDto } from './dto/create-form.dto';
import { CreateFieldDto } from './dto/create-field.dto';
@Controller('form') @Controller('form')
export class FormController { export class FormController {
@ -21,7 +22,7 @@ export class FormController {
@Post('create-form') @Post('create-form')
@ApiOperation({ summary: 'Create a Form' }) @ApiOperation({ summary: 'Create a Form' })
async createOne(@Body() createFormDto: CreateFormDto) { async createOne(@Body() createFormDto: CreateFormDto) {
return await this.formService.createForm(createFormDto) return await this.formService.createForm(createFormDto);
} }
@Get() @Get()
@ -41,6 +42,15 @@ export class FormController {
return await this.formService.findInterface(formId); 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') @Patch(':id/fields/:fieldId')
@ApiOperation({ summary: 'update a field in a form' }) @ApiOperation({ summary: 'update a field in a form' })
async updateField( async updateField(

View File

@ -13,6 +13,8 @@ import { FormQueryBuilder } from './helpers/form-query.builder';
import { PaginatedResponse } from 'src/interfaces/paginated-response.interface'; import { PaginatedResponse } from 'src/interfaces/paginated-response.interface';
import { LlmService } from 'src/common/services/llm.service'; import { LlmService } from 'src/common/services/llm.service';
import { CreateFormDto } from './dto/create-form.dto'; import { CreateFormDto } from './dto/create-form.dto';
import { CreateFieldDto } from './dto/create-field.dto';
import { threadCpuUsage } from 'process';
// Reusable projections // Reusable projections
const LIST_PROJECTION = { const LIST_PROJECTION = {
@ -40,6 +42,16 @@ export class FormService {
return form; return form;
} }
async addField(formId: string, fieldDto: CreateFieldDto): Promise<Form> {
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<PaginatedResponse<Form>> { async findAll(query: QueryFormDto): Promise<PaginatedResponse<Form>> {
const filter = FormQueryBuilder.buildFilter(query); const filter = FormQueryBuilder.buildFilter(query);
const sort = FormQueryBuilder.buildSort(query); const sort = FormQueryBuilder.buildSort(query);
@ -113,7 +125,7 @@ export class FormService {
{ id: formId }, { id: formId },
{ $set: setPayload }, { $set: setPayload },
{ {
new: true, returnDocument: 'after',
arrayFilters: [{ 'elem.id': fieldId }], arrayFilters: [{ 'elem.id': fieldId }],
projection: DETAIL_PROJECTION, projection: DETAIL_PROJECTION,
}, },
@ -126,7 +138,7 @@ export class FormService {
const form = await this.formModel.findOneAndUpdate( const form = await this.formModel.findOneAndUpdate(
{ id: formId }, { id: formId },
{ $pull: { fields: { id: fieldId } } }, { $pull: { fields: { id: fieldId } } },
{ new: true, projection: DETAIL_PROJECTION }, { returnDocument: 'after', projection: DETAIL_PROJECTION },
); );
if (!form) throw new NotFoundException(`Form ${formId} not found`); if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form; return form;
@ -140,7 +152,7 @@ export class FormService {
deletedAt: new Date(), deletedAt: new Date(),
}, },
}, },
{ new: true, projection: DETAIL_PROJECTION }, { returnDocument: 'after', projection: DETAIL_PROJECTION },
); );
if (!form) throw new NotFoundException(`Form ${FormId} not found`); if (!form) throw new NotFoundException(`Form ${FormId} not found`);
@ -155,7 +167,7 @@ export class FormService {
deletedAt: null, deletedAt: null,
}, },
}, },
{ new: true, projection: DETAIL_PROJECTION }, { returnDocument: 'after', projection: DETAIL_PROJECTION },
); );
if (!form) throw new NotFoundException(`Form ${FormId} not found`); if (!form) throw new NotFoundException(`Form ${FormId} not found`);
return form; return form;