one step create form api added
This commit is contained in:
parent
7408e7eb68
commit
4c1ecafa77
17
src/form/dto/create-form.dto.ts
Normal file
17
src/form/dto/create-form.dto.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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[];
|
||||||
|
}
|
||||||
@ -1,22 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
@ -11,17 +11,17 @@ import {
|
|||||||
import { FormService } from './form.service';
|
import { FormService } from './form.service';
|
||||||
import { ApiOperation } from '@nestjs/swagger';
|
import { ApiOperation } from '@nestjs/swagger';
|
||||||
import { UpdateFieldDto } from './dto/update-field.dto';
|
import { UpdateFieldDto } from './dto/update-field.dto';
|
||||||
import { CreateUpdateDto } from './dto/create-update.dto';
|
|
||||||
import { QueryFormDto } from './dto/query-form.dto';
|
import { QueryFormDto } from './dto/query-form.dto';
|
||||||
|
import { CreateFormDto } from './dto/create-form.dto';
|
||||||
|
|
||||||
@Controller('form')
|
@Controller('form')
|
||||||
export class FormController {
|
export class FormController {
|
||||||
constructor(private readonly formService: FormService) {}
|
constructor(private readonly formService: FormService) {}
|
||||||
|
|
||||||
@Post('create-or-update')
|
@Post('create-form')
|
||||||
@ApiOperation({ summary: 'Create Empty Form, Add Field to form' })
|
@ApiOperation({ summary: 'Create a Form'})
|
||||||
async createOne(@Body() createUpdatedDto: CreateUpdateDto) {
|
async createOne(@Body() createFormDto:CreateFormDto){
|
||||||
return await this.formService.createOrUpdate(createUpdatedDto);
|
return await this.formService.createForm(createFormDto)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
|
|||||||
@ -1,14 +1,18 @@
|
|||||||
import { Injectable, NotFoundException, ServiceUnavailableException } from '@nestjs/common';
|
import {
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
ServiceUnavailableException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { InjectModel } from '@nestjs/mongoose';
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
import { Form, FormDocument, Status } from './schemas/form.schema';
|
import { Form, FormDocument, Status } from './schemas/form.schema';
|
||||||
import { Model } from 'mongoose';
|
import { Model } from 'mongoose';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { UpdateFieldDto } from './dto/update-field.dto';
|
import { UpdateFieldDto } from './dto/update-field.dto';
|
||||||
import { CreateUpdateDto } from './dto/create-update.dto';
|
|
||||||
import { QueryFormDto } from './dto/query-form.dto';
|
import { QueryFormDto } from './dto/query-form.dto';
|
||||||
import { FormQueryBuilder } from './helpers/form-query.builder';
|
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';
|
||||||
|
|
||||||
// Reusable projections
|
// Reusable projections
|
||||||
const LIST_PROJECTION = {
|
const LIST_PROJECTION = {
|
||||||
@ -28,29 +32,12 @@ export class FormService {
|
|||||||
private llmService: LlmService,
|
private llmService: LlmService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createOrUpdate(dto: CreateUpdateDto): Promise<Form> {
|
async createForm(formDto: CreateFormDto): Promise<Form> {
|
||||||
if (!dto.id) {
|
const form = await this.formModel.create({
|
||||||
return this.formModel.create({
|
id: uuidv4(),
|
||||||
id: uuidv4(),
|
...formDto,
|
||||||
name: dto.name ?? 'Untitled Form',
|
});
|
||||||
});
|
return 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: dto.id },
|
|
||||||
{ $push: { fields: newField } },
|
|
||||||
{ new: true, projection: DETAIL_PROJECTION },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!updatedForm) throw new NotFoundException(`Form ${dto.id} not found`);
|
|
||||||
return updatedForm;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAll(query: QueryFormDto): Promise<PaginatedResponse<Form>> {
|
async findAll(query: QueryFormDto): Promise<PaginatedResponse<Form>> {
|
||||||
@ -96,22 +83,21 @@ export class FormService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findInterface(formId: string): Promise<string> {
|
async findInterface(formId: string): Promise<string> {
|
||||||
const form = await this.formModel
|
const form = await this.formModel
|
||||||
.findOne({ id: formId, deletedAt: null })
|
.findOne({ id: formId, deletedAt: null })
|
||||||
.select(DETAIL_PROJECTION)
|
.select(DETAIL_PROJECTION)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
if (!form) throw new NotFoundException(`Form ${formId} not found`);
|
if (!form) throw new NotFoundException(`Form ${formId} not found`);
|
||||||
|
|
||||||
try{
|
try {
|
||||||
let tsInference=await this.llmService.generateFormInterface(form)
|
let tsInference = await this.llmService.generateFormInterface(form);
|
||||||
return tsInference
|
return tsInference;
|
||||||
}catch(err){
|
} catch (err) {
|
||||||
throw new ServiceUnavailableException('llm down')
|
throw new ServiceUnavailableException('llm down');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateField(
|
async updateField(
|
||||||
formId: string,
|
formId: string,
|
||||||
fieldId: string,
|
fieldId: string,
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||||
import { Document } from 'mongoose';
|
import { Document } from 'mongoose';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
|
||||||
import { PARENT_TYPE_KEYS, INPUT_SUB_TYPE_KEYS } from '../types/field.type';
|
import { PARENT_TYPE_KEYS, INPUT_SUB_TYPE_KEYS } from '../types/field.type';
|
||||||
@ -9,7 +10,7 @@ export type FieldDocument = Field & Document;
|
|||||||
|
|
||||||
@Schema({ timestamps: true })
|
@Schema({ timestamps: true })
|
||||||
export class Field {
|
export class Field {
|
||||||
@Prop({ required: true, unique:true })
|
@Prop({ required: true, unique:true, default:()=>uuidv4()})
|
||||||
id!: string;
|
id!: string;
|
||||||
|
|
||||||
@Prop({ required: true, enum: PARENT_TYPE_KEYS })
|
@Prop({ required: true, enum: PARENT_TYPE_KEYS })
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user