All files / src/domains/projects/services task.service.ts

0% Statements 0/27
0% Branches 0/18
0% Functions 0/5
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                                                                                       
import {
  Injectable,
  Logger,
  NotFoundException,
  BadRequestException,
} from '@nestjs/common';
import { EventBusService } from '../../../platform/events/event-bus.service';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { Task } from '../schemas/task.schema';
import { TaskDependency } from '../schemas/task-dependency.schema';
 
@Injectable()
export class TaskService {
  private readonly logger = new Logger(TaskService.name);
 
  constructor(
    @InjectModel(Task.name) private readonly taskModel: Model<Task>,
    @InjectModel(TaskDependency.name)
    private readonly dependencyModel: Model<TaskDependency>,
    private readonly eventBus: EventBusService,
  ) {}
 
  async create(tenantId: string, data: any): Promise<any> {
    const task = new this.taskModel({ ...data, tenantId });
    const savedTask = await task.save();
    if (savedTask.assigneeId) {
      await this.eventBus.publish(
        'task.assigned.v1',
        { taskId: savedTask._id, assigneeId: savedTask.assigneeId },
        tenantId,
      );
    }
    return savedTask;
  }
 
  async findAllByProject(tenantId: string, projectId: string): Promise<any> {
    return this.taskModel.find({ tenantId, projectId }).exec();
  }
 
  async transitionStatus(
    tenantId: string,
    taskId: string,
    newStatus: string,
  ): Promise<any> {
    // Validate cycle dependencies if completing
    const task = await this.taskModel
      .findOneAndUpdate(
        { _id: taskId, tenantId },
        { status: newStatus },
        { new: true },
      )
      .exec();
    if (!task) throw new NotFoundException('Task not found');
    return task;
  }
 
  async addDependency(
    tenantId: string,
    predecessorId: string,
    successorId: string,
    type: string,
  ): Promise<any> {
    if (predecessorId === successorId)
      throw new BadRequestException('Cannot depend on itself');
    // Cycle detection logic should be here
    const dep = new this.dependencyModel({
      tenantId,
      predecessorTaskId: predecessorId,
      successorTaskId: successorId,
      type,
    });
    return dep.save();
  }
}