diff --git a/src/components/Challenge/OngoingChallengeDetailsView.tsx b/src/components/Challenge/OngoingChallengeDetailsView.tsx index a4887b7e221e339d6993704a82b08d5e963d6e8f..00f0c4a7ed60be66a8f6451b55bd8312c15518c8 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 a2bd77a20242f05e5c930a2418a51f9f70fbbce3..9445806ac8365e1a4ec8c314c53f99f0f9c084cc 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(