Skip to content
Snippets Groups Projects
Commit 43eb15b7 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat(structures): add admin delete

parent c9067895
3 merge requests!27Recette,!26Dev,!22Feat/admin structure delete
......@@ -131,6 +131,9 @@ export class Structure {
@Prop()
jaccompagneLesUsagersDansLeursDemarchesEnLigne: boolean;
@Prop()
deletedAt: Date;
}
export const StructureSchema = SchemaFactory.createForClass(Structure);
......
......@@ -8,6 +8,7 @@ import { Logger } from '@nestjs/common';
import { structureDto } from '../dto/structure.dto';
import { UsersService } from '../../users/users.service';
import { User } from '../../users/schemas/user.schema';
import { DateTime } from 'luxon';
@Injectable()
export class StructuresService {
......@@ -34,12 +35,18 @@ export class StructuresService {
public async search(searchString: string, filters?: Array<any>): Promise<Structure[]> {
if (searchString && filters) {
return this.structureModel
.find({ $and: [...this.parseFilter(filters), { $text: { $search: searchString } }] })
.find({
$and: [...this.parseFilter(filters), { $text: { $search: searchString }, deletedAt: { $exists: false } }],
})
.exec();
} else if (filters) {
return this.structureModel.find({ $or: this.parseFilter(filters) }).exec();
return this.structureModel
.find({ $and: [{ $or: this.parseFilter(filters), deletedAt: { $exists: false } }] })
.exec();
} else {
return this.structureModel.find({ $or: [{ $text: { $search: searchString } }] }).exec();
return this.structureModel
.find({ $and: [{ $or: [{ $text: { $search: searchString }, deletedAt: { $exists: false } }] }] })
.exec();
}
}
......@@ -59,7 +66,7 @@ export class StructuresService {
}
public async findAll(): Promise<StructureDocument[]> {
const structures = await this.structureModel.find().exec();
const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec();
// Update structures coord and address before sending them
await Promise.all(
structures.map((structure: StructureDocument) => {
......@@ -85,7 +92,7 @@ export class StructuresService {
}
})
);
return this.structureModel.find().exec();
return this.structureModel.find({ deletedAt: { $exists: false } }).exec();
}
public async update(idStructure: string, structure: structureDto): Promise<Structure> {
......@@ -189,7 +196,9 @@ export class StructuresService {
uniqueElements.map(async (value) => {
return {
id: value,
count: await this.structureModel.countDocuments({ [key]: { $elemMatch: { $eq: value } } }).exec(),
count: await this.structureModel
.countDocuments({ $and: [{ [key]: { $elemMatch: { $eq: value } }, deletedAt: { $exists: false } }] })
.exec(),
};
})
);
......@@ -201,4 +210,28 @@ export class StructuresService {
Logger.log(`Request : ${req}`, 'StructureService - getCoord');
return this.httpService.get(encodeURI(req));
}
public async deleteOne(id: string): Promise<Structure> {
const structure = await this.structureModel.findById(Types.ObjectId(id)).exec();
if (!structure) {
throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST);
}
structure.deletedAt = DateTime.local().setZone('Europe/Paris').toString();
this.anonymizeStructure(structure).save();
return structure;
}
private anonymizeStructure(structure: StructureDocument): StructureDocument {
structure.contactName = '';
structure.contactSurname = '';
structure.contactPhone = '';
structure.contactMail = '';
structure.facebook = '';
structure.twitter = '';
structure.instagram = '';
structure.website = '';
structure.fonction = '';
structure.gender = '';
return structure;
}
}
import { Body, Controller, Get, Param, ParseIntPipe, Post, Put, Query, UseGuards } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, UseGuards } from '@nestjs/common';
import { ApiParam } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Roles } from '../users/decorators/roles.decorator';
import { IsStructureOwnerGuard } from '../users/guards/isStructureOwner.guard';
import { RolesGuard } from '../users/guards/roles.guard';
import { User } from '../users/schemas/user.schema';
import { UsersService } from '../users/users.service';
import { CreateStructureDto } from './dto/create-structure.dto';
......@@ -76,4 +78,12 @@ export class StructuresController {
public async find(@Param('id') id: string) {
return this.structureService.findOne(id);
}
@Delete(':id')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@ApiParam({ name: 'id', type: String, required: true })
public async delete(@Param('id') id: string) {
return this.structureService.deleteOne(id);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment