builder,vizualization socket functionality completed
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
import { Socket, Server } from "socket.io";
|
||||
import { EVENTS } from "../../socket/events.ts";
|
||||
import { emitToSenderAndAdmins } from "../../utils/emitEventResponse.ts";
|
||||
import { deleteAssetModel, replaceEventDatas, setAssetModel } from "../../../shared/services/builder/assetService.ts";
|
||||
export const setAssetHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
||||
) => {
|
||||
if (event !== EVENTS.setAssetModel_v1 || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"modelUuid",
|
||||
"modelName",
|
||||
"position",
|
||||
"rotation",
|
||||
"eventData",
|
||||
"modelfileID",
|
||||
"isLocked",
|
||||
"isVisible",
|
||||
"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.asset_v1UpdateResponse, response, connectedUsersByOrg)
|
||||
return;
|
||||
}
|
||||
const result = await setAssetModel(data);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Model created successfully" },
|
||||
"User not found": { message: "User not found" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Updated successfully": { message: "Updated successfully" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const Asset_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,
|
||||
...(Asset_Datas ? { data: Asset_Datas } : {}),
|
||||
};
|
||||
|
||||
|
||||
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1UpdateResponse, response, connectedUsersByOrg)
|
||||
}
|
||||
export const deleteAssetHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
||||
) => {
|
||||
if (event !== EVENTS.delete_v1AssetModel || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"modelUuid",
|
||||
"modelName",
|
||||
"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.asset_v1DeleteResponse, response, connectedUsersByOrg)
|
||||
return;
|
||||
}
|
||||
const result = await deleteAssetModel(data);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Model deleted successfully" },
|
||||
"User not found": { message: "User not found" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"model not found": { message: "model not found" },
|
||||
"Failed to archive asset": { message: "Failed to archive asset" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const Asset_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,
|
||||
...(Asset_Datas ? { data: Asset_Datas } : {}),
|
||||
};
|
||||
|
||||
|
||||
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1DeleteResponse, response, connectedUsersByOrg)
|
||||
}
|
||||
export const replaceEventDatasHandleEvent = async (
|
||||
event: string,
|
||||
socket: Socket,
|
||||
io: Server,
|
||||
data: any,
|
||||
connectedUsersByOrg: { [org: string]: { socketId: string; userId: string; role: string }[] },
|
||||
) => {
|
||||
if (event !== EVENTS.asset_v1EventData || !data?.organization) return;
|
||||
const requiredFields = [
|
||||
"modelUuid",
|
||||
"eventData",
|
||||
"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.asset_v1EventDataResponse, response, connectedUsersByOrg)
|
||||
return;
|
||||
}
|
||||
const result = await replaceEventDatas(data);
|
||||
const status = typeof result?.status === "string" ? result.status : "unknown";
|
||||
|
||||
const messages: Record<string, { message: string }> = {
|
||||
Success: { message: "Data updated successfully" },
|
||||
"User not found": { message: "User not found" },
|
||||
"Project not found": { message: "Project not found" },
|
||||
"Model not for this UUID": { message: "Model not for this UUID" },
|
||||
"Failed to archive asset": { message: "Failed to archive asset" },
|
||||
|
||||
|
||||
};
|
||||
|
||||
const msg = messages[status] || { message: "Internal server error" };
|
||||
const Asset_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,
|
||||
...(Asset_Datas ? { data: Asset_Datas } : {}),
|
||||
};
|
||||
|
||||
|
||||
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1EventDataResponse, response, connectedUsersByOrg)
|
||||
}
|
||||
Reference in New Issue
Block a user