Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix: Crash on E-Mail and Webcron #248

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/src/app/goals/goal.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,10 @@ describe('GoalService', () => {
],
} as FitnessData);

fitnessService.getDatasourcesForUser.mockResolvedValue([
new MockProvider(),
]);

goalRepository.updateGoal.mockResolvedValue({
userId: TestConstants.database.users.exampleUser.id,
type: 'steps',
Expand Down
13 changes: 12 additions & 1 deletion backend/src/app/goals/goal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ export class GoalService {
}

public async refreshGoals(userId, existing: Goal[]): Promise<Goal[]> {
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();
Expand Down
25 changes: 15 additions & 10 deletions backend/src/app/streaks/streak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
16 changes: 10 additions & 6 deletions backend/src/notification/emailNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion backend/src/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class NotificationService {

// only supports email notifications for now
this.notifiers?.[user?.notificationMethod ?? '']?.notify(
userId,
user,
title,
content,
);
Expand Down
Loading