changes in motordata

master^2
Varun 8 months ago
parent db1f524ab9
commit fbc337e142

@ -385,7 +385,7 @@ exports.getTanksofParticularInstaller = async (req, reply) => {
exports.getTankmotordata = async (req, reply) => {
try {
const { startDate, stopDate } = req.body;
const { startDate, stopDate,pumps,users } = req.body;
const { customerId } = req.params;
// Validate and format the input dates
@ -406,10 +406,21 @@ exports.getTankmotordata = async (req, reply) => {
if (user) {
const userName = user.username || "N/A";
const motordatas = await MotorData.find({
customerId,
});
let query = { customerId };
if (pumps !== "All") {
query.motor_id = pumps;
}
if (users !== "All") {
query.started_by = users;
}
// Fetch motor data with applied filters
const motordatas = await MotorData.find(query);
const filtereddatas = motordatas.filter((record) => {
@ -2706,7 +2717,7 @@ exports.getPumpsAndUsers = async (req, reply) => {
const username = user?.username || "";
// Include username at the beginning of staffNames and add "manual" at the end
const updatedStaffNames = [username, ...staffNames, "manual"];
const updatedStaffNames = [username, ...staffNames, "manual","user"];
// Prepare response
const result = {

@ -125,6 +125,7 @@ const customerautopercentages = ({
const motordataSchema = new mongoose.Schema({
customerId: { type: String, default: null },
motor_id: { type: String, default: null },
started_by:{ type: String, default: "user" },
start_instance_id:{type:String,default:null},
supplierTank: { type: String, default: null },
receiverTank: { type: String, default: null },
@ -142,6 +143,36 @@ const motordataSchema = new mongoose.Schema({
});
const updateMotorData = async () => {
try {
// Fetch all motor data where quantity_delivered is null or needs updating
const motorDataRecords = await MotorData.find({});
for (let record of motorDataRecords) {
// Convert string values to numbers by removing commas
const initialLevel = parseInt(record.receiverInitialwaterlevel.replace(/,/g, ""), 10) || 0;
const finalLevel = parseInt(record.receiverfinalwaterlevel.replace(/,/g, ""), 10) || 0;
// Calculate quantity delivered
const quantityDelivered = finalLevel - initialLevel;
// Update the record
await MotorData.updateOne(
{ _id: record._id },
{
$set: {
started_by: "user",
quantity_delivered: quantityDelivered.toString(), // Convert back to string for consistency
},
}
);
}
console.log("Motor data updated successfully!");
} catch (err) {
console.error("Error updating motor data:", err);
}
};
const tankSchema = new mongoose.Schema({
tankhardwareId: { type: String },

@ -864,7 +864,8 @@ module.exports = function (fastify, opts, next) {
properties: {
startDate:{ type: "string" },
stopDate:{type:"string"},
block:{type:"string"},
pumps:{type:"string"},
users:{type:"string"},

Loading…
Cancel
Save