|
|
@ -4551,7 +4551,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
|
|
|
|
exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { support_teamMemberId } = req.params;
|
|
|
|
const { support_teamMemberId } = req.params;
|
|
|
@ -4560,94 +4559,76 @@ exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
|
|
|
|
return reply.code(400).send({ error: "support_teamMemberId is required" });
|
|
|
|
return reply.code(400).send({ error: "support_teamMemberId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Find support document that includes the given team member ID
|
|
|
|
// Step 1: Get support record with categorized issues assigned to the team member
|
|
|
|
const supportRecord = await Support.findOne({
|
|
|
|
const supportRecord = await Support.findOne({
|
|
|
|
"team_member.team_member.support_teamMemberId": support_teamMemberId
|
|
|
|
"team_member.team_member.support_teamMemberId": support_teamMemberId
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
if (!supportRecord) {
|
|
|
|
if (!supportRecord) {
|
|
|
|
return reply.code(404).send({ message: "No support record found for this team member ID" });
|
|
|
|
return reply.code(404).send({ message: "Support record not found" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Extract all hardwareIds from issues
|
|
|
|
// Step 2: Filter categorized issues assigned to this team member
|
|
|
|
// 2. Extract all hardwareIds and masterHardwareIds from issues
|
|
|
|
const assignedIssues = (supportRecord.categorizedIssues || []).filter(
|
|
|
|
const hardwareIds = [];
|
|
|
|
issue => issue.assignedTo?.support_teamMemberId === support_teamMemberId
|
|
|
|
for (const issue of supportRecord.issues || []) {
|
|
|
|
);
|
|
|
|
if (issue.hardwareId) hardwareIds.push(issue.hardwareId);
|
|
|
|
|
|
|
|
if (Array.isArray(issue.hardwareIds)) hardwareIds.push(...issue.hardwareIds);
|
|
|
|
|
|
|
|
if (issue.masterHardwareId) hardwareIds.push(issue.masterHardwareId); // Add this line
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Extract unique hardwareIds from assigned issues
|
|
|
|
|
|
|
|
const assignedHardwareIds = [
|
|
|
|
|
|
|
|
...new Set(
|
|
|
|
|
|
|
|
assignedIssues.map(issue => issue.hardwareId || issue.masterHardwareId).filter(Boolean)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
if (hardwareIds.length === 0) {
|
|
|
|
if (assignedHardwareIds.length === 0) {
|
|
|
|
return reply.code(404).send({ message: "No hardware IDs found in issues" });
|
|
|
|
return reply.code(404).send({ message: "No categorized issues assigned to this team member" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("hardwareIds",hardwareIds)
|
|
|
|
|
|
|
|
// 3. Find disconnected Insensors matching connected_to
|
|
|
|
|
|
|
|
const uniqueHardwareIds = [...new Set(hardwareIds)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const disconnectedSensors = await Insensors.find({
|
|
|
|
// Step 4: Find disconnected insensors (either masters or slaves)
|
|
|
|
connected_to: { $in: uniqueHardwareIds },
|
|
|
|
const disconnectedDevices = await Insensors.find({
|
|
|
|
|
|
|
|
$or: [
|
|
|
|
|
|
|
|
{ hardwareId: { $in: assignedHardwareIds } },
|
|
|
|
|
|
|
|
{ connected_to: { $in: assignedHardwareIds } }
|
|
|
|
|
|
|
|
],
|
|
|
|
connected_status: "disconnected"
|
|
|
|
connected_status: "disconnected"
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
if (disconnectedSensors.length === 0) {
|
|
|
|
if (disconnectedDevices.length === 0) {
|
|
|
|
return reply.code(404).send({ message: "No disconnected issues found" });
|
|
|
|
return reply.code(404).send({ message: "No disconnected devices assigned to this team member" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Get unique customerIds
|
|
|
|
// Step 5: Extract unique customerIds
|
|
|
|
const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
|
|
|
|
const customerIds = [...new Set(disconnectedDevices.map(d => d.customerId))];
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Get User data
|
|
|
|
const users = await User.find({ customerId: { $in: customerIds } }).lean();
|
|
|
|
const customers = await User.find({ customerId: { $in: customerIds } }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Step 6: Prepare final response
|
|
|
|
// 6. Format response
|
|
|
|
const response = users.map(user => ({
|
|
|
|
const uniqueCustomerMap = {};
|
|
|
|
customerId: user.customerId,
|
|
|
|
for (const user of customers) {
|
|
|
|
firstName: user.profile?.firstName || "",
|
|
|
|
if (!uniqueCustomerMap[user.customerId]) {
|
|
|
|
lastName: user.profile?.lastName || "",
|
|
|
|
uniqueCustomerMap[user.customerId] = {
|
|
|
|
phone: user.phone || user.profile?.contactNumber || "",
|
|
|
|
customer: {
|
|
|
|
email: user.emails?.[0]?.email || "",
|
|
|
|
customerId: user.customerId,
|
|
|
|
city: user.profile?.city || "",
|
|
|
|
username: user.username || "",
|
|
|
|
state: user.profile?.state || "",
|
|
|
|
firstName: user.profile?.firstName || "",
|
|
|
|
country: user.profile?.country || "",
|
|
|
|
lastName: user.profile?.lastName || "",
|
|
|
|
latitude: user.latitude,
|
|
|
|
phone: user.phone || user.profile?.contactNumber || "",
|
|
|
|
longitude: user.longitude,
|
|
|
|
email: user.emails?.[0]?.email || "",
|
|
|
|
fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string"),
|
|
|
|
phoneVerified: user.phoneVerified || false,
|
|
|
|
installationId: user.installationId || "",
|
|
|
|
address1: user.profile?.address1 || "",
|
|
|
|
notificationPreferences: {
|
|
|
|
address2: user.profile?.address2 || "",
|
|
|
|
allowNotifications: user.allowNotifications || false,
|
|
|
|
city: user.profile?.city || "",
|
|
|
|
automaticStartAndStopNotify: user.automaticStartAndStopNotify || false,
|
|
|
|
state: user.profile?.state || "",
|
|
|
|
manualStartAndStopNotify: user.manualStartAndStopNotify || false,
|
|
|
|
country: user.profile?.country || "",
|
|
|
|
criticalLowWaterAlert: user.criticalLowWaterAlert || false,
|
|
|
|
zip: user.profile?.zip || "",
|
|
|
|
lowWaterAlert: user.lowWaterAlert || false,
|
|
|
|
notes: user.profile?.notes || "",
|
|
|
|
notificationPreference: user.notificationPreference || "never"
|
|
|
|
latitude: user.latitude,
|
|
|
|
},
|
|
|
|
longitude: user.longitude,
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")),
|
|
|
|
updatedAt: user.updatedAt
|
|
|
|
installationId: user.installationId || "",
|
|
|
|
}));
|
|
|
|
notificationPreferences: {
|
|
|
|
|
|
|
|
allowNotifications: user.allowNotifications || false,
|
|
|
|
|
|
|
|
automaticStartAndStopNotify: user.automaticStartAndStopNotify || false,
|
|
|
|
|
|
|
|
manualStartAndStopNotify: user.manualStartAndStopNotify || false,
|
|
|
|
|
|
|
|
criticalLowWaterAlert: user.criticalLowWaterAlert || false,
|
|
|
|
|
|
|
|
lowWaterAlert: user.lowWaterAlert || false,
|
|
|
|
|
|
|
|
notificationPreference: user.notificationPreference || "never"
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
surveyStatus: user.survey_status || "pending",
|
|
|
|
|
|
|
|
buildingName: user.buildingName,
|
|
|
|
|
|
|
|
stripePaymentStatus: user.stripePaymentStatus || false,
|
|
|
|
|
|
|
|
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
|
|
|
|
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
|
|
|
|
updatedAt: user.updatedAt
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({ status_code: 200, data: response });
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
data: Object.values(uniqueCustomerMap)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error in getDisconnectedCustomerDetailsByTeamMemberId:", error);
|
|
|
|
console.error("Error in getDisconnectedCustomerDetailsByTeamMemberId:", error);
|
|
|
|