|
|
|
@ -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 {
|
|
|
|
|