Mongoose Schema

    import * as mongoose from 'mongoose';


const FeatureSchema = new mongoose.Schema({
name: { type: String, required: true },
status: { type: String, required: true },
assignedTo: { type: String, required: false },
active: { type: Boolean, required: true, default: true },
priority: { type: Number, required: true },
createdDate: { type: String, required: true },
modifiedDate: { type: String, required: true },
tasks: { type: Array, required: true },
});

const ModuleSchema = new mongoose.Schema({
name: { type: String, required: true },
createdDate: { type: String, required: true },
modifiedDate: { type: String, required: true },
features: { type: [FeatureSchema], required: false },
});

const Repository = new mongoose.Schema({
githubUrl: { type: String, required: false },
codeQualityUrl: { type: String, required: false },
testUrl: { type: String, required: false },
developers: { type: Array, required: false }
});

export const ProjectSchema = new mongoose.Schema(
{
name: { type: String, required: true },
description: { type: String, required: false },
prefix: { type: String, required: true },
createdBy: { type: String, required: true },
assignedTo: { type: String, required: true },
startDate: { type: String, required: true },
status: { type: String, required: true },
published: { type: Boolean, required: true },
enableLeaderboard: { type: Boolean, required: true },
modules: { type: [ModuleSchema], required: false },
active: { type: Boolean, required: true, default: true },
createdDate: { type: String, required: true },
modifiedDate: { type: String, required: true },
githubUrl : { type: String, required : false},
sonarCloudUrl : { type: String, required : false},
projectTestUrl : { type: String, required : false},
developers: { type: Array, required: false },
badges: { type: Array, required: false }
}
);

export interface Project extends mongoose.Document {
githubUrl: String;
sonarCloudUrl:String;
projectTestUrl:String;

id: number;
name: String;
prefix:String;
description:String;
published: boolean;
active: boolean;
modules: Array<any>;
badges: Array<any>;
}

ProjectDAO


import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Project, ProjectSchema } from 'src/model/project.model';

@Injectable()
export class ProjectDAO {

private readonly logger = new Logger(ProjectDAO.name);


constructor(
@InjectModel('Project') private readonly projectModel: Model<Project>
) {}


async list() {
const results = await this.projectModel.find().exec();

return results;
}

async findUserProjects(userId:string) {
const results = await this.projectModel.find({assignedTo: userId}).exec();

return results;
}

async exists(name: string, assignedTo: string) {
const result = await this.projectModel.exists({ name: name, assignedTo: assignedTo });

return result;
}


async isProjectExists(id: string) {
const result = await this.projectModel.exists({ _id: id });
this.logger.debug(result);
return result;
}

async findOne(_id: any) {
this.logger.debug('findOne', _id);
const result = await this.projectModel.findById(_id).exec();
return result;
}

async save(projectObj: any): Promise<string> {
let createdDate = new Date();
let startDate = new Date().toJSON().substr(0,10);
const newproject = new this.projectModel({
prefix: projectObj.prefix,
name: projectObj.name,
status: 'IN_PROGRESS',
createdBy: projectObj.createdBy,
assignedTo: projectObj.assignedTo,
startDate: startDate,
createdDate: createdDate.toJSON(),
modifiedDate: createdDate.toJSON(),
endDate: null,
active: true,
modules:[],
published : false,
enableLeaderboard: projectObj.enableLeaderboard
});
const result: any = await newproject.save();
return result.id;
}

async update(projectObj: any) {
const project = await this.projectModel.findById(projectObj._id).exec();
project.published = true;
const result = await project.save();
return result.id;
}

async updateSettings(projectObj: any) {
const project:any = await this.projectModel.findById(projectObj._id).exec();
if(projectObj.githubUrl){
project.githubUrl = projectObj.githubUrl;
}
if(projectObj.sonarCloudUrl){
project.sonarCloudUrl = projectObj.sonarCloudUrl;
}
if(projectObj.description){
project.description = projectObj.description;
}
if(projectObj.developers){
project.developers = projectObj.developers;
}
const result = await project.save();
return result.id;
}


async updateBadge(projecId: string, badge:string, active:boolean ) {
const project:any = await this.projectModel.findById(projecId).exec();
//this.logger.debug(project);
let badges = project["badges"] !=null ? project.badges : [];
this.logger.debug("badges" , badges);
if(active && badges.indexOf(badge) !=-1){
badges.push(badge);
}
project["badges"] = badges;
this.logger.debug(project);
const result = await project.save();
return project;
}

async publish(id: string) {
const project = await this.projectModel.findById(id).exec();
this.logger.debug(project);
project.published = true;
const result = await project.save();
return result.id;
}

async delete(id: string) {
const project = await this.projectModel.findById(id).exec();
project.active = false;
const result = await project.save();
return result.id;
}

async addModule(id: string, moduleObj: any) {
let createdDate = new Date();


const project = await this.projectModel.findById(id).exec();
this.logger.debug(project);
const modules = project.modules ? project.modules : [];
this.logger.debug(modules);
if (!modules.some((obj) => obj.name == moduleObj.name)) {

const cObj = { name: moduleObj.name , features:[] , createdDate: createdDate.toJSON(),
modifiedDate: createdDate.toJSON()};
modules.push(cObj);
}
project.modules = modules;
const result = await project.save();
return result.id;
}

async addFeature(id: string, moduleId: string, feature: any) {
const project = await this.projectModel.findById(id).exec();
const modules = project.modules;
const index = modules.findIndex((obj) => obj._id == moduleId);
this.logger.debug('Index='+ index);
const selectedModule = modules[index];
const features = selectedModule.features ? selectedModule.features : [];
feature["status"]="PENDING";
feature["tasks"] = [];
let createdDate = new Date();
feature["createdDate"] = createdDate.toJSON(),
feature["modifiedDate"] = createdDate.toJSON(),
feature["createdBy"] = feature["createdBy"];
feature["assignedTo"] = project["assignedTo"];
feature["priority"] = feature["priority"] != null ? feature.priority : 1;
features.push(feature);
selectedModule['features'] = features;
project.modules[index] = selectedModule;
const result = await project.save();
this.logger.debug('result', JSON.stringify(result));
return result;
}

async deleteFeature(id: string, moduleId: string, featureId: string) {
const project = await this.projectModel.findById(id).exec();
const modules = project.modules;
const index = modules.findIndex((obj) => obj._id == moduleId);
this.logger.debug('Index='+ index);
if(index != -1){
const selectedModule = modules[index];
const fieldIndex = selectedModule.features.findIndex(
(f) => f._id == featureId
);
selectedModule.features.splice(fieldIndex, 1);
project.modules[index] = selectedModule;
const result = await project.save();
this.logger.debug('result', JSON.stringify(result));
return result;
}
else{
this.logger.debug("not found");
return false;
}
}

async updateFeatureStatus(id: string, moduleId: string, featureId: string,status:string) {
const project = await this.projectModel.findById(id).exec();
const modules = project.modules;
const index = modules.findIndex((obj) => obj._id == moduleId);
this.logger.debug('Index='+ index);
if(index != -1){
const selectedModule = modules[index];
const fieldIndex = selectedModule.features.findIndex(
(f) => f._id == featureId
);
selectedModule.features[fieldIndex].status = status;
project.modules[index] = selectedModule;
const result = await project.save();
this.logger.debug('result', JSON.stringify(result));
return result;
}
else{
this.logger.debug("not found");
return false;
}
}

async deleteModule(id: any, moduleId: string) {
const project = await this.projectModel.findById(id).exec();
const modules = project.modules ? project.modules : [];
const index = modules.findIndex((obj) => obj._id == moduleId);
if (index != -1) {
modules.splice(index, 1);
}
project.modules = modules;
const result = await project.save();
return result.id;
}


}