|
|
@ -6512,6 +6512,112 @@ exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getDisconnectedMoveCustomerDetails = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { supportId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!supportId) {
|
|
|
|
|
|
|
|
// return reply.code(400).send({ error: "supportId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 1. Fetch support record
|
|
|
|
|
|
|
|
// const supportRecord = await Support.findOne({ supportId }).lean();
|
|
|
|
|
|
|
|
// if (!supportRecord) {
|
|
|
|
|
|
|
|
// return reply.code(404).send({ message: "No support record found for this supportId" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 2. Only proceed if categorizedIssues exist
|
|
|
|
|
|
|
|
// if (!Array.isArray(supportRecord.categorizedIssues) || supportRecord.categorizedIssues.length === 0) {
|
|
|
|
|
|
|
|
// return reply.code(404).send({ message: "No categorized issues to process" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 3. Collect hardware IDs from categorized issues
|
|
|
|
|
|
|
|
// const categorizedHardwareIds = [];
|
|
|
|
|
|
|
|
// for (const issue of supportRecord.categorizedIssues) {
|
|
|
|
|
|
|
|
// if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId);
|
|
|
|
|
|
|
|
// if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (categorizedHardwareIds.length === 0) {
|
|
|
|
|
|
|
|
// return reply.code(404).send({ message: "No hardware IDs in categorized issues" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 4. Get disconnected sensors from Insensors
|
|
|
|
|
|
|
|
// const disconnectedSensors = await Insensors.find({
|
|
|
|
|
|
|
|
// $or: [
|
|
|
|
|
|
|
|
// { hardwareId: { $in: categorizedHardwareIds } },
|
|
|
|
|
|
|
|
// { connected_to: { $in: categorizedHardwareIds } }
|
|
|
|
|
|
|
|
// ],
|
|
|
|
|
|
|
|
// connected_status: "disconnected"
|
|
|
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (disconnectedSensors.length === 0) {
|
|
|
|
|
|
|
|
// return reply.code(404).send({ message: "No disconnected sensors found" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 5. Get unique customerIds
|
|
|
|
|
|
|
|
// const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 6. Fetch corresponding customers
|
|
|
|
|
|
|
|
// const customers = await User.find({ customerId: { $in: customerIds } }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const uniqueCustomerMap = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (const user of customers) {
|
|
|
|
|
|
|
|
// const cid = user.customerId;
|
|
|
|
|
|
|
|
// if (!uniqueCustomerMap[cid]) {
|
|
|
|
|
|
|
|
// uniqueCustomerMap[cid] = {
|
|
|
|
|
|
|
|
// customer: {
|
|
|
|
|
|
|
|
// customerId: cid,
|
|
|
|
|
|
|
|
// username: user.username || "",
|
|
|
|
|
|
|
|
// firstName: user.profile?.firstName || user.firstName || "",
|
|
|
|
|
|
|
|
// lastName: user.profile?.lastName || user.lastName || "",
|
|
|
|
|
|
|
|
// phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "",
|
|
|
|
|
|
|
|
// email: user.emails?.[0]?.email || user.email || "",
|
|
|
|
|
|
|
|
// phoneVerified: user.phoneVerified || false,
|
|
|
|
|
|
|
|
// address1: user.profile?.address1 || user.address1 || "",
|
|
|
|
|
|
|
|
// address2: user.profile?.address2 || user.address2 || "",
|
|
|
|
|
|
|
|
// city: user.profile?.city || user.city || "",
|
|
|
|
|
|
|
|
// state: user.profile?.state || user.state || "",
|
|
|
|
|
|
|
|
// country: user.profile?.country || user.country || "",
|
|
|
|
|
|
|
|
// zip: user.profile?.zip || "",
|
|
|
|
|
|
|
|
// notes: user.profile?.notes || "",
|
|
|
|
|
|
|
|
// latitude: user.latitude || 0,
|
|
|
|
|
|
|
|
// longitude: user.longitude || 0,
|
|
|
|
|
|
|
|
// fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")),
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const response = Object.values(uniqueCustomerMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// data: response
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
|
|
|
// console.error("Error fetching disconnected customer details:", error);
|
|
|
|
|
|
|
|
// return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.getDisconnectedMoveCustomerDetails = async (req, reply) => {
|
|
|
|
exports.getDisconnectedMoveCustomerDetails = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { supportId } = req.params;
|
|
|
|
const { supportId } = req.params;
|
|
|
@ -6520,55 +6626,67 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => {
|
|
|
|
return reply.code(400).send({ error: "supportId is required" });
|
|
|
|
return reply.code(400).send({ error: "supportId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Fetch support record
|
|
|
|
|
|
|
|
const supportRecord = await Support.findOne({ supportId }).lean();
|
|
|
|
const supportRecord = await Support.findOne({ supportId }).lean();
|
|
|
|
if (!supportRecord) {
|
|
|
|
if (!supportRecord) {
|
|
|
|
return reply.code(404).send({ message: "No support record found for this supportId" });
|
|
|
|
return reply.code(404).send({ message: "No support record found for this supportId" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Only proceed if categorizedIssues exist
|
|
|
|
|
|
|
|
if (!Array.isArray(supportRecord.categorizedIssues) || supportRecord.categorizedIssues.length === 0) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ message: "No categorized issues to process" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Collect hardware IDs from categorized issues
|
|
|
|
|
|
|
|
const categorizedHardwareIds = [];
|
|
|
|
const categorizedHardwareIds = [];
|
|
|
|
for (const issue of supportRecord.categorizedIssues) {
|
|
|
|
const resolvedHardwareIds = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const issue of supportRecord.categorizedIssues || []) {
|
|
|
|
if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId);
|
|
|
|
if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId);
|
|
|
|
if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds);
|
|
|
|
if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (categorizedHardwareIds.length === 0) {
|
|
|
|
for (const issue of supportRecord.resolvedIssues || []) {
|
|
|
|
return reply.code(404).send({ message: "No hardware IDs in categorized issues" });
|
|
|
|
if (issue.hardwareId) resolvedHardwareIds.push(issue.hardwareId);
|
|
|
|
|
|
|
|
if (Array.isArray(issue.hardwareIds)) resolvedHardwareIds.push(...issue.hardwareIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Get disconnected sensors from Insensors
|
|
|
|
const allHardwareIds = [...new Set([...categorizedHardwareIds, ...resolvedHardwareIds])];
|
|
|
|
const disconnectedSensors = await Insensors.find({
|
|
|
|
if (allHardwareIds.length === 0) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ message: "No hardware IDs in support issues" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const allSensors = await Insensors.find({
|
|
|
|
$or: [
|
|
|
|
$or: [
|
|
|
|
{ hardwareId: { $in: categorizedHardwareIds } },
|
|
|
|
{ hardwareId: { $in: allHardwareIds } },
|
|
|
|
{ connected_to: { $in: categorizedHardwareIds } }
|
|
|
|
{ connected_to: { $in: allHardwareIds } }
|
|
|
|
],
|
|
|
|
]
|
|
|
|
connected_status: "disconnected"
|
|
|
|
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
if (disconnectedSensors.length === 0) {
|
|
|
|
if (allSensors.length === 0) {
|
|
|
|
return reply.code(404).send({ message: "No disconnected sensors found" });
|
|
|
|
return reply.code(404).send({ message: "No sensors found for support issues" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Get unique customerIds
|
|
|
|
const customerStatusMap = {};
|
|
|
|
const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 6. Fetch corresponding customers
|
|
|
|
for (const sensor of allSensors) {
|
|
|
|
const customers = await User.find({ customerId: { $in: customerIds } }).lean();
|
|
|
|
const cid = sensor.customerId;
|
|
|
|
|
|
|
|
if (!cid) continue;
|
|
|
|
|
|
|
|
|
|
|
|
const uniqueCustomerMap = {};
|
|
|
|
if (!customerStatusMap[cid]) {
|
|
|
|
|
|
|
|
customerStatusMap[cid] = { status: "unknown" };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const user of customers) {
|
|
|
|
if (resolvedHardwareIds.includes(sensor.hardwareId) || resolvedHardwareIds.includes(sensor.connected_to)) {
|
|
|
|
|
|
|
|
customerStatusMap[cid].status = "resolved";
|
|
|
|
|
|
|
|
} else if (sensor.connected_status === "disconnected") {
|
|
|
|
|
|
|
|
customerStatusMap[cid].status = "disconnected";
|
|
|
|
|
|
|
|
} else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected" && customerStatusMap[cid].status !== "resolved") {
|
|
|
|
|
|
|
|
customerStatusMap[cid].status = "connected";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const customerIds = Object.keys(customerStatusMap);
|
|
|
|
|
|
|
|
const users = await User.find({ customerId: { $in: customerIds } }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const combinedCustomerList = users.map(user => {
|
|
|
|
const cid = user.customerId;
|
|
|
|
const cid = user.customerId;
|
|
|
|
if (!uniqueCustomerMap[cid]) {
|
|
|
|
return {
|
|
|
|
uniqueCustomerMap[cid] = {
|
|
|
|
|
|
|
|
customer: {
|
|
|
|
|
|
|
|
customerId: cid,
|
|
|
|
customerId: cid,
|
|
|
|
|
|
|
|
connectionStatus: customerStatusMap[cid]?.status || "unknown",
|
|
|
|
username: user.username || "",
|
|
|
|
username: user.username || "",
|
|
|
|
firstName: user.profile?.firstName || user.firstName || "",
|
|
|
|
firstName: user.profile?.firstName || user.firstName || "",
|
|
|
|
lastName: user.profile?.lastName || user.lastName || "",
|
|
|
|
lastName: user.profile?.lastName || user.lastName || "",
|
|
|
@ -6600,24 +6718,21 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => {
|
|
|
|
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
|
|
|
|
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
updatedAt: user.updatedAt
|
|
|
|
updatedAt: user.updatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const response = Object.values(uniqueCustomerMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
data: response
|
|
|
|
data: combinedCustomerList
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching disconnected customer details:", error);
|
|
|
|
console.error("Error in getDisconnectedMoveCustomerDetails:", error);
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getResolvedCustomerDetails = async (req, reply) => {
|
|
|
|
exports.getResolvedCustomerDetails = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { supportId } = req.params;
|
|
|
|
const { supportId } = req.params;
|
|
|
|