From 02ae043a5d4c0e76bc4d59dbcbbde0f56e379988 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Thu, 6 Apr 2023 10:41:33 +0000
Subject: [PATCH] feat(admin): restore deleted structure

---
 src/admin/admin.controller.ts                     | 15 +++++++++++++--
 .../services/structures.service.spec.ts           |  9 +++++++++
 src/structures/services/structures.service.ts     |  5 +++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts
index 6f7b08748..de7d201a3 100644
--- a/src/admin/admin.controller.ts
+++ b/src/admin/admin.controller.ts
@@ -16,7 +16,7 @@ import { validate } from 'class-validator';
 import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
 import { NewsletterSubscription } from '../newsletter/newsletter-subscription.schema';
 import { NewsletterService } from '../newsletter/newsletter.service';
-import { Structure } from '../structures/schemas/structure.schema';
+import { Structure, StructureDocument } from '../structures/schemas/structure.schema';
 import { StructuresService } from '../structures/services/structures.service';
 import { Roles } from '../users/decorators/roles.decorator';
 import { RolesGuard } from '../users/guards/roles.guard';
@@ -315,10 +315,21 @@ export class AdminController {
   @ApiBearerAuth('JWT')
   @ApiOperation({ description: 'Get deleted structures' })
   @Get('getDeletedStructures')
-  public async getDeletedStructures() {
+  public async getDeletedStructures(): Promise<StructureDocument[]> {
     this.logger.debug('getDeletedStructures');
     const deletedStructures = await this.structuresService.findAll(true);
     this.logger.debug(`Found ${deletedStructures.length} deleted structures`);
     return deletedStructures;
   }
+
+  @UseGuards(JwtAuthGuard, RolesGuard)
+  @Roles('admin')
+  @ApiBearerAuth('JWT')
+  @ApiOperation({ description: 'Get deleted structures' })
+  @Post('restoreDeletedStructure/:structureId')
+  public async restoreDeletedStructures(@Param('structureId') structureId: string): Promise<void> {
+    this.logger.debug(`restoreDeletedStructures: ${structureId}`);
+    await this.structuresService.restoreStructure(structureId);
+    await this.structuresService.initiateStructureIndex();
+  }
 }
diff --git a/src/structures/services/structures.service.spec.ts b/src/structures/services/structures.service.spec.ts
index 26674c4ad..3d38b1b29 100644
--- a/src/structures/services/structures.service.spec.ts
+++ b/src/structures/services/structures.service.spec.ts
@@ -619,4 +619,13 @@ describe('StructuresService', () => {
       expect(mockSetCNFSid).toHaveBeenCalledTimes(2);
     });
   });
+
+  describe('restoreStructure', () => {
+    it('should restore a structure', async () => {
+      mockStructureModel.findByIdAndUpdate.mockReturnThis();
+      mockStructureModel.exec.mockImplementation(() => null);
+      await service.restoreStructure('someId');
+      expect(mockStructureModel.findByIdAndUpdate).toBeCalled();
+    });
+  });
 });
diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 5018ad442..85191dd96 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -1158,4 +1158,9 @@ export class StructuresService {
     }
     return null;
   }
+
+  public async restoreStructure(structureId: string) {
+    this.logger.debug(`restoreDeletedStructures: ${structureId}`);
+    this.structureModel.findByIdAndUpdate(structureId, { $unset: { deletedAt: 1 } }).exec();
+  }
 }
-- 
GitLab