Compare commits

..

No commits in common. "feat/reduce-api-call" and "main" have entirely different histories.

6 changed files with 73 additions and 112 deletions

View File

@ -1,17 +0,0 @@
import { ApiPropertyOptional } from "@nestjs/swagger";
import { IsArray, IsOptional, IsString, ValidateNested } from "class-validator";
import { CreateFieldDto } from "./create-field.dto";
import { Type } from "class-transformer";
export class CreateFormDto {
@ApiPropertyOptional({ example: 'Customer Feedback Survey' })
@IsString()
name?: string;
@ApiPropertyOptional({ type: [CreateFieldDto] })
@ValidateNested({each:true})
@Type(() => CreateFieldDto)
@IsArray()
@IsOptional()
fields?: CreateFieldDto[];
}

View File

@ -0,0 +1,22 @@
import { ApiPropertyOptional } from "@nestjs/swagger";
import { IsOptional, IsString, ValidateNested } from "class-validator";
import { CreateFieldDto } from "./create-field.dto";
import { Type } from "class-transformer";
export class CreateUpdateDto {
@ApiPropertyOptional({ example: '550e8400-uuid', description: 'Form ID — omit to create a new form' })
@IsString()
@IsOptional()
id?: string;
@ApiPropertyOptional({ example: 'Customer Feedback Survey' })
@IsString()
@IsOptional()
name?: string;
@ApiPropertyOptional({ type: CreateFieldDto })
@ValidateNested()
@Type(() => CreateFieldDto)
@IsOptional()
field?: CreateFieldDto;
}

View File

@ -11,18 +11,17 @@ import {
import { FormService } from './form.service';
import { ApiOperation } from '@nestjs/swagger';
import { UpdateFieldDto } from './dto/update-field.dto';
import { CreateUpdateDto } from './dto/create-update.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);
@Post('create-or-update')
@ApiOperation({ summary: 'Create Empty Form, Add Field to form' })
async createOne(@Body() createUpdatedDto: CreateUpdateDto) {
return await this.formService.createOrUpdate(createUpdatedDto);
}
@Get()
@ -31,33 +30,17 @@ export class FormController {
return this.formService.findAll(query);
}
@Get('stats')
@ApiOperation({ summary: 'Get Stats' })
async getStats() {
return await this.formService.getStats();
}
@Get(':formId')
@ApiOperation({ summary: 'Find a form' })
async find(@Param('formId') formId: string) {
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){
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(

View File

@ -1,19 +1,14 @@
import {
Injectable,
NotFoundException,
ServiceUnavailableException,
} from '@nestjs/common';
import { Injectable, NotFoundException, ServiceUnavailableException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Form, FormDocument, Status } from './schemas/form.schema';
import { Model } from 'mongoose';
import { v4 as uuidv4 } from 'uuid';
import { UpdateFieldDto } from './dto/update-field.dto';
import { CreateUpdateDto } from './dto/create-update.dto';
import { QueryFormDto } from './dto/query-form.dto';
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';
// Reusable projections
const LIST_PROJECTION = {
@ -33,21 +28,28 @@ export class FormService {
private llmService: LlmService,
) {}
async createForm(formDto: CreateFormDto): Promise<Form> {
const form = await this.formModel.create({
async createOrUpdate(dto: CreateUpdateDto): Promise<Form> {
if (!dto.id) {
return this.formModel.create({
id: uuidv4(),
...formDto,
name: dto.name ?? 'Untitled Form',
});
return form;
}
async addField(formId: string, fieldDto: CreateFieldDto): Promise<Form> {
const form = await this.formModel.findOne({ id: dto.id });
if (!form) throw new NotFoundException(`Form ${dto.id} not found`);
if (!dto.field) return form; // nothing to add, return as-is
const newField = { id: uuidv4(), ...dto.field };
const updatedForm = await this.formModel.findOneAndUpdate(
{ id: formId },
{ $push: { fields: fieldDto } },
{ returnDocument: 'after' }, //as {new:true} will be deprecated
{ id: dto.id },
{ $push: { fields: newField } },
{ new: true, projection: DETAIL_PROJECTION },
);
if (!updatedForm) throw new NotFoundException(`Form ${formId} not found`);
if (!updatedForm) throw new NotFoundException(`Form ${dto.id} not found`);
return updatedForm;
}
@ -93,36 +95,6 @@ export class FormService {
return form;
}
async getStats() {
const [totalForms, totalFields, fieldTypeBreakdown, formStatusBreakdown] =
await Promise.all([
this.formModel.countDocuments(),
this.formModel.aggregate([
{ $project: { fieldCount: { $size: '$fields' } } },
{ $group: { _id: null, total: { $sum: '$fieldCount' } } },
]),
this.formModel.aggregate([
{ $unwind: '$fields' },
{ $group: { _id: '$fields.keyType', count: { $sum: 1 } } },
]),
this.formModel.aggregate([
{
$group: {
_id: {
$cond: {
if: { $ifNull: ['$deletedAt', false] },
then: 'deleted',
else: 'active',
},
},
count: { $sum: 1 },
},
},
]),
]);
return { totalForms, totalFields, fieldTypeBreakdown, formStatusBreakdown };
}
async findInterface(formId: string): Promise<string> {
const form = await this.formModel
.findOne({ id: formId, deletedAt: null })
@ -132,11 +104,12 @@ export class FormService {
if (!form) throw new NotFoundException(`Form ${formId} not found`);
try{
let tsInference = await this.llmService.generateFormInterface(form);
return tsInference;
let tsInference=await this.llmService.generateFormInterface(form)
return tsInference
}catch(err){
throw new ServiceUnavailableException('llm down');
throw new ServiceUnavailableException('llm down')
}
}
async updateField(
@ -154,7 +127,7 @@ export class FormService {
{ id: formId },
{ $set: setPayload },
{
returnDocument: 'after',
new: true,
arrayFilters: [{ 'elem.id': fieldId }],
projection: DETAIL_PROJECTION,
},
@ -167,7 +140,7 @@ export class FormService {
const form = await this.formModel.findOneAndUpdate(
{ id: formId },
{ $pull: { fields: { id: fieldId } } },
{ returnDocument: 'after', projection: DETAIL_PROJECTION },
{ new: true, projection: DETAIL_PROJECTION },
);
if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form;
@ -181,7 +154,7 @@ export class FormService {
deletedAt: new Date(),
},
},
{ returnDocument: 'after', projection: DETAIL_PROJECTION },
{ new: true, projection: DETAIL_PROJECTION },
);
if (!form) throw new NotFoundException(`Form ${FormId} not found`);
@ -196,7 +169,7 @@ export class FormService {
deletedAt: null,
},
},
{ returnDocument: 'after', projection: DETAIL_PROJECTION },
{ new: true, projection: DETAIL_PROJECTION },
);
if (!form) throw new NotFoundException(`Form ${FormId} not found`);
return form;

View File

@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { v4 as uuidv4 } from 'uuid';
import { PARENT_TYPE_KEYS, INPUT_SUB_TYPE_KEYS } from '../types/field.type';
import type { ParentKeyType, InputSubType } from '../types/field.type';
@ -9,7 +9,7 @@ export type FieldDocument = Field & Document;
@Schema({ timestamps: true })
export class Field {
@Prop({ required: true, unique: true, default: () => uuidv4() })
@Prop({ required: true, unique:true })
id!: string;
@Prop({ required: true, enum: PARENT_TYPE_KEYS })
@ -27,7 +27,7 @@ export class Field {
@Prop({ required: false })
placeholder?: string;
@Prop({ required: false, default: undefined })
@Prop({ required: false })
options?: string[];
@Prop({ required: true, default: false })