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