Project Create and Get All projects API completed
This commit is contained in:
@@ -2,6 +2,8 @@ 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";
|
||||
import { uploadProjectThumbnail } from "../blob/blobServices.ts";
|
||||
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
@@ -10,7 +12,10 @@ interface CreateProjectInput {
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
|
||||
interface GetInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const {
|
||||
@@ -21,9 +26,9 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
sharedUsers,
|
||||
organization,
|
||||
} = data;
|
||||
|
||||
if (
|
||||
!projectName ||
|
||||
!projectUuid ||
|
||||
!userId ||
|
||||
!thumbnail ||
|
||||
// !sharedUsers ||
|
||||
@@ -36,7 +41,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProject(projectUuid, organization);
|
||||
const projectExisting = await existingProject(projectName, organization,userId);
|
||||
|
||||
if (projectExisting) {
|
||||
return {
|
||||
@@ -49,17 +54,18 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
projectName: projectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail || "",
|
||||
thumbnail: thumbnail,
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
const versionData = previousVersion(project._id, organization);
|
||||
if (!versionData) {
|
||||
await versionModel(organization).create({
|
||||
const versionData = await previousVersion(project._id, organization);
|
||||
if (!versionData || versionData.length === 0) {
|
||||
const newVersion= await versionModel(organization).create({
|
||||
projectId: project._id,
|
||||
createdBy: userId,
|
||||
version: 0.01,
|
||||
});
|
||||
await projectModel(organization).findByIdAndUpdate({_id:project._id,isArchive:false},{total_versions:`v-${newVersion.version.toFixed(2)}`})
|
||||
}
|
||||
return {
|
||||
status: "success",
|
||||
@@ -73,7 +79,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const GetAllProjects = async (data: CreateProjectInput) => {
|
||||
export const GetAllProjects = async (data: GetInterface) => {
|
||||
try {
|
||||
const { userId, organization } = data;
|
||||
await existingUser(userId, organization);
|
||||
@@ -83,18 +89,20 @@ export const GetAllProjects = async (data: CreateProjectInput) => {
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail");
|
||||
if (projectDatas) return { Datas: projectDatas };
|
||||
if (projectDatas) return {status:"success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
export const existingProject = async (
|
||||
projectUuid: string,
|
||||
organization: string
|
||||
projectName: string,
|
||||
organization: string,
|
||||
userId:string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
projectUuid: projectUuid,
|
||||
projectName: projectName,
|
||||
createdBy:userId,
|
||||
isArchive: false,
|
||||
});
|
||||
return projectData;
|
||||
@@ -124,12 +132,10 @@ export const archiveProject = async (
|
||||
export const previousVersion = async (
|
||||
projectId: string,
|
||||
organization: string
|
||||
): Promise<void> => {
|
||||
const result = await versionModel(organization)
|
||||
.findOne({
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
})
|
||||
.sort({ version: -1 });
|
||||
)=> {
|
||||
console.log('projectId: ', projectId);
|
||||
const result = await versionModel(organization).findOne({ projectId: projectId, isArchive: false})
|
||||
console.log('result: ', result);
|
||||
// .sort({ version: -1 });
|
||||
return result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user