storing latest 3 iot data entries and deleting remaining

master
varun 3 years ago
commit 02c1276966

@ -8,6 +8,7 @@ const fastify = require("fastify")({
}); });
// const tanksController = require("./tanksController") // const tanksController = require("./tanksController")
const moment = require('moment');
exports.addTanks = async (req, reply) => { exports.addTanks = async (req, reply) => {
@ -27,6 +28,7 @@ console.log(req.params);
// console.log("This is the reply in the handler after the validations", reply); // console.log("This is the reply in the handler after the validations", reply);
tankData = { tankData = {
customerId: customerId, customerId: customerId,
hardwareId: req.body.hardwareId,
tankName: req.body.tankName, tankName: req.body.tankName,
blockName: req.body.blockName, blockName: req.body.blockName,
capacity: req.body.capacity, capacity: req.body.capacity,
@ -52,6 +54,7 @@ console.log(req.params);
usertobeInserted = checkFormEncoding.tank; usertobeInserted = checkFormEncoding.tank;
console.log("thsi true url string"); console.log("thsi true url string");
tank.customerId = usertobeInserted.customerId tank.customerId = usertobeInserted.customerId
tank.hardwareId = usertobeInserted.hardwareId;
tank.tankName = usertobeInserted.tankName; tank.tankName = usertobeInserted.tankName;
tank.blockName = usertobeInserted.blockName; tank.blockName = usertobeInserted.blockName;
tank.capacity = usertobeInserted.capacity; tank.capacity = usertobeInserted.capacity;
@ -661,7 +664,7 @@ if(shape==="horizontalellipse"){
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body;
// create a new tank document with the current date and time // create a new tank document with the current date and time
const currentDate = new Date(); const currentDate = new Date();

@ -1024,16 +1024,16 @@ exports.getPendingSuppliers = async (req, reply) => {
.skip(startindex) .skip(startindex)
.exec() .exec()
.then((docs) => { .then((docs) => {
// const supplierDataWithTimestamp = docs.map((doc, index) => ({ const customerDataWithTimestamp = docs.map((doc, index) => ({
// data: doc, ...doc._doc,
// //timestamp: timestamps[index], timestamp: timestamps[index],
// })); }));
// reply.send({ reply.send({
// status_code: 200, status_code: 200,
// data: supplierDataWithTimestamp, data: customerDataWithTimestamp,
// count: docs.length, count: docs.length,
// }); });
reply.send({ status_code: 200, data: docs, count: docs.length }); //reply.send({ status_code: 200, data: docs, count: docs.length });
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
@ -1076,39 +1076,79 @@ exports.getRejectSuppliers = async (req, reply) => {
} }
}; };
// exports.getPendingCustomers = async (req, reply) => {
// const limit = parseInt(req.query.limit) || 100;
// const page = parseInt(req.query.page) || 1;
// const startindex = (page - 1) * limit;
// const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object
// try {
// const friendRequests = await FriendRequest.find({
// supplierId,
// status: ["pending"],
// });
// console.log(friendRequests, supplierId, "su....");
// const supplierIdsToInclude = friendRequests.map(
// (request) => request.customerId
// );
// console.log(supplierIdsToInclude, "supplierIdsToInclude..");
// const timestamps = friendRequests.map((request) => request.timestamp);
// console.log(timestamps, "timestamps");
// await User.find({ customerId: { $in: supplierIdsToInclude } })
// .limit(limit)
// .skip(startindex)
// .exec()
// .then((docs) => {
// // const customerDataWithTimestamp = docs.map((doc, index) => ({
// // data: doc,
// // //timestamp: timestamps[index],
// // }));
// reply.send({ status_code: 200, data: docs, count: docs.length });
// // reply.send({
// // status_code: 200,
// // data: customerDataWithTimestamp,
// // count: docs.length,
// // });
// })
// .catch((err) => {
// console.log(err);
// reply.send({ error: err });
// });
// } catch (err) {
// throw boom.boomify(err);
// }
// };
exports.getPendingCustomers = async (req, reply) => { exports.getPendingCustomers = async (req, reply) => {
const limit = parseInt(req.query.limit) || 100; const limit = parseInt(req.query.limit) || 100;
const page = parseInt(req.query.page) || 1; const page = parseInt(req.query.page) || 1;
const startindex = (page - 1) * limit; const startindex = (page - 1) * limit;
const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object const supplierId = req.params.supplierId;
try { try {
const friendRequests = await FriendRequest.find({ const friendRequests = await FriendRequest.find({
supplierId, supplierId,
status: ["pending"], status: ["pending"],
}); });
console.log(friendRequests, supplierId, "su....");
const supplierIdsToInclude = friendRequests.map( const supplierIdsToInclude = friendRequests.map(
(request) => request.customerId (request) => request.customerId
); );
console.log(supplierIdsToInclude, "supplierIdsToInclude..");
const timestamps = friendRequests.map((request) => request.timestamp); const timestamps = friendRequests.map((request) => request.timestamp);
console.log(timestamps, "timestamps");
await User.find({ customerId: { $in: supplierIdsToInclude } }) await User.find({ customerId: { $in: supplierIdsToInclude } })
.limit(limit) .limit(limit)
.skip(startindex) .skip(startindex)
.exec() .exec()
.then((docs) => { .then((docs) => {
// const customerDataWithTimestamp = docs.map((doc, index) => ({ const customerDataWithTimestamp = docs.map((doc, index) => ({
// data: doc, ...doc._doc,
// //timestamp: timestamps[index], timestamp: timestamps[index],
// })); }));
reply.send({ status_code: 200, data: docs, count: docs.length }); reply.send({
// reply.send({ status_code: 200,
// status_code: 200, data: customerDataWithTimestamp,
// data: customerDataWithTimestamp, count: docs.length,
// count: docs.length, });
// });
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);

@ -1,4 +1,5 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const moment = require('moment');
@ -9,6 +10,7 @@ const ObjectId = Schema.Types.ObjectId;
const code = Math.floor(100000 + Math.random() * 900000); const code = Math.floor(100000 + Math.random() * 900000);
const RoleSchema = new Schema({ name: String }); const RoleSchema = new Schema({ name: String });
const tanksSchema = new mongoose.Schema({ const tanksSchema = new mongoose.Schema({
hardwareId: { type: String, unique: true, required: true },
customerId: { type: String, default: null }, customerId: { type: String, default: null },
tankName: { type: String, default: null }, tankName: { type: String, default: null },
blockName: { type: String, default: null }, blockName: { type: String, default: null },

@ -23,6 +23,7 @@ module.exports = function (fastify, opts, next) {
body: { body: {
type: "object", type: "object",
properties: { properties: {
hardwareId: { type: "string" },
tankName: { type: "string" }, tankName: { type: "string" },
blockName: { type: "string"}, blockName: { type: "string"},
capacity: { type: "string" }, capacity: { type: "string" },

Loading…
Cancel
Save