Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
added findUserById, notification on task completion
Browse files Browse the repository at this point in the history
  • Loading branch information
OG-NI committed Jun 18, 2024
1 parent efa2672 commit 3c809a2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
3 changes: 2 additions & 1 deletion backend/src/app/tasks/task.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { TaskService } from './task.service';
import { TaskController } from './task.controller';
import FitnessModule from '../../integration/fitness/fitness.module';
import { StreakModule } from '../streaks/streak.module';
import { NotificationModule } from 'src/notification/notification.module';

@Module({
imports: [PrismaModule, FitnessModule, StreakModule],
imports: [PrismaModule, FitnessModule, StreakModule, NotificationModule],
providers: [TaskService],
controllers: [TaskController],
})
Expand Down
27 changes: 20 additions & 7 deletions backend/src/app/tasks/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FitnessService } from '../../integration/fitness/fitness.service';
import { Task2 } from './tasks/static/task2';
import { Task3 } from './tasks/static/task3';
import { StreakService } from '../streaks/streak.service';
import { NotificationService } from 'src/notification/notification.service';

export class ConcurrentTaskError extends Error {}
export class TaskNotAvailableError extends Error {}
Expand All @@ -21,6 +22,7 @@ export class TaskService {
private taskRepository: TaskRepository,
private fitnessService: FitnessService,
private streakService: StreakService,
private notificationService: NotificationService,
) {}

private availableTasks = {
Expand Down Expand Up @@ -146,19 +148,30 @@ export class TaskService {
const fitnessData = await this.fitnessService.getFitnessDataForUser(user);

// Verify the task
const taskValidator = new this.availableTasks[log.task]();
const result = taskValidator.validate(
const taskValidator: Task = new this.availableTasks[log.task]();
const isCompleted = taskValidator.validate(
JSON.parse(log.metadata!),
fitnessData,
fitnessData!,
);

console.debug(`Fitness Task verified: ${result}`);
console.debug(`Fitness Task verified: ${isCompleted}`);

// Increase the points of the user
this.streakService.addPoints(user, taskValidator.getInfo().points);
if (isCompleted) {
const taskInfo = taskValidator.getInfo();

// Increase the points of the user
this.streakService.addPoints(user, taskInfo.points);

// send notification
this.notificationService.notify(
user,
'DuoGradus Task completed!',
`Congratulations! You completed the task ${taskInfo.title}. You received ${taskInfo.points} points.`,
);
}

this.taskRepository.updateTaskLog(user, task, {
status: result ? 'completed' : 'failed',
status: isCompleted ? 'completed' : 'failed',
});
}
}
14 changes: 14 additions & 0 deletions backend/src/db/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import { PrismaService } from '../prisma.service';
export class UserRepository {
constructor(private prisma: PrismaService) {}

/**
* Returns a user by his user id
*
* @param id
* @returns
*/
public async findById(id: string): Promise<User | null> {
return await this.prisma.user.findUnique({
where: {
id,
},
});
}

/**
* Returns a user by his email address
*
Expand Down
3 changes: 2 additions & 1 deletion backend/src/notification/notification.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { NotificationService } from './notification.service';
import { ConfigModule } from '@nestjs/config';
import { UserRepository } from 'src/db/repositories/user.repository';

@Module({
imports: [ConfigModule.forRoot()],
imports: [ConfigModule.forRoot(), UserRepository],
providers: [NotificationService],
})
export class NotificationModule {}
16 changes: 12 additions & 4 deletions backend/src/notification/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { Injectable } from '@nestjs/common';
import { User } from '@prisma/client';
import { EmailNotifier } from './emailNotifier';
import { UserRepository } from 'src/db/repositories/user.repository';

@Injectable()
export class NotificationService {
constructor(private userRepository: UserRepository) {}

private readonly notifiers = {
EMAIL: new EmailNotifier(),
};

/**
* Sends a notification to a user via the method specified in the users settings.
* @param user User receiving the notification
* @param user User id receiving the notification
* @param title Title of the notification
* @param content Message content of the notification
*/
notify(user: User, title: string, content: string): void {
async notify(userId: string, title: string, content: string) {
const user = await this.userRepository.findById(userId);

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

0 comments on commit 3c809a2

Please sign in to comment.