Compare commits
No commits in common. "c9fa1a59d191313913f6e7883afd16663d2ef5be" and "99c88d2800e8e0a91be3ea33629f27934a033af5" have entirely different histories.
c9fa1a59d1
...
99c88d2800
@ -12,7 +12,6 @@ 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) {}
|
||||||
@ -29,12 +28,6 @@ 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(
|
||||||
@ -47,22 +40,7 @@ export class FormController {
|
|||||||
|
|
||||||
@Delete(':id/fields/:fieldId')
|
@Delete(':id/fields/:fieldId')
|
||||||
@ApiOperation({ summary: 'Delete a field' })
|
@ApiOperation({ summary: 'Delete a field' })
|
||||||
async deleteField(
|
deleteField(@Param('id') id: string, @Param('fieldId') fieldId: string) {
|
||||||
@Param('id') id: string,
|
return this.formService.deleteField(id, fieldId);
|
||||||
@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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
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>) {}
|
||||||
@ -37,11 +36,11 @@ export class FormService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findAll(): Promise<Form[]> {
|
async findAll(): Promise<Form[]> {
|
||||||
return await this.formModel.find({deletedAt:null}).exec();
|
return await this.formModel.find().exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
async find(formId: string): Promise<Form> {
|
async find(formId: string): Promise<Form> {
|
||||||
const form = await this.formModel.findOne({ id: formId, deletedAt:null }).exec();
|
const form = await this.formModel.findOne({ id: formId }).exec();
|
||||||
if (!form) throw new NotFoundException(`Form ${formId} not found`);
|
if (!form) throw new NotFoundException(`Form ${formId} not found`);
|
||||||
return form;
|
return form;
|
||||||
}
|
}
|
||||||
@ -78,33 +77,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,9 +25,6 @@ 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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user