Files
Dwinzo-Backend-V0.0/src/shared/services/v1home/v1homeservice.ts
2025-06-23 17:51:48 +05:30

85 lines
2.9 KiB
TypeScript

import projectModel from "../../V1Models/Project/project-model.ts";
import UsersDataModel from "../../V1Models/Auth/user.ts";
import { existingUser } from "../helpers/v1projecthelperFns.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;
}
export const RecentlyAdded = async (data: IRecentData) => {
try {
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 })
.populate({
path: "recentlyViewed",
model: projectModel(organization),
select: "_id",
});
const populatedProjects = userRecentData.recentlyViewed as IProject[];
const RecentDatas = await Promise.all(
populatedProjects.map(async (project) => {
const projectExisting = await projectModel(organization)
.findOne({ _id: project._id, isArchive: false })
.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" },
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 };
}
};