From e6639bb8a5f9f5c1e7781c283af2ba1e8ee48ce6 Mon Sep 17 00:00:00 2001 From: Henry Brink Date: Tue, 18 Jun 2024 21:14:54 +0200 Subject: [PATCH 1/2] fix: crash when sending a notification --- backend/src/notification/emailNotifier.ts | 16 ++++++++++------ backend/src/notification/notification.service.ts | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/src/notification/emailNotifier.ts b/backend/src/notification/emailNotifier.ts index d6c6e11..99cbeb6 100644 --- a/backend/src/notification/emailNotifier.ts +++ b/backend/src/notification/emailNotifier.ts @@ -17,11 +17,15 @@ export class EmailNotifier implements Notifier { } async notify(user: User, title: string, content: string) { - await this.transporter.sendMail({ - from: process.env.EMAIL_USER, - to: user.email, - subject: title, - text: content, - }); + try { + await this.transporter.sendMail({ + from: process.env.EMAIL_USER, + to: user.email, + subject: title, + text: content, + }); + } catch (e) { + console.warn('Error while sending an email: ', e); + } } } diff --git a/backend/src/notification/notification.service.ts b/backend/src/notification/notification.service.ts index 93eff2d..f52c9c3 100644 --- a/backend/src/notification/notification.service.ts +++ b/backend/src/notification/notification.service.ts @@ -21,7 +21,7 @@ export class NotificationService { // only supports email notifications for now this.notifiers?.[user?.notificationMethod ?? '']?.notify( - userId, + user, title, content, ); From 1451a394f3f73197781968260f2911f31f795bd6 Mon Sep 17 00:00:00 2001 From: Henry Brink Date: Tue, 18 Jun 2024 21:31:58 +0200 Subject: [PATCH 2/2] fix: crash on webcron when there is no fitness provider --- backend/src/app/goals/goal.service.spec.ts | 5 +++++ backend/src/app/goals/goal.service.ts | 13 ++++++++++- backend/src/app/streaks/streak.service.ts | 25 +++++++++++++--------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/backend/src/app/goals/goal.service.spec.ts b/backend/src/app/goals/goal.service.spec.ts index 410806c..8659016 100644 --- a/backend/src/app/goals/goal.service.spec.ts +++ b/backend/src/app/goals/goal.service.spec.ts @@ -8,6 +8,7 @@ import { TestConstants } from '../../../test/lib/constants'; import * as dayjs from 'dayjs'; import { FitnessData } from '../../integration/fitness/fitness.data'; import { FitnessGoal } from '../../integration/fitness/fitness.goal'; +import { MockProvider } from '../../integration/fitness/providers/mock.provider'; describe('GoalService', () => { let goalService: GoalService; @@ -73,6 +74,10 @@ describe('GoalService', () => { ], } as FitnessData); + fitnessService.getDatasourcesForUser.mockResolvedValue([ + new MockProvider(), + ]); + goalRepository.updateGoal.mockResolvedValue({ userId: TestConstants.database.users.exampleUser.id, type: 'steps', diff --git a/backend/src/app/goals/goal.service.ts b/backend/src/app/goals/goal.service.ts index 5ac16a9..eee0c41 100644 --- a/backend/src/app/goals/goal.service.ts +++ b/backend/src/app/goals/goal.service.ts @@ -24,7 +24,18 @@ export class GoalService { } public async refreshGoals(userId, existing: Goal[]): Promise { - const fitnessData = await this.fitnessService.getFitnessDataForUser(userId); + // Verify that the user has at least ony fitness service configured + const providers = await this.fitnessService.getDatasourcesForUser(userId); + + if (providers.length < 1) return []; + + let fitnessData; + try { + fitnessData = await this.fitnessService.getFitnessDataForUser(userId); + } catch (e) { + // Seems that the fitness provider is not available anymore + return []; + } if (!fitnessData) { throw new FitnessDataNotAvailable(); diff --git a/backend/src/app/streaks/streak.service.ts b/backend/src/app/streaks/streak.service.ts index 3e5cb39..a5d94e2 100644 --- a/backend/src/app/streaks/streak.service.ts +++ b/backend/src/app/streaks/streak.service.ts @@ -121,21 +121,26 @@ export class StreakService { * @param user ID of the user */ private async verifyDailyGoalsForUser(user: string) { - // Verify that the daily goals have not been met yet. - const streak = await this.getStreakOf(user); + try { + // Verify that the daily goals have not been met yet. + const streak = await this.getStreakOf(user); - if (streak.dailyGoalsReached == true) { - return; - } + if (streak.dailyGoalsReached == true) { + return; + } - const goals = await this.goalService.getGoalsForUser(user); + const goals = await this.goalService.getGoalsForUser(user); - if (goals.length > 0) { - for (const goal of goals) { - if (goal.value > goal.target) { - await this.addPoints(user, 10, true); + if (goals.length > 0) { + for (const goal of goals) { + if (goal.value > goal.target) { + await this.addPoints(user, 10, true); + } } } + } catch (e) { + // If something fails here, this should not stop the others + console.warn(e); } }