139 lines
3.4 KiB
TypeScript
139 lines
3.4 KiB
TypeScript
import { Socket, Server } from "socket.io";
|
|
import { EVENTS } from "../../socket/events.ts";
|
|
import { emitToSenderAndAdmins } from "../../utils/emitEventResponse.ts";
|
|
import {
|
|
DelZone,
|
|
SetZone,
|
|
} from "../../../shared/services/builder/zoneService.ts";
|
|
import {
|
|
ErrorResponse,
|
|
FinalResponse,
|
|
validateFields,
|
|
} from "../../utils/socketfunctionHelpers.ts";
|
|
|
|
export const SetZoneHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.setZone_v1 || !data?.organization) return;
|
|
const requiredFields = [
|
|
"zoneData",
|
|
"projectId",
|
|
"versionId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
const missingFields = validateFields(data, requiredFields);
|
|
|
|
if (missingFields.length > 0) {
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.zone_v1UpdateResponse,
|
|
ErrorResponse(missingFields, socket, data.organization),
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await SetZone(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "zone 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",
|
|
},
|
|
"zone updated": { message: "zone updated" },
|
|
};
|
|
|
|
const msg = messages[status] || { message: "Internal server error" };
|
|
const zone_Datas = status === "Success" && result?.data ? {} : undefined;
|
|
|
|
const response = FinalResponse(
|
|
status,
|
|
socket,
|
|
data.organization,
|
|
messages,
|
|
zone_Datas
|
|
);
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.zone_v1UpdateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|
|
export const DeleteZoneHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.deleteZone_v1 || !data?.organization) return;
|
|
const requiredFields = [
|
|
"zoneUuid",
|
|
"projectId",
|
|
"versionId",
|
|
"userId",
|
|
"organization",
|
|
];
|
|
|
|
const missingFields = validateFields(data, requiredFields);
|
|
|
|
if (missingFields.length > 0) {
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Zone_v1DeleteResponse,
|
|
ErrorResponse(missingFields, socket, data.organization),
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await DelZone(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "zone deleted 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",
|
|
},
|
|
};
|
|
|
|
const zone_Datas = status === "Success" && result?.data ? {} : undefined;
|
|
|
|
const response = FinalResponse(
|
|
status,
|
|
socket,
|
|
data.organization,
|
|
messages,
|
|
zone_Datas
|
|
);
|
|
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.Zone_v1DeleteResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
};
|