diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 3c24e41f..a67e88a7 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -377,16 +377,16 @@ exports.addStore = async (request, reply) => { 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, @@ -409,7 +409,7 @@ exports.addStore = async (request, reply) => { updatedBy, }); - // Save the new install to the database + await store.save(); reply.send({ message: 'Install Account Created Successfully' }); @@ -533,7 +533,7 @@ exports.editSalesUser = async (request, reply) => { profile, } = request.body; - // Check if the Sales user exists + const existingSales = await Sales.findOne({ salesId }); if (!existingSales) { return reply.status(404).send({ message: 'Sales user not found' }); @@ -544,12 +544,12 @@ exports.editSalesUser = async (request, reply) => { return reply.status(400).send({ message: 'Phone is already registered to another user' }); } - // Update the sales user's details + existingSales.username = username || existingSales.username; existingSales.phone = phone || existingSales.phone; existingSales.emails = emails || existingSales.emails; - // Update the profile fields (firstName, lastName) + if (profile) { existingSales.profile.firstName = profile.firstName || existingSales.profile.firstName; existingSales.profile.lastName = profile.lastName || existingSales.profile.lastName; @@ -557,13 +557,13 @@ exports.editSalesUser = async (request, reply) => { } - // Optionally, you can update the password as well (but hash it before saving) + if (request.body.password) { const hashedPassword = await bcrypt.hash(request.body.password, 10); existingSales.services.password.bcrypt = hashedPassword; } - // Save the updated user + await existingSales.save(); reply.send({ message: 'Sales user updated successfully' }); @@ -581,7 +581,7 @@ exports.editStore = async (request, reply) => { address1, address2, emails, - profile, // Profile fields + profile, alternativeNumber, longitude, latitude, @@ -589,29 +589,29 @@ exports.editStore = async (request, reply) => { firstName, lastName, city, - password, // Optional password field + password, } = request.body; - // Check if the store exists + const existingStore = await Store.findOne({ store_id: storeId }); if (!existingStore) { return reply.status(404).send({ message: 'Store not found' }); } - // Check if the phone number is already registered for another store + const phoneExists = await Store.findOne({ phone, store_id: { $ne: storeId } }); if (phoneExists) { return reply.status(400).send({ message: 'Phone is already registered to another store' }); } - // Update the store details + existingStore.phone = phone || existingStore.phone; existingStore.address = address || existingStore.address; existingStore.address1 = address1 || existingStore.address1; existingStore.address2 = address2 || existingStore.address2; existingStore.emails = emails || existingStore.emails; - // Update the profile fields + if (profile) { existingStore.profile.firstName = profile.firstName || existingStore.profile.firstName; existingStore.profile.lastName = profile.lastName || existingStore.profile.lastName; @@ -638,6 +638,44 @@ exports.editStore = async (request, reply) => { } }; + + + + exports.getAllUsers = async (request, reply) => { + try { + const users = await Install.find({}); + reply.send(users); + } catch (error) { + reply.status(500).send({ message: "Error retrieving users" }); + } + }, + + + exports.deleteUserByInstallationId = async (request, reply) => { + const { installationId } = request.params; + try { + await Install.findOneAndDelete({ installationId }); + reply.send({ message: "User deleted successfully" }); + } catch (error) { + reply.status(500).send({ message: "Error deleting user" }); + } + }, + + + exports.updateUserByInstallationId = async (request, reply) => { + const { installationId } = request.params; + const updatedData = request.body; + try { + await Install.findOneAndUpdate({ installationId }, updatedData, { new: true }); + reply.send({ message: "User updated successfully" }); + } catch (error) { + reply.status(500).send({ message: "Error updating user" }); + } + } + + + + // exports.addStore = async (req, reply) => { // try { // const { storename, password, phone, emails } = req.body; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 20a1f28a..dbc36191 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -2,7 +2,7 @@ const fastify = require("fastify"); const storeController = require('../controllers/storeController') const customJwtAuth = require('../customAuthJwt'); const fastifyJwt = require('fastify-jwt'); - +const userController = require('../controllers/userController'); module.exports = function (fastify, opts, next) { @@ -308,6 +308,125 @@ fastify.put('/api/editStore/:storeId', { handler: storeController.editStore, }); + + +fastify.get("/api/getAllInstallers", { + schema: { + description: "Retrieve all users", + tags: ["Install Management"], + summary: "Get all users", + // response: { + // 200: { + // type: "array", + // items: { + // type: "object", + // properties: { + // installationId: { type: "string" }, + // phone: { type: "string" }, + // password: { type: "string" }, + // emails: { type: "array", items: { type: "object", properties: { email: { type: "string" } } } }, + // team: { type: "string" }, + // manager: { type: "string" }, + // address: { type: "string" }, + // address1: { type: "string" }, + // address2: { type: "string" }, + // city: { type: "string" }, + // state: { type: "string" }, + // zip: { type: "string" }, + // country: { type: "string" }, + // notes: { type: "string" }, + // latitude: { type: "number" }, + // longitude: { type: "number" }, + // fcmId: { type: "string" }, + // alternativeNumber: { type: "string" }, + // firstName: { type: "string" }, + // lastName: { type: "string" }, + // }, + // }, + // }, + // }, + }, + handler: storeController.getAllUsers, +}); + + +fastify.delete("/api/users/:installationId", { + schema: { + description: "Delete a user by installationId", + tags: ["Install Management"], + summary: "Delete user", + params: { + type: "object", + properties: { + installationId: { type: "string" }, + }, + required: ["installationId"], + }, + response: { + 200: { + type: "object", + properties: { + message: { type: "string" }, + }, + }, + }, + }, + handler: storeController.deleteUserByInstallationId, +}); + + +fastify.put("/api/users/:installationId", { + schema: { + description: "Update user details by installationId", + tags: ["Install Management"], + summary: "Update user", + params: { + type: "object", + properties: { + installationId: { type: "string" }, + }, + required: ["installationId"], + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + password: { type: "string" }, + emails: { type: "array", items: { type: "object", properties: { email: { type: "string" } } } }, + team: { type: "string" }, + manager: { type: "string" }, + address: { type: "string" }, + address1: { type: "string" }, + address2: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + zip: { type: "string" }, + country: { type: "string" }, + notes: { type: "string" }, + latitude: { type: "number" }, + longitude: { type: "number" }, + fcmId: { type: "string" }, + alternativeNumber: { type: "string" }, + firstName: { type: "string" }, + lastName: { type: "string" }, + }, + }, + response: { + 200: { + type: "object", + properties: { + message: { type: "string" }, + }, + }, + }, + }, + handler: storeController.updateUserByInstallationId, +}); + + + + + fastify.get("/api/getusersofParticularInstaller", { schema: { tags: ["Install"],