<commit_before>/* istanbul ignore file */
// TODO add tests to improve coverage

import { Injectable, UnauthorizedException } from '@nestjs/common';
// import { LearnroomFacade } from '@modules/learnroom';
import { EntityId, IPagination, Counted, ICurrentUser } from '@shared/domain';

import { LearnroomFacade } from '../../learnroom';

import { TaskRepo, SubmissionRepo } from '../repo';
import { EntityArray } from '../utils';
import { TaskDomainService, TaskWithSubmissionStatus } from '../domain';

// define interface for task and submission repo

@Injectable()
export class TaskUC {
	// It should remove later
	permissions = {
		teacherDashboard: 'TASK_DASHBOARD_TEACHER_VIEW_V3',
		studentDashboard: 'TASK_DASHBOARD_VIEW_V3',
	};

	// TODO: IMPORTANT steps for this PULL REQUEST
	// delete all learnroom stuff, move repo of course and coursegroups to a new top level repo layer
	// do the same for course and coursgroup entity
	// delete coursegroup info entity and replace it with final entity
	// remove coursegroup entity from repo and pass it from uc
	constructor(
		private readonly taskRepo: TaskRepo,
		private readonly submissionRepo: SubmissionRepo,
		private readonly learnroomFacade: LearnroomFacade
	) {}

	async findAllOpenForStudent(userId: EntityId, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		// Important the facade stategue is only a temporary solution until we established a better way for resolving the dependency graph
		const [coursesWithGroups] = await this.learnroomFacade.findCoursesWithGroupsByUserId(userId);
		const courses = new EntityArray(coursesWithGroups);

		const [submissionsOfStudent] = await this.submissionRepo.getAllSubmissionsByUser(userId);
		const taskIdsThatHaveSubmissions = submissionsOfStudent.map((submission) => submission.task.id);

		const [tasks, total] = await this.taskRepo.findAllByStudent(
			courses.getIds(),
			pagination,
			taskIdsThatHaveSubmissions
		);

		const domain = new TaskDomainService(courses, tasks);
		domain.addParentToTasks();
		// after add status to task it is not nessasray to return it directly
		// we can do the step and in the end use prepareTasks.getResult();
		const computedTasks = domain.computeStatusForStudents(submissionsOfStudent);

		return [computedTasks, total];
	}

	async findAllOpenByTeacher(userId: EntityId, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		// Important the facade stategue is only a temporary solution until we established a better way for resolving the dependency graph
		const [coursesWithGroups] = await this.learnroomFacade.findCoursesWithGroupsByUserId(userId);

		// Add Authorization service or logic until it is avaible for learnroom
		const courseWithGroupsWithWritePermissions = coursesWithGroups.filter((course) =>
			course.hasWritePermission(userId)
		);
		const courses = new EntityArray(courseWithGroupsWithWritePermissions);

		const [tasks, total] = await this.taskRepo.findAllAssignedByTeacher(courses.getIds(), pagination);
		const [submissionsOfTeacher] = await this.submissionRepo.getSubmissionsByTasksList(tasks);

		const domain = new TaskDomainService(courses, tasks);
		domain.addParentToTasks();
		// after add status to task it is not nessasray to return it directly
		// we can do the step and in the end use prepareTasks.getResult();
		const computedTasks = domain.computeStatusForTeachers(submissionsOfTeacher);

		return [computedTasks, total];
	}

	// should remove in future, permissions are not needed
	// maybe add different endpoints and if student ask teacher endpoint they get nothing
	async findAllOpen(currentUser: ICurrentUser, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		const {
			user: { id, permissions },
		} = currentUser;

		let response: Counted<TaskWithSubmissionStatus[]>;
		if (permissions.includes(this.permissions.teacherDashboard)) {
			response = await this.findAllOpenByTeacher(id, pagination);
		} else if (permissions.includes(this.permissions.studentDashboard)) {
			response = await this.findAllOpenForStudent(id, pagination);
		} else {
			throw new UnauthorizedException();
		}

		return response;
	}
}
<commit_msg>Add comment with optional steps for this pr.<commit_after>import { Injectable, UnauthorizedException } from '@nestjs/common';
import { EntityId, IPagination, Counted, ICurrentUser } from '@shared/domain';

import { LearnroomFacade } from '../../learnroom';

import { TaskRepo, SubmissionRepo } from '../repo';
import { EntityArray } from '../utils';
import { TaskDomainService, TaskWithSubmissionStatus } from '../domain';

@Injectable()
export class TaskUC {
	// It should remove later
	permissions = {
		teacherDashboard: 'TASK_DASHBOARD_TEACHER_VIEW_V3',
		studentDashboard: 'TASK_DASHBOARD_VIEW_V3',
	};

	// TODO: IMPORTANT steps for this PULL REQUEST
	// delete all learnroom stuff, move repo of course and coursegroups to a new top level repo layer
	// do the same for course and coursgroup entity
	// delete coursegroup info entity and replace it with final entity
	// remove coursegroup entity from repo and pass it from uc
	// move submissions and task with repos to new area
	// cleanup and refactor task and submissions entitys > props and methodes
	// write tests for uc
	// -------------optional----------
	// add short cut imports for entitys and for repos
	// think about if status can move to task entity, than we can move logic also to it.
	// ------------------
	// next steps remove lesson info and replace with lesson
	// add lesson repo to this uc and pass data over uc
	constructor(
		private readonly taskRepo: TaskRepo,
		private readonly submissionRepo: SubmissionRepo,
		private readonly learnroomFacade: LearnroomFacade
	) {}

	async findAllOpenForStudent(userId: EntityId, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		// Important the facade stategue is only a temporary solution until we established a better way for resolving the dependency graph
		const [coursesWithGroups] = await this.learnroomFacade.findCoursesWithGroupsByUserId(userId);
		const courses = new EntityArray(coursesWithGroups);

		const [submissionsOfStudent] = await this.submissionRepo.getAllSubmissionsByUser(userId);
		const taskIdsThatHaveSubmissions = submissionsOfStudent.map((submission) => submission.task.id);

		const [tasks, total] = await this.taskRepo.findAllByStudent(
			courses.getIds(),
			pagination,
			taskIdsThatHaveSubmissions
		);

		const domain = new TaskDomainService(courses, tasks);
		domain.addParentToTasks();
		// after add status to task it is not nessasray to return it directly
		// we can do the step and in the end use prepareTasks.getResult();
		const computedTasks = domain.computeStatusForStudents(submissionsOfStudent);

		return [computedTasks, total];
	}

	async findAllOpenByTeacher(userId: EntityId, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		// Important the facade stategue is only a temporary solution until we established a better way for resolving the dependency graph
		const [coursesWithGroups] = await this.learnroomFacade.findCoursesWithGroupsByUserId(userId);

		// Add Authorization service or logic until it is avaible for learnroom
		const courseWithGroupsWithWritePermissions = coursesWithGroups.filter((course) =>
			course.hasWritePermission(userId)
		);
		const courses = new EntityArray(courseWithGroupsWithWritePermissions);

		const [tasks, total] = await this.taskRepo.findAllAssignedByTeacher(courses.getIds(), pagination);
		const [submissionsOfTeacher] = await this.submissionRepo.getSubmissionsByTasksList(tasks);

		const domain = new TaskDomainService(courses, tasks);
		domain.addParentToTasks();
		// after add status to task it is not nessasray to return it directly
		// we can do the step and in the end use prepareTasks.getResult();
		const computedTasks = domain.computeStatusForTeachers(submissionsOfTeacher);

		return [computedTasks, total];
	}

	// should remove in future, permissions are not needed
	// maybe add different endpoints and if student ask teacher endpoint they get nothing
	async findAllOpen(currentUser: ICurrentUser, pagination: IPagination): Promise<Counted<TaskWithSubmissionStatus[]>> {
		const {
			user: { id, permissions },
		} = currentUser;

		let response: Counted<TaskWithSubmissionStatus[]>;
		if (permissions.includes(this.permissions.teacherDashboard)) {
			response = await this.findAllOpenByTeacher(id, pagination);
		} else if (permissions.includes(this.permissions.studentDashboard)) {
			response = await this.findAllOpenForStudent(id, pagination);
		} else {
			throw new UnauthorizedException();
		}

		return response;
	}
}

{'fraction_non_alphanumeric': 0.08830631252155915, 'fraction_numerical': 0.00045992871104978727, 'mean_word_length': 4.945317840054682, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6948663', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2409, 'n_words': 784}