task updated and product manual completed

This commit is contained in:
Sibunnayak 2024-08-27 13:39:07 +05:30
parent 059e39e13c
commit c6cee73fdc
4 changed files with 74 additions and 23 deletions

22
package-lock.json generated
View File

@ -28,6 +28,7 @@
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"multer-storage-cloudinary": "^4.0.0", "multer-storage-cloudinary": "^4.0.0",
"nanoid": "^5.0.7", "nanoid": "^5.0.7",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.4", "nodemailer": "^6.9.4",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"pm2": "^5.3.1", "pm2": "^5.3.1",
@ -4825,6 +4826,27 @@
"node": ">= 0.4.0" "node": ">= 0.4.0"
} }
}, },
"node_modules/node-cron": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"license": "ISC",
"dependencies": {
"uuid": "8.3.2"
},
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/node-cron/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"license": "MIT",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/node-fetch": { "node_modules/node-fetch": {
"version": "2.7.0", "version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",

View File

@ -33,6 +33,7 @@
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"multer-storage-cloudinary": "^4.0.0", "multer-storage-cloudinary": "^4.0.0",
"nanoid": "^5.0.7", "nanoid": "^5.0.7",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.4", "nodemailer": "^6.9.4",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"pm2": "^5.3.1", "pm2": "^5.3.1",

View File

@ -1,6 +1,37 @@
import Task from "./TaskModel.js"; import Task from "./TaskModel.js";
import SalesCoOrdinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js";
import crypto from "crypto"; import crypto from "crypto";
import cron from 'node-cron';
// Function to update task statuses
const updateOverdueTasks = async () => {
try {
const currentDate = new Date();
const currentDateOnly = new Date(currentDate.setHours(0, 0, 0, 0));
// Find tasks where dueDate is before the current date and status is "New"
const overdueTasks = await Task.find({
taskDueDate: { $lt: currentDateOnly.toISOString().split('T')[0] },
taskStatus: 'New',
});
// Update tasks to "Pending"
for (const task of overdueTasks) {
task.taskStatus = 'Pending';
await task.save();
}
console.log('Overdue tasks updated to "Pending".');
} catch (error) {
console.error('Error updating overdue tasks:', error);
}
};
// Schedule the cron job to run daily at midnight
cron.schedule('0 0 * * *', updateOverdueTasks);
const parseDate = (dateStr) => {
const [day, month, year] = dateStr.split('/').map(Number);
return new Date(year, month - 1, day);
};
export const assignTask = async (req, res) => { export const assignTask = async (req, res) => {
try { try {
@ -15,9 +46,25 @@ export const assignTask = async (req, res) => {
tradename, tradename,
} = req.body; } = req.body;
// Convert the taskDueDate from DD/MM/YYYY string to Date object
const dueDate = parseDate(taskDueDate);
const currentDate = new Date();
// Set the time of the currentDate to the start of the day for comparison
currentDate.setHours(0, 0, 0, 0);
// Check if the due date is in the past
if (dueDate < currentDate) {
return res.status(400).json({
success: false,
message: "Due date cannot be earlier than the current date.",
});
}
const currentYear = new Date().getFullYear().toString().slice(-2); const currentYear = new Date().getFullYear().toString().slice(-2);
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase(); const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
const uniqueId = `${currentYear}-${randomChars}`; const uniqueId = `${currentYear}-${randomChars}`;
// Create a new task // Create a new task
const newTask = await Task.create({ const newTask = await Task.create({
taskId: uniqueId, taskId: uniqueId,
@ -25,7 +72,7 @@ export const assignTask = async (req, res) => {
note, note,
taskStatus: "New", taskStatus: "New",
taskPriority, taskPriority,
taskDueDate, taskDueDate: dueDate, // Save the date as a Date object
taskAssignedTo, taskAssignedTo,
taskAssignedBy: req.user._id, taskAssignedBy: req.user._id,
addedFor, addedFor,
@ -46,6 +93,7 @@ export const assignTask = async (req, res) => {
} }
}; };
export const getTasksByStatus = async (req, res) => { export const getTasksByStatus = async (req, res) => {
try { try {
const { status } = req.params; // This should be "New", "Pending", or "Completed" const { status } = req.params; // This should be "New", "Pending", or "Completed"

View File

@ -32,9 +32,8 @@ const TaskSchema = new mongoose.Schema(
enum: ["Low", "Medium", "High"], enum: ["Low", "Medium", "High"],
}, },
taskDueDate: { taskDueDate: {
type: String, type: Date, // Change to Date
required: true, required: true,
match: /^\d{2}\/\d{2}\/\d{4}$/, // e.g., "DD/MM/YYYY"
}, },
taskAssignedTo: { taskAssignedTo: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
@ -70,25 +69,6 @@ const TaskSchema = new mongoose.Schema(
{ timestamps: true } { 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); const Task = mongoose.model("Task", TaskSchema);
export default Task; export default Task;