mapping sales coordinator and territory manager
This commit is contained in:
parent
5d91747fa1
commit
e4514df821
@ -212,7 +212,88 @@ export const getAllSalesCoOrdinator = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
export const getAllSalesCoOrdinatorbytmId = async (req, res) => {
|
||||
try {
|
||||
const PAGE_SIZE = parseInt(req.query?.show || "10");
|
||||
const page = parseInt(req.query?.page || "1") - 1;
|
||||
let filter = { mappedby: req.params.id }; // Include the mappedby field in the filter
|
||||
|
||||
// Adding optional filters
|
||||
if (req.query?.name) {
|
||||
filter.name = {
|
||||
$regex: new RegExp(req.query.name, "i"),
|
||||
};
|
||||
}
|
||||
if (req.query?.mobileNumber) {
|
||||
filter.mobileNumber = {
|
||||
$regex: new RegExp(req.query.mobileNumber, "i"),
|
||||
};
|
||||
}
|
||||
if (req.query?.isVerified) {
|
||||
filter.isVerified = req.query.isVerified === "true";
|
||||
}
|
||||
|
||||
const total = await SalesCoOrdinator.countDocuments(filter);
|
||||
const salesCoOrinators = await SalesCoOrdinator.find(filter)
|
||||
.limit(PAGE_SIZE)
|
||||
.skip(PAGE_SIZE * page)
|
||||
.sort({ createdAt: -1 });
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
total_data: total,
|
||||
total_pages: Math.ceil(total / PAGE_SIZE),
|
||||
salesCoOrinators,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
export const mappedbyTM = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params; // SalesCoOrdinator ID from URL parameters
|
||||
const { mappedby } = req.body; // TerritoryManager ID from request body
|
||||
// console.log(id, mappedby);
|
||||
// Validate that the TerritoryManager ID is provided
|
||||
if (!mappedby) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Territory Manager ID (mappedby) is required.",
|
||||
});
|
||||
}
|
||||
|
||||
// Find the SalesCoOrdinator by ID
|
||||
const salesCoordinator = await SalesCoOrdinator.findById(id);
|
||||
|
||||
// If no SalesCoOrdinator is found
|
||||
if (!salesCoordinator) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "Sales Coordinator not found.",
|
||||
});
|
||||
}
|
||||
|
||||
// Update the mappedby field
|
||||
salesCoordinator.mappedby = mappedby;
|
||||
|
||||
// Save the updated SalesCoOrdinator
|
||||
await salesCoordinator.save();
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Sales Coordinator successfully updated.",
|
||||
salesCoordinator,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
export const getOneSalesCoOrdinator = async (req, res) => {
|
||||
try {
|
||||
if (!req.params.id) {
|
||||
|
@ -8,6 +8,11 @@ import crypto from "crypto";
|
||||
|
||||
const salescoordinatorSchema = new mongoose.Schema(
|
||||
{
|
||||
designation: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: "Sales Coordinator",
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
@ -16,6 +16,8 @@ import {
|
||||
ChangePassword,
|
||||
getOneSalesCoOrdinator,
|
||||
logout,
|
||||
getAllSalesCoOrdinatorbytmId,
|
||||
mappedbyTM,
|
||||
} from "./SalesCoOrdinatorController.js";
|
||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||
@ -37,6 +39,18 @@ router.get(
|
||||
isAuthenticatedTerritoryManager,
|
||||
getAllSalesCoOrdinator
|
||||
);
|
||||
router.get(
|
||||
"/getbyTmId/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
getAllSalesCoOrdinatorbytmId
|
||||
);
|
||||
router.put(
|
||||
"/mappedtm/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
mappedbyTM
|
||||
);
|
||||
router.get(
|
||||
"/getOne/:id",
|
||||
isAuthenticatedUser,
|
||||
@ -75,11 +89,7 @@ router.patch(
|
||||
authorizeRoles("admin"),
|
||||
UpdateProfile
|
||||
);
|
||||
router.patch(
|
||||
"/profile/update",
|
||||
isAuthenticatedSalesCoOrdinator,
|
||||
UpdateProfile
|
||||
);
|
||||
router.patch("/profile/update", isAuthenticatedSalesCoOrdinator, UpdateProfile);
|
||||
//change password
|
||||
router.put(
|
||||
"/password/update/:id",
|
||||
|
@ -49,20 +49,20 @@ const TaskSchema = new mongoose.Schema(
|
||||
type: String,
|
||||
enum: ['PrincipalDistributor', 'RetailDistributor'],
|
||||
required: function () {
|
||||
return this.task === "Update Inventory Data";
|
||||
return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
|
||||
},
|
||||
},
|
||||
addedForId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
refPath: 'addedFor',
|
||||
required: function () {
|
||||
return this.task === "Update Inventory Data";
|
||||
return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
|
||||
},
|
||||
},
|
||||
tradename: {
|
||||
type: String,
|
||||
required: function () {
|
||||
return this.task === "Update Inventory Data";
|
||||
return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -8,6 +8,11 @@ import crypto from "crypto";
|
||||
|
||||
const territorymanagerSchema = new mongoose.Schema(
|
||||
{
|
||||
designation: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: "Territory Manager",
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
Loading…
Reference in New Issue
Block a user