add field to form api added
This commit is contained in:
parent
4c1ecafa77
commit
5f34171a4a
@ -13,6 +13,7 @@ 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 {
|
||||
@ -21,7 +22,7 @@ export class FormController {
|
||||
@Post('create-form')
|
||||
@ApiOperation({ summary: 'Create a Form' })
|
||||
async createOne(@Body() createFormDto: CreateFormDto) {
|
||||
return await this.formService.createForm(createFormDto)
|
||||
return await this.formService.createForm(createFormDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ -41,6 +42,15 @@ export class FormController {
|
||||
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(
|
||||
|
||||
@ -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<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>> {
|
||||
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user