|
|
@ -635,7 +635,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
// 🔁 Raise ticket if applicable
|
|
|
|
// 🔁 Raise ticket if applicable
|
|
|
|
const sensor = await Insensors.findOne({ hardwareId }).lean();
|
|
|
|
const sensor = await Insensors.findOne({ hardwareId }).lean();
|
|
|
|
if (sensor?.customerId) {
|
|
|
|
if (sensor?.customerId) {
|
|
|
|
await raiseATicketLikeLogic(sensor.customerId, hardwareId);
|
|
|
|
//await raiseATicketLikeLogic(sensor.customerId, hardwareId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
@ -716,7 +716,7 @@ exports.getByHardwareIdSupport = async (req, reply) => {
|
|
|
|
// 🔁 Raise ticket if applicable
|
|
|
|
// 🔁 Raise ticket if applicable
|
|
|
|
const sensor = await Insensors.findOne({ hardwareId }).lean();
|
|
|
|
const sensor = await Insensors.findOne({ hardwareId }).lean();
|
|
|
|
if (sensor?.customerId) {
|
|
|
|
if (sensor?.customerId) {
|
|
|
|
await raiseATicketLikeLogic(sensor.customerId, hardwareId);
|
|
|
|
//await raiseATicketLikeLogic(sensor.customerId, hardwareId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
@ -3873,13 +3873,13 @@ const raiseATicketLikeLogic = async (customerId, connected_to) => {
|
|
|
|
s =>
|
|
|
|
s =>
|
|
|
|
s.hardwareId?.trim().toLowerCase() === normalizedConnectedTo &&
|
|
|
|
s.hardwareId?.trim().toLowerCase() === normalizedConnectedTo &&
|
|
|
|
s.type === "master" &&
|
|
|
|
s.type === "master" &&
|
|
|
|
s.support_issue_status === "inactive"
|
|
|
|
s.support_issue_status !== "active" // 🔴 Don't raise if already active
|
|
|
|
);
|
|
|
|
);
|
|
|
|
if (!masterSensor) return;
|
|
|
|
if (!masterSensor) return;
|
|
|
|
|
|
|
|
|
|
|
|
const orderMap = {};
|
|
|
|
const orderMap = {};
|
|
|
|
orders.forEach(order => {
|
|
|
|
orders.forEach(order => {
|
|
|
|
order.master_connections.forEach(conn => {
|
|
|
|
order.master_connections?.forEach(conn => {
|
|
|
|
orderMap[conn.hardwareId] = {
|
|
|
|
orderMap[conn.hardwareId] = {
|
|
|
|
masterName: conn.master_name || null,
|
|
|
|
masterName: conn.master_name || null,
|
|
|
|
location: conn.location || null
|
|
|
|
location: conn.location || null
|
|
|
@ -3897,12 +3897,15 @@ const raiseATicketLikeLogic = async (customerId, connected_to) => {
|
|
|
|
const connectedSlaves = sensors.filter(
|
|
|
|
const connectedSlaves = sensors.filter(
|
|
|
|
s =>
|
|
|
|
s =>
|
|
|
|
s.connected_to?.trim().toLowerCase() === normalizedConnectedTo &&
|
|
|
|
s.connected_to?.trim().toLowerCase() === normalizedConnectedTo &&
|
|
|
|
s.type === "slave" &&
|
|
|
|
s.type === "slave"
|
|
|
|
s.support_issue_status === "inactive"
|
|
|
|
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const disconnectedSlaves = connectedSlaves
|
|
|
|
const disconnectedSlaves = connectedSlaves
|
|
|
|
.filter(s => s.connected_status === "disconnected")
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
s =>
|
|
|
|
|
|
|
|
s.connected_status === "disconnected" &&
|
|
|
|
|
|
|
|
s.support_issue_status !== "active" // 🔴 Don't re-raise for already active
|
|
|
|
|
|
|
|
)
|
|
|
|
.map(s => ({
|
|
|
|
.map(s => ({
|
|
|
|
slaveHardwareId: s.tankhardwareId?.trim().toLowerCase(),
|
|
|
|
slaveHardwareId: s.tankhardwareId?.trim().toLowerCase(),
|
|
|
|
slaveName: s.tankName || "Unknown Slave"
|
|
|
|
slaveName: s.tankName || "Unknown Slave"
|
|
|
@ -3999,40 +4002,45 @@ cron.schedule("* * * * *", async () => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
console.log("🔁 Running auto-disconnect ticket check...");
|
|
|
|
console.log("🔁 Running auto-disconnect ticket check...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get all master sensors
|
|
|
|
const allMasters = await Insensors.find({ type: "master" }).lean();
|
|
|
|
const allMasters = await Insensors.find({ type: "master" }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
for (const master of allMasters) {
|
|
|
|
for (const master of allMasters) {
|
|
|
|
const customerId = master.customerId;
|
|
|
|
const customerId = master.customerId;
|
|
|
|
const hardwareId = master.hardwareId;
|
|
|
|
const hardwareId = master.hardwareId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get all slaves connected to this master
|
|
|
|
const connectedSlaves = await Insensors.find({
|
|
|
|
const connectedSlaves = await Insensors.find({
|
|
|
|
connected_to: hardwareId,
|
|
|
|
connected_to: hardwareId,
|
|
|
|
type: "slave"
|
|
|
|
type: "slave"
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Filter disconnected slaves (regardless of support_issue_status)
|
|
|
|
const disconnectedSlaves = connectedSlaves.filter(
|
|
|
|
const disconnectedSlaves = connectedSlaves.filter(
|
|
|
|
s => s.connected_status === "disconnected"
|
|
|
|
s => s.connected_status === "disconnected"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check master disconnected and active status
|
|
|
|
const masterIsDisconnected = master.connected_status === "disconnected";
|
|
|
|
const masterIsDisconnected = master.connected_status === "disconnected";
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Check if master already has unresolved ticket
|
|
|
|
|
|
|
|
const masterAlreadyActive = master.support_issue_status === "active";
|
|
|
|
const masterAlreadyActive = master.support_issue_status === "active";
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Check if any disconnected slave already has unresolved ticket
|
|
|
|
// Check if any disconnected slave is already marked active
|
|
|
|
const anySlaveAlreadyActive = disconnectedSlaves.some(
|
|
|
|
const anySlaveAlreadyActive = disconnectedSlaves.some(
|
|
|
|
slave => slave.support_issue_status === "active"
|
|
|
|
slave => slave.support_issue_status === "active"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Only raise a ticket if not already active
|
|
|
|
// Decide if ticket should be raised:
|
|
|
|
|
|
|
|
// Raise if master is disconnected and not already active
|
|
|
|
|
|
|
|
// OR there are disconnected slaves and none of them are active
|
|
|
|
const shouldRaiseTicket =
|
|
|
|
const shouldRaiseTicket =
|
|
|
|
(masterIsDisconnected && !masterAlreadyActive) ||
|
|
|
|
(masterIsDisconnected && !masterAlreadyActive) ||
|
|
|
|
(disconnectedSlaves.length > 0 && !anySlaveAlreadyActive);
|
|
|
|
(disconnectedSlaves.length > 0 && !anySlaveAlreadyActive);
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldRaiseTicket) {
|
|
|
|
if (shouldRaiseTicket) {
|
|
|
|
|
|
|
|
console.log(`⚠️ Raising ticket for master: ${hardwareId}`);
|
|
|
|
await raiseATicketLikeLogic(customerId, hardwareId);
|
|
|
|
await raiseATicketLikeLogic(customerId, hardwareId);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.log(`Skipping ticket for ${hardwareId} — already active.`);
|
|
|
|
console.log(`✅ Skipping ticket for ${hardwareId} — already active.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
@ -4041,6 +4049,8 @@ cron.schedule("* * * * *", async () => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
|