|
|
|
@ -2021,6 +2021,155 @@ exports.getSlaveTankDetails = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.editTankDimensions = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, teamMemberId, hardwareId, tankHardwareId } = req.params;
|
|
|
|
|
const { height, width, length, unit } = req.body; // unit: 'cm' or 'feet'
|
|
|
|
|
|
|
|
|
|
if (!customerId || !teamMemberId || !hardwareId || !tankHardwareId) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'customerId, teamMemberId, hardwareId and tankHardwareId are required'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!height || !width || !length || !unit) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'height, width, length and unit are required in body'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse input numbers
|
|
|
|
|
const heightNum = Number(height);
|
|
|
|
|
const widthNum = Number(width);
|
|
|
|
|
const lengthNum = Number(length);
|
|
|
|
|
|
|
|
|
|
if (isNaN(heightNum) || isNaN(widthNum) || isNaN(lengthNum)) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'height, width and length must be numeric values'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!['cm', 'feet'].includes(unit)) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "unit must be either 'cm' or 'feet'"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Convert to feet if input is cm
|
|
|
|
|
let heightInFeet, widthInFeet, lengthInFeet;
|
|
|
|
|
|
|
|
|
|
if (unit === 'cm') {
|
|
|
|
|
heightInFeet = heightNum / 30.48;
|
|
|
|
|
widthInFeet = widthNum / 30.48;
|
|
|
|
|
lengthInFeet = lengthNum / 30.48;
|
|
|
|
|
} else {
|
|
|
|
|
heightInFeet = heightNum;
|
|
|
|
|
widthInFeet = widthNum;
|
|
|
|
|
lengthInFeet = lengthNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Round to nearest integer
|
|
|
|
|
const heightFeetInt = Math.round(heightInFeet);
|
|
|
|
|
const widthFeetInt = Math.round(widthInFeet);
|
|
|
|
|
const lengthFeetInt = Math.round(lengthInFeet);
|
|
|
|
|
|
|
|
|
|
// Step 2: Calculate capacity & waterCapacityPerCm (still use float here for precision)
|
|
|
|
|
const height_m = heightFeetInt * 0.3048;
|
|
|
|
|
const width_m = widthFeetInt * 0.3048;
|
|
|
|
|
const length_m = lengthFeetInt * 0.3048;
|
|
|
|
|
|
|
|
|
|
const capacity = Math.round(length_m * width_m * height_m * 1000); // liters, integer
|
|
|
|
|
const waterCapacityPerCm = Math.round(length_m * width_m * 0.01 * 1000); // liters per cm, integer
|
|
|
|
|
|
|
|
|
|
// Step 3: Find install record
|
|
|
|
|
const installRecord = await Install.findOne({
|
|
|
|
|
'team_member.team_member.teamMemberId': teamMemberId
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
if (!installRecord) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Team member not found or not assigned'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const teamMemberDetails = installRecord.team_member?.team_member?.find(
|
|
|
|
|
member => member.teamMemberId === teamMemberId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!teamMemberDetails) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Team member details not found under install record'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: Find order record
|
|
|
|
|
const orderRecord = await Order.findOne({
|
|
|
|
|
installationId: installRecord.installationId,
|
|
|
|
|
customerId
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
if (!orderRecord) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Order not found for this installation and customer'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 5: Update tank dimensions in feet & calculated fields
|
|
|
|
|
const updatedTank = await Tank.findOneAndUpdate(
|
|
|
|
|
{
|
|
|
|
|
customerId,
|
|
|
|
|
hardwareId,
|
|
|
|
|
tankhardwareId: tankHardwareId
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
height: heightFeetInt, // save as integer in feet
|
|
|
|
|
width: widthFeetInt,
|
|
|
|
|
length: lengthFeetInt,
|
|
|
|
|
capacity: capacity.toString(), // save as string
|
|
|
|
|
waterCapacityPerCm: waterCapacityPerCm.toString()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ new: true }
|
|
|
|
|
).lean();
|
|
|
|
|
|
|
|
|
|
if (!updatedTank) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Tank not found with given customerId, hardwareId and tankHardwareId'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
success: true,
|
|
|
|
|
message: 'Tank dimensions updated successfully',
|
|
|
|
|
updatedTank,
|
|
|
|
|
teamMember: {
|
|
|
|
|
teamMemberId: teamMemberDetails.teamMemberId,
|
|
|
|
|
firstName: teamMemberDetails.firstName,
|
|
|
|
|
phone: teamMemberDetails.phone,
|
|
|
|
|
email: teamMemberDetails.email,
|
|
|
|
|
alternativePhone: teamMemberDetails.alternativePhone,
|
|
|
|
|
installationTeamMemId: teamMemberDetails.installationTeamMemId,
|
|
|
|
|
status: teamMemberDetails.status
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error updating tank dimensions:', error);
|
|
|
|
|
return reply.code(500).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Internal server error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -3426,136 +3575,136 @@ exports.getMasterWithSlaves = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.editTankDimensions = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, teamMemberId, hardwareId, tankHardwareId } = req.params;
|
|
|
|
|
const { height, width, length } = req.body;
|
|
|
|
|
// exports.editTankDimensions = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId, teamMemberId, hardwareId, tankHardwareId } = req.params;
|
|
|
|
|
// const { height, width, length } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!customerId || !teamMemberId || !hardwareId || !tankHardwareId) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'customerId, teamMemberId, hardwareId and tankHardwareId are required'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!customerId || !teamMemberId || !hardwareId || !tankHardwareId) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'customerId, teamMemberId, hardwareId and tankHardwareId are required'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (height === undefined || width === undefined || length === undefined) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'height, width and length are required in body (in cm)'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// // if (height === undefined || width === undefined || length === undefined) {
|
|
|
|
|
// // return reply.code(400).send({
|
|
|
|
|
// // success: false,
|
|
|
|
|
// // message: 'height, width and length are required in body (in cm)'
|
|
|
|
|
// // });
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
isNaN(parseFloat(height)) ||
|
|
|
|
|
isNaN(parseFloat(width)) ||
|
|
|
|
|
isNaN(parseFloat(length))
|
|
|
|
|
) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'height, width and length must be numeric values (in cm)'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (
|
|
|
|
|
// isNaN(parseFloat(height)) ||
|
|
|
|
|
// isNaN(parseFloat(width)) ||
|
|
|
|
|
// isNaN(parseFloat(length))
|
|
|
|
|
// ) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'height, width and length must be numeric values (in cm)'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ✅ Helper: cm to feet
|
|
|
|
|
const cmToFeet = (cm) => {
|
|
|
|
|
const v = parseFloat(cm);
|
|
|
|
|
return !isNaN(v) ? parseFloat((v / 30.48).toFixed(2)) : null;
|
|
|
|
|
};
|
|
|
|
|
// // ✅ Helper: cm to feet
|
|
|
|
|
// const cmToFeet = (cm) => {
|
|
|
|
|
// const v = parseFloat(cm);
|
|
|
|
|
// return !isNaN(v) ? parseFloat((v / 30.48).toFixed(2)) : null;
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// Step 1: Find install record with teamMemberId
|
|
|
|
|
const installRecord = await Install.findOne({
|
|
|
|
|
'team_member.team_member.teamMemberId': teamMemberId
|
|
|
|
|
}).lean();
|
|
|
|
|
// // Step 1: Find install record with teamMemberId
|
|
|
|
|
// const installRecord = await Install.findOne({
|
|
|
|
|
// 'team_member.team_member.teamMemberId': teamMemberId
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
if (!installRecord) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Team member not found or not assigned'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!installRecord) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'Team member not found or not assigned'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const teamMemberDetails = installRecord.team_member?.team_member?.find(
|
|
|
|
|
member => member.teamMemberId === teamMemberId
|
|
|
|
|
) || null;
|
|
|
|
|
// const teamMemberDetails = installRecord.team_member?.team_member?.find(
|
|
|
|
|
// member => member.teamMemberId === teamMemberId
|
|
|
|
|
// ) || null;
|
|
|
|
|
|
|
|
|
|
if (!teamMemberDetails) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Team member details not found under install record'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!teamMemberDetails) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'Team member details not found under install record'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Step 2: Find the matching order using installationId and customerId
|
|
|
|
|
const orderRecord = await Order.findOne({
|
|
|
|
|
installationId: installRecord.installationId,
|
|
|
|
|
customerId
|
|
|
|
|
}).lean();
|
|
|
|
|
// // Step 2: Find the matching order using installationId and customerId
|
|
|
|
|
// const orderRecord = await Order.findOne({
|
|
|
|
|
// installationId: installRecord.installationId,
|
|
|
|
|
// customerId
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
if (!orderRecord) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Order not found for this installation and customer'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!orderRecord) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'Order not found for this installation and customer'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Step 3: Convert cm to feet before saving
|
|
|
|
|
const heightInFeet = cmToFeet(height);
|
|
|
|
|
const widthInFeet = cmToFeet(width);
|
|
|
|
|
const lengthInFeet = cmToFeet(length);
|
|
|
|
|
// // Step 3: Convert cm to feet before saving
|
|
|
|
|
// const heightInFeet = cmToFeet(height);
|
|
|
|
|
// const widthInFeet = cmToFeet(width);
|
|
|
|
|
// const lengthInFeet = cmToFeet(length);
|
|
|
|
|
|
|
|
|
|
// Step 4: Update the tank
|
|
|
|
|
const updatedTank = await Tank.findOneAndUpdate(
|
|
|
|
|
{
|
|
|
|
|
customerId,
|
|
|
|
|
hardwareId,
|
|
|
|
|
tankhardwareId: tankHardwareId
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
height: heightInFeet,
|
|
|
|
|
width: widthInFeet,
|
|
|
|
|
length: lengthInFeet
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ new: true }
|
|
|
|
|
).lean();
|
|
|
|
|
// // Step 4: Update the tank
|
|
|
|
|
// const updatedTank = await Tank.findOneAndUpdate(
|
|
|
|
|
// {
|
|
|
|
|
// customerId,
|
|
|
|
|
// hardwareId,
|
|
|
|
|
// tankhardwareId: tankHardwareId
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// $set: {
|
|
|
|
|
// height: heightInFeet,
|
|
|
|
|
// width: widthInFeet,
|
|
|
|
|
// length: lengthInFeet
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// { new: true }
|
|
|
|
|
// ).lean();
|
|
|
|
|
|
|
|
|
|
if (!updatedTank) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Tank not found with given customerId, hardwareId and tankHardwareId'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!updatedTank) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'Tank not found with given customerId, hardwareId and tankHardwareId'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Step 5: Return success response with both feet & original cm
|
|
|
|
|
return reply.send({
|
|
|
|
|
success: true,
|
|
|
|
|
message: 'Tank dimensions updated successfully',
|
|
|
|
|
updatedTank: {
|
|
|
|
|
...updatedTank,
|
|
|
|
|
heightIncm: parseFloat(height),
|
|
|
|
|
widthIncm: parseFloat(width),
|
|
|
|
|
lengthIncm: parseFloat(length)
|
|
|
|
|
},
|
|
|
|
|
teamMember: {
|
|
|
|
|
teamMemberId: teamMemberDetails.teamMemberId,
|
|
|
|
|
firstName: teamMemberDetails.firstName,
|
|
|
|
|
phone: teamMemberDetails.phone,
|
|
|
|
|
email: teamMemberDetails.email,
|
|
|
|
|
alternativePhone: teamMemberDetails.alternativePhone,
|
|
|
|
|
installationTeamMemId: teamMemberDetails.installationTeamMemId,
|
|
|
|
|
status: teamMemberDetails.status
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// // Step 5: Return success response with both feet & original cm
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// success: true,
|
|
|
|
|
// message: 'Tank dimensions updated successfully',
|
|
|
|
|
// updatedTank: {
|
|
|
|
|
// ...updatedTank,
|
|
|
|
|
// heightIncm: parseFloat(height),
|
|
|
|
|
// widthIncm: parseFloat(width),
|
|
|
|
|
// lengthIncm: parseFloat(length)
|
|
|
|
|
// },
|
|
|
|
|
// teamMember: {
|
|
|
|
|
// teamMemberId: teamMemberDetails.teamMemberId,
|
|
|
|
|
// firstName: teamMemberDetails.firstName,
|
|
|
|
|
// phone: teamMemberDetails.phone,
|
|
|
|
|
// email: teamMemberDetails.email,
|
|
|
|
|
// alternativePhone: teamMemberDetails.alternativePhone,
|
|
|
|
|
// installationTeamMemId: teamMemberDetails.installationTeamMemId,
|
|
|
|
|
// status: teamMemberDetails.status
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error updating tank dimensions:', error);
|
|
|
|
|
return reply.code(500).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Internal server error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error('Error updating tank dimensions:', error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// success: false,
|
|
|
|
|
// message: 'Internal server error'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPendingMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|