Varun 8 months ago
commit 4e8f70ff16

@ -3,7 +3,7 @@ const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department");
const { Install } = require("../models/store");
const { Install, SensorStock, SensorQuotation, Order } = require("../models/store");
const { Counter } = require("../models/User")
const fastify = require("fastify")({
logger: true,
@ -135,72 +135,160 @@ exports.createTeamMember = async (request, reply) => {
}
};
exports.assignTeamMemberToQuotation = async (request, reply) => {
try {
const { installationId } = request.params; // Get installationId from URL params
const { teamMemberId } = request.body; // Get teamMemberId from request body
if (!teamMemberId) {
return reply.status(400).send({
simplydata: {
error: true,
message: "teamMemberId is required",
},
});
}
// Find installation by installationId
const installation = await Install.findOne({ installationId });
if (!installation) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Installation not found",
},
});
}
// Extract team members list
const teamMembers = installation.team_member.team_member;
// Check if provided teamMemberId exists in the installation's team
const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId);
if (!assignedTeamMember) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Team member not found in this installation",
},
});
}
// Here, you would save the assigned team member to the quotation (modify as needed)
const quotation = {
installationId,
assignedTeamMember
};
return reply.send({
simplydata: {
error: false,
message: "Team member assigned to quotation successfully",
quotation
},
});
// exports.assignTeamMemberToQuotation = async (request, reply) => {
// try {
// const { installationId } = request.params; // Get installationId from URL params
// const { teamMemberId } = request.body; // Get teamMemberId from request body
} catch (err) {
console.error("Error assigning team member to quotation:", err);
reply.status(500).send({
simplydata: {
error: true,
message: "Internal server error",
},
});
// if (!teamMemberId) {
// return reply.status(400).send({
// simplydata: {
// error: true,
// message: "teamMemberId is required",
// },
// });
// }
// // Find installation by installationId
// const installation = await Install.findOne({ installationId });
// if (!installation) {
// return reply.status(404).send({
// simplydata: {
// error: true,
// message: "Installation not found",
// },
// });
// }
// // Extract team members list
// const teamMembers = installation.team_member.team_member;
// // Check if provided teamMemberId exists in the installation's team
// const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId);
// if (!assignedTeamMember) {
// return reply.status(404).send({
// simplydata: {
// error: true,
// message: "Team member not found in this installation",
// },
// });
// }
// // Here, you would save the assigned team member to the quotation (modify as needed)
// const quotation = {
// installationId,
// assignedTeamMember
// };
// return reply.send({
// simplydata: {
// error: false,
// message: "Team member assigned to quotation successfully",
// quotation
// },
// });
// } catch (err) {
// console.error("Error assigning team member to quotation:", err);
// reply.status(500).send({
// simplydata: {
// error: true,
// message: "Internal server error",
// },
// });
// }
// };
exports.assignTeamMemberToQuotation = async (request, reply) => {
try {
const { installationId } = request.params;
const { teamMemberId, quotationId } = request.body;
if (!teamMemberId || !quotationId) {
return reply.status(400).send({
simplydata: {
error: true,
message: "Both teamMemberId and quotationId are required",
},
});
}
};
// 🔹 Find installation by installationId
const installation = await Install.findOne({ installationId });
if (!installation) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Installation not found",
},
});
}
// 🔹 Extract team members list
const teamMembers = installation.team_member?.team_member || [];
// 🔹 Check if the provided teamMemberId exists in the installation's team
const assignedTeamMember = teamMembers.find(
(member) => member.teamMemberId === teamMemberId
);
if (!assignedTeamMember) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Team member not found in this installation",
},
});
}
// 🔹 Find or create the quotation for the given installationId
let quotation = await Order.findOne({ installationId, quatationId: quotationId });
if (!quotation) {
quotation = new Order({
installationId,
quatationId: quotationId,
assignedTeamMembers: [],
status: "Pending", // Default status when created
});
}
// 🔹 Assign the team member to the quotation
if (!quotation.assignedTeamMembers) {
quotation.assignedTeamMembers = [];
}
if (!quotation.assignedTeamMembers.includes(teamMemberId)) {
quotation.assignedTeamMembers.push(teamMemberId);
}
// 🔹 Update order status when a team member is assigned
quotation.status = "Assigned"; // Update status
// 🔹 Save the updated quotation
await quotation.save();
return reply.send({
simplydata: {
error: false,
message: "Team member assigned to quotation successfully",
quotation,
},
});
} catch (err) {
console.error("Error assigning team member to quotation:", err);
reply.status(500).send({
simplydata: {
error: true,
message: "Internal server error",
},
});
}
};
exports.getAllInstallers = async (request, reply) => {
try {

@ -1713,71 +1713,75 @@ eventEmitter.on('sendThresholdTimeNotification', async (customerId, fcmTokens, t
// );
// 🚀 Motor Start Notification
eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, stopCriteria, typeOfWater, manualThresholdTime) => {
eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, manualThresholdTime) => {
try {
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
console.log(`✅ Received sendMotorStartNotification event for ${customerId}`);
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor started in **${motorOnType}** mode.`);
return;
}
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor started in **${motorOnType}** mode.`);
return;
}
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const stopConditionMessage = stopCriteria === "level"
? `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`
: `⚠️ Pump will stop **manually**.`;
// const stopConditionMessage = stopCriteria === "level"
// ? `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`
// : `⚠️ Pump will stop **manually**.`;
const message = `🚰 Motor Started 🚀\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Started\n` +
`🕒 Pump started at: ${formattedTime}\n`
const message = `🚰 Motor Started Manually 🚀\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Started\n` +
`🕒 Pump started at: ${formattedTime}\n`;
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
} catch (error) {
console.error(`❌ Error in sendMotorStartNotification for Customer ID: ${customerId}`, error);
console.error(`❌ Error in sendMotorStartNotification for Customer ID: ${customerId}`, error);
}
});
// 🛑 Motor Stop Notification
eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, typeOfWater) => {
eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType) => {
try {
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
console.log(`✅ Received sendMotorStopNotification event for ${customerId}`);
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor stopped in **${motorOnType}** mode.`);
return;
}
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor stopped in **${motorOnType}** mode.`);
return;
}
const message = `🛑 Motor Stopped ❌\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Stopped\n` +
`🕒 Pump stopped at: ${formattedTime}`;
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const message = `🛑 Motor Stopped Manually ❌\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Stopped\n` +
`🕒 Pump stopped at: ${formattedTime}`;
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Stopped ❌", message);
console.log(`✅ Motor stop notification sent for Customer ID: ${customerId}`);
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Stopped ❌", message);
console.log(`✅ Motor stop notification sent for Customer ID: ${customerId}`);
} catch (error) {
console.error(`❌ Error in sendMotorStopNotification for Customer ID: ${customerId}`, error);
console.error(`❌ Error in sendMotorStopNotification for Customer ID: ${customerId}`, error);
}
});
// eventEmitter.on('sendLowWaterNotification', (fcmTokens, message) => {
// const notificationMessage = `Warning: Water level is low in the tank.
// Tank Name: ${tankName},
@ -5987,6 +5991,87 @@ client.on('message', async (topic, message) => {
const sendMotorNotifications = async () => {
// console.log("🔄 Checking for motor notifications...");
// Find motors that need a start or stop notification
const motors = await Tank.find({
"connections.inputConnections.motor_id": { $exists: true },
});
for (const motorTank of motors) {
const inputConnection = motorTank.connections.inputConnections.find(
(conn) => conn.motor_id
);
if (!inputConnection) continue;
const { customerId, blockName, tankName } = motorTank;
const fcmTokens = await getFcmTokens(customerId); // Get FCM tokens for this customer
if (!fcmTokens.length) continue;
// 🔹 Motor Start Condition
if (
inputConnection.motor_stop_status === "2" &&
!motorTank.motor_start_notified
) {
console.log("✅ Sending Motor Start Notification...");
eventEmitter.emit(
"sendMotorStartNotification",
inputConnection.motor_id,
customerId,
fcmTokens,
inputConnection.water_level || 0,
blockName,
tankName,
"forced_manual",
inputConnection.manual_threshold_time
);
// Mark notification as sent
motorTank.motor_start_notified = true;
motorTank.motor_stop_notified = false; // Reset stop notification flag
await motorTank.save();
}
// 🔹 Motor Stop Condition
if (
inputConnection.motor_stop_status === "1" &&
!motorTank.motor_stop_notified
) {
console.log("✅ Sending Motor Stop Notification...");
eventEmitter.emit(
"sendMotorStopNotification",
inputConnection.motor_id,
customerId,
fcmTokens,
inputConnection.water_level || 0,
blockName,
tankName,
"forced_manual"
);
// Mark notification as sent
motorTank.motor_stop_notified = true;
motorTank.motor_start_notified = false; // Reset start notification flag
await motorTank.save();
}
}
};
const getFcmTokens = async (customerId) => {
const user = await User.findOne({ customerId }).select("fcmIds");
return user?.fcmIds?.filter((token) => token) || [];
};
// Run the notification check every second
cron.schedule("* * * * * *", async () => {
await sendMotorNotifications();
});
//
// API function to get survey data for a particular installer

@ -109,7 +109,10 @@ const tanksSchema = new mongoose.Schema({
],
inputWaterlevelPercentage: { type: String, default: null },
outputWaterlevelPercentage: { type: String, default: null }
}
},
motor_start_notified: { type: Boolean, default: false },
motor_stop_notified: { type: Boolean, default: false }
});
const customerautopercentages = ({

@ -67,9 +67,13 @@ module.exports = function (fastify, opts, next) {
teamMemberId: {
type: "string",
description: "The team member ID to assign"
},
quotationId: {
type: "string",
description: "The team member ID to assign"
}
},
required: ["teamMemberId"]
// required: ["teamMemberId"]
},
},
handler: installationController.assignTeamMemberToQuotation

Loading…
Cancel
Save