70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { Socket, Server } from "socket.io";
|
|
import { EVENTS } from "../events/events";
|
|
import { ErrorResponse, FinalResponse, validateFields } from "../utils/socketfunctionHelpers";
|
|
import { emitToSenderAndAdmins } from "../utils/emitEventResponse";
|
|
import { projectCreationService } from "../../shared/services/projectService";
|
|
|
|
export const projectHandleEvent = async (
|
|
event: string,
|
|
socket: Socket,
|
|
io: Server,
|
|
data: any,
|
|
connectedUsersByOrg: {
|
|
[org: string]: { socketId: string; userId: string; role: string }[];
|
|
}
|
|
) => {
|
|
if (event !== EVENTS.ProjectCreate || !data?.organization) return;
|
|
const requiredFields = [
|
|
"application",
|
|
"architecture",
|
|
"apiType",
|
|
"projectName",
|
|
"useableLanguage",
|
|
"organization",
|
|
];
|
|
const missingFields = validateFields(data, requiredFields);
|
|
|
|
if (missingFields.length > 0) {
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProjectCreateResponse,
|
|
ErrorResponse(missingFields, socket, data.organization),
|
|
connectedUsersByOrg
|
|
);
|
|
return;
|
|
}
|
|
const result = await projectCreationService(data);
|
|
const status = typeof result?.status === "string" ? result.status : "unknown";
|
|
|
|
const messages: Record<string, { message: string }> = {
|
|
Success: { message: "Project Created Successfully" },
|
|
"Project Already Exists": { message: "Project Already Exists" },
|
|
"Already MVC architecture assigned to this projectId": { message: "Already MVC architecture assigned to this projectId" },
|
|
"Project creation unsuccessfull": {
|
|
message: "Project creation unsuccessfull",
|
|
},
|
|
"New architecture": { message: "New architecture" },
|
|
|
|
|
|
};
|
|
|
|
const result_Datas =
|
|
status === "Success" && result?.data ? result.data : undefined;
|
|
const response = FinalResponse(
|
|
status,
|
|
socket,
|
|
data.organization,
|
|
messages,
|
|
result_Datas
|
|
);
|
|
emitToSenderAndAdmins(
|
|
io,
|
|
socket,
|
|
data.organization,
|
|
EVENTS.ProjectCreateResponse,
|
|
response,
|
|
connectedUsersByOrg
|
|
);
|
|
}; |