Varun 10 months ago
commit 9acb43bd86

@ -353,70 +353,93 @@ async function bcryptComparePassword(pwd, encpassword) {
exports.addStore = async (request, reply) => { exports.addStore = async (request, reply) => {
try { try {
// Generate a unique store ID
const s_id = await generateStoreId(); const s_id = await generateStoreId();
const store_id = `AWIN${s_id}`; const storeId = `AWST${s_id}`;
const { const {
// name, storename,
phone, phone,
address, contactPersonName,
address1, contactPersonPhone,
address2,
emails, emails,
password, password,
profile, description,
startingPrice,
longitude, longitude,
latitude, latitude,
fcmId, fcmId,
alternativeNumber, isActive,
firstName,
lastName,
city,
createdBy, createdBy,
updatedBy, updatedBy,
profile = {}, // Default to an empty object to prevent errors
} = request.body; } = 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 }); const existingStore = await Store.findOne({ phone });
if (existingStore) { if (existingStore) {
return reply.status(400).send({ message: 'Phone is already registered' }); 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); const hashedPassword = await bcrypt.hash(password, 10);
// Create a new store document
const store = new Store({ const store = new Store({
// name, storename,
store_id,
phone, phone,
address, contactPersonName,
address1, contactPersonPhone,
address2, storeId,
emails, emails,
services: { password: { bcrypt: hashedPassword } }, services: { password: { bcrypt: hashedPassword } },
profile, description,
startingPrice,
profile: {
store_address,
firstName,
lastName,
contactNumber,
alternativeContactNumber,
city,
state,
zip,
country,
},
longitude, longitude,
latitude, latitude,
fcmId, fcmId,
alternativeNumber, isActive: isActive ?? true, // Default to true if not provided
firstName,
lastName,
city,
createdBy, createdBy,
updatedBy, updatedBy,
}); });
// Save the store document
await store.save(); await store.save();
reply.send({ message: 'Install Account Created Successfully' }); reply.send({ message: 'Store Account Created Successfully' });
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
} }
} };
exports.addSales = async (request, reply) => { exports.addSales = async (request, reply) => {
try { try {
@ -576,6 +599,7 @@ exports.editStore = async (request, reply) => {
try { try {
const { storeId } = request.params; const { storeId } = request.params;
const { const {
storename,
phone, phone,
address, address,
address1, address1,
@ -586,50 +610,57 @@ exports.editStore = async (request, reply) => {
longitude, longitude,
latitude, latitude,
fcmId, fcmId,
firstName, description,
lastName, startingPrice,
city,
password, password,
} = request.body; } = request.body;
const existingStore = await Store.findOne({ store_id: storeId }); const existingStore = await Store.findOne({ storeId: storeId });
if (!existingStore) { if (!existingStore) {
return reply.status(404).send({ message: 'Store not found' }); 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) { if (phoneExists) {
return reply.status(400).send({ message: 'Phone is already registered to another store' }); return reply.status(400).send({ message: 'Phone is already registered to another store' });
} }
existingStore.storename = storename || existingStore.storename;
existingStore.phone = phone || existingStore.phone; existingStore.phone = phone || existingStore.phone;
existingStore.address = address || existingStore.address; existingStore.address = address || existingStore.address;
existingStore.address1 = address1 || existingStore.address1; existingStore.address1 = address1 || existingStore.address1;
existingStore.address2 = address2 || existingStore.address2; existingStore.address2 = address2 || existingStore.address2;
existingStore.emails = emails || existingStore.emails; 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) { if (profile) {
existingStore.profile.firstName = profile.firstName || existingStore.profile.firstName; existingStore.profile.firstName = profile.firstName || existingStore.profile.firstName;
existingStore.profile.lastName = profile.lastName || existingStore.profile.lastName; 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.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) { if (password) {
const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10);
existingStore.services.password.bcrypt = hashedPassword; existingStore.services.password.bcrypt = hashedPassword;
} }
// Save the updated store information
await existingStore.save(); await existingStore.save();
reply.send({ message: 'Store updated successfully' }); reply.send({ message: 'Store updated successfully' });
@ -641,6 +672,7 @@ exports.editStore = async (request, reply) => {
exports.getAllUsers = async (request, reply) => { exports.getAllUsers = async (request, reply) => {
try { try {
const users = await Install.find({}); const users = await Install.find({});

@ -93,27 +93,39 @@ module.exports = function (fastify, opts, next) {
fastify.post('/api/stores', { fastify.post('/api/stores', {
schema: { schema: {
description: "This is for Create New Store", description: "Create a new store account",
tags: ["Store-Data"], tags: ["Store-Data"],
summary: "This is for Create New Store.", summary: "create a new store account.",
body: { body: {
type: "object", type: "object",
required: ["storename", "phone", "password"], // required: ["storename", "phone", "password"],
properties: { properties: {
storename: { type: "string" }, storename: { type: "string" },
phone: { type: "string" }, password: {type: "string"},
alternativeContactNumber: { type: "string" }, phone: { type: "string", unique: true, trim: true },
password: { type: "string" }, contactPersonName: { type: "string" },
contactPersonPhone: { type: "string", unique: true, trim: true },
emails: { type: "string" }, 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 }, 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: [] }], security: [{ basicAuth: [] }],
@ -121,6 +133,7 @@ fastify.post('/api/stores', {
handler: storeController.addStore, handler: storeController.addStore,
}); });
fastify.post('/api/salesSignUp', { fastify.post('/api/salesSignUp', {
schema: { schema: {
description: "This is for Create New Sales", description: "This is for Create New Sales",
@ -262,7 +275,7 @@ fastify.put('/api/editSalesUser/:salesId', {
}, },
} }
}, },
//required: ["username", "phone", "emails", "profile"]
} }
}, },
handler: storeController.editSalesUser, handler: storeController.editSalesUser,
@ -270,19 +283,20 @@ fastify.put('/api/editSalesUser/:salesId', {
fastify.put('/api/editStore/:storeId', { fastify.put('/api/editStore/:storeId', {
schema: { schema: {
description: "Edit store user details by Store ID", description: "Edit store details by Store ID",
tags: ["Sales-Data"], tags: ["Store-Data"],
summary: "Edit store user details.", summary: "Edit store details.",
params: { params: {
type: "object", type: "object",
properties: { properties: {
storeId: { type: "string" }, // Store ID storeId: { type: "string" },
}, },
required: ["storeId"], required: ["storeId"],
}, },
body: { body: {
type: "object", type: "object",
properties: { properties: {
storename: { type: "string" },
phone: { type: "string" }, phone: { type: "string" },
address: { type: "string" }, address: { type: "string" },
address1: { type: "string" }, address1: { type: "string" },
@ -293,16 +307,24 @@ fastify.put('/api/editStore/:storeId', {
properties: { properties: {
firstName: { type: "string" }, firstName: { type: "string" },
lastName: { type: "string" }, lastName: { type: "string" },
contactNumber: { type: "string" },
alternativeContactNumber: { type: "string" },
store_address: { type: "string" },
city: { type: "string" }, city: { type: "string" },
state: { type: "string" },
country: { type: "string" },
zip: { type: "string" },
}, },
}, },
alternativeNumber: { type: "string" }, alternativeNumber: { type: "string" },
longitude: { type: "number" }, longitude: { type: "number" },
latitude: { type: "number" }, latitude: { type: "number" },
fcmId: { type: "string" }, 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, handler: storeController.editStore,
@ -310,6 +332,7 @@ fastify.put('/api/editStore/:storeId', {
fastify.get("/api/getAllInstallers", { fastify.get("/api/getAllInstallers", {
schema: { schema: {
description: "Retrieve all users", description: "Retrieve all users",
@ -350,7 +373,7 @@ fastify.get("/api/getAllInstallers", {
}); });
fastify.delete("/api/users/:installationId", { fastify.delete("/api/deleteInstaller/:installationId", {
schema: { schema: {
description: "Delete a user by installationId", description: "Delete a user by installationId",
tags: ["Install Management"], tags: ["Install Management"],
@ -375,7 +398,7 @@ fastify.delete("/api/users/:installationId", {
}); });
fastify.put("/api/users/:installationId", { fastify.put("/api/updaeInstaller/:installationId", {
schema: { schema: {
description: "Update user details by installationId", description: "Update user details by installationId",
tags: ["Install Management"], tags: ["Install Management"],

Loading…
Cancel
Save