added form soft deletion ,recovery.

This commit is contained in:
Suman991 2026-04-09 12:53:09 +05:30
parent 99c88d2800
commit b2f181122e
3 changed files with 65 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import { ApiOperation } from '@nestjs/swagger';
import { UpdateFieldDto } from './dto/update-field.dto';
import { CreateUpdateDto } from './dto/create-update.dto';
@Controller('form')
export class FormController {
constructor(private readonly formService: FormService) {}
@ -28,6 +29,12 @@ export class FormController {
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')
@ApiOperation({ summary: 'update a field in a form' })
async updateField(
@ -40,7 +47,22 @@ export class FormController {
@Delete(':id/fields/:fieldId')
@ApiOperation({ summary: 'Delete a field' })
deleteField(@Param('id') id: string, @Param('fieldId') fieldId: string) {
return this.formService.deleteField(id, fieldId);
async deleteField(
@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,3 +1,4 @@
import { FormModule } from './form.module';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Form, FormDocument, Status } from './schemas/form.schema';
@ -7,6 +8,7 @@ import { CreateFieldDto } from './dto/create-field.dto';
import { UpdateFieldDto } from './dto/update-field.dto';
import { CreateUpdateDto } from './dto/create-update.dto';
@Injectable()
export class FormService {
constructor(@InjectModel(Form.name) private formModel: Model<FormDocument>) {}
@ -36,11 +38,11 @@ export class FormService {
}
async findAll(): Promise<Form[]> {
return await this.formModel.find().exec();
return await this.formModel.find({isDeleted:false}).exec();
}
async find(formId: string): Promise<Form> {
const form = await this.formModel.findOne({ id: formId }).exec();
const form = await this.formModel.findOne({ id: formId, isDeleted:false }).exec();
if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form;
}
@ -77,4 +79,35 @@ export class FormService {
if (!form) throw new NotFoundException(`Form ${formId} not found`);
return form;
}
async softDelete(FormId: string): Promise<Form> {
const form = await this.formModel.findOneAndUpdate(
{ id: FormId, isDeleted: false }, // prevent double deletion
{
$set: {
isDeleted: true,
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, isDeleted: true },
{
$set: {
isDeleted: false,
deletedAt: null,
},
},
{ new: true },
);
if (!form) throw new NotFoundException(`Form ${FormId} not found`);
return form;
}
}

View File

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