comments added for support

master^2
Bhaskar 4 months ago
parent c879298174
commit 7ad407187c

@ -4606,6 +4606,159 @@ exports.raiseATicketSlave = async (req, reply) => {
// }
// };
// exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
// try {
// const { supportId, customerId } = req.params;
// if (!supportId || !customerId) {
// return reply.code(400).send({ error: "supportId and customerId are required" });
// }
// const supportRecord = await Support.findOne({ supportId }).lean();
// if (!supportRecord) {
// return reply.code(404).send({ message: "No support record found for this supportId and customerId" });
// }
// const allIssues = supportRecord.issues || [];
// const hardwareSet = new Set();
// for (const issue of allIssues) {
// if (issue.hardwareId) hardwareSet.add(issue.hardwareId);
// if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId);
// }
// const hardwareIds = [...hardwareSet];
// const sensors = await Insensors.find({
// customerId,
// $or: [
// { hardwareId: { $in: hardwareIds } },
// { tankhardwareId: { $in: hardwareIds } }
// ]
// }).lean();
// const sensorMap = {};
// for (const sensor of sensors) {
// if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor;
// if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor;
// }
// const orders = await Order.find({ customerId }).lean();
// const orderMap = {};
// for (const order of orders) {
// (order.master_connections || []).forEach(conn => {
// if (conn.hardwareId) {
// orderMap[conn.hardwareId] = {
// masterName: conn.master_name || null,
// location: conn.location || null
// };
// }
// });
// }
// const slaveOrderMap = {};
// for (const order of orders) {
// (order.tank_connections || []).forEach(conn => {
// if (conn.hardwareId) {
// slaveOrderMap[conn.hardwareId] = {
// location: conn.location || null,
// typeOfWater: conn.typeOfWater || null
// };
// }
// });
// }
// const masterMap = {};
// for (const issue of allIssues) {
// const masterId = issue.masterHardwareId || issue.hardwareId;
// const masterSensor = sensorMap[masterId];
// if (!masterSensor || masterSensor.type !== "master") continue;
// if (!masterMap[masterSensor.hardwareId]) {
// const enriched = orderMap[masterSensor.hardwareId] || {};
// masterMap[masterSensor.hardwareId] = {
// hardwareId: masterSensor.hardwareId,
// masterName: enriched.masterName || masterSensor.masterName || "",
// location: enriched.location || masterSensor.location || "",
// type: "master",
// connected_status: masterSensor.connected_status,
// gsm_last_check_time: masterSensor.gsm_last_check_time,
// gsm_last_disconnect_time: masterSensor.gsm_last_disconnect_time,
// connected_gsm_date: masterSensor.connected_gsm_date,
// connected_gsm_time: masterSensor.connected_gsm_time,
// connected_lora_date: masterSensor.connected_lora_date,
// connected_lora_time: masterSensor.connected_lora_time,
// support_gsm_last_check_time: masterSensor.support_gsm_last_check_time,
// support_lora_last_check_time: masterSensor.support_lora_last_check_time,
// team_member_support_gsm_last_check_time: masterSensor.team_member_support_gsm_last_check_time,
// team_member_support_lora_last_check_time: masterSensor.team_member_support_lora_last_check_time,
// connected_slave_count: 0,
// connected_slaves: []
// };
// }
// const master = masterMap[masterSensor.hardwareId];
// const connectedSlaves = await Insensors.find({
// connected_to: masterSensor.hardwareId,
// type: "slave",
// customerId
// }).lean();
// const slaveSet = new Set(master.connected_slaves.map(s => s.hardwareId));
// for (const slave of connectedSlaves) {
// const slaveHardwareId = slave.tankhardwareId || slave.hardwareId;
// if (slaveSet.has(slaveHardwareId)) continue;
// slaveSet.add(slaveHardwareId);
// const tankInfo = await Tank.findOne({
// $or: [
// { hardwareId: slaveHardwareId },
// { tankhardwareId: slaveHardwareId }
// ]
// }).lean();
// const slaveOrderInfo = slaveOrderMap[slaveHardwareId] || {};
// const slaveEnriched = {
// hardwareId: slaveHardwareId,
// tankName: slave.tankName || (tankInfo?.tankName ?? ""),
// location: slave.location || tankInfo?.tankLocation || slaveOrderInfo.location || "",
// connected_status: slave.connected_status,
// connected_lora_time: slave.connected_lora_time,
// connected_lora_date: slave.connected_lora_date,
// lora_last_check_time: slave.lora_last_check_time,
// lora_last_disconnect_time: slave.lora_last_disconnect_time,
// connected_to: slave.connected_to,
// masterName: master.masterName,
// type: "slave",
// typeOfWater: slave.typeOfWater || tankInfo?.typeOfWater || slaveOrderInfo.typeOfWater || "",
// tankHeight: slave.tankHeight,
// support_lora_last_check_time: slave.support_lora_last_check_time,
// team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time
// };
// master.connected_slaves.push(slaveEnriched);
// master.connected_slave_count++;
// }
// }
// return reply.send({
// status_code: 200,
// supportId,
// customerId,
// totalMasters: Object.keys(masterMap).length,
// comments: supportRecord.comments || "",
// disconnectedIssues: Object.values(masterMap)
// });
// } catch (error) {
// console.error("Error fetching disconnected issues:", error);
// return reply.code(500).send({ error: "Internal server error" });
// }
// };
exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
try {
const { supportId, customerId } = req.params;
@ -4744,12 +4897,19 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
}
}
// Map comments to only the text strings
const commentTexts = (supportRecord.comments || []).map(c => c.text);
// Add these comments to each master in disconnectedIssues
for (const master of Object.values(masterMap)) {
master.comments = commentTexts;
}
return reply.send({
status_code: 200,
supportId,
customerId,
totalMasters: Object.keys(masterMap).length,
comments: supportRecord.comments || "",
disconnectedIssues: Object.values(masterMap)
});
@ -6228,18 +6388,23 @@ exports.StatusTeamMember = async (request, reply) => {
exports.updateComments = async (req, reply) => {
try {
const { supportId} = req.params;
const { comments, customerId, hardwareId } = req.body;
const { supportId } = req.params;
const { comments, customerId, hardwareId } = req.body;
console.log("Incoming request body:", req.body);
console.log("typeof comments:", typeof comments);
console.log("value of comments:", comments);
if (!supportId || !customerId || !hardwareId) {
return reply.code(400).send({ error: "supportId, customerId and hardwareId are required in path params" });
return reply.code(400).send({ error: "supportId, customerId and hardwareId are required" });
}
if (typeof comments !== 'string') {
return reply.code(400).send({ error: "comments must be a string in the request body" });
const trimmedComment = typeof comments === "string" ? comments.trim() : "";
if (!trimmedComment) {
return reply.code(400).send({ error: "comments must be a non-empty string" });
}
// Verify customerId is valid for the hardwareId by looking into Insensors
// Step 1: Validate sensor
const sensor = await Insensors.findOne({
customerId,
$or: [{ hardwareId }, { tankhardwareId: hardwareId }]
@ -6249,14 +6414,13 @@ exports.updateComments = async (req, reply) => {
return reply.code(404).send({ error: "No sensor found with this hardwareId for this customerId" });
}
// Find support record only by supportId (no customerId in Support schema)
// Step 2: Load support record
const supportRecord = await Support.findOne({ supportId });
if (!supportRecord) {
return reply.code(404).send({ error: "Support record not found" });
}
// Check if hardwareId exists in supportRecord issues
// Step 3: Validate issue exists
const issueExists = supportRecord.issues.some(issue =>
issue.hardwareId === hardwareId || issue.masterHardwareId === hardwareId
);
@ -6265,15 +6429,28 @@ exports.updateComments = async (req, reply) => {
return reply.code(404).send({ error: "HardwareId not found in this support record's issues" });
}
// Update the comments field (top-level comments)
supportRecord.comments = comments;
// Step 4: Append comment object correctly
const commentObj = {
text: trimmedComment,
customerId,
hardwareId,
createdAt: new Date()
};
if (!Array.isArray(supportRecord.comments)) {
supportRecord.comments = [];
}
// Use push instead of spreading to keep Mongoose subdoc validation intact
supportRecord.comments.push(commentObj);
await supportRecord.save();
return reply.send({ message: "Comments updated successfully", comments });
return reply.send({ message: "Comment added successfully", comment: commentObj });
} catch (error) {
console.error("Error updating comments:", error);
return reply.code(500).send({ error: "Internal server error" });
}
};

@ -231,8 +231,15 @@ const installationschema = new mongoose.Schema({
dateOfLogin: { type: String, default: null },
timeOfLogin: { type: String, default: null },
currentTime: { type: Date, default: Date.now },
comments: { type: String, default: "" },
lastTicketRaisedAt: {type : String},
comments: [
{
text: { type: String, required: true },
customerId: String,
hardwareId: String,
createdAt: { type: Date, default: Date.now }
}
],
lastTicketRaisedAt: {type : String},
issues: [{ type: Object }], // existing issues array
masterDisconnected: [{ // new field for master disconnected details
hardwareId: String,

Loading…
Cancel
Save