projectData and version model added
This commit is contained in:
@@ -1,46 +1,47 @@
|
||||
import { Request, Response } from "express";
|
||||
import { createProject } from "../../../shared/services/project/project-Serivices.ts";
|
||||
import { createProject } from "../../../shared/services/project/project-Services.ts";
|
||||
|
||||
export const createProjectController = async (req: Request, res: Response): Promise<void> => {
|
||||
export const createProjectController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const result = await createProject(req.body);
|
||||
console.log("result:", result);
|
||||
console.log("result: ", result);
|
||||
|
||||
switch (result.status) {
|
||||
case "project_exists":
|
||||
res.status(409).json({
|
||||
success: false,
|
||||
message: "Project already exists",
|
||||
});
|
||||
break;
|
||||
|
||||
case "user_not_found":
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
message: "User not found",
|
||||
});
|
||||
break;
|
||||
|
||||
case "success":
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: "Project created successfully",
|
||||
data: result.project,
|
||||
projectId: result.project._id,
|
||||
});
|
||||
break;
|
||||
case "All fields are required":
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error in controller:", error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Internal server error",
|
||||
message: "Unknown error",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import mongoose, { Schema, Connection, Model } from "mongoose";
|
||||
|
||||
import { Client } from "minio";
|
||||
interface ConnectionCache {
|
||||
[key: string]: Connection;
|
||||
}
|
||||
@@ -49,5 +49,12 @@ const MainModel = <T>(
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
export const minioClient = new Client({
|
||||
endPoint: "185.100.212.76", // MinIO server IP or hostname
|
||||
port: 9999, // MinIO server port
|
||||
useSSL: false, // Set to true if SSL is configured
|
||||
accessKey: "sabarinathan", // Access key
|
||||
secretKey: "sabarinathan",
|
||||
});
|
||||
|
||||
export default MainModel;
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
import { Schema, Document, Types } from "mongoose";
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../../connect/mongoose.ts";
|
||||
import {User} from "../user-Model.ts";
|
||||
import { User } from "../user-Model.ts";
|
||||
|
||||
export interface Project extends Document {
|
||||
projectUuid: string;
|
||||
projectName: string;
|
||||
createdBy: User["_id"];
|
||||
isArchive: boolean
|
||||
thumbnail: string
|
||||
sharedUsers: []
|
||||
|
||||
projectUuid: string;
|
||||
projectName: string;
|
||||
createdBy: User["_id"];
|
||||
isArchive: boolean;
|
||||
thumbnail: string;
|
||||
sharedUsers: [];
|
||||
DeletedAt: Date;
|
||||
total_versions: string;
|
||||
Present_version: string;
|
||||
}
|
||||
const projectSchema:Schema = new Schema({
|
||||
const projectSchema: Schema = new Schema(
|
||||
{
|
||||
projectUuid: { type: String, required: true },
|
||||
projectName: { type: String },
|
||||
thumbnail: { type: String },
|
||||
isArchive: { type: Boolean,default:false },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "user" },
|
||||
sharedUsers: [{ type: Schema.Types.ObjectId, ref: "user" }],
|
||||
|
||||
|
||||
}, { timestamps: true })
|
||||
sharedUsers: [{ type: Schema.Types.ObjectId, ref: "user" }],
|
||||
DeletedAt: { type: Date, default: null },
|
||||
total_versions: { type: String },
|
||||
Present_version: { type: String },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
const projectModel = (db: string) => {
|
||||
return MainModel(db, "Projects", projectSchema, "Projects");
|
||||
return MainModel(db, "Projects", projectSchema, "Projects");
|
||||
};
|
||||
|
||||
export default projectModel;
|
||||
export default projectModel;
|
||||
|
||||
27
src/shared/model/version/versionModel.ts
Normal file
27
src/shared/model/version/versionModel.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../../connect/mongoose.ts";
|
||||
import { Project } from "../project/project-model.ts";
|
||||
import { User } from "../user-Model.ts";
|
||||
export interface Version extends Document {
|
||||
versionName: string;
|
||||
projectId: Project["_id"];
|
||||
createdBy: User["_id"];
|
||||
isArchive: boolean;
|
||||
version: number;
|
||||
}
|
||||
const versionSchema: Schema = new Schema(
|
||||
{
|
||||
versionName: { type: String },
|
||||
version: { type: Number },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
projectId: { type: Schema.Types.ObjectId, ref: "project" },
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "user" },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
const versionModel = (db: string) => {
|
||||
return MainModel(db, "Versions", versionSchema, "Versions");
|
||||
};
|
||||
|
||||
export default versionModel;
|
||||
@@ -1,80 +0,0 @@
|
||||
import projectModel from "../../model/project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import { Types } from 'mongoose';
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
createdBy: string; // user ID
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization:string
|
||||
}
|
||||
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
console.log('data: ', data);
|
||||
try {
|
||||
const{projectName,projectUuid,createdBy,thumbnail,sharedUsers,organization}=data
|
||||
console.log('createdBy: ', typeof createdBy);
|
||||
const userExisting =await existingUser(createdBy,organization)
|
||||
if (!userExisting)
|
||||
{
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
|
||||
}
|
||||
const projectExisting = await existingProject(projectUuid, organization);
|
||||
console.log('projectExisting: ', projectExisting);
|
||||
|
||||
if (projectExisting) {
|
||||
return {
|
||||
status: "project_exists",
|
||||
project: projectExisting,
|
||||
};
|
||||
}
|
||||
|
||||
const project = await projectModel(organization).create({
|
||||
projectName: projectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: createdBy,
|
||||
thumbnail: thumbnail || "",
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
return {
|
||||
status: "success",
|
||||
project: project,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return {
|
||||
exists: false,
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export const existingProject = async (projectUuid: string,organization:string) => {
|
||||
console.log("projectUuid",typeof projectUuid);
|
||||
const projectData= await projectModel(organization).findOne({projectUuid:projectUuid,isArchive:false})
|
||||
console.log('projectData: ', projectData);
|
||||
return projectData
|
||||
};
|
||||
|
||||
export const existingUser = async (createdBy: string, organization: string) => {
|
||||
console.log('createdBy: ', typeof createdBy);
|
||||
if (!Types.ObjectId.isValid(createdBy)) {
|
||||
console.log('Invalid ObjectId format');
|
||||
return null;
|
||||
}
|
||||
const userData = await userModel(organization).findOne({
|
||||
_id: createdBy,
|
||||
});
|
||||
console.log('userData:', userData);
|
||||
return userData; // ✅ Make sure you return it
|
||||
};
|
||||
|
||||
|
||||
export const archiveProject = async (projectId: string,organization:string) => {
|
||||
return await projectModel(organization).findByIdAndUpdate(projectId, { isArchive: true }, { new: true });
|
||||
};
|
||||
135
src/shared/services/project/project-Services.ts
Normal file
135
src/shared/services/project/project-Services.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import projectModel from "../../model/project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import { Types } from "mongoose";
|
||||
import versionModel from "../../model/version/versionModel.ts";
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
userId: string; // user ID
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const {
|
||||
projectName,
|
||||
projectUuid,
|
||||
userId,
|
||||
thumbnail,
|
||||
sharedUsers,
|
||||
organization,
|
||||
} = data;
|
||||
if (
|
||||
!projectName ||
|
||||
!projectUuid ||
|
||||
!userId ||
|
||||
!thumbnail ||
|
||||
// !sharedUsers ||
|
||||
!organization
|
||||
)
|
||||
return { status: "All fields are required" };
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProject(projectUuid, organization);
|
||||
|
||||
if (projectExisting) {
|
||||
return {
|
||||
status: "project_exists",
|
||||
project: projectExisting,
|
||||
};
|
||||
}
|
||||
|
||||
const project = await projectModel(organization).create({
|
||||
projectName: projectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail || "",
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
const versionData = previousVersion(project._id, organization);
|
||||
if (!versionData) {
|
||||
await versionModel(organization).create({
|
||||
projectId: project._id,
|
||||
createdBy: userId,
|
||||
version: 0.01,
|
||||
});
|
||||
}
|
||||
return {
|
||||
status: "success",
|
||||
project: project,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const GetAllProjects = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const { userId, organization } = data;
|
||||
await existingUser(userId, organization);
|
||||
if (!existingUser) return { status: "User not found" };
|
||||
const projectDatas = await projectModel(organization)
|
||||
.find({
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail");
|
||||
if (projectDatas) return { Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
export const existingProject = async (
|
||||
projectUuid: string,
|
||||
organization: string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
projectUuid: projectUuid,
|
||||
isArchive: false,
|
||||
});
|
||||
return projectData;
|
||||
};
|
||||
|
||||
export const existingUser = async (userId: string, organization: string) => {
|
||||
if (!Types.ObjectId.isValid(userId)) {
|
||||
console.log("Invalid ObjectId format");
|
||||
return null;
|
||||
}
|
||||
const userData = await userModel(organization).findOne({
|
||||
_id: userId,
|
||||
});
|
||||
return userData; // ✅ Make sure you return it
|
||||
};
|
||||
|
||||
export const archiveProject = async (
|
||||
projectId: string,
|
||||
organization: string
|
||||
) => {
|
||||
return await projectModel(organization).findByIdAndUpdate(
|
||||
projectId,
|
||||
{ isArchive: true },
|
||||
{ new: true }
|
||||
);
|
||||
};
|
||||
export const previousVersion = async (
|
||||
projectId: string,
|
||||
organization: string
|
||||
): Promise<void> => {
|
||||
const result = await versionModel(organization)
|
||||
.findOne({
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
})
|
||||
.sort({ version: -1 });
|
||||
return result;
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Socket } from "socket.io";
|
||||
import { createProject } from "../../../shared/services/project/project-Serivices.ts";
|
||||
import { createProject } from "../../../shared/services/project/project-Services.ts";
|
||||
import { EVENTS } from "../../socket/events.ts";
|
||||
import { emitEventResponse } from "../../socket/socketManager.ts";
|
||||
|
||||
export const projectHandleEvent = async (
|
||||
event: string,
|
||||
@@ -40,7 +39,7 @@ export const projectHandleEvent = async (
|
||||
};
|
||||
|
||||
// Emit response to the organization room
|
||||
emitEventResponse(socket, data.organization, EVENTS.projectResponse, response);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1050,7 +1050,7 @@ try {
|
||||
}
|
||||
|
||||
}
|
||||
export const emitEventResponse = (socket: Socket, organization: string, event: string, result: any) => {
|
||||
const emitEventResponse = (socket: Socket, organization: string, event: string, result: any) => {
|
||||
if (organization) {
|
||||
socket.to(organization).emit(event, {
|
||||
success: result.success,
|
||||
|
||||
Reference in New Issue
Block a user