feat: tasks jounrey

This commit is contained in:
Abdalhamid Alhamad
2024-12-11 10:27:51 +03:00
parent d539073f29
commit 749ee5457f
27 changed files with 654 additions and 3 deletions

View File

@ -0,0 +1,75 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { FindOptionsWhere } from 'typeorm';
import { IJwtPayload } from '~/auth/interfaces';
import { CreateTaskRequestDto, TasksFilterOptions, TaskSubmissionRequestDto } from '../dtos/request';
import { Task } from '../entities';
import { SubmissionStatus, TaskStatus } from '../enums';
import { TaskRepository } from '../repositories';
@Injectable()
export class TaskService {
constructor(private readonly taskRepository: TaskRepository) {}
async createTask(userId: string, body: CreateTaskRequestDto) {
const task = await this.taskRepository.createTask(userId, body);
return this.findTask({ id: task.id });
}
async findTask(where: FindOptionsWhere<Task>) {
const task = await this.taskRepository.findTask(where);
if (!task) {
throw new BadRequestException('TASK.NOT_FOUND');
}
return task;
}
findTasks(user: IJwtPayload, query: TasksFilterOptions) {
return this.taskRepository.findTasks(user, query);
}
async submitTask(userId: string, taskId: string, body: TaskSubmissionRequestDto) {
const task = await this.findTask({ id: taskId, assignedToId: userId });
if (task.status == TaskStatus.COMPLETED) {
throw new BadRequestException('TASK.ALREADY_COMPLETED');
}
if (task.submission && task.submission.status !== SubmissionStatus.REJECTED) {
throw new BadRequestException('TASK.ALREADY_SUBMITTED');
}
if (task.isProofRequired && !body.imageId) {
throw new BadRequestException('TASK.PROOF_REQUIRED');
}
await this.taskRepository.createSubmission(task, body);
}
async approveTaskSubmission(userId: string, taskId: string) {
const task = await this.findTask({ id: taskId, assignedById: userId });
if (!task.submission) {
throw new BadRequestException('TASK.NO_SUBMISSION');
}
if (task.submission.status !== SubmissionStatus.PENDING) {
throw new BadRequestException('TASK.SUBMISSION_ALREADY_REVIEWED');
}
await this.taskRepository.approveSubmission(task.submission);
}
async rejectTaskSubmission(userId: string, taskId: string) {
const task = await this.findTask({ id: taskId, assignedById: userId });
if (!task.submission) {
throw new BadRequestException('TASK.NO_SUBMISSION');
}
if (task.submission.status !== SubmissionStatus.PENDING) {
throw new BadRequestException('TASK.SUBMISSION_ALREADY_REVIEWED');
}
await this.taskRepository.rejectSubmission(task.submission);
}
}