add multiple pd update

This commit is contained in:
Sibunnayak 2024-10-18 12:51:59 +05:30
parent d4ad02de6a
commit 88496fb9d0
6 changed files with 25 additions and 22 deletions

View File

@ -6,7 +6,7 @@ const shippingAddressSchema = new mongoose.Schema(
type: String,
},
phoneNumber: {
type: Number,
type: String,
},
street: {
type: String,

View File

@ -6,7 +6,7 @@ const shippingAddressSchema = new mongoose.Schema(
type: String,
},
phoneNumber: {
type: Number,
type: String,
},
street: {
type: String,

View File

@ -384,27 +384,31 @@ export const getTodaysTasks = async (req, res) => {
const skip = (currentPage - 1) * itemsPerPage;
// Find tasks that are due today, with pagination
let tasksQuery = Task.find({
const tasks = await Task.find({
taskDueDate: {
$gte: startOfToday,
$lte: endOfToday,
},
})
.populate('taskAssignedTo') // Optional: populate assigned coordinator details
.populate('taskAssignedBy') // Optional: populate assigned manager details
.populate("taskAssignedTo") // Optional: populate assigned coordinator details
.populate("taskAssignedBy") // Optional: populate assigned manager details
.skip(skip) // Skip documents for pagination
.limit(itemsPerPage); // Limit the number of documents
.limit(itemsPerPage) // Limit the number of documents
.exec();
// Modify the population based on the `addedFor` field value
tasksQuery = tasksQuery.populate({
path: 'addedForId',
model: function (doc) {
return doc.addedFor === 'PrincipalDistributor' ? 'User' : 'RetailDistributor';
},
});
// Execute the query
const tasks = await tasksQuery.exec();
// Populate addedForId conditionally
const populatedTasks = await Promise.all(
tasks.map(async (task) => {
if (task.addedFor === "PrincipalDistributor") {
// Populate with PrincipalDistributor
await task.populate("addedForId", User);
} else if (task.addedFor === "RetailDistributor") {
// Populate with RetailDistributor
await task.populate("addedForId", RetailDistributor);
}
return task; // Return the populated task
})
);
// Count the total number of tasks for pagination metadata
const totalTasks = await Task.countDocuments({
@ -419,14 +423,14 @@ export const getTodaysTasks = async (req, res) => {
// Send paginated tasks in response
res.status(200).json({
tasks, // Paginated tasks
tasks: populatedTasks, // Paginated and populated tasks
currentPage, // Current page number
itemsPerPage, // Number of tasks per page
totalTasks, // Total number of tasks
totalPages, // Total number of pages
});
} catch (error) {
console.error('Error fetching today\'s tasks with pagination:', error);
res.status(500).json({ message: 'Failed to retrieve tasks for today' });
console.error("Error fetching today's tasks with pagination:", error);
res.status(500).json({ message: "Failed to retrieve tasks for today" });
}
};
};

View File

@ -553,7 +553,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
// Check for changes in address details
const addressData = {
Name: item.name,
phoneNumber: item.phone.toString(),
phoneNumber: item.phone.toString().trim(),
street: item.street,
city: item.city,
state: item.state,

View File

@ -38,7 +38,6 @@ const userSchema = new mongoose.Schema(
name: {
type: String,
required: [true, "Please Enter Your Name"],
maxLength: [30, "Name cannot exceed 30 characters"],
},
email: {
type: String,