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) => {
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) => {

@ -460,6 +460,48 @@ fastify.get('/api/slave-tank-details/:customerId/:hardwareId/:tankHardwareId', {
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(
'/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', {
schema: {
tags: ['Installation'],
summary: 'Update tank dimensions',
description: 'Edit tank details (height, width, length) by customerId, teamMemberId, hardwareId and tankHardwareId.',
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'],
properties: {
height: { type: 'number', description: 'New tank height (in cm)' },
width: { type: 'number', description: 'New tank width (in cm)' },
length: { type: 'number', description: 'New tank length (in cm)' }
}
},
// fastify.put('/api/tanks/:customerId/:teamMemberId/:hardwareId/:tankHardwareId', {
// schema: {
// tags: ['Installation'],
// summary: 'Update tank dimensions',
// description: 'Edit tank details (height, width, length) by customerId, teamMemberId, hardwareId and tankHardwareId.',
// 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'],
// properties: {
// height: { type: 'number', description: 'New tank height (in cm)' },
// width: { type: 'number', description: 'New tank width (in cm)' },
// length: { type: 'number', description: 'New tank length (in cm)' }
// }
// },
},
handler : installationController.editTankDimensions
});
// },
// handler : installationController.editTankDimensions
// });
fastify.post(

Loading…
Cancel
Save