Refactor event handling and add collection event handlers
This commit is contained in:
2
.env
2
.env
@@ -1,4 +1,4 @@
|
||||
MONGO_URI=mongodb://192.168.0.113/
|
||||
MONGO_URI=mongodb://192.168.0.110/
|
||||
MONGO_USER=mydata
|
||||
MONGO_PASSWORD=mongodb@hexr2002
|
||||
MONGO_AUTH_DB=admin
|
||||
|
||||
450
src/socket-server/controllers/collectionNodeController.ts
Normal file
450
src/socket-server/controllers/collectionNodeController.ts
Normal file
@@ -0,0 +1,450 @@
|
||||
import { Socket, Server } from "socket.io";
|
||||
import { EVENTS } from "../events/events";
|
||||
import { ErrorResponse, FinalResponse, validateFields } from "../utils/socketfunctionHelpers";
|
||||
import { emitToSenderAndAdmins } from "../utils/emitEventResponse";
|
||||
import { deleteEdge, edgecreation } from "../../shared/services/edgeService";
|
||||
import { addAttributes, DelAttributes, delCollection, DuplicateCollection, Nodecreation, SetCollectionName, UpdateAttributes } from "../../shared/services/collectionService";
|
||||
|
||||
|
||||
|
||||
export const CollectionHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionCreate || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"position",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionCreateResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await Nodecreation(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Node created successfully" },
|
||||
"Collection node creation unsuccessfull": { message: "Collection node creation unsuccessfull" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionCreateResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const CollectioNamenHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionNameSet || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"collectionName",
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionNameSetResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await SetCollectionName(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "collection name updated" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionNameSetResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const CollectioDeleteHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionDelete || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionDeleteResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await delCollection(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Collection deleted successfully" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionDeleteResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const CollectioDuplicateHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionDuplicate || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionDeleteResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await DuplicateCollection(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Collection deleted successfully" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
"Duplication unsuccessfull": { message: "Duplication unsuccessfull" },
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionDuplicateResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const setAttributesHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionAttributeSet || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"attributes",
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeSetResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await addAttributes(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Collection Attributes Added" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeSetResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const AttributesDeleteHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionAttributeDelete || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"attributeId",
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeDeleteResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await DelAttributes(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Field deleted successfully" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeDeleteResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
export const AttributesUpdateHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: {
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.collectionAttributeUpdate || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"attributeId",
|
||||
"collectionNodeId",
|
||||
"projectId",
|
||||
"organization",
|
||||
];
|
||||
const missingFields = validateFields(data, requiredFields);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeUpdateResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await UpdateAttributes(data);
|
||||
console.log('result: ', result);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Field updated successfully" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Collection not found": { message: "Collection not found" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const result_Datas =
|
||||
status === "Success" && result?.data ? result.data : undefined;
|
||||
console.log('result_Datas: ', result_Datas);
|
||||
|
||||
const response = FinalResponse(
|
||||
status,
|
||||
socket,
|
||||
data.organization,
|
||||
messages,
|
||||
result_Datas
|
||||
);
|
||||
console.log('response: ', response);
|
||||
|
||||
emitToSenderAndAdmins(
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.collectionAttributeUpdateResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
};
|
||||
@@ -13,7 +13,7 @@ export const projectHandleEvent = async (
|
||||
[org: string]: { socketId: string; userId: string; role: string }[];
|
||||
}
|
||||
) => {
|
||||
if (event !== EVENTS.ProjectCreate || !data?.organization) return;
|
||||
if (event !== EVENTS.projectCreate || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"application",
|
||||
"architecture",
|
||||
@@ -29,7 +29,7 @@ export const projectHandleEvent = async (
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.ProjectCreateResponse,
|
||||
EVENTS.projectCreateResponse,
|
||||
ErrorResponse(missingFields, socket, data.organization),
|
||||
connectedUsersByOrg
|
||||
);
|
||||
@@ -63,7 +63,7 @@ export const projectHandleEvent = async (
|
||||
io,
|
||||
socket,
|
||||
data.organization,
|
||||
EVENTS.ProjectCreateResponse,
|
||||
EVENTS.projectCreateResponse,
|
||||
response,
|
||||
connectedUsersByOrg
|
||||
);
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
import { AttributesDeleteHandleEvent, AttributesUpdateHandleEvent, CollectioDeleteHandleEvent, CollectioDuplicateHandleEvent, CollectioNamenHandleEvent, CollectionHandleEvent, setAttributesHandleEvent } from "../controllers/collectionNodeController";
|
||||
import { edgeHandleEvent } from "../controllers/edgeController";
|
||||
import { projectHandleEvent } from "../controllers/projectController";
|
||||
import { EVENTS } from "./events";
|
||||
|
||||
export const eventHandlerMap: Record<string, Function> = {
|
||||
[EVENTS.edgeConnect]: edgeHandleEvent,
|
||||
[EVENTS.ProjectCreate]: projectHandleEvent,
|
||||
[EVENTS.projectCreate]: projectHandleEvent,
|
||||
|
||||
[EVENTS.collectionCreate]: CollectionHandleEvent,
|
||||
[EVENTS.collectionNameSet]: CollectioNamenHandleEvent,
|
||||
[EVENTS.collectionDelete]: CollectioDeleteHandleEvent,
|
||||
[EVENTS.collectionDuplicate]: CollectioDuplicateHandleEvent,
|
||||
[EVENTS.collectionAttributeSet]: setAttributesHandleEvent,
|
||||
[EVENTS.collectionAttributeDelete]: AttributesDeleteHandleEvent,
|
||||
[EVENTS.collectionAttributeUpdate]: AttributesUpdateHandleEvent,
|
||||
};
|
||||
@@ -10,13 +10,26 @@ export const EVENTS = {
|
||||
roomDeleted: "roomDeleted",
|
||||
|
||||
|
||||
edgeConnect:"v1:edge:connect",
|
||||
edgeConnectResponse:"v1:response:edge:connect",
|
||||
deleteEdgeConnect:"v1:edge:deleteConnect",
|
||||
deleteEdgeConnectResponse:"v1:response:edge:deleteConnect",
|
||||
projectCreate: "v1:project:create",
|
||||
projectCreateResponse: "v1:response:project:create",
|
||||
|
||||
ProjectCreate:"v1:project:create",
|
||||
ProjectCreateResponse:"v1:response:project:create",
|
||||
collectionCreate: "v1:collection:create",
|
||||
collectionCreateResponse: "v1:response:collection:create",
|
||||
collectionNameSet: "v1:collection:setName",
|
||||
collectionNameSetResponse: "v1:response:collection:setName",
|
||||
collectionDelete: "v1:collection:delete",
|
||||
collectionDeleteResponse: "v1:response:collection:delete",
|
||||
collectionDuplicate: "v1:collection:duplicate",
|
||||
collectionDuplicateResponse: "v1:response:collection:duplicate",
|
||||
collectionAttributeSet: "v1:collection:attributeSet",
|
||||
collectionAttributeSetResponse: "v1:response:collection:attributeSet",
|
||||
collectionAttributeUpdate: "v1:collection:attributeUpdate",
|
||||
collectionAttributeUpdateResponse: "v1:response:collection:attributeUpdate",
|
||||
collectionAttributeDelete: "v1:collection:attributeDelete",
|
||||
collectionAttributeDeleteResponse: "v1:response:collection:attributeDelete",
|
||||
|
||||
|
||||
edgeConnect: "v1:edge:connect",
|
||||
edgeConnectResponse: "v1:response:edge:connect",
|
||||
deleteEdgeConnect: "v1:edge:deleteConnect",
|
||||
deleteEdgeConnectResponse: "v1:response:edge:deleteConnect",
|
||||
}
|
||||
@@ -19,12 +19,11 @@ const connectedUsersByOrg: { [organization: string]: UserSocketInfo[] } = {};
|
||||
|
||||
export const SocketServer = async (io: Server) => {
|
||||
|
||||
// ✅ Declare all namespaces here
|
||||
const namespaceNames = ["/edge", "/project", "/graph"];
|
||||
const namespaceNames = ["/edge", "/project", "/collection"];
|
||||
|
||||
const onlineUsers: { [organization: string]: Set<string> } = {};
|
||||
|
||||
// ✅ Attach common handler to each namespace
|
||||
|
||||
namespaceNames.forEach((nspName) => {
|
||||
const namespace = io.of(nspName);
|
||||
namespace.use(async (socket: Socket, next) => {
|
||||
|
||||
Reference in New Issue
Block a user