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 { 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';
|
||||
|
||||
@Controller('form')
|
||||
export class FormController {
|
||||
constructor(private readonly formService: FormService) {}
|
||||
|
||||
@Post('create-or-update')
|
||||
@ApiOperation({ summary: 'Create Empty Form, Add Field to form' })
|
||||
async createOne(@Body() createUpdatedDto: CreateUpdateDto) {
|
||||
return await this.formService.createOrUpdate(createUpdatedDto);
|
||||
@Post('create-form')
|
||||
@ApiOperation({ summary: 'Create a Form'})
|
||||
async createOne(@Body() createFormDto:CreateFormDto){
|
||||
return await this.formService.createForm(createFormDto)
|
||||
}
|
||||
|
||||
@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 { 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';
|
||||
|
||||
// Reusable projections
|
||||
const LIST_PROJECTION = {
|
||||
@ -28,29 +32,12 @@ export class FormService {
|
||||
private llmService: LlmService,
|
||||
) {}
|
||||
|
||||
async createOrUpdate(dto: CreateUpdateDto): Promise<Form> {
|
||||
if (!dto.id) {
|
||||
return this.formModel.create({
|
||||
async createForm(formDto: CreateFormDto): Promise<Form> {
|
||||
const form = await this.formModel.create({
|
||||
id: uuidv4(),
|
||||
name: dto.name ?? 'Untitled Form',
|
||||
...formDto,
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
return form;
|
||||
}
|
||||
|
||||
async findAll(query: QueryFormDto): Promise<PaginatedResponse<Form>> {
|
||||
@ -104,12 +91,11 @@ 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(
|
||||
|
||||
@ -1,5 +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';
|
||||
@ -9,7 +10,7 @@ export type FieldDocument = Field & Document;
|
||||
|
||||
@Schema({ timestamps: true })
|
||||
export class Field {
|
||||
@Prop({ required: true, unique:true })
|
||||
@Prop({ required: true, unique:true, default:()=>uuidv4()})
|
||||
id!: string;
|
||||
|
||||
@Prop({ required: true, enum: PARENT_TYPE_KEYS })
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user