mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
feat: tasks jounrey
This commit is contained in:
75
src/task/services/task.service.ts
Normal file
75
src/task/services/task.service.ts
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user