diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c26bb282..9fd3db05 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -8,6 +8,7 @@ const fastify = require("fastify")({ }); // const tanksController = require("./tanksController") +const moment = require('moment'); 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); tankData = { customerId: customerId, + hardwareId: req.body.hardwareId, tankName: req.body.tankName, blockName: req.body.blockName, capacity: req.body.capacity, @@ -52,6 +54,7 @@ console.log(req.params); usertobeInserted = checkFormEncoding.tank; console.log("thsi true url string"); tank.customerId = usertobeInserted.customerId + tank.hardwareId = usertobeInserted.hardwareId; tank.tankName = usertobeInserted.tankName; tank.blockName = usertobeInserted.blockName; tank.capacity = usertobeInserted.capacity; @@ -661,7 +664,7 @@ if(shape==="horizontalellipse"){ exports.IotDevice = async (req, reply) => { 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 const currentDate = new Date(); diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 47396428..d9bb1cbf 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1024,16 +1024,16 @@ exports.getPendingSuppliers = async (req, reply) => { .skip(startindex) .exec() .then((docs) => { - // const supplierDataWithTimestamp = docs.map((doc, index) => ({ - // data: doc, - // //timestamp: timestamps[index], - // })); - // reply.send({ - // status_code: 200, - // data: supplierDataWithTimestamp, - // count: docs.length, - // }); - reply.send({ status_code: 200, data: docs, count: docs.length }); + const customerDataWithTimestamp = docs.map((doc, index) => ({ + ...doc._doc, + timestamp: timestamps[index], + })); + reply.send({ + status_code: 200, + data: customerDataWithTimestamp, + count: docs.length, + }); + //reply.send({ status_code: 200, data: docs, count: docs.length }); }) .catch((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) => { 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 + const supplierId = req.params.supplierId; 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, - // }); + const customerDataWithTimestamp = docs.map((doc, index) => ({ + ...doc._doc, + timestamp: timestamps[index], + })); + reply.send({ + status_code: 200, + data: customerDataWithTimestamp, + count: docs.length, + }); }) .catch((err) => { console.log(err); diff --git a/src/models/tanks.js b/src/models/tanks.js index 9ac69bb2..8fa2745f 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -1,4 +1,5 @@ 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 RoleSchema = new Schema({ name: String }); const tanksSchema = new mongoose.Schema({ + hardwareId: { type: String, unique: true, required: true }, customerId: { type: String, default: null }, tankName: { type: String, default: null }, blockName: { type: String, default: null }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 1dedcbd3..642781c7 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -23,6 +23,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { + hardwareId: { type: "string" }, tankName: { type: "string" }, blockName: { type: "string"}, capacity: { type: "string" },