137 lines
5.3 KiB
TypeScript
137 lines
5.3 KiB
TypeScript
import cameraModel from "../../../shared/model/camera/camera-Model.ts"
|
|
import userModel from "../../../shared/model/user-Model.ts"
|
|
|
|
export const activeUsers = async (data: any) => {
|
|
const {organization}=data
|
|
// console.log('data: ', data);
|
|
try {
|
|
if (data && data.email) {
|
|
|
|
const email = data.email
|
|
const organization = email.split("@")[1].split(".")[0]
|
|
|
|
const findUser = await userModel(organization).findOne({email})
|
|
|
|
if (findUser) {
|
|
const updateActiveStatus = await userModel(organization).findOneAndUpdate({email:findUser.email},{activeStatus:"online"},{new:true})
|
|
|
|
if (updateActiveStatus) {
|
|
const cameraDatas=await cameraModel(organization).findOne({userId:updateActiveStatus._id})
|
|
.select("position target rotation -_id");
|
|
|
|
if (cameraDatas) {
|
|
const result = {
|
|
position: cameraDatas.position,
|
|
target: cameraDatas.target,
|
|
rotation: cameraDatas.rotation,
|
|
userData: {
|
|
_id: updateActiveStatus._id,
|
|
userName: updateActiveStatus.userName,
|
|
email: updateActiveStatus.email,
|
|
},
|
|
};
|
|
|
|
|
|
|
|
return { success: true, message: 'user connect ', data: result,organization:organization }
|
|
// return result;
|
|
}
|
|
}
|
|
|
|
}
|
|
}else {
|
|
// console.error('Invalid data or missing email:', data);
|
|
// Handle the error or return a default value
|
|
// Example: Return an error response if the email is invalid
|
|
|
|
return { success: false, message: 'Email is missing or invalid', organization:organization }
|
|
// return res.status(400).send({ message: 'Email is missing or invalid' });
|
|
}
|
|
|
|
|
|
// // return [];
|
|
} catch (error:any) {
|
|
return { success: false, message: error?.message || "Error occurred while activeUser", error, organization: organization }
|
|
|
|
// return { success: false, message:error}
|
|
}
|
|
}
|
|
|
|
export const activeUserOffline = async (data: any) => {
|
|
|
|
|
|
const email = data.email
|
|
const organization = email.split("@")[1].split(".")[0]
|
|
try {
|
|
|
|
const findUsers = await userModel(organization).findOne({email})
|
|
// console.log('findUsers: ', findUsers);
|
|
if (findUsers) {
|
|
const updateActiveStatus = await userModel(organization).findOneAndUpdate({email:email},{activeStatus:"offline"},{new:true})
|
|
// console.log('updateActiveStatus: ',updateActiveStatus);
|
|
if (updateActiveStatus) {
|
|
const cameraDatas=await cameraModel(organization).findOne({userId:updateActiveStatus._id})
|
|
.select("position target rotation -_id");
|
|
// console.log('cameraDatas: ', cameraDatas);
|
|
if (cameraDatas) {
|
|
const result = {
|
|
position: cameraDatas.position,
|
|
target: cameraDatas.target,
|
|
rotation: cameraDatas.rotation,
|
|
userData: {
|
|
_id: updateActiveStatus._id,
|
|
userName: updateActiveStatus.userName,
|
|
email: updateActiveStatus.email,
|
|
},
|
|
};
|
|
|
|
// console.log("Formatted Result:", result);
|
|
|
|
|
|
// return result;
|
|
return { success: true, message: 'user disconnect', data: result,organization:organization }
|
|
}
|
|
}
|
|
}
|
|
// // return [];
|
|
} catch (error:any) {
|
|
return { success: false, message: error?.message || "Error occurred while activeUser", error, organization: organization }
|
|
|
|
// return { success: false, message: error}
|
|
}
|
|
}
|
|
type OnlineUsersMap = Map<string, Set<string>>;
|
|
|
|
// export const addUserToOnlineList = async (
|
|
// organization: string,
|
|
// email: string,
|
|
// onlineUsers: OnlineUsersMap,
|
|
// socket: any,
|
|
// namespace: string
|
|
// ) => {
|
|
// if (!organization || !email) return;
|
|
// console.log('organization: ', organization);
|
|
|
|
// // Ensure the organization entry exists
|
|
// if (!onlineUsers.has(organization)) {
|
|
// onlineUsers.set(organization, new Set());
|
|
// }
|
|
// const findUser = await userModel(organization).findOne({email})
|
|
// if (!findUser) {
|
|
// return { success: false, message: "User not found", organization };
|
|
// }
|
|
// const userId = findUser._id;
|
|
// console.log(`🔍 Found user with ID: ${userId}`);
|
|
// // Add user to the online set
|
|
// onlineUsers.get(organization)!.add(userId);
|
|
|
|
// console.log(`✅ User ${userId} is online (Org: ${organization})`);
|
|
|
|
// // Emit updated online users list
|
|
// socket.emit("users:online", {
|
|
// organization,
|
|
// users: [...onlineUsers.get(organization)!],
|
|
// });
|
|
// };
|
|
|
|
|