RecentlyViewed API, view API, home page file added
This commit is contained in:
7
src/api-server/Routes/homepageRoutes.ts
Normal file
7
src/api-server/Routes/homepageRoutes.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import * as express from "express";
|
||||||
|
import { recentDataController } from "../controller/home/recentsControllers.ts";
|
||||||
|
|
||||||
|
const homePageRouter = express.Router();
|
||||||
|
|
||||||
|
homePageRouter.get("/RecentlyViewed/:userId/:organization", recentDataController);
|
||||||
|
export default homePageRouter;
|
||||||
@@ -21,11 +21,12 @@ import widget3dRoutes from "./Routes/widget3dRoutes.ts";
|
|||||||
import productRouter from "./Routes/productRoutes.ts";
|
import productRouter from "./Routes/productRoutes.ts";
|
||||||
import projectRouter from "./Routes/projectRoutes.ts";
|
import projectRouter from "./Routes/projectRoutes.ts";
|
||||||
import trashRouter from "./Routes/trashRoutes.ts";
|
import trashRouter from "./Routes/trashRoutes.ts";
|
||||||
|
import homePageRouter from "./Routes/homepageRoutes.ts";
|
||||||
// import productFlowRoutes from "./Routes/productFlowRouts.ts";
|
// import productFlowRoutes from "./Routes/productFlowRouts.ts";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
// const allowedOriginsDev = [
|
// const allowedOriginsDev = [
|
||||||
// "http://localhost:3000",
|
// "http://localhost:3000",
|
||||||
// "http://192.168.0.183:8200",
|
// "http://192.168.0.183:8200",
|
||||||
// "http://192.168.0.101:8200",
|
// "http://192.168.0.101:8200",
|
||||||
@@ -45,11 +46,11 @@ app.use(cors());
|
|||||||
// ) {
|
// ) {
|
||||||
// return callback(null, true);
|
// return callback(null, true);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// if (typeof allowedOrigin === "string" && origin === allowedOrigin) {
|
// if (typeof allowedOrigin === "string" && origin === allowedOrigin) {
|
||||||
// return callback(null, true);
|
// return callback(null, true);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// return callback(new Error("Not allowed by CORS"));
|
// return callback(new Error("Not allowed by CORS"));
|
||||||
// },
|
// },
|
||||||
// credentials: true
|
// credentials: true
|
||||||
@@ -57,7 +58,7 @@ app.use(cors());
|
|||||||
|
|
||||||
app.use(express.json({ limit: "50mb" }));
|
app.use(express.json({ limit: "50mb" }));
|
||||||
app.use(
|
app.use(
|
||||||
express.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 })
|
express.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 })
|
||||||
);
|
);
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
@@ -87,6 +88,7 @@ app.use("/api/v2", templateRoutes);
|
|||||||
app.use("/api/v2", widget3dRoutes);
|
app.use("/api/v2", widget3dRoutes);
|
||||||
app.use("/api/v2", productRouter);
|
app.use("/api/v2", productRouter);
|
||||||
// app.use("/api/v2", productFlowRoutes);
|
// app.use("/api/v2", productFlowRoutes);
|
||||||
app.use("/api/v1",projectRouter)
|
app.use("/api/v1", projectRouter);
|
||||||
app.use("/api/v1",trashRouter)
|
app.use("/api/v1", trashRouter);
|
||||||
|
app.use("/api/v1", homePageRouter);
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
46
src/api-server/controller/home/recentsControllers.ts
Normal file
46
src/api-server/controller/home/recentsControllers.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import { RecentlyAdded } from "../../../shared/services/home/recentDatasService.ts";
|
||||||
|
|
||||||
|
export const recentDataController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { userId, organization } = req.params;
|
||||||
|
if (!userId || !organization) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await RecentlyAdded({ userId, organization });
|
||||||
|
|
||||||
|
switch (result.status) {
|
||||||
|
case "User not found":
|
||||||
|
res.status(404).json({
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Datas were empty":
|
||||||
|
res.status(200).json({
|
||||||
|
RecentlyViewed: [],
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Success":
|
||||||
|
res.status(200).json({
|
||||||
|
RecentlyViewed: result.data,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -186,6 +186,7 @@ export const updateProjectController = async (
|
|||||||
};
|
};
|
||||||
export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
|
console.log("req.query: ", req.query);
|
||||||
const { projectId, organization, userId } = req.query as {
|
const { projectId, organization, userId } = req.query as {
|
||||||
organization: string;
|
organization: string;
|
||||||
projectId: string;
|
projectId: string;
|
||||||
@@ -202,6 +203,7 @@ export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
|||||||
organization,
|
organization,
|
||||||
userId,
|
userId,
|
||||||
});
|
});
|
||||||
|
console.log("result: ", result);
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export interface Project extends Document {
|
|||||||
thumbnail: string;
|
thumbnail: string;
|
||||||
sharedUsers: [];
|
sharedUsers: [];
|
||||||
DeletedAt: Date;
|
DeletedAt: Date;
|
||||||
|
isViewed: number;
|
||||||
total_versions: string;
|
total_versions: string;
|
||||||
Present_version: string;
|
Present_version: string;
|
||||||
}
|
}
|
||||||
@@ -24,6 +25,7 @@ const projectSchema: Schema = new Schema(
|
|||||||
sharedUsers: [{ type: Schema.Types.ObjectId, ref: "user" }],
|
sharedUsers: [{ type: Schema.Types.ObjectId, ref: "user" }],
|
||||||
DeletedAt: { type: Date, default: null },
|
DeletedAt: { type: Date, default: null },
|
||||||
isDeleted: { type: Boolean, default: false },
|
isDeleted: { type: Boolean, default: false },
|
||||||
|
isViewed: { type: Number },
|
||||||
total_versions: { type: String },
|
total_versions: { type: String },
|
||||||
Present_version: { type: String },
|
Present_version: { type: String },
|
||||||
},
|
},
|
||||||
|
|||||||
34
src/shared/services/home/recentDatasService.ts
Normal file
34
src/shared/services/home/recentDatasService.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import projectModel from "../../model/project/project-model.ts";
|
||||||
|
import userModel from "../../model/user-Model.ts";
|
||||||
|
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
||||||
|
|
||||||
|
interface IRecentData {
|
||||||
|
organization: string;
|
||||||
|
userId: 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 userModel(organization)
|
||||||
|
.findOne({ _id: userId })
|
||||||
|
.populate({
|
||||||
|
path: "recentlyViewed",
|
||||||
|
model: projectModel(organization),
|
||||||
|
select: "_id projectName createdBy thumbnail createdAt isViewed",
|
||||||
|
});
|
||||||
|
let RecentDatas = [];
|
||||||
|
userRecentData.recentlyViewed.map(async (project: object) => {
|
||||||
|
console.log("project: ", typeof project);
|
||||||
|
const projectExisting = await projectModel(organization).findOne({
|
||||||
|
_id: project,
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
if (!userRecentData) return { status: "Datas were empty" };
|
||||||
|
return { status: "Success", data: userRecentData.recentlyViewed };
|
||||||
|
} catch (error) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -179,11 +179,23 @@ export const viewProject = async (data: ProjectInterface) => {
|
|||||||
newArr.pop();
|
newArr.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await userExisting.updateOne(
|
await userModel(organization).updateOne(
|
||||||
{ _id: userId, isArchive: false },
|
{ _id: userId },
|
||||||
{ recentlyViewed: newArr }
|
{ recentlyViewed: newArr },
|
||||||
|
{ new: true }
|
||||||
);
|
);
|
||||||
return { data: existingProject };
|
const projectData = await projectModel(organization)
|
||||||
|
.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) {
|
} catch (error: unknown) {
|
||||||
return { status: error };
|
return { status: error };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user