From 26cf7271f55612d4fa4aeb2cab92d1bf237061c8 Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Tue, 2 Feb 2021 18:52:33 +0100
Subject: [PATCH] fix(form) : add logic verifiedStructureShow

---
 src/structures/services/structures.service.ts | 25 ++++++++++++++++---
 src/structures/structures.controller.ts       |  5 ++++
 src/users/users.service.ts                    |  3 ++-
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index ddd4e2bd4..6fc689024 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -39,16 +39,21 @@ export class StructuresService {
     if (searchString && filters) {
       return this.structureModel
         .find({
-          $and: [...this.parseFilter(filters), { $text: { $search: searchString }, deletedAt: { $exists: false } }],
+          $and: [
+            ...this.parseFilter(filters),
+            { $text: { $search: searchString }, deletedAt: { $exists: false }, accountVerified: true },
+          ],
         })
         .exec();
     } else if (filters) {
       return this.structureModel
-        .find({ $and: [{ $or: this.parseFilter(filters), deletedAt: { $exists: false } }] })
+        .find({ $and: [{ $or: this.parseFilter(filters), deletedAt: { $exists: false }, accountVerified: true }] })
         .exec();
     } else {
       return this.structureModel
-        .find({ $and: [{ $or: [{ $text: { $search: searchString }, deletedAt: { $exists: false } }] }] })
+        .find({
+          $and: [{ $or: [{ $text: { $search: searchString }, deletedAt: { $exists: false }, accountVerified: true }] }],
+        })
         .exec();
     }
   }
@@ -95,7 +100,7 @@ export class StructuresService {
         }
       })
     );
-    return this.structureModel.find({ deletedAt: { $exists: false } }).exec();
+    return this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec();
   }
 
   public async update(idStructure: string, structure: structureDto): Promise<Structure> {
@@ -292,4 +297,16 @@ export class StructuresService {
     });
     this.mailerService.send(userEmail, jsonConfig.subject, html);
   }
+
+  public async updateAfterOwnerVerify(emailUser: string): Promise<Structure> {
+    const user = await this.userService.findOne(emailUser);
+    const structureLinked = await this.findOne(user.structuresLink[0]);
+    const structure = new this.structureModel(structureLinked);
+    if (!structure) {
+      throw new HttpException('Invalid structure', HttpStatus.NOT_FOUND);
+    }
+    structure.accountVerified = true;
+    structure.save();
+    return structure;
+  }
 }
diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index b301a463f..748493ee0 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -26,6 +26,11 @@ export class StructuresController {
     return this.structureService.search(query.query, body ? body.filters : null);
   }
 
+  @Put('updateAfterOwnerVerify/:id')
+  public async updateAfterOwnerVerify(@Param('id') id: string, @Body() body: structureDto): Promise<Structure> {
+    return this.structureService.update(id, body);
+  }
+
   @Put(':id')
   @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
   @Roles('admin')
diff --git a/src/users/users.service.ts b/src/users/users.service.ts
index 920a233da..28a7bac34 100644
--- a/src/users/users.service.ts
+++ b/src/users/users.service.ts
@@ -185,12 +185,13 @@ export class UsersService {
    * @param userId string
    * @param token string
    */
-  public async validateUser(userId: string, token: string): Promise<any> {
+  public async validateUser(userId: string, token: string): Promise<User> {
     const user = await this.findById(userId);
     if (user && user.validationToken === token) {
       user.validationToken = null;
       user.emailVerified = true;
       user.save();
+      return user;
     } else {
       throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);
     }
-- 
GitLab