340 lines
9.1 KiB
TypeScript
340 lines
9.1 KiB
TypeScript
import { Socket, Server } from "socket.io";
|
|
import { EVENTS } from "../../socket/events.ts";
|
|
import { emitToSenderAndAdmins } from "../../utils/emitEventResponse.ts";
|
|
import {
|
|
EventDataDelete,
|
|
productAdd,
|
|
productDataDelete,
|
|
productRename,
|
|
} from "../../../shared/services/simulation/productService.ts";
|
|
export const productAddHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.setProductModel_v1 || !data?.organization) return;
|
|
const requiredFields = [
|
|
"productName",
|
|
"productUuid",
|
|
"projectId",
|
|
"versionId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
const missingFields = requiredFields.filter((field) => !data?.[field]);
|
|
|
|
if (missingFields.length > 0) {
|
|
const response = {
|
|
success: false,
|
|
message: `Missing required field(s): ${missingFields.join(", ")}`,
|
|
status: "MissingFields",
|
|
socketId: socket.id,
|
|
organization: data?.organization ?? "unknown",
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Product_v1UpdateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await productAdd(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "Product created successfully" },
|
|
"User not found": { message: "User not found" },
|
|
"Project not found": { message: "Project not found" },
|
|
"Version Data not found": {
|
|
message: "Version Data not found",
|
|
},
|
|
"EventData updated successfully": {
|
|
message: "EventData updated successfully",
|
|
},
|
|
"EventData add successfully": { message: "EventData add successfully" },
|
|
};
|
|
|
|
const msg = messages[status] || { message: "Internal server error" };
|
|
const product_Datas = status === "Success" && result?.data ? {} : undefined;
|
|
|
|
const response = {
|
|
success: status === "Success",
|
|
message: msg.message,
|
|
status,
|
|
socketId: socket.id,
|
|
organization: data.organization,
|
|
...(product_Datas ? { data: product_Datas } : {}),
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Product_v1UpdateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|
|
export const productDataDeleteHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.delete_v1ProductModel || !data?.organization) return;
|
|
const requiredFields = [
|
|
"productUuid",
|
|
"versionId",
|
|
"projectId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
const missingFields = requiredFields.filter((field) => !data?.[field]);
|
|
|
|
if (missingFields.length > 0) {
|
|
const response = {
|
|
success: false,
|
|
message: `Missing required field(s): ${missingFields.join(", ")}`,
|
|
status: "MissingFields",
|
|
socketId: socket.id,
|
|
organization: data?.organization ?? "unknown",
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Product_v1DeleteResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await productDataDelete(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "Product deleted successfully" },
|
|
"User not found": { message: "User not found" },
|
|
"Project not found": { message: "Project not found" },
|
|
"Version Data not found": {
|
|
message: "Version Data not found",
|
|
},
|
|
"Product not found": { message: "Product not found" },
|
|
};
|
|
|
|
const msg = messages[status] || { message: "Internal server error" };
|
|
const product_Datas =
|
|
status === "Success" && result?.data
|
|
? {
|
|
// widget: {
|
|
// id: result.data.widgetID,
|
|
// type: result.data.projectName,
|
|
// position: result.data.position,
|
|
// },
|
|
// Data: result.data.Data,
|
|
// zoneId: result.data.zoneId,
|
|
}
|
|
: undefined;
|
|
|
|
const response = {
|
|
success: status === "Success",
|
|
message: msg.message,
|
|
status,
|
|
socketId: socket.id,
|
|
organization: data.organization,
|
|
...(product_Datas ? { data: product_Datas } : {}),
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Product_v1DeleteResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|
|
export const EventDataDeleteHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.deleteEvent_v1ProductModel || !data?.organization)
|
|
return;
|
|
const requiredFields = [
|
|
"modelUuid",
|
|
"productUuid",
|
|
"projectId",
|
|
"versionId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
const missingFields = requiredFields.filter((field) => !data?.[field]);
|
|
|
|
if (missingFields.length > 0) {
|
|
const response = {
|
|
success: false,
|
|
message: `Missing required field(s): ${missingFields.join(", ")}`,
|
|
status: "MissingFields",
|
|
socketId: socket.id,
|
|
organization: data?.organization ?? "unknown",
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProductEvent_v1DeleteResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await EventDataDelete(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "EventData deleted successfully" },
|
|
"User not found": { message: "User not found" },
|
|
"Project not found": { message: "Project not found" },
|
|
"Version Data not found": {
|
|
message: "Version Data not found",
|
|
},
|
|
"Product not found": { message: "Product not found" },
|
|
"Event Delete Unsuccessful": { message: "Event Delete Unsuccessful" },
|
|
};
|
|
|
|
const msg = messages[status] || { message: "Internal server error" };
|
|
const product_Datas =
|
|
status === "Success" && result?.data
|
|
? {
|
|
// widget: {
|
|
// id: result.data.widgetID,
|
|
// type: result.data.projectName,
|
|
// position: result.data.position,
|
|
// },
|
|
// Data: result.data.Data,
|
|
// zoneId: result.data.zoneId,
|
|
}
|
|
: undefined;
|
|
|
|
const response = {
|
|
success: status === "Success",
|
|
message: msg.message,
|
|
status,
|
|
socketId: socket.id,
|
|
organization: data.organization,
|
|
...(product_Datas ? { data: product_Datas } : {}),
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProductEvent_v1DeleteResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|
|
export const productRenameHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.ProductRenameModel_v1 || !data?.organization) return;
|
|
const requiredFields = [
|
|
"productName",
|
|
"productUuid",
|
|
"projectId",
|
|
"versionId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
const missingFields = requiredFields.filter((field) => !data?.[field]);
|
|
|
|
if (missingFields.length > 0) {
|
|
const response = {
|
|
success: false,
|
|
message: `Missing required field(s): ${missingFields.join(", ")}`,
|
|
status: "MissingFields",
|
|
socketId: socket.id,
|
|
organization: data?.organization ?? "unknown",
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProductRename_v1UpdateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await productRename(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "product Rename successfully" },
|
|
"User not found": { message: "User not found" },
|
|
"Project not found": { message: "Project not found" },
|
|
"Version Data not found": {
|
|
message: "Version Data not found",
|
|
},
|
|
"Product not found": { message: "Product not found" },
|
|
"Rename Unsuccessful": { message: "Rename Unsuccessful" },
|
|
};
|
|
|
|
const msg = messages[status] || { message: "Internal server error" };
|
|
const product_Datas =
|
|
status === "Success" && result?.data
|
|
? {
|
|
// widget: {
|
|
// id: result.data.widgetID,
|
|
// type: result.data.projectName,
|
|
// position: result.data.position,
|
|
// },
|
|
// Data: result.data.Data,
|
|
// zoneId: result.data.zoneId,
|
|
}
|
|
: undefined;
|
|
|
|
const response = {
|
|
success: status === "Success",
|
|
message: msg.message,
|
|
status,
|
|
socketId: socket.id,
|
|
organization: data.organization,
|
|
...(product_Datas ? { data: product_Datas } : {}),
|
|
};
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProductRename_v1UpdateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|