2025-05-29 15:39:12 +05:30
|
|
|
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";
|
2025-06-03 14:54:00 +05:30
|
|
|
import { ErrorResponse, FinalResponse, validateFields } from "../../utils/socketfunctionHelpers.ts";
|
2025-05-29 15:39:12 +05:30
|
|
|
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",
|
|
|
|
|
];
|
2025-06-03 14:54:00 +05:30
|
|
|
const missingFields = validateFields(data, requiredFields);
|
2025-05-29 15:39:12 +05:30
|
|
|
|
|
|
|
|
if (missingFields.length > 0) {
|
2025-06-03 14:54:00 +05:30
|
|
|
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1UpdateResponse,
|
|
|
|
|
ErrorResponse(missingFields, socket, data.organization), connectedUsersByOrg);
|
2025-05-29 15:39:12 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
|
|
? {
|
2025-06-03 14:54:00 +05:30
|
|
|
|
2025-05-29 15:39:12 +05:30
|
|
|
}
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2025-06-03 14:54:00 +05:30
|
|
|
|
|
|
|
|
const response = FinalResponse(status, socket, data.organization, messages, Asset_Datas);
|
2025-05-29 15:39:12 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
];
|
2025-06-03 14:54:00 +05:30
|
|
|
const missingFields = validateFields(data, requiredFields);
|
2025-05-29 15:39:12 +05:30
|
|
|
|
|
|
|
|
if (missingFields.length > 0) {
|
2025-06-03 14:54:00 +05:30
|
|
|
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1DeleteResponse,
|
|
|
|
|
ErrorResponse(missingFields, socket, data.organization), connectedUsersByOrg);
|
2025-05-29 15:39:12 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
|
|
? {
|
2025-06-03 14:54:00 +05:30
|
|
|
|
2025-05-29 15:39:12 +05:30
|
|
|
}
|
|
|
|
|
: undefined;
|
2025-06-03 14:54:00 +05:30
|
|
|
const response = FinalResponse(status, socket, data.organization, messages, Asset_Datas);
|
2025-05-29 15:39:12 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
];
|
2025-06-03 14:54:00 +05:30
|
|
|
const missingFields = validateFields(data, requiredFields);
|
2025-05-29 15:39:12 +05:30
|
|
|
if (missingFields.length > 0) {
|
2025-06-03 14:54:00 +05:30
|
|
|
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1EventDataResponse,
|
|
|
|
|
ErrorResponse(missingFields, socket, data.organization), connectedUsersByOrg);
|
2025-05-29 15:39:12 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
|
|
? {
|
2025-06-03 14:54:00 +05:30
|
|
|
|
2025-05-29 15:39:12 +05:30
|
|
|
}
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2025-06-03 14:54:00 +05:30
|
|
|
const response = FinalResponse(status, socket, data.organization, messages, Asset_Datas);
|
2025-05-29 15:39:12 +05:30
|
|
|
|
|
|
|
|
emitToSenderAndAdmins(io, socket, data.organization, EVENTS.asset_v1EventDataResponse, response, connectedUsersByOrg)
|
|
|
|
|
}
|