Role nd token Based Routing completed for Project,trash,home which is in Controller. Token, Auth Purpose,Rolebased middlewares created. Auth API,Project token Based API, Home Token Based API, Trash Token Based API In v1 AuthRoutes
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
import projectModel from "../../model/project/project-model.ts";
|
||||
import userModel from "../../model/user-Model.ts";
|
||||
import versionModel from "../../model/version/versionModel.ts";
|
||||
import { AuthenticatedRequest } from "../../utils/token.ts";
|
||||
import {
|
||||
existingProject,
|
||||
existingUser,
|
||||
@@ -8,6 +10,7 @@ import {
|
||||
previousVersion,
|
||||
generateUntitledProjectName,
|
||||
} from "../helpers/ProjecthelperFn.ts";
|
||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
projectUuid: string;
|
||||
@@ -23,26 +26,32 @@ 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 { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
||||
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,
|
||||
@@ -93,13 +102,15 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
|
||||
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
try {
|
||||
const { userId, organization } = data;
|
||||
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({
|
||||
isArchive: false,
|
||||
})
|
||||
.find(filter)
|
||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
@@ -109,41 +120,42 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
|
||||
export const DeleteProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId } = data;
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
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 updateProject = await projectModel(organization).findOneAndUpdate(
|
||||
{ _id: projectId, isArchive: false },
|
||||
filter,
|
||||
{ isArchive: true, DeletedAt: new Date() },
|
||||
{ new: true }
|
||||
);
|
||||
if (updateProject) return { status: "Success",project: updateProject };
|
||||
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 } = data;
|
||||
const { projectId, organization, userId, projectName, thumbnail, role } =
|
||||
data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
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" };
|
||||
if (projectName !== undefined) projectName;
|
||||
if (thumbnail !== undefined) thumbnail;
|
||||
const updateProject = await projectModel(organization)
|
||||
.findOneAndUpdate(
|
||||
{ _id: projectId, isArchive: false },
|
||||
filter,
|
||||
{ projectName: projectName, thumbnail: thumbnail },
|
||||
{ new: true }
|
||||
)
|
||||
@@ -156,18 +168,23 @@ export const updateProject = async (data: updateProjectInput) => {
|
||||
const maxLength: number = 6;
|
||||
export const viewProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId } = data;
|
||||
const { projectId, organization, userId, role } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
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 = userExisting?.recentlyViewed || [];
|
||||
if (userExisting?.recentlyViewed.length === 0) {
|
||||
const newArr = RecentUserDoc?.recentlyViewed || [];
|
||||
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
||||
newArr.push(projectId);
|
||||
await RecentUserDoc.save();
|
||||
} else {
|
||||
const index = newArr.indexOf(projectId);
|
||||
if (index !== -1) {
|
||||
@@ -179,21 +196,13 @@ export const viewProject = async (data: ProjectInterface) => {
|
||||
newArr.pop();
|
||||
}
|
||||
}
|
||||
await userModel(organization).updateOne(
|
||||
await UsersDataModel(organization).updateOne(
|
||||
{ _id: userId },
|
||||
{ recentlyViewed: newArr },
|
||||
{ new: true }
|
||||
);
|
||||
const projectData = await projectModel(organization)
|
||||
.findOneAndUpdate(
|
||||
{
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
},
|
||||
{ isViewed: Date.now() },
|
||||
{ new: true }
|
||||
)
|
||||
.findOneAndUpdate(filter, { 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