Compare commits

..

2 Commits

Author SHA1 Message Date
Suman991
c9fa1a59d1 removed isDeleted field from schema and updated related services 2026-04-09 13:19:01 +05:30
Suman991
b2f181122e added form soft deletion ,recovery. 2026-04-09 12:53:09 +05:30
3 changed files with 60 additions and 5 deletions

View File

@ -12,6 +12,7 @@ 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 { CreateUpdateDto } from './dto/create-update.dto';
@Controller('form') @Controller('form')
export class FormController { export class FormController {
constructor(private readonly formService: FormService) {} constructor(private readonly formService: FormService) {}
@ -28,6 +29,12 @@ export class FormController {
return await this.formService.findAll(); return await this.formService.findAll();
} }
@Get(':formId')
@ApiOperation({ summary: 'Find a form' })
async find(@Param('formId') formId: string) {
return await this.formService.find(formId);
}
@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(
@ -40,7 +47,22 @@ export class FormController {
@Delete(':id/fields/:fieldId') @Delete(':id/fields/:fieldId')
@ApiOperation({ summary: 'Delete a field' }) @ApiOperation({ summary: 'Delete a field' })
deleteField(@Param('id') id: string, @Param('fieldId') fieldId: string) { async deleteField(
return this.formService.deleteField(id, fieldId); @Param('id') id: string,
@Param('fieldId') fieldId: string,
) {
return await this.formService.deleteField(id, fieldId);
}
@Delete(':id')
@ApiOperation({ summary: 'Soft delete a form' })
async softDelete(@Param('id') id: string) {
return await this.formService.softDelete(id);
}
@Patch(':id/restore')
@ApiOperation({ summary: 'Restore a soft deleted form' })
restore(@Param('id') id: string) {
return this.formService.restore(id);
} }
} }

View File

@ -1,12 +1,13 @@
import { FormModule } from './form.module';
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, NotFoundException } 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 { CreateFieldDto } from './dto/create-field.dto';
import { UpdateFieldDto } from './dto/update-field.dto'; import { UpdateFieldDto } from './dto/update-field.dto';
import { CreateUpdateDto } from './dto/create-update.dto'; import { CreateUpdateDto } from './dto/create-update.dto';
@Injectable() @Injectable()
export class FormService { export class FormService {
constructor(@InjectModel(Form.name) private formModel: Model<FormDocument>) {} constructor(@InjectModel(Form.name) private formModel: Model<FormDocument>) {}
@ -36,11 +37,11 @@ export class FormService {
} }
async findAll(): Promise<Form[]> { async findAll(): Promise<Form[]> {
return await this.formModel.find().exec(); return await this.formModel.find({deletedAt:null}).exec();
} }
async find(formId: string): Promise<Form> { async find(formId: string): Promise<Form> {
const form = await this.formModel.findOne({ id: formId }).exec(); const form = await this.formModel.findOne({ id: formId, deletedAt:null }).exec();
if (!form) throw new NotFoundException(`Form ${formId} not found`); if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form; return form;
} }
@ -77,4 +78,33 @@ export class FormService {
if (!form) throw new NotFoundException(`Form ${formId} not found`); if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form; return form;
} }
async softDelete(FormId: string): Promise<Form> {
const form = await this.formModel.findOneAndUpdate(
{ id: FormId, deletedAt:null }, // prevent double deletion
{
$set: {
deletedAt: new Date(),
},
},
{ new: true },
);
if (!form) throw new NotFoundException(`Form ${FormId} not found`);
return form;
}
async restore(FormId: string): Promise<Form> {
const form = await this.formModel.findOneAndUpdate(
{ id: FormId, deletedAt:{$ne:null} }, // only restore if actually deleted
{
$set: {
deletedAt: null,
},
},
{ new: true },
);
if (!form) throw new NotFoundException(`Form ${FormId} not found`);
return form;
}
} }

View File

@ -25,6 +25,9 @@ export class Form {
default: Status.DRAFT, default: Status.DRAFT,
}) })
status!: Status; status!: Status;
@Prop({ type: Date, default: null })
deletedAt?: Date;
} }
export const FormSchema = SchemaFactory.createForClass(Form); export const FormSchema = SchemaFactory.createForClass(Form);