Deleted useless files such as backend, node_modules and package-lock. Modified readme.
This commit is contained in:
@@ -1,157 +0,0 @@
|
||||
const Crop = require("../Models/crop.model.js");
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
const { uploadOnCloudinary } = require("../Utils/cloudinary.js");
|
||||
|
||||
// Create a new crop
|
||||
const createCrop = async (req, res) => {
|
||||
try {
|
||||
const { name, farm, harvestDate, growthStage, healthStatus } = req.body;
|
||||
if (!req.file.path) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Avatar not uploaded on cloudinary.",
|
||||
});
|
||||
}
|
||||
|
||||
const imageUrl = await uploadOnCloudinary(req.file.path);
|
||||
|
||||
console.log("Image url is : ", imageUrl);
|
||||
|
||||
if (!imageUrl) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Image not uploaded on cloudinary.",
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the farm exists
|
||||
const existingFarm = await Farm.findById(farm);
|
||||
if (!existingFarm)
|
||||
return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
const crop = new Crop({
|
||||
name,
|
||||
farm,
|
||||
image: imageUrl,
|
||||
harvestDate,
|
||||
growthStage,
|
||||
healthStatus,
|
||||
});
|
||||
|
||||
await crop.save();
|
||||
|
||||
// Add crop to farm
|
||||
existingFarm.crops.push(crop._id);
|
||||
await existingFarm.save();
|
||||
|
||||
res.status(201).json(crop);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get all crops for a specific farm
|
||||
const getCropsByFarm = async (req, res) => {
|
||||
try {
|
||||
console.log("My farm id is : ", req.params.farmId);
|
||||
const crops = await Crop.find({ farm: req.params.farmId });
|
||||
|
||||
res.status(200).json(crops);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get a single crop by ID
|
||||
const getCropById = async (req, res) => {
|
||||
try {
|
||||
const crop = await Crop.findById(req.params.cropId).populate("farm");
|
||||
|
||||
if (!crop) return res.status(404).json({ message: "Crop not found" });
|
||||
|
||||
res.status(200).json(crop);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update crop details
|
||||
const updateCrop = async (req, res) => {
|
||||
try {
|
||||
const updatedCrop = await Crop.findByIdAndUpdate(
|
||||
req.params.cropId,
|
||||
req.body,
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
if (!updatedCrop)
|
||||
return res.status(404).json({ message: "Crop not found" });
|
||||
|
||||
res.status(200).json(updatedCrop);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Delete a crop
|
||||
const deleteCrop = async (req, res) => {
|
||||
try {
|
||||
const crop = await Crop.findById(req.params.cropId);
|
||||
|
||||
if (!crop) return res.status(404).json({ message: "Crop not found" });
|
||||
|
||||
await crop.deleteOne();
|
||||
|
||||
// Remove crop from the farm
|
||||
await Farm.findByIdAndUpdate(crop.farm, { $pull: { crops: crop._id } });
|
||||
|
||||
res.status(200).json({ message: "Crop deleted successfully" });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update crop growth stage
|
||||
const updateGrowthStage = async (req, res) => {
|
||||
try {
|
||||
const { growthStage } = req.body;
|
||||
|
||||
const crop = await Crop.findById(req.params.cropId);
|
||||
if (!crop) return res.status(404).json({ message: "Crop not found" });
|
||||
|
||||
crop.growthStage = growthStage;
|
||||
await crop.save();
|
||||
|
||||
res.status(200).json({ message: "Growth stage updated", crop });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update crop health status
|
||||
const updateHealthStatus = async (req, res) => {
|
||||
try {
|
||||
const { healthStatus } = req.body;
|
||||
|
||||
const crop = await Crop.findById(req.params.cropId);
|
||||
if (!crop) return res.status(404).json({ message: "Crop not found" });
|
||||
|
||||
crop.healthStatus = healthStatus;
|
||||
await crop.save();
|
||||
|
||||
res.status(200).json({ message: "Health status updated", crop });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
createCrop,
|
||||
getCropsByFarm,
|
||||
getCropById,
|
||||
updateCrop,
|
||||
deleteCrop,
|
||||
updateGrowthStage,
|
||||
updateHealthStatus,
|
||||
};
|
||||
@@ -1,131 +0,0 @@
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
const Crop = require("../Models/crop.model.js");
|
||||
const Finance = require("../Models/finance.model.js");
|
||||
|
||||
// Create a farm
|
||||
const createFarm = async (req, res) => {
|
||||
try {
|
||||
const { name, location, waterContent, soilType, size } = req.body;
|
||||
|
||||
const farm = new Farm({
|
||||
name,
|
||||
location,
|
||||
size,
|
||||
waterContent,
|
||||
soilType,
|
||||
owner: req.user._id,
|
||||
});
|
||||
|
||||
await farm.save();
|
||||
res.status(201).json(farm);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get all farms for a user
|
||||
const getUserFarms = async (req, res) => {
|
||||
try {
|
||||
const farms = await Farm.find({ owner: req.user._id })
|
||||
.populate("crops")
|
||||
.populate("finances");
|
||||
|
||||
res.status(200).json(farms);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get a single farm by ID
|
||||
const getFarmById = async (req, res) => {
|
||||
try {
|
||||
const farm = await Farm.findById(req.params.farmId)
|
||||
.populate("crops")
|
||||
.populate("finances");
|
||||
|
||||
if (!farm) return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
res.status(200).json(farm);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update farm details
|
||||
const updateFarm = async (req, res) => {
|
||||
try {
|
||||
const updatedFarm = await Farm.findByIdAndUpdate(
|
||||
req.params.farmId,
|
||||
req.body,
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
if (!updatedFarm)
|
||||
return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
res.status(200).json(updatedFarm);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Delete a farm
|
||||
const deleteFarm = async (req, res) => {
|
||||
try {
|
||||
const farm = await Farm.findById(req.params.farmId);
|
||||
|
||||
if (!farm) return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
await Crop.deleteMany({ farm: farm._id });
|
||||
await Finance.findByIdAndDelete(farm.finances);
|
||||
await farm.deleteOne();
|
||||
|
||||
res.status(200).json({ message: "Farm deleted successfully" });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Add fertilizer to a farm
|
||||
const addFertilizer = async (req, res) => {
|
||||
try {
|
||||
const { name, quantity } = req.body;
|
||||
|
||||
const farm = await Farm.findById(req.params.farmId);
|
||||
if (!farm) return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
farm.fertilizer.push({ name, quantity });
|
||||
await farm.save();
|
||||
|
||||
res.status(200).json({ message: "Fertilizer added", farm });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Add pesticide to a farm
|
||||
const addPesticide = async (req, res) => {
|
||||
try {
|
||||
const { name, quantity } = req.body;
|
||||
|
||||
const farm = await Farm.findById(req.params.farmId);
|
||||
if (!farm) return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
farm.pestisides.push({ name, quantity });
|
||||
await farm.save();
|
||||
|
||||
res.status(200).json({ message: "Pesticide added", farm });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createFarm,
|
||||
getUserFarms,
|
||||
getFarmById,
|
||||
updateFarm,
|
||||
deleteFarm,
|
||||
addFertilizer,
|
||||
addPesticide,
|
||||
};
|
||||
@@ -1,136 +0,0 @@
|
||||
const Finance = require("../Models/finance.model.js");
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
|
||||
// Create finance record for a farm
|
||||
const createFinance = async (req, res) => {
|
||||
try {
|
||||
const { farm } = req.body;
|
||||
|
||||
// Check if the farm exists
|
||||
const existingFarm = await Farm.findById(farm);
|
||||
if (!existingFarm)
|
||||
return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
const finance = new Finance({
|
||||
farm,
|
||||
transactions: [],
|
||||
totalExpenses: 0,
|
||||
totalRevenue: 0,
|
||||
});
|
||||
|
||||
await finance.save();
|
||||
|
||||
// Link finance to farm
|
||||
existingFarm.finances = finance._id;
|
||||
await existingFarm.save();
|
||||
|
||||
res.status(201).json(finance);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get finance details by farm ID
|
||||
const getFinanceByFarm = async (req, res) => {
|
||||
try {
|
||||
console.log("My farm id is : ", req.params.farmId);
|
||||
const finance = await Finance.findOne({ farm: req.params.farmId });
|
||||
|
||||
if (!finance)
|
||||
return res.status(404).json({ message: "Finance record not found" });
|
||||
|
||||
res.status(200).json(finance);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Add a transaction (expense/revenue)
|
||||
const addTransaction = async (req, res) => {
|
||||
try {
|
||||
const { type, amount, description } = req.body;
|
||||
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
return res.status(404).json({ message: "Finance record not found" });
|
||||
|
||||
finance.transactions.push({ type, amount, description });
|
||||
|
||||
// Update totals
|
||||
if (type === "Expense") {
|
||||
finance.totalExpenses += amount;
|
||||
} else if (type === "Revenue") {
|
||||
finance.totalRevenue += amount;
|
||||
}
|
||||
|
||||
await finance.save();
|
||||
res.status(200).json({ message: "Transaction added", finance });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Delete a transaction
|
||||
const deleteTransaction = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
return res.status(404).json({ message: "Finance record not found" });
|
||||
|
||||
const transaction = finance.transactions.id(req.params.transactionId);
|
||||
if (!transaction)
|
||||
return res.status(404).json({ message: "Transaction not found" });
|
||||
|
||||
// Adjust totals before removing
|
||||
if (transaction.type === "Expense") {
|
||||
finance.totalExpenses -= transaction.amount;
|
||||
} else if (transaction.type === "Revenue") {
|
||||
finance.totalRevenue -= transaction.amount;
|
||||
}
|
||||
|
||||
transaction.remove();
|
||||
await finance.save();
|
||||
|
||||
res.status(200).json({ message: "Transaction deleted", finance });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get all transactions for a farm's finance
|
||||
const getTransactions = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
return res.status(404).json({ message: "Finance record not found" });
|
||||
|
||||
res.status(200).json(finance.transactions);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get total expenses and revenue
|
||||
const getFinancialSummary = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
return res.status(404).json({ message: "Finance record not found" });
|
||||
|
||||
res.status(200).json({
|
||||
totalExpenses: finance.totalExpenses,
|
||||
totalRevenue: finance.totalRevenue,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createFinance,
|
||||
getFinanceByFarm,
|
||||
addTransaction,
|
||||
deleteTransaction,
|
||||
getTransactions,
|
||||
getFinancialSummary,
|
||||
};
|
||||
@@ -1,120 +0,0 @@
|
||||
const Task = require("../Models/task.model.js");
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
const Crop = require("../Models/crop.model.js");
|
||||
|
||||
// Create a new task
|
||||
const createTask = async (req, res) => {
|
||||
try {
|
||||
const { farm, crop, taskType, description, assignedDate, status } =
|
||||
req.body;
|
||||
|
||||
// Validate farm existence
|
||||
const existingFarm = await Farm.findById(farm);
|
||||
if (!existingFarm)
|
||||
return res.status(404).json({ message: "Farm not found" });
|
||||
|
||||
// Validate crop if provided
|
||||
if (crop) {
|
||||
const existingCrop = await Crop.findById(crop);
|
||||
if (!existingCrop)
|
||||
return res.status(404).json({ message: "Crop not found" });
|
||||
}
|
||||
|
||||
const task = new Task({
|
||||
farm,
|
||||
crop,
|
||||
taskType,
|
||||
description,
|
||||
assignedDate,
|
||||
status,
|
||||
});
|
||||
|
||||
await task.save();
|
||||
res.status(201).json(task);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get all tasks for a specific farm
|
||||
const getTasksByFarm = async (req, res) => {
|
||||
try {
|
||||
const tasks = await Task.find({ farm: req.params.farmId }).populate("crop");
|
||||
|
||||
res.status(200).json(tasks);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get a single task by ID
|
||||
const getTaskById = async (req, res) => {
|
||||
try {
|
||||
const task = await Task.findById(req.params.taskId).populate("farm crop");
|
||||
|
||||
if (!task) return res.status(404).json({ message: "Task not found" });
|
||||
|
||||
res.status(200).json(task);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update task details
|
||||
const updateTask = async (req, res) => {
|
||||
try {
|
||||
const updatedTask = await Task.findByIdAndUpdate(
|
||||
req.params.taskId,
|
||||
req.body,
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
if (!updatedTask)
|
||||
return res.status(404).json({ message: "Task not found" });
|
||||
|
||||
res.status(200).json(updatedTask);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Delete a task
|
||||
const deleteTask = async (req, res) => {
|
||||
try {
|
||||
const task = await Task.findById(req.params.taskId);
|
||||
|
||||
if (!task) return res.status(404).json({ message: "Task not found" });
|
||||
|
||||
await task.deleteOne();
|
||||
|
||||
res.status(200).json({ message: "Task deleted successfully" });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Update task status (Pending → Completed)
|
||||
const updateTaskStatus = async (req, res) => {
|
||||
try {
|
||||
const { status } = req.body;
|
||||
|
||||
const task = await Task.findById(req.params.taskId);
|
||||
if (!task) return res.status(404).json({ message: "Task not found" });
|
||||
|
||||
task.status = status;
|
||||
await task.save();
|
||||
|
||||
res.status(200).json({ message: "Task status updated", task });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createTask,
|
||||
getTasksByFarm,
|
||||
getTaskById,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
updateTaskStatus,
|
||||
};
|
||||
@@ -1,466 +0,0 @@
|
||||
const catchAsyncErrors = require("../Middlewares/catchAsyncErrors.js");
|
||||
const User = require("../Models/user.model.js");
|
||||
const { uploadOnCloudinary } = require("../Utils/cloudinary.js");
|
||||
const sendEmail = require("../Utils/sendmail.js");
|
||||
const crypto = require("crypto");
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
// Register or Sign up new User -- Done
|
||||
const registerUser = catchAsyncErrors(async (req, res) => {
|
||||
const { name, email, password, role } = req.body;
|
||||
|
||||
const user = await User.create({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
role,
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "User not created something went wrong.",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User is registered successfully",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// Login user in our web app -- Done
|
||||
const loginUser = catchAsyncErrors(async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
|
||||
const user = await User.findOne({ email });
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
const checkUser = await user.isPasswordCorrect(password);
|
||||
|
||||
if (!checkUser) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Password is incorrect",
|
||||
});
|
||||
}
|
||||
|
||||
const token = await user.generateRefreshToken();
|
||||
|
||||
if (!token) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "token not created something went wrong.",
|
||||
});
|
||||
}
|
||||
|
||||
user.password = null;
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.cookie(process.env.TOKEN_NAME, token, {
|
||||
path: "/",
|
||||
sameSite: "None",
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
httpOnly: true,
|
||||
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
|
||||
})
|
||||
.json({
|
||||
success: true,
|
||||
message: "User is successfully logged in.",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// Logout user in our web app -- Done
|
||||
const logoutUser = catchAsyncErrors(async (req, res) => {
|
||||
return res
|
||||
.clearCookie(process.env.TOKEN_NAME, {
|
||||
path: "/",
|
||||
sameSite: "None",
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
httpOnly: true,
|
||||
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
|
||||
})
|
||||
.status(201)
|
||||
.json({
|
||||
success: true,
|
||||
message: "User is logged out successfully",
|
||||
});
|
||||
});
|
||||
|
||||
// -- DONE
|
||||
const intializeUser = catchAsyncErrors(async (req, res) => {
|
||||
const tokenValue = req.cookies[process.env.TOKEN_NAME];
|
||||
|
||||
// console.log("I am the one who is doing this : ", tokenValue);
|
||||
|
||||
if (!tokenValue) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User is not logged in.",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = await jwt.verify(
|
||||
tokenValue,
|
||||
process.env.REFRESH_TOKEN_SECRET
|
||||
);
|
||||
|
||||
if (!payload) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "Something went wrong",
|
||||
});
|
||||
}
|
||||
|
||||
const user = await User.findById(payload._id).select("-password");
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User data get successfully",
|
||||
data: user,
|
||||
});
|
||||
} catch (error) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "Something went wrong",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Update user deatails -- ADMIN
|
||||
const updateUserDetails = catchAsyncErrors(async (req, res) => {
|
||||
const user = await User.findById(req.params.id);
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: true,
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
const { name, email } = req.body;
|
||||
|
||||
const updateUser = await User.findByIdAndUpdate(req.params.id, {
|
||||
$set: {
|
||||
name: name,
|
||||
email: email,
|
||||
},
|
||||
}).select("-password");
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User is updated successfully",
|
||||
data: updateUser,
|
||||
});
|
||||
});
|
||||
|
||||
// forget password -- Done
|
||||
const forgetPassword = catchAsyncErrors(async (req, res) => {
|
||||
const { email } = req.body;
|
||||
const user = await User.findOne({ email });
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User not found ",
|
||||
});
|
||||
}
|
||||
|
||||
// get reset password
|
||||
|
||||
const resetToken = await user.getResetPassword();
|
||||
|
||||
await user.save({ validateBeforeSave: false });
|
||||
|
||||
/*const resetPasswordUrl = `${req.protocol}://${req.get(
|
||||
"host"
|
||||
)}/api/v1/password/reset/${resetToken}`;*/
|
||||
|
||||
const resetPasswordUrl = `${process.env.FRONTEND_URI}/user/api/v1/password/reset/${resetToken}`;
|
||||
|
||||
const message = `Your password token is :-\n\n${resetPasswordUrl}\n\nIf you are not requested this email then please ingore this mail.`;
|
||||
|
||||
try {
|
||||
await sendEmail({
|
||||
email: user.email,
|
||||
subject: "MentorFlux password recovery",
|
||||
message: message,
|
||||
});
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: `Email sent to ${email} successfully`,
|
||||
});
|
||||
} catch (error) {
|
||||
user.resetPasswordToken = undefined;
|
||||
user.resetPasswordExpiry = undefined;
|
||||
await user.save({ validateBeforeSave: false });
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Something went wrong ",
|
||||
error: error,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// reset users password -- DONE
|
||||
const resetPassword = catchAsyncErrors(async (req, res) => {
|
||||
const token = req.params.token;
|
||||
|
||||
const { password, confirmPassword } = req.body;
|
||||
|
||||
//console.log("My password is :", password);
|
||||
//console.log("My confirmPassword is :", confirmPassword);
|
||||
//console.log("My token is :", token);
|
||||
const resetPasswordToken = await crypto
|
||||
.createHash("sha256")
|
||||
.update(token)
|
||||
.digest("hex");
|
||||
|
||||
const user = await User.findOne({
|
||||
resetPasswordToken,
|
||||
resetPasswordExpiry: { $gte: Date.now() },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: "Reset Password token is invalid or has been expired",
|
||||
});
|
||||
}
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: "Please enter password and confirm password",
|
||||
});
|
||||
}
|
||||
|
||||
user.password = password;
|
||||
user.resetPasswordToken = undefined;
|
||||
user.resetPasswordExpiry = undefined;
|
||||
|
||||
//console.log("To check the user ", user);
|
||||
|
||||
await user.save();
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Password changed successfully",
|
||||
});
|
||||
});
|
||||
|
||||
// get user personal details
|
||||
const getUserDetails = catchAsyncErrors(async (req, res) => {
|
||||
|
||||
|
||||
const user = await User.findById(req.user._id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Something went wrong ",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User details are fetched successfully",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// Update users password
|
||||
const updatePassword = catchAsyncErrors(async (req, res) => {
|
||||
const { password, oldPassword, confirmPassword } = req.body;
|
||||
|
||||
const user = await User.findById(req.user._id);
|
||||
|
||||
const isPasswordMatched = await user.isPasswordCorrect(oldPassword);
|
||||
|
||||
if (!user) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (!isPasswordMatched) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Old password is incorrect.Please enter correct password ",
|
||||
});
|
||||
}
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Password and Confirm password should be same.",
|
||||
});
|
||||
}
|
||||
|
||||
user.password = password;
|
||||
await user.save({ validateBeforeSave: false });
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Password upadated successfully",
|
||||
});
|
||||
});
|
||||
|
||||
// update personal details
|
||||
const updatePersonalDetails = catchAsyncErrors(async (req, res) => {
|
||||
const { name, email } = req.body;
|
||||
const user = await User.findByIdAndUpdate(req.user._id, {
|
||||
$set: {
|
||||
name,
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Something went wrong",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User details updated successfully",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// Get all users details -- ADMIN
|
||||
const getAllusersDetail = catchAsyncErrors(async (req, res) => {
|
||||
const users = await find();
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "All user fetch successfully",
|
||||
data: users,
|
||||
});
|
||||
});
|
||||
|
||||
// get single user details
|
||||
const getSingaluserDetail = catchAsyncErrors(async (req, res) => {
|
||||
const user = await User.findById(req.params.id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// upadate user Role -- ADMIN
|
||||
const updateUserRole = catchAsyncErrors(async (req, res) => {
|
||||
const { name, email, role } = req.body;
|
||||
const user = await User.findByIdAndUpdate(req.params.id, {
|
||||
$set: {
|
||||
name,
|
||||
email,
|
||||
role,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Something went wrong",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User Role updated successfully",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// Delete user -- ADMIN
|
||||
const DeleteUser = catchAsyncErrors(async (req, res) => {
|
||||
const user = await User.findByIdAndDelete(req.params.id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User does not exist",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "User deleted successfully",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
// update avatar -- user
|
||||
const updateAvatar = catchAsyncErrors(async (req, res) => {
|
||||
//console.log("Our file is : ", req.file.path);
|
||||
|
||||
if (!req.file.path) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Avatar not uploaded on cloudinary.",
|
||||
});
|
||||
}
|
||||
|
||||
const avatarUrl = await uploadOnCloudinary(req.file.path);
|
||||
|
||||
if (!avatarUrl) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Avatar not uploaded on cloudinary.",
|
||||
});
|
||||
}
|
||||
|
||||
//console.log("Avatar url is : ", avatarUrl);
|
||||
|
||||
//console.log("our user is : ", req.user);
|
||||
|
||||
const user = await User.findByIdAndUpdate(req.user._id, {
|
||||
$set: {
|
||||
avatar: avatarUrl,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "User not found.",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Avatar updated successfully.",
|
||||
data: user,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
registerUser,
|
||||
loginUser,
|
||||
logoutUser,
|
||||
updateUserDetails,
|
||||
forgetPassword,
|
||||
resetPassword,
|
||||
getUserDetails,
|
||||
updatePassword,
|
||||
updatePersonalDetails,
|
||||
updateUserRole,
|
||||
DeleteUser,
|
||||
intializeUser,
|
||||
updateAvatar,
|
||||
};
|
||||
Reference in New Issue
Block a user