tutorial api creation and project changes
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import { recentDataController, searchProjectController, searchTrashProjectController } from "../controller/home/homeControllers.ts";
|
import { recentDataController, searchProjectController, searchTrashProjectController } from "../controller/home/homeControllers.ts";
|
||||||
import { searchTrashProject } from "../../shared/services/home/homeService.ts";
|
import { searchTrashProject } from "../../shared/services/home/homeService.ts";
|
||||||
|
import { tutorialsDataController } from "../controller/home/tutorialControllers.ts";
|
||||||
|
|
||||||
const homePageRouter = express.Router();
|
const homePageRouter = express.Router();
|
||||||
|
|
||||||
homePageRouter.get("/RecentlyViewed/:userId/:organization", recentDataController);
|
homePageRouter.get("/RecentlyViewed/:userId/:organization", recentDataController);
|
||||||
homePageRouter.get("/searchProjects", searchProjectController);
|
homePageRouter.get("/searchProjects", searchProjectController);
|
||||||
homePageRouter.get("/searchTrashProjects", searchTrashProjectController);
|
homePageRouter.get("/searchTrashProjects", searchTrashProjectController);
|
||||||
|
homePageRouter.get("/tutorials", tutorialsDataController);
|
||||||
export default homePageRouter;
|
export default homePageRouter;
|
||||||
|
|||||||
37
src/api-server/controller/home/tutorialControllers.ts
Normal file
37
src/api-server/controller/home/tutorialControllers.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { getTutorials } from "../../../shared/services/home/tutorialsService.ts";
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
|
export const tutorialsDataController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { organization = "tutorials" } = req.body;
|
||||||
|
if (!organization) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "Organization is required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const result = await getTutorials({ organization });
|
||||||
|
|
||||||
|
switch (result?.status) {
|
||||||
|
case "Success":
|
||||||
|
res.status(200).json({
|
||||||
|
tutorials: result.data,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
20
src/shared/services/home/tutorialsService.ts
Normal file
20
src/shared/services/home/tutorialsService.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import projectModel from '../../model/project/project-model.ts';
|
||||||
|
|
||||||
|
|
||||||
|
interface GetTutorials {
|
||||||
|
organization: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getTutorials = async (data: GetTutorials) => {
|
||||||
|
try {
|
||||||
|
const { organization } = data;
|
||||||
|
|
||||||
|
const tutorials = await projectModel(organization).find()
|
||||||
|
.select("_id projectName createdBy thumbnail createdAt projectUuid createdAt");
|
||||||
|
|
||||||
|
return { status: 'Success', data: tutorials };
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching data:', err);
|
||||||
|
return { status: 'Error' };
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -102,7 +102,7 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
|||||||
createdBy:userId,
|
createdBy:userId,
|
||||||
isArchive: false,
|
isArchive: false,
|
||||||
})
|
})
|
||||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
.select("_id projectName createdBy thumbnail createdAt projectUuid createdAt");
|
||||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: error };
|
return { status: error };
|
||||||
@@ -149,7 +149,7 @@ export const updateProject = async (data: updateProjectInput) => {
|
|||||||
{ projectName: projectName, thumbnail: thumbnail },
|
{ projectName: projectName, thumbnail: thumbnail },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
)
|
)
|
||||||
.select("_id projectName createdBy thumbnail createdAt");
|
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||||
if (updateProject) return { status: "Success", data: updateProject };
|
if (updateProject) return { status: "Success", data: updateProject };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: error };
|
return { status: error };
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import { emitEventResponse, emitToSenderAndAdmins, } from "../../utils/emitEvent
|
|||||||
|
|
||||||
export const projectHandleEvent = async ( event: string,socket: Socket,io:Server,data: any, connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] }, callback?: Function
|
export const projectHandleEvent = async ( event: string,socket: Socket,io:Server,data: any, connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] }, callback?: Function
|
||||||
) => {
|
) => {
|
||||||
console.log('event: ', event);
|
|
||||||
console.log('data:controller ', data);
|
|
||||||
if (event !== EVENTS.addProject || !data?.organization) return;
|
if (event !== EVENTS.addProject || !data?.organization) return;
|
||||||
const requiredFields = ["projectUuid", "userId", "thumbnail", "organization"];
|
const requiredFields = ["projectUuid", "userId", "thumbnail", "organization"];
|
||||||
const missingFields = requiredFields.filter(field => !data?.[field]);
|
const missingFields = requiredFields.filter(field => !data?.[field]);
|
||||||
@@ -59,7 +57,6 @@ export const projectDeleteHandleEvent = async (
|
|||||||
data: any,
|
data: any,
|
||||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
||||||
) => {
|
) => {
|
||||||
console.log('event: ', event);
|
|
||||||
if (event !== EVENTS.deleteProject || !data?.organization) return;
|
if (event !== EVENTS.deleteProject || !data?.organization) return;
|
||||||
const requiredFields = ["projectId", "userId", "organization"];
|
const requiredFields = ["projectId", "userId", "organization"];
|
||||||
const missingFields = requiredFields.filter(field => !data?.[field]);
|
const missingFields = requiredFields.filter(field => !data?.[field]);
|
||||||
@@ -77,7 +74,6 @@ export const projectDeleteHandleEvent = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await DeleteProject(data);
|
const result = await DeleteProject(data);
|
||||||
console.log('result: ', result);
|
|
||||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||||
|
|
||||||
const messages: Record<string, { message: string }> = {
|
const messages: Record<string, { message: string }> = {
|
||||||
@@ -116,7 +112,6 @@ export const projecUpdateHandleEvent = async (
|
|||||||
data: any,
|
data: any,
|
||||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
||||||
) => {
|
) => {
|
||||||
console.log('data:update ', data);
|
|
||||||
if (event !== EVENTS.ProjectUpdate || !data?.organization) return;
|
if (event !== EVENTS.ProjectUpdate || !data?.organization) return;
|
||||||
const requiredFields = ["projectId", "userId", "organization"];
|
const requiredFields = ["projectId", "userId", "organization"];
|
||||||
const missingFields = requiredFields.filter(field => !data?.[field]);
|
const missingFields = requiredFields.filter(field => !data?.[field]);
|
||||||
@@ -145,6 +140,13 @@ export const projecUpdateHandleEvent = async (
|
|||||||
const msg = messages[status] || { message: "Internal server error" };
|
const msg = messages[status] || { message: "Internal server error" };
|
||||||
const projectDeleteDatas =
|
const projectDeleteDatas =
|
||||||
status === "Success" && result?.data
|
status === "Success" && result?.data
|
||||||
|
? {
|
||||||
|
projectUuid: result.data.projectUuid,
|
||||||
|
projectName: result.data.projectName,
|
||||||
|
thumbnail: result.data.thumbnail,
|
||||||
|
projectId: result.data._id,
|
||||||
|
}
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
@@ -158,6 +160,5 @@ export const projecUpdateHandleEvent = async (
|
|||||||
|
|
||||||
|
|
||||||
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.projectUpdateResponse, response, connectedUsersByOrg)
|
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.projectUpdateResponse, response, connectedUsersByOrg)
|
||||||
console.log('response: ', response);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1103,7 +1103,7 @@ export const initSocketServer = (httpServer: any) => {
|
|||||||
// const organization = socket.handshake.query.organization as string;
|
// const organization = socket.handshake.query.organization as string;
|
||||||
// const email = socket.handshake.query.email as string;
|
// const email = socket.handshake.query.email as string;
|
||||||
const { organization, email, userId } = socket.handshake.auth
|
const { organization, email, userId } = socket.handshake.auth
|
||||||
console.log(' socket.handshake.auth: ', socket.handshake.auth);
|
// console.log(' socket.handshake.auth: ', socket.handshake.auth);
|
||||||
|
|
||||||
if (organization) {
|
if (organization) {
|
||||||
socket.join(organization);
|
socket.join(organization);
|
||||||
@@ -1119,7 +1119,7 @@ export const initSocketServer = (httpServer: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const role = await getUserRole(userId, organization);
|
const role = await getUserRole(userId, organization);
|
||||||
console.log('role: ', role);
|
// console.log('role: ', role);
|
||||||
if (role === "Admin") {
|
if (role === "Admin") {
|
||||||
socket.join(`${organization}_admins`);
|
socket.join(`${organization}_admins`);
|
||||||
}
|
}
|
||||||
@@ -1138,7 +1138,7 @@ export const initSocketServer = (httpServer: any) => {
|
|||||||
// console.log(`🔗 User connected: ${userId} with role ${role} in ${organization}`);
|
// console.log(`🔗 User connected: ${userId} with role ${role} in ${organization}`);
|
||||||
// console.log("🧩 connectedUsersByOrg: ", connectedUsersByOrg);
|
// console.log("🧩 connectedUsersByOrg: ", connectedUsersByOrg);
|
||||||
} else {
|
} else {
|
||||||
console.warn(`❌ Cannot store user. Missing data:`,);
|
// console.warn(`❌ Cannot store user. Missing data:`,);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ export const emitToSenderAndAdmins = (
|
|||||||
result: EmitOptions,
|
result: EmitOptions,
|
||||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] }
|
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] }
|
||||||
) => {
|
) => {
|
||||||
console.log('result.data,: ', result.data,);
|
|
||||||
// Emit to sender directly
|
// Emit to sender directly
|
||||||
socket.emit(event, {
|
socket.emit(event, {
|
||||||
message: result.message,
|
message: result.message,
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import userModel from "../../shared/model/user-Model.ts";
|
|||||||
|
|
||||||
|
|
||||||
export async function getUserRole(userId: string, organization: string): Promise<string> {
|
export async function getUserRole(userId: string, organization: string): Promise<string> {
|
||||||
console.log('userId: ', userId);
|
|
||||||
const user = await userModel(organization).findOne({ _id: userId });
|
const user = await userModel(organization).findOne({ _id: userId });
|
||||||
console.log('user: ', user);
|
|
||||||
return user?.role || "User"; // default to 'user' if no role found
|
return user?.role || "User"; // default to 'user' if no role found
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user