task updated
This commit is contained in:
parent
fcc4eab495
commit
768fc3c77f
Binary file not shown.
@ -13,7 +13,7 @@ export const assignTask = async (req, res) => {
|
||||
addedFor,
|
||||
addedForId,
|
||||
} = req.body;
|
||||
// console.log(req.body);
|
||||
|
||||
const currentYear = new Date().getFullYear().toString().slice(-2);
|
||||
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
|
||||
const uniqueId = `${currentYear}-${randomChars}`;
|
||||
@ -22,11 +22,11 @@ export const assignTask = async (req, res) => {
|
||||
taskId: uniqueId,
|
||||
task,
|
||||
note,
|
||||
taskStatus: "Pending",
|
||||
taskStatus: "New",
|
||||
taskPriority,
|
||||
taskDueDate,
|
||||
taskAssignedTo,
|
||||
taskAssignedBy: req.user._id, // The Territory Manager's ID
|
||||
taskAssignedBy: req.user._id,
|
||||
addedFor,
|
||||
addedForId,
|
||||
});
|
||||
@ -44,27 +44,41 @@ export const assignTask = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getTasksForSalesCoordinator = async (req, res) => {
|
||||
export const getTasksByStatus = async (req, res) => {
|
||||
try {
|
||||
const tasks = await Task.find({ taskAssignedTo: req.user._id });
|
||||
const { status } = req.params; // This should be "New", "Pending", or "Completed"
|
||||
|
||||
// Validate the provided status
|
||||
if (!["New", "Pending", "Completed"].includes(status)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid status type provided.",
|
||||
});
|
||||
}
|
||||
|
||||
// Find tasks assigned to the user, filtered by status, and sorted by creation date (newest to oldest)
|
||||
const tasks = await Task.find({
|
||||
taskAssignedTo: req.user._id,
|
||||
taskStatus: status,
|
||||
}).sort({ createdAt: -1 }); // Sort by createdAt in descending order (-1 means newest first)
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
tasks,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const updateTaskStatus = async (req, res) => {
|
||||
try {
|
||||
const { taskId } = req.params;
|
||||
|
||||
// Find the task to ensure it belongs to the logged-in Sales Coordinator
|
||||
const task = await Task.findOne({ _id: taskId, taskAssignedTo: req.user._id });
|
||||
|
||||
if (!task) {
|
||||
@ -89,4 +103,4 @@ export const updateTaskStatus = async (req, res) => {
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -12,7 +12,7 @@ const TaskSchema = new mongoose.Schema(
|
||||
task: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ["Visit Retailers", "Update Sales Data", "Update Inventory Data", "Collect KYC"], // Restrict to specific tasks
|
||||
enum: ["Visit Retailers", "Update Sales Data", "Update Inventory Data", "Collect KYC"],
|
||||
},
|
||||
note: {
|
||||
type: String,
|
||||
@ -23,17 +23,18 @@ const TaskSchema = new mongoose.Schema(
|
||||
taskStatus: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ["Pending", "In Progress", "Completed"],
|
||||
enum: ["New", "Pending", "Completed"],
|
||||
default: "New",
|
||||
},
|
||||
taskPriority: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ["Low", "Medium", "High"],
|
||||
enum: ["Low", "Medium", "High"],
|
||||
},
|
||||
taskDueDate: {
|
||||
type: String,
|
||||
required: true,
|
||||
match: /^\d{2}\/\d{2}\/\d{4}$/,
|
||||
match: /^\d{2}\/\d{2}\/\d{4}$/, // e.g., "DD/MM/YYYY"
|
||||
},
|
||||
taskAssignedTo: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
@ -63,6 +64,25 @@ const TaskSchema = new mongoose.Schema(
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
// Middleware to update task status based on due date
|
||||
TaskSchema.pre("save", function (next) {
|
||||
const currentDate = new Date();
|
||||
const [day, month, year] = this.taskDueDate.split("/").map(Number);
|
||||
const dueDate = new Date(year, month - 1, day);
|
||||
|
||||
// Convert dates to the start of the day for comparison
|
||||
const currentDateOnly = new Date(currentDate.setHours(0, 0, 0, 0));
|
||||
const dueDateOnly = new Date(dueDate.setHours(0, 0, 0, 0));
|
||||
|
||||
// Check if the current date is after the due date
|
||||
if (currentDateOnly > dueDateOnly && this.taskStatus === "New") {
|
||||
this.taskStatus = "Pending";
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
const Task = mongoose.model("Task", TaskSchema);
|
||||
|
||||
export default Task;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import express from "express";
|
||||
import {
|
||||
assignTask,
|
||||
getTasksForSalesCoordinator,
|
||||
getTasksByStatus,
|
||||
updateTaskStatus,
|
||||
} from "./TaskController.js";
|
||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||
@ -16,13 +16,14 @@ router.post(
|
||||
assignTask
|
||||
);
|
||||
|
||||
// Route for Sales Coordinator to view their tasks
|
||||
// Route for Sales Coordinator to view their tasks by status
|
||||
router.get(
|
||||
"/tasks",
|
||||
"/tasks/:status",
|
||||
isAuthenticatedSalesCoOrdinator,
|
||||
getTasksForSalesCoordinator
|
||||
getTasksByStatus
|
||||
);
|
||||
|
||||
// Route to update task status
|
||||
router.put(
|
||||
"/update-task-status/:taskId",
|
||||
isAuthenticatedSalesCoOrdinator,
|
||||
|
@ -955,7 +955,8 @@ export const updateProfile = catchAsyncErrors(async (req, res, next) => {
|
||||
// 9.Get all users(admin)
|
||||
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
|
||||
// Assuming your User model is imported as 'User'
|
||||
const users = await User.find({ role: "principal-Distributor" });
|
||||
const users = await User.find({ role: "principal-Distributor" })
|
||||
.sort({ createdAt: -1 });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
|
Loading…
Reference in New Issue
Block a user