101 lines
3.3 KiB
TypeScript
101 lines
3.3 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",
|
|
"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" },
|
|
"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",
|
|
"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" },
|
|
|
|
|
|
|
|
};
|
|
|
|
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)
|
|
} |