RBAC, jwt implemented in Projects,home and collections routing
This commit is contained in:
51
src/shared/services/homePageService.ts
Normal file
51
src/shared/services/homePageService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import ProjectType from "../../shared/model/projectmodel";
|
||||
import userDataModel from "../model/userDataModel";
|
||||
import userModel from "../model/userModel";
|
||||
|
||||
interface IrecentlyViewed {
|
||||
organization: string;
|
||||
userId: string;
|
||||
}
|
||||
interface Iresponse {
|
||||
status: string;
|
||||
data?: any;
|
||||
}
|
||||
export const recentlyViewedServices = async (
|
||||
data: IrecentlyViewed
|
||||
): Promise<Iresponse> => {
|
||||
const { organization, userId } = data;
|
||||
try {
|
||||
const ExistingUser = await userModel(organization).findOne({
|
||||
_id: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
const userDatas = await userDataModel(organization)
|
||||
.findOne({ userId: userId, isArchive: false })
|
||||
.select("recentlyViewed userId");
|
||||
const populatedProjects = userDatas.recentlyViewed;
|
||||
const RecentDatas = await Promise.all(
|
||||
populatedProjects.map(async (projectId: any) => {
|
||||
const projectExisting = await ProjectType(organization)
|
||||
.findOne({
|
||||
_id: projectId,
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
||||
return projectExisting;
|
||||
})
|
||||
);
|
||||
const filteredProjects = RecentDatas.filter(Boolean);
|
||||
return { status: "Success", data: filteredProjects };
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user