diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 89cf11c0..5566714d 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -329,81 +329,164 @@ exports.installSignUp = async (request, reply) => { ); return result.seq; }; - - - exports.addStore = async (req, reply) => { - try { - var s_id = await generateStoreId(); - var building = ((req.body.storename).slice(0, 3)).toUpperCase(); - var store_id = `AWSST${building}${s_id}`; + const saltRounds = 10; + + + async function bcryptPassword(password) { + encryptedPwd = bcrypt.hash(password, saltRounds); + return encryptedPwd; +} + +async function bcryptComparePassword(pwd, encpassword) { + isSame = bcrypt.compare(pwd, encpassword); + return isSame; +} + + + +exports.addStore = async (request, reply) => { + try { + const s_id = await generateStoreId(); + const store_id = `AWIN${s_id}`; + + const { + // name, + phone, + address, + address1, + address2, + emails, + password, + profile, + + longitude, + latitude, + fcmId, + alternativeNumber, + firstName, + lastName, + city, + createdBy, + updatedBy, + } = request.body; + + // Check if a user with the same phone number already exists + const existingStore = await Store.findOne({ phone }); + if (existingStore) { + return reply.status(400).send({ message: 'Phone is already registered' }); + } + + // Hash the password using bcrypt + const hashedPassword = await bcrypt.hash(password, 10); + + // Create a new install object with the hashed password and other details + const store = new Store({ + // name, + store_id, + phone, + address, + address1, + address2, + emails, + services: { password: { bcrypt: hashedPassword } }, + profile, - s_data = { - storeId: store_id, - storename: req.body.storename, - emails: req.body.emails, - password: req.body.password, - phone: req.body.phone, - description: req.body.description, - profile: { - firstName: req.body.firstName, - lastName: req.body.lastName, - contactNumber: req.body.phone, - alternativeContactNumber: req.body.alternativeContactNumber, - office_address: req.body.office_address, - country: req.body.country, - state: req.body.state, - city: req.body.city, - zip: req.body.zip, - }, - latitude: req.body.latitude, - longitude: req.body.longitude, - fcmId: req.body.fcmId, - }; + longitude, + latitude, + fcmId, + alternativeNumber, + firstName, + lastName, + city, + createdBy, + updatedBy, + }); + + // Save the new install to the database + await store.save(); + + reply.send({ message: 'Install Account Created Successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +} + + + // exports.addStore = async (req, reply) => { + // try { + // const { storename, password, phone, emails } = req.body; - var store = new Store(s_data); - storepass = req.body.password; + // if (!emails) { + // return reply.status(400).send({ error: 'Email is required and cannot be null.' }); + // } - // Store hash in your password DB. - hash = await bcryptPassword(storepass); + // var s_id = await generateStoreId(); + // var building = storename.slice(0, 3).toUpperCase(); + // var store_id = `AWSST${building}${s_id}`; - if (hash) { - store.services.password.bcrypt = hash; - if (req.body.role) { - store.profile.role = req.body.role; - } else { - role = ["store"]; - store.profile.role = role; - } + // const s_data = { + // storeId: store_id, + // storename, + // emails: emails.trim(), + // password, + // phone, + // description: req.body.description, + // profile: { + // firstName: req.body.firstName, + // lastName: req.body.lastName, + // contactNumber: phone, + // alternativeContactNumber: req.body.alternativeContactNumber, + // office_address: req.body.office_address, + // country: req.body.country, + // state: req.body.state, + // city: req.body.city, + // zip: req.body.zip, + // }, + // latitude: req.body.latitude, + // longitude: req.body.longitude, + // fcmId: req.body.fcmId, + // }; - insertedStore = await store.save(); - if (insertedStore) { - var retStore = { - armintatankdata: { - storename: insertedStore.storename, - phone: insertedStore.phone, - storeId: insertedStore.storeId, - office_address: insertedStore.profile.office_address, - emails: [ - { - email: insertedStore.emails[0].email, - }, - ], - profile: insertedStore.profile, - latitude: insertedStore.latitude, - longitude: insertedStore.longitude, - fcmId: insertedStore.fcmId, - description: insertedStore.description, - }, - status_code: 200, - }; + // var store = new Store(s_data); + // var storepass = password; + + // // Store hash in your password DB. + // var hash = await bcryptPassword(storepass); + + // if (hash) { + // store.services.password.bcrypt = hash; + // store.profile.role = req.body.role || ["store"]; + + // var insertedStore = await Store.save(); + // if (insertedStore) { + // var retStore = { + // armintatankdata: { + // storename: insertedStore.storename, + // phone: insertedStore.phone, + // storeId: insertedStore.storeId, + // office_address: insertedStore.profile.office_address, + // emails: insertedStore.emails, + // profile: insertedStore.profile, + // latitude: insertedStore.latitude, + // longitude: insertedStore.longitude, + // fcmId: insertedStore.fcmId, + // description: insertedStore.description, + // }, + // status_code: 200, + // }; + + // return reply.status(200).send(retStore); + // } + // } + // } catch (err) { + // if (err.code === 11000) { + // // Handle duplicate key error + // return reply.status(400).send({ error: 'Duplicate key error', message: err.message }); + // } + // throw boom.boomify(err); + // } + // }; - return retStore; - } - } - } catch (err) { - throw boom.boomify(err); - } - }; const generatewaterlevelsensorId = async () => { const result = await Counter.findOneAndUpdate( @@ -417,12 +500,13 @@ exports.installSignUp = async (request, reply) => { const moment = require('moment'); exports.createwaterlevelSensor = async (req, reply) => { try { - const { hardwareId,hardwareId_company, type, indate } = req.body; + const { hardwareId,hardwareId_company, type, indate,storeId } = req.body; var mater_seq_id = await generatewaterlevelsensorId(); const date = moment().format('MM-DD'); const prefix = 'AS-' + date + '--MALOV1-'; var masterId = `${prefix}${mater_seq_id}`; const newSensor = new WaterLeverSensor({ + storeId, hardwareId, masterId, type, @@ -436,4 +520,26 @@ exports.installSignUp = async (request, reply) => { }; + exports.qccheckwaterlevelSensor = async (request, reply) => { + try { + const { hardwareId } = request.params; + const updateData = request.body; + + // Find the document by hardwareId and update it with the fields received in the body + const updatedSensor = await WaterLeverSensor.findOneAndUpdate( + { hardwareId: hardwareId }, + { $set: updateData }, + { new: true } // Return the updated document + ); + + if (!updatedSensor) { + return reply.status(404).send({ error: 'Sensor not found' }); + } + + return reply.status(200).send(updatedSensor); + } catch (error) { + console.error(error); + return reply.status(500).send({ error: 'An error occurred while updating the sensor' }); + } + }; \ No newline at end of file diff --git a/src/models/store.js b/src/models/store.js index 68d0248c..49708428 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -101,16 +101,21 @@ const installationschema = new mongoose.Schema({ } }); + const storeSchema = new mongoose.Schema({ storename: { type: String }, phone: { type: String, unique: true, trim: true }, storeId: { type: String, default: null }, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, - passwordResetCode: { type: Number, default: code }, + passwordResetCode: { type: Number, default: 11111 }, oneTimePasswordSetFlag: { type: Boolean, default: false }, - emails: [{ email: String, verified: { type: Boolean, default: false } }], - services: { password: { bcrypt: String } }, + emails: { type: String, unique: true }, + services: { + password: { + bcrypt: { type: String, required: true } + } + }, description: { type: String, default: null }, startingPrice: { type: String, default: 0.0 }, profile: { @@ -151,9 +156,16 @@ const installationschema = new mongoose.Schema({ updatedBy: ObjectId, }, { versionKey: false }); + module.exports = mongoose.model('Store', storeSchema); + + + + + const waterLeverSensorInSchema = new mongoose.Schema({ + storeeId:{ type: String }, hardwareId: { type: String }, masterId: { type: String, default: null }, type: { type: String }, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 4df03ac6..d43bb7d5 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -98,21 +98,13 @@ fastify.post('/api/stores', { summary: "This is for Create New Store.", body: { type: "object", + required: ["storename", "phone", "emails", "password"], properties: { storename: { type: "string" }, phone: { type: "string" }, alternativeContactNumber: { type: "string" }, password: { type: "string" }, - emails: { - type: "array", - maxItems: 2, - items: { - type: "object", - properties: { - email: { type: "string", default: null }, - }, - }, - }, + emails: { type: "string" }, office_address: { type: "string", default: null }, city: { type: "string", default: null }, state: { type: "string", default: null }, @@ -126,23 +118,22 @@ fastify.post('/api/stores', { }, security: [{ basicAuth: [] }], }, - // preHandler: [ - // validationHandler.fieldCheck, - // validationHandler.verifyStore, - // validationHandler.validateEmailFormat, - // ], handler: storeController.addStore, }); + + fastify.post("/api/createwaterlevelSensor", { schema: { description: "This is for creating waterlevel Sensor", tags: ["Store-Data"], summary: "This is for creating waterlevel Sensor", + body: { type: "object", properties: { + storeId: { type: "string" }, hardwareId: { type: "string" }, type: { type: "string" }, indate: { type: "string" }, @@ -153,5 +144,38 @@ fastify.post("/api/createwaterlevelSensor", { handler: storeController.createwaterlevelSensor, }) +fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", { + schema: { + description: "This is for checking waterlevel Sensor", + tags: ["Store-Data"], + summary: "This is for checking waterlevel Sensor", + params: { + required: ["hardwareId"], + type: "object", + properties: { + hardwareId: { + type: "string", + description: "hardwareId", + }, + }, + }, + body: { + type: "object", + + properties: { + qccheck: { type: "string" }, + qccheckdate: { type: "string" }, + qcby: { type: "string" }, + comment: { type: "string" }, + outforrepairdate: { type: "string" }, + sendto: { type: "string" }, + repairfeedback: { type: "string" }, + }, + }, + }, + handler: storeController.qccheckwaterlevelSensor, +}) + + next(); };