From f4e5b86e199310d6e7b6de58c2014a0dd94ee82c Mon Sep 17 00:00:00 2001 From: Naidu Date: Thu, 7 Nov 2024 03:27:50 -0800 Subject: [PATCH] Sales&store latest update --- src/controllers/storeController.js | 230 ++++++++++++++++++++++++++++- src/models/store.js | 60 +++++++- src/routes/storeRoute.js | 187 +++++++++++++++++++++++ 3 files changed, 473 insertions(+), 4 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 5465460f..3c24e41f 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -10,7 +10,7 @@ const fastify = require("fastify")({ return uuidv4(); }, }); -const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart} = require("../models/store"); +const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') @@ -330,7 +330,14 @@ exports.installSignUp = async (request, reply) => { return result.seq; }; const saltRounds = 10; - + const generateSalesId = async () => { + const result = await Counter.findOneAndUpdate( + { _id: 'sales_id' }, + { $inc: { seq: 1 } }, + { upsert: true, new: true } + ); + return result.seq; + }; async function bcryptPassword(password) { encryptedPwd = bcrypt.hash(password, saltRounds); @@ -411,6 +418,225 @@ exports.addStore = async (request, reply) => { } } +exports.addSales = async (request, reply) => { + try { + const s_id = await generateSalesId(); + const sales_id = `AWSL${s_id}`; + + const { + phone, + username, + emails, + password, + profile, + createdBy, + updatedBy, + } = request.body; + + const { firstName, lastName ,address} = profile || {}; + + const existingStore = await Sales.findOne({ phone }); + if (existingStore) { + return reply.status(400).send({ message: 'Phone is already registered' }); + } + + const hashedPassword = await bcrypt.hash(password, 10); + + const store = new Sales({ + salesId: sales_id, + username, + phone, + address, + emails, + services: { password: { bcrypt: hashedPassword } }, + profile: { + firstName, + lastName, + address, + ...profile, + }, + createdBy, + updatedBy, + }); + + await store.save(); + + reply.send({ message: 'Account Created Successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + +exports.getallsales = async (req, reply) => { + try { + await Sales.find() + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.getallstore = async (req, reply) => { + try { + await Store.find() + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.deleteUserInfo = async (req, reply) => { + try { + const salesId = req.params.salesId; + + const sales = await Sales.findOneAndDelete({ salesId:salesId }); + + reply.send({ status_code: 200, message: 'Delete Sucessfully'}); + } catch (err) { + throw boom.boomify(err); + } +}; +exports.deleteStoreInfo = async (req, reply) => { + try { + const storeId = req.params.storeId; + + const store = await Store.findOneAndDelete({ storeId:storeId }); + + reply.send({ status_code: 200, message: 'Delete Sucessfully'}); + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.editSalesUser = async (request, reply) => { + try { + const { salesId } = request.params; + const { + username, + phone, + emails, + address, + 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' }); + } + + const phoneExists = await Sales.findOne({ phone, salesId: { $ne: salesId } }); + if (phoneExists) { + 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; + existingSales.profile.address = profile.address || existingSales.profile.address; + + } + + // 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' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + +exports.editStore = async (request, reply) => { + try { + const { storeId } = request.params; + const { + phone, + address, + address1, + address2, + emails, + profile, // Profile fields + alternativeNumber, + longitude, + latitude, + fcmId, + firstName, + lastName, + city, + password, // Optional password field + } = 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; + existingStore.profile.city = profile.city || existingStore.profile.city; + } + + existingStore.alternativeNumber = alternativeNumber || existingStore.alternativeNumber; + existingStore.longitude = longitude || existingStore.longitude; + existingStore.latitude = latitude || existingStore.latitude; + existingStore.fcmId = fcmId || existingStore.fcmId; + + // Update password if provided + if (password) { + const hashedPassword = await bcrypt.hash(password, 10); + existingStore.services.password.bcrypt = hashedPassword; + } + + // Save the updated store information + await existingStore.save(); + + reply.send({ message: 'Store updated successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; // exports.addStore = async (req, reply) => { // try { diff --git a/src/models/store.js b/src/models/store.js index 838e2387..60db65ca 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -115,6 +115,8 @@ const installationschema = new mongoose.Schema({ const storeSchema = new mongoose.Schema({ storename: { type: String }, phone: { type: String, unique: true, trim: true }, + contactPersonName: {type: String}, + contactPersonPhone: { type: String, unique: true, trim: true }, storeId: { type: String, default: null }, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, @@ -134,7 +136,7 @@ const installationschema = new mongoose.Schema({ lastName: { type: String, default: null }, contactNumber: { type: String, default: null }, alternativeContactNumber: { type: String, default: null }, - office_address: { type: String, default: null }, + store_address: { type: String, default: null }, city: { type: String, default: null }, state: { type: String, default: null }, country: { type: String, default: null }, @@ -322,6 +324,59 @@ const serviceCartSchema = new mongoose.Schema({ timestamps: true, }); +const salesSchema = new mongoose.Schema({ + username: { type: String }, + phone: { type: String, unique: true, trim: true }, + salesId: { type: String, default: null }, + phoneVerified: { type: Boolean, default: false }, + phoneVerificationCode: { type: Number, default: 11111 }, + passwordResetCode: { type: Number, default: 11111 }, + oneTimePasswordSetFlag: { type: Boolean, default: false }, + emails: {type: String}, + services: { + password: { + bcrypt: { type: String, required: true } + } + }, + description: { type: String, default: null }, + profile: { + role: [{ type: String, default: "sales" }], + firstName: { type: String, default: null }, + lastName: { type: String, default: null }, + contactNumber: { type: String, default: null }, + alternativeContactNumber: { type: String, default: null }, + address: { type: String, default: null }, + city: { type: String, default: null }, + state: { type: String, default: null }, + country: { type: String, default: null }, + zip: { type: String, default: null }, + }, + status: { + type: String, + enum: ['inactive', 'active'], + default: 'active' + }, + longitude: { type: Number, default: 0.0 }, + latitude: { type: Number, default: 0.0 }, + isActive: Boolean, + tenantId: ObjectId, + fcmId: { type: String, default: null }, + createdAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + createdBy: ObjectId, + updatedAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + updatedBy: ObjectId, +}, { versionKey: false }); + const Insensors = mongoose.model('Insensors', insensorsSchema); const Store = mongoose.model("Store", storeSchema); const WaterLeverSensor = mongoose.model('WaterLeverSensor', waterLeverSensorInSchema); @@ -333,6 +388,7 @@ const serviceCartSchema = new mongoose.Schema({ const Install = mongoose.model("Install", installationschema); const HardwareCart = mongoose.model("HardwareCart", hardwareCartSchema); const ServiceCart = mongoose.model("ServiceCart", serviceCartSchema); + const Sales = mongoose.model("Sales", salesSchema); - module.exports = { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; + module.exports = {Sales, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index b48cc9b1..20a1f28a 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -121,6 +121,193 @@ fastify.post('/api/stores', { handler: storeController.addStore, }); +fastify.post('/api/salesSignUp', { + schema: { + description: "This is for Create New Sales", + tags: ["Sales-Data"], + summary: "This is for Create New Sales.", + body: { + type: "object", + properties: { + username: { type: "string" }, + phone: { type: "string" }, + password: { type: "string" }, + emails: { type: "string" }, + address: { type: "string", default: null }, + profile: { + type: "object", + properties: { + firstName: { type: "string" }, + lastName: { type: "string" }, + address: { type: "string" }, + }, + required: ["firstName", "lastName"] + } + } + }, + security: [{ basicAuth: [] }], + }, + handler: storeController.addSales, +}); + +fastify.get("/api/getallsales", { + schema: { + tags: ["Sales-Data"], + description: "This is for Get all Sales Person Data", + summary: "This is for to Get all Sales Person Data", + + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: storeController.getallsales, +}); + +fastify.get("/api/getallstore", { + schema: { + tags: ["Sales-Data"], + description: "This is for Get all Store Person Data", + summary: "This is for to Get all Store Person Data", + + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: storeController.getallstore, +}); + +fastify.delete("/api/deletesale/:salesId", { + schema: { + description: "Delete a Sales by salesId", + tags: ["Sales-Data"], + summary: "Delete a user by salesId", + params: { + type: "object", + properties: { + salesId: { type: "string" }, // Customer ID + }, + required: ["salesId"], + }, + response: { + 200: { + type: "object", + properties: { + success: { type: "boolean" }, + message: { type: "string" }, + } + } + } + }, + handler: storeController.deleteUserInfo, +}); + +fastify.delete("/api/deleteStore/:storeId", { + schema: { + description: "Delete a Store by storeId", + tags: ["Sales-Data"], + summary: "Delete a Store by storeId", + params: { + type: "object", + properties: { + storeId: { type: "string" }, // Customer ID + }, + required: ["storeId"], + }, + response: { + 200: { + type: "object", + properties: { + success: { type: "boolean" }, + message: { type: "string" }, + } + } + } + }, + handler: storeController.deleteStoreInfo, +}); + +fastify.put('/api/editSalesUser/:salesId', { + schema: { + description: "Edit Sales user details by Sales ID", + tags: ["Sales-Data"], + summary: "Edit sales user details.", + params: { + type: "object", + properties: { + salesId: { type: "string" }, + }, + required: ["salesId"], + }, + body: { + type: "object", + properties: { + username: { type: "string" }, + phone: { type: "string" }, + password: { type: "string" }, + emails: { type: "string" }, + address: { type: "string" }, + profile: { + type: "object", + properties: { + firstName: { type: "string" }, + lastName: { type: "string" }, + address: { type: "string" } + + }, + } + }, + //required: ["username", "phone", "emails", "profile"] + } + }, + handler: storeController.editSalesUser, +}); + +fastify.put('/api/editStore/:storeId', { + schema: { + description: "Edit store user details by Store ID", + tags: ["Sales-Data"], + summary: "Edit store user details.", + params: { + type: "object", + properties: { + storeId: { type: "string" }, // Store ID + }, + required: ["storeId"], + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + address: { type: "string" }, + address1: { type: "string" }, + address2: { type: "string" }, + emails: { type: "string" }, + profile: { + type: "object", + properties: { + firstName: { type: "string" }, + lastName: { type: "string" }, + city: { type: "string" }, + }, + }, + alternativeNumber: { type: "string" }, + longitude: { type: "number" }, + latitude: { type: "number" }, + fcmId: { type: "string" }, + password: { type: "string" }, // Optional password update + }, + // required: ["phone", "emails", "profile"] + } + }, + handler: storeController.editStore, +}); + fastify.get("/api/getusersofParticularInstaller", { schema: { tags: ["Install"],