V1 folder created For auth Data project, trash, home page based on the token and role based
This commit is contained in:
@@ -2,7 +2,6 @@ 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 AuthModel from "../../V1Models/Auth/userAuthModel.ts";
|
||||
export const existingProject = async (
|
||||
projectUuid: string,
|
||||
organization: string,
|
||||
@@ -21,7 +20,7 @@ export const existingUser = async (userId: string, organization: string) => {
|
||||
console.log("Invalid ObjectId format");
|
||||
return null;
|
||||
}
|
||||
const userData = await AuthModel(organization).findOne({
|
||||
const userData = await userModel(organization).findOne({
|
||||
_id: userId,
|
||||
});
|
||||
return userData;
|
||||
|
||||
90
src/shared/services/helpers/v1projecthelperFns.ts
Normal file
90
src/shared/services/helpers/v1projecthelperFns.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||
import { Types } from "mongoose";
|
||||
import versionModel from "../../V1Models/Version/versionModel.ts";
|
||||
import AuthModel from "../../V1Models/Auth/userAuthModel.ts";
|
||||
export const existingProject = async (
|
||||
projectUuid: string,
|
||||
organization: string,
|
||||
userId: string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
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 AuthModel(organization).findOne({
|
||||
_id: userId,
|
||||
});
|
||||
return userData;
|
||||
};
|
||||
|
||||
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
|
||||
) => {
|
||||
const result = await versionModel(organization).findOne({
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
});
|
||||
// .sort({ version: -1 });
|
||||
return result;
|
||||
};
|
||||
export const generateUntitledProjectName = async (
|
||||
organization: string,
|
||||
userId: string
|
||||
): Promise<string> => {
|
||||
const projects = await projectModel(organization)
|
||||
.find({
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
projectName: { $regex: /^Untitled(?: \s?\d+)?$/, $options: "i" },
|
||||
})
|
||||
.select("projectName");
|
||||
|
||||
const usedNumbers = new Set<number>();
|
||||
|
||||
for (const proj of projects) {
|
||||
const match = proj.projectName.match(/^Untitled(?:\s?(\d+))?$/);
|
||||
if (match) {
|
||||
const num = match[1] ? parseInt(match[1], 10) : 0;
|
||||
usedNumbers.add(num);
|
||||
}
|
||||
}
|
||||
|
||||
let newNumber = 0;
|
||||
while (usedNumbers.has(newNumber)) {
|
||||
newNumber++;
|
||||
}
|
||||
|
||||
return newNumber === 0 ? "Untitled" : `Untitled ${newNumber}`;
|
||||
};
|
||||
export const existingProjectById = async (
|
||||
projectId: string,
|
||||
organization: string,
|
||||
userId: string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
return projectData;
|
||||
};
|
||||
@@ -1,12 +1,10 @@
|
||||
import projectModel from "../../model/project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
||||
|
||||
interface IRecentData {
|
||||
organization: string;
|
||||
userId: string;
|
||||
role: string;
|
||||
}
|
||||
interface IProject {
|
||||
_id: string;
|
||||
@@ -21,31 +19,26 @@ interface searchProjectInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
export const RecentlyAdded = async (data: IRecentData) => {
|
||||
try {
|
||||
const { userId, organization, role } = data;
|
||||
const { userId, organization } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const userRecentData = await UsersDataModel(organization)
|
||||
.findOne({ userId: userId, isArchive: false })
|
||||
const userRecentData = await userModel(organization)
|
||||
.findOne({ _id: userId })
|
||||
.populate({
|
||||
path: "recentlyViewed",
|
||||
model: projectModel(organization),
|
||||
select: "_id",
|
||||
});
|
||||
let filter = { isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const populatedProjects = userRecentData.recentlyViewed as IProject[];
|
||||
const RecentDatas = await Promise.all(
|
||||
populatedProjects.map(async (project) => {
|
||||
const projectExisting = await projectModel(organization)
|
||||
.findOne(filter)
|
||||
.findOne({
|
||||
_id: project._id,
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
||||
return projectExisting;
|
||||
})
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
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 { AuthenticatedRequest } from "../../utils/token.ts";
|
||||
import {
|
||||
existingProject,
|
||||
existingUser,
|
||||
@@ -10,7 +9,6 @@ import {
|
||||
previousVersion,
|
||||
generateUntitledProjectName,
|
||||
} from "../helpers/ProjecthelperFn.ts";
|
||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
@@ -26,26 +24,20 @@ interface updateProjectInput {
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface GetProjectsInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface ProjectInterface {
|
||||
projectId: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const { thumbnail, sharedUsers, projectUuid, userId, organization } = data;
|
||||
const { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
@@ -102,16 +94,17 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
|
||||
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
try {
|
||||
const { userId, organization, role } = data;
|
||||
const { userId, organization } = data;
|
||||
await existingUser(userId, organization);
|
||||
if (!existingUser) return { status: "User not found" };
|
||||
let filter = { isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const projectDatas = await projectModel(organization)
|
||||
.find(filter)
|
||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||
.find({
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
})
|
||||
.select(
|
||||
"_id projectName createdBy thumbnail createdAt projectUuid createdAt"
|
||||
);
|
||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
@@ -120,17 +113,17 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
|
||||
export const DeleteProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const { projectId, organization, userId } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const updateProject = await projectModel(organization).findOneAndUpdate(
|
||||
filter,
|
||||
{ _id: projectId, isArchive: false },
|
||||
{ isArchive: true, DeletedAt: new Date() },
|
||||
{ new: true }
|
||||
);
|
||||
@@ -141,25 +134,24 @@ export const DeleteProject = async (data: ProjectInterface) => {
|
||||
};
|
||||
export const updateProject = async (data: updateProjectInput) => {
|
||||
try {
|
||||
const { projectId, organization, userId, projectName, thumbnail, role } =
|
||||
data;
|
||||
const { projectId, organization, userId, projectName, thumbnail } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
if (projectName !== undefined) projectName;
|
||||
if (thumbnail !== undefined) thumbnail;
|
||||
const updateProject = await projectModel(organization)
|
||||
.findOneAndUpdate(
|
||||
filter,
|
||||
{ _id: projectId, isArchive: false },
|
||||
{ projectName: projectName, thumbnail: thumbnail },
|
||||
{ new: true }
|
||||
)
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||
if (updateProject) return { status: "Success", data: updateProject };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
@@ -168,23 +160,18 @@ export const updateProject = async (data: updateProjectInput) => {
|
||||
const maxLength: number = 6;
|
||||
export const viewProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const { projectId, organization, userId } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const RecentUserDoc = await UsersDataModel(organization).findOne({
|
||||
userId: userId,
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const newArr = RecentUserDoc?.recentlyViewed || [];
|
||||
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
||||
const newArr = userExisting?.recentlyViewed || [];
|
||||
if (userExisting?.recentlyViewed.length === 0) {
|
||||
newArr.push(projectId);
|
||||
await RecentUserDoc.save();
|
||||
} else {
|
||||
const index = newArr.indexOf(projectId);
|
||||
if (index !== -1) {
|
||||
@@ -196,13 +183,21 @@ export const viewProject = async (data: ProjectInterface) => {
|
||||
newArr.pop();
|
||||
}
|
||||
}
|
||||
await UsersDataModel(organization).updateOne(
|
||||
await userModel(organization).updateOne(
|
||||
{ _id: userId },
|
||||
{ recentlyViewed: newArr },
|
||||
{ new: true }
|
||||
);
|
||||
const projectData = await projectModel(organization)
|
||||
.findOneAndUpdate(filter, { isViewed: Date.now() }, { new: true })
|
||||
.findOneAndUpdate(
|
||||
{
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
},
|
||||
{ isViewed: Date.now() },
|
||||
{ new: true }
|
||||
)
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
return { status: "Success", data: projectData };
|
||||
} catch (error: unknown) {
|
||||
|
||||
@@ -1,27 +1,19 @@
|
||||
import projectModel from "../../model/project/project-model.ts";
|
||||
interface IOrg {
|
||||
organization: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
}
|
||||
interface IRestore {
|
||||
projectId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
export const TrashDatas = async (data: IOrg) => {
|
||||
try {
|
||||
const { organization, role, userId } = data;
|
||||
let filter = { isArchive: true, isDeleted: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const TrashLists = await projectModel(organization).find(filter);
|
||||
const { organization } = data;
|
||||
|
||||
const TrashLists = await projectModel(organization).find({
|
||||
isArchive: true,
|
||||
isDeleted: false,
|
||||
});
|
||||
if (!TrashLists) return { staus: "Trash is Empty" };
|
||||
const TrashDocs: any[] = [];
|
||||
for (const Trash of TrashLists) {
|
||||
@@ -55,15 +47,14 @@ export const TrashDatas = async (data: IOrg) => {
|
||||
};
|
||||
export const RestoreTrashData = async (data: IRestore) => {
|
||||
try {
|
||||
const { projectId, organization, role, userId } = data;
|
||||
let filter = { isArchive: true, _id: projectId } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const findProject = await projectModel(organization).findOne(filter);
|
||||
const { projectId, organization } = data;
|
||||
const findProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
isArchive: true,
|
||||
});
|
||||
if (!findProject) return { status: "Project not found" };
|
||||
const restoreData = await projectModel(organization).findOneAndUpdate(
|
||||
filter,
|
||||
{ _id: projectId, isArchive: true },
|
||||
{ isArchive: false, DeletedAt: null },
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
208
src/shared/services/v1Project/v1projectservice.ts
Normal file
208
src/shared/services/v1Project/v1projectservice.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||
import versionModel from "../../V1Models/Version/versionModel.ts";
|
||||
import {
|
||||
existingProject,
|
||||
existingUser,
|
||||
archiveProject,
|
||||
previousVersion,
|
||||
generateUntitledProjectName,
|
||||
} from "../helpers/v1projecthelperFns.ts";
|
||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
userId: string; // user ID
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
interface updateProjectInput {
|
||||
projectName: string;
|
||||
projectId: string;
|
||||
userId: string; // user ID
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface GetProjectsInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface ProjectInterface {
|
||||
projectId: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const { thumbnail, sharedUsers, projectUuid, userId, organization } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProject(
|
||||
projectUuid,
|
||||
organization,
|
||||
userId
|
||||
);
|
||||
|
||||
if (projectExisting) {
|
||||
return {
|
||||
status: "project_exists",
|
||||
project: projectExisting,
|
||||
};
|
||||
}
|
||||
const newProjectName = await generateUntitledProjectName(
|
||||
organization,
|
||||
userId
|
||||
);
|
||||
const project = await projectModel(organization).create({
|
||||
projectName: newProjectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail,
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
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.0,
|
||||
});
|
||||
await projectModel(organization).findByIdAndUpdate(
|
||||
{ _id: project._id, isArchive: false },
|
||||
{ total_versions: `v-${newVersion.version.toFixed(2)}` }
|
||||
);
|
||||
}
|
||||
return {
|
||||
status: "Success",
|
||||
project: project,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log("error: ", error);
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
try {
|
||||
const { userId, organization, role } = data;
|
||||
await existingUser(userId, organization);
|
||||
if (!existingUser) return { status: "User not found" };
|
||||
let filter = { isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const projectDatas = await projectModel(organization)
|
||||
.find(filter)
|
||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
export const DeleteProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const updateProject = await projectModel(organization).findOneAndUpdate(
|
||||
filter,
|
||||
{ isArchive: true, DeletedAt: new Date() },
|
||||
{ new: true }
|
||||
);
|
||||
if (updateProject) return { status: "Success", project: updateProject };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
export const updateProject = async (data: updateProjectInput) => {
|
||||
try {
|
||||
const { projectId, organization, userId, projectName, thumbnail, role } =
|
||||
data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
if (projectName !== undefined) projectName;
|
||||
if (thumbnail !== undefined) thumbnail;
|
||||
const updateProject = await projectModel(organization)
|
||||
.findOneAndUpdate(
|
||||
filter,
|
||||
{ projectName: projectName, thumbnail: thumbnail },
|
||||
{ new: true }
|
||||
)
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
if (updateProject) return { status: "Success", data: updateProject };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
const maxLength: number = 6;
|
||||
export const viewProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const RecentUserDoc = await UsersDataModel(organization).findOne({
|
||||
userId: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const newArr = RecentUserDoc?.recentlyViewed || [];
|
||||
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
||||
newArr.push(projectId);
|
||||
await RecentUserDoc.save();
|
||||
} else {
|
||||
const index = newArr.indexOf(projectId);
|
||||
if (index !== -1) {
|
||||
newArr.splice(index, 1);
|
||||
}
|
||||
newArr.unshift(projectId);
|
||||
|
||||
if (newArr.length > maxLength) {
|
||||
newArr.pop();
|
||||
}
|
||||
}
|
||||
await UsersDataModel(organization).updateOne(
|
||||
{ _id: userId },
|
||||
{ recentlyViewed: newArr },
|
||||
{ new: true }
|
||||
);
|
||||
const projectData = await projectModel(organization)
|
||||
.findOneAndUpdate(filter, { isViewed: Date.now() }, { new: true })
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
return { status: "Success", data: projectData };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
92
src/shared/services/v1home/v1homeservice.ts
Normal file
92
src/shared/services/v1home/v1homeservice.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
||||
|
||||
interface IRecentData {
|
||||
organization: string;
|
||||
userId: string;
|
||||
role: string;
|
||||
}
|
||||
interface IProject {
|
||||
_id: string;
|
||||
projectName: string;
|
||||
createdBy: string;
|
||||
thumbnail?: string;
|
||||
createdAt: Date;
|
||||
isViewed?: number;
|
||||
}
|
||||
interface searchProjectInterface {
|
||||
searchName: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
export const RecentlyAdded = async (data: IRecentData) => {
|
||||
try {
|
||||
const { userId, organization, role } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const userRecentData = await UsersDataModel(organization)
|
||||
.findOne({ userId: userId, isArchive: false })
|
||||
.populate({
|
||||
path: "recentlyViewed",
|
||||
model: projectModel(organization),
|
||||
select: "_id",
|
||||
});
|
||||
let filter = { isArchive: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const populatedProjects = userRecentData.recentlyViewed as IProject[];
|
||||
const RecentDatas = await Promise.all(
|
||||
populatedProjects.map(async (project) => {
|
||||
const projectExisting = await projectModel(organization)
|
||||
.findOne(filter)
|
||||
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
||||
return projectExisting;
|
||||
})
|
||||
);
|
||||
|
||||
const filteredProjects = RecentDatas.filter(Boolean);
|
||||
return { status: "Success", data: filteredProjects };
|
||||
} catch (error) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
export const searchProject = async (data: searchProjectInterface) => {
|
||||
try {
|
||||
const { userId, organization, searchName } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const findprojectName = await projectModel(organization).find({
|
||||
projectName: { $regex: `${searchName}`, $options: "i" }, // 'i' makes it case-insensitive
|
||||
isArchive: false,
|
||||
});
|
||||
if (!findprojectName || findprojectName.length === 0)
|
||||
return { status: "Project not found" };
|
||||
return { status: "Success", data: findprojectName };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
export const searchTrashProject = async (data: searchProjectInterface) => {
|
||||
try {
|
||||
const { userId, organization, searchName } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const findprojectName = await projectModel(organization).find({
|
||||
projectName: { $regex: `${searchName}`, $options: "i" },
|
||||
isArchive: true,
|
||||
isDeleted: false,
|
||||
});
|
||||
if (!findprojectName || findprojectName.length === 0)
|
||||
return { status: "Project not found" };
|
||||
return { status: "Success", data: findprojectName };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
75
src/shared/services/v1trash/v1trashservice.ts
Normal file
75
src/shared/services/v1trash/v1trashservice.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||
interface IOrg {
|
||||
organization: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
}
|
||||
interface IRestore {
|
||||
projectId: string;
|
||||
organization: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
}
|
||||
interface RoleFilter {
|
||||
isArchive: boolean;
|
||||
createdBy?: string;
|
||||
}
|
||||
export const TrashDatas = async (data: IOrg) => {
|
||||
try {
|
||||
const { organization, role, userId } = data;
|
||||
let filter = { isArchive: true, isDeleted: false } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const TrashLists = await projectModel(organization).find(filter);
|
||||
if (!TrashLists) return { staus: "Trash is Empty" };
|
||||
const TrashDocs: any[] = [];
|
||||
for (const Trash of TrashLists) {
|
||||
const now = new Date();
|
||||
const deletedPlus15 = new Date(
|
||||
Trash.DeletedAt.getTime() + 15 * 24 * 60 * 60 * 1000
|
||||
);
|
||||
if (now > deletedPlus15) {
|
||||
console.log("now > deletedPlus15: ", now > deletedPlus15);
|
||||
await projectModel(organization).updateOne(
|
||||
{ _id: Trash._id },
|
||||
{ $set: { isDeleted: true } }
|
||||
);
|
||||
} else {
|
||||
TrashDocs.push(Trash);
|
||||
}
|
||||
}
|
||||
const ListDatas = TrashDocs.map((data) => {
|
||||
return {
|
||||
projectName: data.projectName,
|
||||
thumbnail: data.thumbnail,
|
||||
createdBy: data.createdBy,
|
||||
_id: data._id,
|
||||
};
|
||||
});
|
||||
|
||||
return { status: "Success", ListDatas };
|
||||
} catch (error) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
export const RestoreTrashData = async (data: IRestore) => {
|
||||
try {
|
||||
const { projectId, organization, role, userId } = data;
|
||||
let filter = { isArchive: true, _id: projectId } as RoleFilter;
|
||||
if (role === "User") {
|
||||
filter.createdBy = userId;
|
||||
}
|
||||
const findProject = await projectModel(organization).findOne(filter);
|
||||
if (!findProject) return { status: "Project not found" };
|
||||
const restoreData = await projectModel(organization).findOneAndUpdate(
|
||||
filter,
|
||||
{ isArchive: false, DeletedAt: null },
|
||||
{ new: true }
|
||||
);
|
||||
if (!restoreData) return { status: "Project Restore unsuccessfull" };
|
||||
return { status: "Project Restored successfully" };
|
||||
} catch (error) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
@@ -59,10 +59,10 @@ class VersionService {
|
||||
// Get all assets from previous version
|
||||
const previousVersion = parseFloat((newVersion.version - 0.1).toFixed(1));
|
||||
const previousVersionDoc = await versionModel(db).findOne({
|
||||
projectId,
|
||||
version: previousVersion,
|
||||
projectId,
|
||||
version: previousVersion,
|
||||
});
|
||||
console.log('previousVersionDoc: ', previousVersionDoc);
|
||||
console.log("previousVersionDoc: ", previousVersionDoc);
|
||||
|
||||
let previousAssets = [];
|
||||
if (previousVersionDoc) {
|
||||
@@ -75,8 +75,8 @@ class VersionService {
|
||||
|
||||
// Copy assets to new version
|
||||
const newAssets = await Promise.all(
|
||||
previousAssets.map(async (asset) => {
|
||||
console.log('previousAssets: ', previousAssets);
|
||||
previousAssets.map(async (asset) => {
|
||||
console.log("previousAssets: ", previousAssets);
|
||||
const newAsset = { ...asset.toObject(), versionId: newVersion._id };
|
||||
delete newAsset._id;
|
||||
return await assetModel(db).create(newAsset);
|
||||
|
||||
Reference in New Issue
Block a user