From ee73c6e8954c8de55fda18708738c563aaa4c16f Mon Sep 17 00:00:00 2001
From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com>
Date: Fri, 25 Sep 2020 17:33:29 +0200
Subject: [PATCH] feat: review of service for cancelChallenge

---
 .../Challenge/OngoingChallengeDetailsView.tsx |   5 +-
 src/services/challenge.service.ts             | 118 +++++++++---------
 2 files changed, 63 insertions(+), 60 deletions(-)

diff --git a/src/components/Challenge/OngoingChallengeDetailsView.tsx b/src/components/Challenge/OngoingChallengeDetailsView.tsx
index a4887b7e2..00f0c4a7e 100644
--- a/src/components/Challenge/OngoingChallengeDetailsView.tsx
+++ b/src/components/Challenge/OngoingChallengeDetailsView.tsx
@@ -70,10 +70,7 @@ const OngoingChallengeDetailsView: React.FC<OngoingChallengeDetailsViewProps> =
 
   async function stopChallenge(_challenge: UserChallenge) {
     if (_challenge) {
-      await challengeService.updateChallengeState(
-        _challenge.id,
-        ChallengeState.ABANDONED
-      )
+      await challengeService.cancelChallenge(_challenge.id)
       const updatednotificationEcogesture = userProfile.notificationEcogesture.filter(
         (x: string) =>
           x !== _challenge.selectedEcogestures[0].id &&
diff --git a/src/services/challenge.service.ts b/src/services/challenge.service.ts
index a2bd77a20..9445806ac 100644
--- a/src/services/challenge.service.ts
+++ b/src/services/challenge.service.ts
@@ -218,19 +218,24 @@ export default class ChallengeService {
   public async updateChallengeState(
     id: string | undefined,
     newState: number
-  ): Promise<UserChallenge | null> {
-    const updateUserChallenge = await this._client
-      .query(
-        this._client
-          .find(USERCHALLENGE_DOCTYPE)
-          .where({ _id: id })
-          .limitBy(1)
-      )
-      .then(async ({ data }) => {
-        const doc = data[0]
-        await this._client.save({ ...doc, state: newState })
-      })
-    return updateUserChallenge
+  ): Promise<boolean> {
+    try {
+      await this._client
+        .query(
+          this._client
+            .find(USERCHALLENGE_DOCTYPE)
+            .where({ _id: id })
+            .limitBy(1)
+        )
+        .then(async ({ data }) => {
+          const doc = data[0]
+          await this._client.save({ ...doc, state: newState })
+        })
+      return true
+    } catch (err) {
+      console.log(err)
+      throw err
+    }
   }
 
   public async isChallengeOver(
@@ -318,30 +323,37 @@ export default class ChallengeService {
   public async endChallenge(
     challenge: UserChallenge,
     fluidTypes: FluidType[]
-  ): Promise<UserChallenge | null> {
-    let endedChallenge = null
-    if (challenge && challenge.challengeType) {
-      if (await this.isChallengeOver(challenge, fluidTypes)) {
-        if (challenge.challengeType.type === TypeChallenge.ACHIEVEMENT) {
-          endedChallenge = await this.updateChallengeState(
-            challenge.id,
-            ChallengeState.FINISHED
-          )
-          await this.updateUserLevel(challenge.challengeType.level)
-        } else {
-          if (
-            this.getTheRightBadge(challenge.currentEnergy, challenge.maxEnergy)
-          ) {
+  ): Promise<boolean> {
+    try {
+      if (challenge && challenge.challengeType) {
+        if (await this.isChallengeOver(challenge, fluidTypes)) {
+          if (challenge.challengeType.type === TypeChallenge.ACHIEVEMENT) {
+            await this.updateChallengeState(
+              challenge.id,
+              ChallengeState.FINISHED
+            )
             await this.updateUserLevel(challenge.challengeType.level)
+          } else {
+            if (
+              this.getTheRightBadge(
+                challenge.currentEnergy,
+                challenge.maxEnergy
+              )
+            ) {
+              await this.updateUserLevel(challenge.challengeType.level)
+            }
+            await this.updateChallengeState(
+              challenge.id,
+              ChallengeState.FINISHED
+            )
           }
-          endedChallenge = await this.updateChallengeState(
-            challenge.id,
-            ChallengeState.FINISHED
-          )
         }
       }
+      return true
+    } catch (err) {
+      console.log(err)
+      throw err
     }
-    return endedChallenge
   }
 
   /*
@@ -398,12 +410,17 @@ export default class ChallengeService {
   }
 
   public async deleteAllChallengeTypeEntities(): Promise<boolean> {
-    const challengeType = await this.getAllChallengeTypeEntities()
-    if (!challengeType) return true
-    for (let index = 0; index < challengeType.length; index++) {
-      await this._client.destroy(challengeType[index])
+    try {
+      const challengeType = await this.getAllChallengeTypeEntities()
+      if (!challengeType) return true
+      for (let index = 0; index < challengeType.length; index++) {
+        await this._client.destroy(challengeType[index])
+      }
+      return true
+    } catch (err) {
+      console.log(err)
+      throw err
     }
-    return true
   }
 
   public async getAvailableChallenges(
@@ -635,26 +652,15 @@ export default class ChallengeService {
     return userChallenge
   }
 
-  public async cancelChallenge(
-    id: string | undefined
-  ): Promise<UserChallenge | null> {
-    const updateUserChallenge = await this._client
-      .query(
-        this._client
-          .find(USERCHALLENGE_DOCTYPE)
-          .where({ _id: id })
-          .limitBy(1)
-      )
-      .then(async ({ data }) => {
-        const doc = data[0]
-        await this._client.save({
-          ...doc,
-          state: ChallengeState.ABANDONED,
-        })
-      })
-    return updateUserChallenge
+  public async cancelChallenge(id: string): Promise<boolean> {
+    try {
+      await this.updateChallengeState(id, ChallengeState.ABANDONED)
+      return true
+    } catch (err) {
+      console.log(err)
+      throw err
+    }
   }
-
   public async getUnlockedEcogestures(): Promise<string[] | null> {
     const relationShipsToInclude = ['selectedEcogestures']
     const ecogestures = await this._client.query(
-- 
GitLab