mapping sales coordinator and territory manager

This commit is contained in:
Sibunnayak 2024-08-28 17:06:03 +05:30
parent 5d91747fa1
commit e4514df821
5 changed files with 110 additions and 9 deletions

View File

@ -7,7 +7,7 @@ import password from "secure-random-password";
import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js"; import catchAsyncErrors from "../../middlewares/catchAsyncErrors.js";
export const register = async (req, res) => { export const register = async (req, res) => {
let { name, email, countryCode, mobileNumber,territoryManager } = req.body; let { name, email, countryCode, mobileNumber, territoryManager } = req.body;
// console.log(req.body); // console.log(req.body);
countryCode = countryCode?.trim(); countryCode = countryCode?.trim();
mobileNumber = mobileNumber?.trim(); mobileNumber = mobileNumber?.trim();
@ -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) => { export const getOneSalesCoOrdinator = async (req, res) => {
try { try {
if (!req.params.id) { if (!req.params.id) {

View File

@ -8,6 +8,11 @@ import crypto from "crypto";
const salescoordinatorSchema = new mongoose.Schema( const salescoordinatorSchema = new mongoose.Schema(
{ {
designation: {
type: String,
required: true,
default: "Sales Coordinator",
},
name: { name: {
type: String, type: String,
required: true, required: true,

View File

@ -16,6 +16,8 @@ import {
ChangePassword, ChangePassword,
getOneSalesCoOrdinator, getOneSalesCoOrdinator,
logout, logout,
getAllSalesCoOrdinatorbytmId,
mappedbyTM,
} from "./SalesCoOrdinatorController.js"; } from "./SalesCoOrdinatorController.js";
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
@ -37,6 +39,18 @@ router.get(
isAuthenticatedTerritoryManager, isAuthenticatedTerritoryManager,
getAllSalesCoOrdinator getAllSalesCoOrdinator
); );
router.get(
"/getbyTmId/:id",
isAuthenticatedUser,
authorizeRoles("admin"),
getAllSalesCoOrdinatorbytmId
);
router.put(
"/mappedtm/:id",
isAuthenticatedUser,
authorizeRoles("admin"),
mappedbyTM
);
router.get( router.get(
"/getOne/:id", "/getOne/:id",
isAuthenticatedUser, isAuthenticatedUser,
@ -75,11 +89,7 @@ router.patch(
authorizeRoles("admin"), authorizeRoles("admin"),
UpdateProfile UpdateProfile
); );
router.patch( router.patch("/profile/update", isAuthenticatedSalesCoOrdinator, UpdateProfile);
"/profile/update",
isAuthenticatedSalesCoOrdinator,
UpdateProfile
);
//change password //change password
router.put( router.put(
"/password/update/:id", "/password/update/:id",

View File

@ -49,20 +49,20 @@ const TaskSchema = new mongoose.Schema(
type: String, type: String,
enum: ['PrincipalDistributor', 'RetailDistributor'], enum: ['PrincipalDistributor', 'RetailDistributor'],
required: function () { required: function () {
return this.task === "Update Inventory Data"; return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
}, },
}, },
addedForId: { addedForId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
refPath: 'addedFor', refPath: 'addedFor',
required: function () { required: function () {
return this.task === "Update Inventory Data"; return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
}, },
}, },
tradename: { tradename: {
type: String, type: String,
required: function () { required: function () {
return this.task === "Update Inventory Data"; return this.task === "Update Inventory Data" || this.task === "Visit RD/PD";
}, },
}, },
}, },

View File

@ -8,6 +8,11 @@ import crypto from "crypto";
const territorymanagerSchema = new mongoose.Schema( const territorymanagerSchema = new mongoose.Schema(
{ {
designation: {
type: String,
required: true,
default: "Territory Manager",
},
name: { name: {
type: String, type: String,
required: true, required: true,