update tank dimemsion from installer team member

master^2
Bhaskar 3 months ago
parent 00ec6b9f89
commit a8b93f4919

@ -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) => { // exports.editTankDimensions = async (req, reply) => {
try { // try {
const { customerId, teamMemberId, hardwareId, tankHardwareId } = req.params; // const { customerId, teamMemberId, hardwareId, tankHardwareId } = req.params;
const { height, width, length } = req.body; // const { height, width, length } = req.body;
if (!customerId || !teamMemberId || !hardwareId || !tankHardwareId) { // if (!customerId || !teamMemberId || !hardwareId || !tankHardwareId) {
return reply.code(400).send({ // return reply.code(400).send({
success: false, // success: false,
message: 'customerId, teamMemberId, hardwareId and tankHardwareId are required' // message: 'customerId, teamMemberId, hardwareId and tankHardwareId are required'
}); // });
} // }
// if (height === undefined || width === undefined || length === undefined) { // // if (height === undefined || width === undefined || length === undefined) {
// return reply.code(400).send({ // // return reply.code(400).send({
// success: false, // // success: false,
// message: 'height, width and length are required in body (in cm)' // // message: 'height, width and length are required in body (in cm)'
// }); // // });
// } // // }
if ( // if (
isNaN(parseFloat(height)) || // isNaN(parseFloat(height)) ||
isNaN(parseFloat(width)) || // isNaN(parseFloat(width)) ||
isNaN(parseFloat(length)) // isNaN(parseFloat(length))
) { // ) {
return reply.code(400).send({ // return reply.code(400).send({
success: false, // success: false,
message: 'height, width and length must be numeric values (in cm)' // message: 'height, width and length must be numeric values (in cm)'
}); // });
} // }
// ✅ Helper: cm to feet // // ✅ Helper: cm to feet
const cmToFeet = (cm) => { // const cmToFeet = (cm) => {
const v = parseFloat(cm); // const v = parseFloat(cm);
return !isNaN(v) ? parseFloat((v / 30.48).toFixed(2)) : null; // return !isNaN(v) ? parseFloat((v / 30.48).toFixed(2)) : null;
}; // };
// Step 1: Find install record with teamMemberId // // Step 1: Find install record with teamMemberId
const installRecord = await Install.findOne({ // const installRecord = await Install.findOne({
'team_member.team_member.teamMemberId': teamMemberId // 'team_member.team_member.teamMemberId': teamMemberId
}).lean(); // }).lean();
if (!installRecord) { // if (!installRecord) {
return reply.code(404).send({ // return reply.code(404).send({
success: false, // success: false,
message: 'Team member not found or not assigned' // message: 'Team member not found or not assigned'
}); // });
} // }
const teamMemberDetails = installRecord.team_member?.team_member?.find( // const teamMemberDetails = installRecord.team_member?.team_member?.find(
member => member.teamMemberId === teamMemberId // member => member.teamMemberId === teamMemberId
) || null; // ) || null;
if (!teamMemberDetails) { // if (!teamMemberDetails) {
return reply.code(404).send({ // return reply.code(404).send({
success: false, // success: false,
message: 'Team member details not found under install record' // message: 'Team member details not found under install record'
}); // });
} // }
// Step 2: Find the matching order using installationId and customerId // // Step 2: Find the matching order using installationId and customerId
const orderRecord = await Order.findOne({ // const orderRecord = await Order.findOne({
installationId: installRecord.installationId, // installationId: installRecord.installationId,
customerId // customerId
}).lean(); // }).lean();
if (!orderRecord) { // if (!orderRecord) {
return reply.code(404).send({ // return reply.code(404).send({
success: false, // success: false,
message: 'Order not found for this installation and customer' // message: 'Order not found for this installation and customer'
}); // });
} // }
// Step 3: Convert cm to feet before saving // // Step 3: Convert cm to feet before saving
const heightInFeet = cmToFeet(height); // const heightInFeet = cmToFeet(height);
const widthInFeet = cmToFeet(width); // const widthInFeet = cmToFeet(width);
const lengthInFeet = cmToFeet(length); // const lengthInFeet = cmToFeet(length);
// Step 4: Update the tank // // Step 4: Update the tank
const updatedTank = await Tank.findOneAndUpdate( // const updatedTank = await Tank.findOneAndUpdate(
{ // {
customerId, // customerId,
hardwareId, // hardwareId,
tankhardwareId: tankHardwareId // tankhardwareId: tankHardwareId
}, // },
{ // {
$set: { // $set: {
height: heightInFeet, // height: heightInFeet,
width: widthInFeet, // width: widthInFeet,
length: lengthInFeet // length: lengthInFeet
} // }
}, // },
{ new: true } // { new: true }
).lean(); // ).lean();
if (!updatedTank) { // if (!updatedTank) {
return reply.code(404).send({ // return reply.code(404).send({
success: false, // success: false,
message: 'Tank not found with given customerId, hardwareId and tankHardwareId' // message: 'Tank not found with given customerId, hardwareId and tankHardwareId'
}); // });
} // }
// Step 5: Return success response with both feet & original cm // // Step 5: Return success response with both feet & original cm
return reply.send({ // return reply.send({
success: true, // success: true,
message: 'Tank dimensions updated successfully', // message: 'Tank dimensions updated successfully',
updatedTank: { // updatedTank: {
...updatedTank, // ...updatedTank,
heightIncm: parseFloat(height), // heightIncm: parseFloat(height),
widthIncm: parseFloat(width), // widthIncm: parseFloat(width),
lengthIncm: parseFloat(length) // lengthIncm: parseFloat(length)
}, // },
teamMember: { // teamMember: {
teamMemberId: teamMemberDetails.teamMemberId, // teamMemberId: teamMemberDetails.teamMemberId,
firstName: teamMemberDetails.firstName, // firstName: teamMemberDetails.firstName,
phone: teamMemberDetails.phone, // phone: teamMemberDetails.phone,
email: teamMemberDetails.email, // email: teamMemberDetails.email,
alternativePhone: teamMemberDetails.alternativePhone, // alternativePhone: teamMemberDetails.alternativePhone,
installationTeamMemId: teamMemberDetails.installationTeamMemId, // installationTeamMemId: teamMemberDetails.installationTeamMemId,
status: teamMemberDetails.status // status: teamMemberDetails.status
} // }
}); // });
} catch (error) { // } catch (error) {
console.error('Error updating tank dimensions:', error); // console.error('Error updating tank dimensions:', error);
return reply.code(500).send({ // return reply.code(500).send({
success: false, // success: false,
message: 'Internal server error' // message: 'Internal server error'
}); // });
} // }
}; // };
exports.getPendingMasterSlaveSummary = async (req, reply) => { exports.getPendingMasterSlaveSummary = async (req, reply) => {

@ -460,6 +460,48 @@ fastify.get('/api/slave-tank-details/:customerId/:hardwareId/:tankHardwareId', {
handler: installationController.getSlaveTankDetails handler: installationController.getSlaveTankDetails
}); });
fastify.put('/api/Updatetanksdimensisons/:customerId/:teamMemberId/:hardwareId/:tankHardwareId', {
schema: {
tags: ['Installation'],
summary: 'Update tank dimensions',
description: 'Edit tank dimensions by customerId, teamMemberId, hardwareId, and tankHardwareId. '
+ 'Provide dimensions in either centimeters or feet, specify using the "unit" field. '
+ 'Saves the data in feet (rounded to integer) and updates capacity fields.',
params: {
type: 'object',
required: ['customerId', 'teamMemberId', 'hardwareId', 'tankHardwareId'],
properties: {
customerId: { type: 'string', description: 'Customer ID' },
teamMemberId: { type: 'string', description: 'Team member ID' },
hardwareId: { type: 'string', description: 'Master hardwareId' },
tankHardwareId: { type: 'string', description: 'Tank hardwareId' }
}
},
body: {
type: 'object',
required: ['height', 'width', 'length', 'unit'],
properties: {
height: { type: 'string', description: 'Tank height (numeric string)' },
width: { type: 'string', description: 'Tank width (numeric string)' },
length: { type: 'string', description: 'Tank length (numeric string)' },
unit: { type: 'string', enum: ['cm', 'feet'], description: 'Unit of the dimensions provided (must be either "cm" or "feet")' }
},
// example: {
// height: "210", // in cm
// width: "150", // in cm
// length: "300", // in cm
// unit: "cm"
// }
},
},
handler: installationController.editTankDimensions
});
fastify.post( fastify.post(
'/api/update-status/:connectedTo/:teamMemberId/:customerId', '/api/update-status/:connectedTo/:teamMemberId/:customerId',
{ {
@ -519,34 +561,34 @@ fastify.get('/api/slave-tank-details/:customerId/:hardwareId/:tankHardwareId', {
} }
); );
fastify.put('/api/tanks/:customerId/:teamMemberId/:hardwareId/:tankHardwareId', { // fastify.put('/api/tanks/:customerId/:teamMemberId/:hardwareId/:tankHardwareId', {
schema: { // schema: {
tags: ['Installation'], // tags: ['Installation'],
summary: 'Update tank dimensions', // summary: 'Update tank dimensions',
description: 'Edit tank details (height, width, length) by customerId, teamMemberId, hardwareId and tankHardwareId.', // description: 'Edit tank details (height, width, length) by customerId, teamMemberId, hardwareId and tankHardwareId.',
params: { // params: {
type: 'object', // type: 'object',
required: ['customerId', 'teamMemberId', 'hardwareId', 'tankHardwareId'], // required: ['customerId', 'teamMemberId', 'hardwareId', 'tankHardwareId'],
properties: { // properties: {
customerId: { type: 'string', description: 'Customer ID' }, // customerId: { type: 'string', description: 'Customer ID' },
teamMemberId: { type: 'string', description: 'Team member ID' }, // teamMemberId: { type: 'string', description: 'Team member ID' },
hardwareId: { type: 'string', description: 'Master hardwareId' }, // hardwareId: { type: 'string', description: 'Master hardwareId' },
tankHardwareId: { type: 'string', description: 'Tank hardwareId' } // tankHardwareId: { type: 'string', description: 'Tank hardwareId' }
} // }
}, // },
body: { // body: {
type: 'object', // type: 'object',
//required: ['height', 'width', 'length'], // //required: ['height', 'width', 'length'],
properties: { // properties: {
height: { type: 'number', description: 'New tank height (in cm)' }, // height: { type: 'number', description: 'New tank height (in cm)' },
width: { type: 'number', description: 'New tank width (in cm)' }, // width: { type: 'number', description: 'New tank width (in cm)' },
length: { type: 'number', description: 'New tank length (in cm)' } // length: { type: 'number', description: 'New tank length (in cm)' }
} // }
}, // },
}, // },
handler : installationController.editTankDimensions // handler : installationController.editTankDimensions
}); // });
fastify.post( fastify.post(

Loading…
Cancel
Save