|
|
@ -377,16 +377,16 @@ exports.addStore = async (request, reply) => {
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if a user with the same phone number already exists
|
|
|
|
|
|
|
|
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' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hash the password using bcrypt
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new install object with the hashed password and other details
|
|
|
|
|
|
|
|
const store = new Store({
|
|
|
|
const store = new Store({
|
|
|
|
// name,
|
|
|
|
// name,
|
|
|
|
store_id,
|
|
|
|
store_id,
|
|
|
@ -409,7 +409,7 @@ exports.addStore = async (request, reply) => {
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Save the new install to the database
|
|
|
|
|
|
|
|
await store.save();
|
|
|
|
await store.save();
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Install Account Created Successfully' });
|
|
|
|
reply.send({ message: 'Install Account Created Successfully' });
|
|
|
@ -533,7 +533,7 @@ exports.editSalesUser = async (request, reply) => {
|
|
|
|
profile,
|
|
|
|
profile,
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the Sales user exists
|
|
|
|
|
|
|
|
const existingSales = await Sales.findOne({ salesId });
|
|
|
|
const existingSales = await Sales.findOne({ salesId });
|
|
|
|
if (!existingSales) {
|
|
|
|
if (!existingSales) {
|
|
|
|
return reply.status(404).send({ message: 'Sales user not found' });
|
|
|
|
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' });
|
|
|
|
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.username = username || existingSales.username;
|
|
|
|
existingSales.phone = phone || existingSales.phone;
|
|
|
|
existingSales.phone = phone || existingSales.phone;
|
|
|
|
existingSales.emails = emails || existingSales.emails;
|
|
|
|
existingSales.emails = emails || existingSales.emails;
|
|
|
|
|
|
|
|
|
|
|
|
// Update the profile fields (firstName, lastName)
|
|
|
|
|
|
|
|
if (profile) {
|
|
|
|
if (profile) {
|
|
|
|
existingSales.profile.firstName = profile.firstName || existingSales.profile.firstName;
|
|
|
|
existingSales.profile.firstName = profile.firstName || existingSales.profile.firstName;
|
|
|
|
existingSales.profile.lastName = profile.lastName || existingSales.profile.lastName;
|
|
|
|
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) {
|
|
|
|
if (request.body.password) {
|
|
|
|
const hashedPassword = await bcrypt.hash(request.body.password, 10);
|
|
|
|
const hashedPassword = await bcrypt.hash(request.body.password, 10);
|
|
|
|
existingSales.services.password.bcrypt = hashedPassword;
|
|
|
|
existingSales.services.password.bcrypt = hashedPassword;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Save the updated user
|
|
|
|
|
|
|
|
await existingSales.save();
|
|
|
|
await existingSales.save();
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Sales user updated successfully' });
|
|
|
|
reply.send({ message: 'Sales user updated successfully' });
|
|
|
@ -581,7 +581,7 @@ exports.editStore = async (request, reply) => {
|
|
|
|
address1,
|
|
|
|
address1,
|
|
|
|
address2,
|
|
|
|
address2,
|
|
|
|
emails,
|
|
|
|
emails,
|
|
|
|
profile, // Profile fields
|
|
|
|
profile,
|
|
|
|
alternativeNumber,
|
|
|
|
alternativeNumber,
|
|
|
|
longitude,
|
|
|
|
longitude,
|
|
|
|
latitude,
|
|
|
|
latitude,
|
|
|
@ -589,29 +589,29 @@ exports.editStore = async (request, reply) => {
|
|
|
|
firstName,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
lastName,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
password, // Optional password field
|
|
|
|
password,
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the store exists
|
|
|
|
|
|
|
|
const existingStore = await Store.findOne({ store_id: storeId });
|
|
|
|
const existingStore = await Store.findOne({ store_id: storeId });
|
|
|
|
if (!existingStore) {
|
|
|
|
if (!existingStore) {
|
|
|
|
return reply.status(404).send({ message: 'Store not found' });
|
|
|
|
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 } });
|
|
|
|
const phoneExists = await Store.findOne({ phone, store_id: { $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' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update the store details
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
// Update the profile fields
|
|
|
|
|
|
|
|
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;
|
|
|
@ -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) => {
|
|
|
|
// exports.addStore = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const { storename, password, phone, emails } = req.body;
|
|
|
|
// const { storename, password, phone, emails } = req.body;
|
|
|
|