update and get all team members list

master^2
Bhaskar 5 months ago
parent ad445326cc
commit b9a827c9d1

@ -1616,7 +1616,6 @@ exports.mastrerList = async (req, reply) => {
// }
// };
exports.getMasterSlaveSummary = async (req, reply) => {
try {
const { customerId } = req.params;
@ -1626,12 +1625,26 @@ exports.getMasterSlaveSummary = async (req, reply) => {
}
const allDevices = await Insensors.find({ customerId }).lean();
const masters = allDevices.filter(dev => dev.type === 'master');
const slaves = allDevices.filter(dev => dev.type === 'slave');
const slaveMap = {};
// Fetch orders for the customer to get masterName, location, and typeOfWater
const orders = await Order.find({ customerId }).lean();
const orderDetails = {};
console.log("orders",orders.master_connections)
// Map order information by installationId
orders.forEach(order => {
order.master_connections.forEach(connection => {
orderDetails[connection._id] = {
masterName: connection.master_name || 'Unknown', // masterName
location: connection.location || 'Unknown', // location
typeOfWater: connection.tanks[0]?.tankLocation || 'Unknown' // typeOfWater based on tank location
};
});
});
for (const slave of slaves) {
const masterId = slave.connected_to;
if (!slaveMap[masterId]) {
@ -1641,30 +1654,23 @@ exports.getMasterSlaveSummary = async (req, reply) => {
const loraTime = slave.connected_lora_time || null;
const loraDate = slave.connected_lora_date || null;
const master = masters.find(m => m.hardwareId === masterId);
const masterName = master ? master.masterName : 'Unknown';
// Get master info from the order details map
const orderInfo = orderDetails[slave.installationId] || {};
const masterName = orderInfo.masterName || 'Unknown';
const location = orderInfo.location || 'Unknown';
const typeOfWater = orderInfo.typeOfWater || 'Unknown';
// If disconnected, update LoRa last disconnect time
let loraLastDisconnect = slave.lora_last_disconnect_time || null;
if (slave.connected_status === 'disconnected') {
// If connected_lora_date and connected_lora_time are available, combine them
const loraDate = slave.connected_lora_date; // e.g., "23-04-2025"
const loraTime = slave.connected_lora_time; // e.g., "15:38:07"
if (loraDate && loraTime) {
// Combine to get formatted lora_last_disconnect_time
const formattedTime = `${loraDate} ${loraTime}`; // e.g., "23-04-2025 15:38:07"
// Update the database with this formatted value
await Insensors.updateOne(
{ hardwareId: slave.hardwareId },
{ $set: { lora_last_disconnect_time: formattedTime } }
);
loraLastDisconnect = formattedTime; // Save the updated value to loraLastDisconnect
}
const formattedTime = `${loraDate} ${loraTime}`;
await Insensors.updateOne(
{ hardwareId: slave.hardwareId },
{ $set: { lora_last_disconnect_time: formattedTime } }
);
loraLastDisconnect = formattedTime;
}
// Get tankHeight from IotData
const tankHeight = await getTankHeight(slave.hardwareId);
slaveMap[masterId].push({
@ -1678,8 +1684,9 @@ exports.getMasterSlaveSummary = async (req, reply) => {
lora_last_disconnect_time: loraLastDisconnect,
connected_to: slave.connected_to,
masterName: masterName,
location: location, // Adding location from the order
typeOfWater: typeOfWater, // Adding typeOfWater
type: slave.type || 'N/A',
typeOfWater: slave.typeOfWater || 'N/A',
tankHeight: tankHeight || null
});
}
@ -1689,32 +1696,25 @@ exports.getMasterSlaveSummary = async (req, reply) => {
for (const master of masters) {
const connectedSlaves = slaveMap[master.hardwareId] || [];
// If disconnected, update GSM last disconnect time
let gsmLastDisconnect = master.gsm_last_disconnect_time || null;
if (master.connected_status === 'disconnected') {
// If connected_gsm_date and connected_gsm_time are available, combine them
const gsmDate = master.connected_gsm_date; // e.g., "23-04-2025"
const gsmTime = master.connected_gsm_time; // e.g., "15:38:07"
const gsmDate = master.connected_gsm_date;
const gsmTime = master.connected_gsm_time;
if (gsmDate && gsmTime) {
// Combine to get formatted gsm_last_disconnect_time
const formattedTime = `${gsmDate} ${gsmTime}`; // e.g., "23-04-2025 15:38:07"
// Update the database with this formatted value
const formattedTime = `${gsmDate} ${gsmTime}`;
await Insensors.updateOne(
{ hardwareId: master.hardwareId },
{ $set: { gsm_last_disconnect_time: formattedTime } }
);
gsmLastDisconnect = formattedTime; // Save the updated value to gsmLastDisconnect
gsmLastDisconnect = formattedTime;
}
}
const enriched = {
hardwareId: master.hardwareId,
masterName: master.masterName || null,
location: master.location || null,
masterName: master.masterName || 'Unknown', // You can override this if you want to fetch it from orders
location: master.location || 'Unknown', // Same for location
type: master.type || 'master',
connected_status: master.connected_status || 'disconnected',
connected_slave_count: connectedSlaves.length,
@ -1748,6 +1748,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
}
};
// 🔍 Helper to get tankHeight from latest IotData record
async function getTankHeight(hardwareId) {
const iotData = await IotData.findOne({ 'tanks.tankhardwareId': hardwareId })
@ -3282,3 +3283,68 @@ exports.createTeamMemberSupport = async (req, reply) => {
return reply.code(500).send({ error: "Internal server error" });
}
};
exports.getAllTeamMembersListSupport = async (req, reply) => {
try {
const { supportId } = req.params;
const support = await Support.findOne({ supportId });
if (!support) {
return reply.code(404).send({ error: "Support record not found" });
}
const teamMembers = support.team_member?.team_member || [];
return reply.send({
status_code: 200,
message: "Team members fetched successfully",
count: teamMembers.length,
teamMembers
});
} catch (error) {
console.error("Error fetching team members:", error);
return reply.code(500).send({ error: "Internal server error" });
}
}
exports.updateTeamMemberSupport = async (req, reply) => {
try {
const { supportId, teamMemberId } = req.params;
const updateData = req.body;
const support = await Support.findOne({ supportId });
if (!support) {
return reply.code(404).send({ error: "Support record not found" });
}
const teamMembers = support.team_member?.team_member || [];
const memberIndex = teamMembers.findIndex(m => m.support_teamMemberId === teamMemberId);
if (memberIndex === -1) {
return reply.code(404).send({ error: "Team member not found" });
}
Object.assign(teamMembers[memberIndex], updateData);
await Support.updateOne(
{ supportId },
{
$set: {
"team_member.team_member": teamMembers,
updatedAt: new Date()
}
}
);
return reply.send({
status_code: 200,
message: "Team member updated successfully",
teamMember: teamMembers[memberIndex]
});
} catch (error) {
console.error("Error updating team member:", error);
return reply.code(500).send({ error: "Internal server error" });
}
};

@ -577,8 +577,52 @@ module.exports = function (fastify, opts, next) {
},
handler: installationController.createTeamMemberSupport
});
fastify.get("/api/supportTeamMembersList/:supportId/", {
schema: {
description: "Get all team members for a support user",
tags: ["Support"],
summary: "Get all team members for a support user",
params: {
type: "object",
required: ["supportId"],
properties: {
supportId: { type: "string", description: "Support ID" }
}
}
},
handler: installationController.getAllTeamMembersListSupport
});
fastify.put("/api/supportUpdateTeamMember/:supportId/:teamMemberId", {
schema: {
description: "Update a support team member by ID",
tags: ["Support"],
summary: "Update a support team member by ID",
params: {
type: "object",
required: ["supportId", "teamMemberId"],
properties: {
supportId: { type: "string" },
teamMemberId: { type: "string" }
}
},
body: {
type: "object",
properties: {
name: { type: "string" },
phone: { type: "string" },
password: { type: "string" },
email: { type: "string" },
alternativePhone: { type: "string" },
status: { type: "string" }
}
}
},
handler: installationController.updateTeamMemberSupport
});
next();
}
Loading…
Cancel
Save