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

0% Statements 0/29
0% Branches 0/14
0% Functions 0/6
0% Lines 0/25

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                                                                                                                                   
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 { Project } from '../schemas/project.schema';
 
@Injectable()
export class ProjectService {
  private readonly logger = new Logger(ProjectService.name);
 
  constructor(
    @InjectModel(Project.name) private readonly projectModel: Model<Project>,
    private readonly eventBus: EventBusService,
  ) {}
 
  async create(tenantId: string, data: any): Promise<any> {
    const project = new this.projectModel({ ...data, tenantId });
    const savedProject = await project.save();
    await this.eventBus.publish(
      'project.created.v1',
      { projectId: savedProject._id, projectName: savedProject.projectName },
      tenantId,
    );
    return savedProject;
  }
 
  async findAll(tenantId: string): Promise<any> {
    return this.projectModel.find({ tenantId }).exec();
  }
 
  async findOne(tenantId: string, id: string): Promise<any> {
    const project = await this.projectModel
      .findOne({ _id: id, tenantId })
      .exec();
    if (!project) throw new NotFoundException('Project not found');
    return project;
  }
 
  async update(tenantId: string, id: string, data: any): Promise<any> {
    const project = await this.projectModel
      .findOneAndUpdate({ _id: id, tenantId }, data, { new: true })
      .exec();
    if (!project) throw new NotFoundException('Project not found');
    return project;
  }
 
  async transitionStatus(
    tenantId: string,
    id: string,
    newStatus: string,
  ): Promise<any> {
    const project = await this.findOne(tenantId, id);
    // Allowed transitions check could go here
    project.status = newStatus;
    if (newStatus === 'completed') {
      project.actualEndDate = new Date();
    }
    return project.save();
  }
}