V1 folder created For auth Data project, trash, home page based on the token and role based
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user