From e1bab81d1794b10d94ec3d7a43e3ac7c184a2f66 Mon Sep 17 00:00:00 2001 From: Naidu Date: Wed, 27 Nov 2024 21:58:12 -0800 Subject: [PATCH] store schema changes & edit also --- src/controllers/storeController.js | 122 ++++++++++++++++++----------- src/routes/storeRoute.js | 65 ++++++++++----- 2 files changed, 121 insertions(+), 66 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index a67e88a7..5b388d63 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -353,70 +353,93 @@ async function bcryptComparePassword(pwd, encpassword) { exports.addStore = async (request, reply) => { try { + // Generate a unique store ID const s_id = await generateStoreId(); - const store_id = `AWIN${s_id}`; + const storeId = `AWST${s_id}`; const { - // name, + storename, phone, - address, - address1, - address2, + contactPersonName, + contactPersonPhone, emails, password, - profile, - + description, + startingPrice, longitude, latitude, fcmId, - alternativeNumber, - firstName, - lastName, - city, + isActive, createdBy, updatedBy, + profile = {}, // Default to an empty object to prevent errors } = request.body; - + const { + firstName = null, + lastName = null, + contactNumber = null, + alternativeContactNumber = null, + store_address = null, + city = null, + state = null, + zip = null, + country = null, + } = profile; // Extract profile fields + + // Check if the phone is already registered const existingStore = await Store.findOne({ phone }); if (existingStore) { return reply.status(400).send({ message: 'Phone is already registered' }); } - + // Check if the contact person phone is already registered + const existingContactPhone = await Store.findOne({ contactPersonPhone }); + if (existingContactPhone) { + return reply.status(400).send({ message: 'Contact Person Phone is already registered' }); + } + + // Hash the password const hashedPassword = await bcrypt.hash(password, 10); - + // Create a new store document const store = new Store({ - // name, - store_id, + storename, phone, - address, - address1, - address2, + contactPersonName, + contactPersonPhone, + storeId, emails, services: { password: { bcrypt: hashedPassword } }, - profile, - + description, + startingPrice, + profile: { + store_address, + firstName, + lastName, + contactNumber, + alternativeContactNumber, + city, + state, + zip, + country, + }, longitude, latitude, fcmId, - alternativeNumber, - firstName, - lastName, - city, + isActive: isActive ?? true, // Default to true if not provided createdBy, updatedBy, }); - + // Save the store document await store.save(); - reply.send({ message: 'Install Account Created Successfully' }); + reply.send({ message: 'Store Account Created Successfully' }); } catch (err) { reply.status(500).send({ message: err.message }); } -} +}; exports.addSales = async (request, reply) => { try { @@ -576,60 +599,68 @@ exports.editStore = async (request, reply) => { try { const { storeId } = request.params; const { + storename, phone, address, address1, address2, emails, - profile, + profile, alternativeNumber, longitude, latitude, fcmId, - firstName, - lastName, - city, - password, + description, + startingPrice, + password, } = request.body; - - const existingStore = await Store.findOne({ store_id: storeId }); + + const existingStore = await Store.findOne({ storeId: storeId }); if (!existingStore) { return reply.status(404).send({ message: 'Store not found' }); } - - const phoneExists = await Store.findOne({ phone, store_id: { $ne: storeId } }); + + const phoneExists = await Store.findOne({ phone, storeId: { $ne: storeId } }); if (phoneExists) { return reply.status(400).send({ message: 'Phone is already registered to another store' }); } - + existingStore.storename = storename || existingStore.storename; 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; + existingStore.alternativeNumber = alternativeNumber || existingStore.alternativeNumber; + existingStore.longitude = longitude || existingStore.longitude; + existingStore.latitude = latitude || existingStore.latitude; + existingStore.fcmId = fcmId || existingStore.fcmId; + existingStore.description = description || existingStore.description; + existingStore.startingPrice = startingPrice || existingStore.startingPrice; if (profile) { existingStore.profile.firstName = profile.firstName || existingStore.profile.firstName; existingStore.profile.lastName = profile.lastName || existingStore.profile.lastName; + existingStore.profile.contactNumber = profile.contactNumber || existingStore.profile.contactNumber; + existingStore.profile.alternativeContactNumber = + profile.alternativeContactNumber || existingStore.profile.alternativeContactNumber; + existingStore.profile.store_address = profile.store_address || existingStore.profile.store_address; existingStore.profile.city = profile.city || existingStore.profile.city; + existingStore.profile.state = profile.state || existingStore.profile.state; + existingStore.profile.country = profile.country || existingStore.profile.country; + existingStore.profile.zip = profile.zip || existingStore.profile.zip; } - 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' }); @@ -640,6 +671,7 @@ exports.editStore = async (request, reply) => { + exports.getAllUsers = async (request, reply) => { try { diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index f51aad31..f2ac13b9 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -93,27 +93,39 @@ module.exports = function (fastify, opts, next) { fastify.post('/api/stores', { schema: { - description: "This is for Create New Store", + description: "Create a new store account", tags: ["Store-Data"], - summary: "This is for Create New Store.", + summary: "create a new store account.", body: { type: "object", - required: ["storename", "phone", "password"], + // required: ["storename", "phone", "password"], properties: { storename: { type: "string" }, - phone: { type: "string" }, - alternativeContactNumber: { type: "string" }, - password: { type: "string" }, + password: {type: "string"}, + phone: { type: "string", unique: true, trim: true }, + contactPersonName: { type: "string" }, + contactPersonPhone: { type: "string", unique: true, trim: true }, emails: { type: "string" }, - office_address: { type: "string", default: null }, - city: { type: "string", default: null }, - state: { type: "string", default: null }, - zip: { type: "string", default: null }, - country: { type: "string", default: null }, - latitude: { type: 'number', default: 0.0 }, - longitude: { type: 'number', default: 0.0 }, - fcmId: { type: "string", default: null }, description: { type: "string", default: null }, + startingPrice: { type: "string", default: 0.0 }, + profile: { + type: "object", + properties: { + role: { type: "array", items: { type: "string" }, default: ["store"] }, + firstName: { type: "string", default: null }, + lastName: { type: "string", default: null }, + contactNumber: { type: "string", default: null }, + alternativeContactNumber: { 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 }, + zip: { type: "string", default: null }, + }, + }, + longitude: { type: "number", default: 0.0 }, + latitude: { type: "number", default: 0.0 }, + fcmId: { type: "string", default: null }, }, }, security: [{ basicAuth: [] }], @@ -121,6 +133,7 @@ fastify.post('/api/stores', { handler: storeController.addStore, }); + fastify.post('/api/salesSignUp', { schema: { description: "This is for Create New Sales", @@ -262,7 +275,7 @@ fastify.put('/api/editSalesUser/:salesId', { }, } }, - //required: ["username", "phone", "emails", "profile"] + } }, handler: storeController.editSalesUser, @@ -270,19 +283,20 @@ fastify.put('/api/editSalesUser/:salesId', { fastify.put('/api/editStore/:storeId', { schema: { - description: "Edit store user details by Store ID", - tags: ["Sales-Data"], - summary: "Edit store user details.", + description: "Edit store details by Store ID", + tags: ["Store-Data"], + summary: "Edit store details.", params: { type: "object", properties: { - storeId: { type: "string" }, // Store ID + storeId: { type: "string" }, }, required: ["storeId"], }, body: { type: "object", properties: { + storename: { type: "string" }, phone: { type: "string" }, address: { type: "string" }, address1: { type: "string" }, @@ -293,16 +307,24 @@ fastify.put('/api/editStore/:storeId', { properties: { firstName: { type: "string" }, lastName: { type: "string" }, + contactNumber: { type: "string" }, + alternativeContactNumber: { type: "string" }, + store_address: { type: "string" }, city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + zip: { type: "string" }, }, }, alternativeNumber: { type: "string" }, longitude: { type: "number" }, latitude: { type: "number" }, fcmId: { type: "string" }, - password: { type: "string" }, // Optional password update + description: { type: "string" }, + startingPrice: { type: "string" }, + password: { type: "string" }, }, - // required: ["phone", "emails", "profile"] + required: ["phone"], } }, handler: storeController.editStore, @@ -310,6 +332,7 @@ fastify.put('/api/editStore/:storeId', { + fastify.get("/api/getAllInstallers", { schema: { description: "Retrieve all users",