location controller updated with heading and created new models and controllers for remaining modules
This commit is contained in:
+263
-39
@@ -2,15 +2,19 @@ import { createAccess } from "../models/Access/operations.js";
|
||||
import { createRole, getAllRoles } from "../models/Role/operations.js";
|
||||
import {
|
||||
createDesignation,
|
||||
updateDesignation,
|
||||
getAllDesignations,
|
||||
} from "../models/Designation/operations.js";
|
||||
import { aggregateUser } from "../models/User/operations.js";
|
||||
import {
|
||||
createDepartment,
|
||||
getAllDepartments,
|
||||
} from "../models/Department/operations.js";
|
||||
import { getAllVenues, createVenue } from "../models/Venue/operations.js";
|
||||
import { coordinatesToGeoJson } from "../helpers/geoJson.helper.js";
|
||||
import { getLocationsByQuery } from "../models/Location/operations.js";
|
||||
import dayjs from "dayjs";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
export const createAccessController = async (req, res) => {
|
||||
try {
|
||||
@@ -31,57 +35,36 @@ export const createAccessController = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllMetadataController = async (req, res) => {
|
||||
export const updateDesignationController = async (req, res, next) => {
|
||||
try {
|
||||
const roles = await getAllRoles();
|
||||
const designations = await getAllDesignations();
|
||||
const departments = await getAllDepartments();
|
||||
const branches = await getAllVenues({ category: "OWN" });
|
||||
const metadata = {
|
||||
roles,
|
||||
designations,
|
||||
departments,
|
||||
branches,
|
||||
};
|
||||
res.status(200).json({ result: metadata });
|
||||
const { id } = req.params;
|
||||
const designation = await updateDesignation(id, req.body);
|
||||
res.status(200).json({ result: designation });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
console.log("updateDesignationController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllRolesController = async (req, res) => {
|
||||
export const updateDepartmentController = async (req, res, next) => {
|
||||
try {
|
||||
const roles = await getAllRoles();
|
||||
res.status(200).json({ result: roles });
|
||||
const { id } = req.params;
|
||||
const department = await updateDepartment(id, req.body);
|
||||
res.status(200).json({ result: department });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
console.log("updateDepartmentController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllDesignationsController = async (req, res) => {
|
||||
export const updateRoleController = async (req, res, next) => {
|
||||
try {
|
||||
const designations = await getAllDesignations();
|
||||
res.status(200).json({ result: designations });
|
||||
const { id } = req.params;
|
||||
const role = await updateRole(id, req.body);
|
||||
res.status(200).json({ result: role });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllDepartmentsController = async (req, res) => {
|
||||
try {
|
||||
const departments = await getAllDepartments();
|
||||
res.status(200).json({ result: departments });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllBranchesController = async (req, res) => {
|
||||
try {
|
||||
const branches = await getAllVenues({ category: "OWN" });
|
||||
res.status(200).json({ result: branches });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
console.log("updateRoleController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -149,3 +132,244 @@ export const createBranchController = async (req, res) => {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllEmployeeController = async (req, res, next) => {
|
||||
try {
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
isActive: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "profiles",
|
||||
localField: "_id",
|
||||
foreignField: "user",
|
||||
as: "profile",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$profile",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "accesses",
|
||||
localField: "_id",
|
||||
foreignField: "user",
|
||||
as: "access",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$access",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "designations",
|
||||
localField: "access.designation",
|
||||
foreignField: "_id",
|
||||
as: "designation",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$designation",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "departments",
|
||||
localField: "access.department",
|
||||
foreignField: "_id",
|
||||
as: "department",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$department",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
name: 1,
|
||||
id: 1,
|
||||
profile: 1,
|
||||
designation: "$designation.name",
|
||||
department: "$department.name",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const data = await aggregateUser(pipeline);
|
||||
|
||||
res.status(200).json({ result: data });
|
||||
} catch (error) {
|
||||
console.log("getAllEmployeeController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllMetadataController = async (req, res) => {
|
||||
try {
|
||||
const roles = await getAllRoles();
|
||||
const designations = await getAllDesignations();
|
||||
const departments = await getAllDepartments();
|
||||
const branches = await getAllVenues({ category: "OWN" });
|
||||
const metadata = {
|
||||
roles,
|
||||
designations,
|
||||
departments,
|
||||
branches,
|
||||
};
|
||||
res.status(200).json({ result: metadata });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllBranchesController = async (req, res) => {
|
||||
try {
|
||||
const branches = await getAllVenues({ category: "OWN" });
|
||||
res.status(200).json({ result: branches });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getEmployeeDetailsController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
_id: new ObjectId.createFromHexString(id),
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "profiles",
|
||||
localField: "_id",
|
||||
foreignField: "user",
|
||||
as: "profile",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$profile",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "accesses",
|
||||
localField: "_id",
|
||||
foreignField: "user",
|
||||
as: "access",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$access",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "designations",
|
||||
localField: "access.designation",
|
||||
foreignField: "_id",
|
||||
as: "designation",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$designation",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "departments",
|
||||
localField: "access.department",
|
||||
foreignField: "_id",
|
||||
as: "department",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$department",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "roles",
|
||||
localField: "access.role",
|
||||
foreignField: "_id",
|
||||
as: "role",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$role",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "venues",
|
||||
localField: "access.branch",
|
||||
foreignField: "_id",
|
||||
as: "branches",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
name: 1,
|
||||
department: "$department.name",
|
||||
designation: "$designation.name",
|
||||
role: "$role.name",
|
||||
profile: 1,
|
||||
branches: 1,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const [data] = await aggregateUser(pipeline);
|
||||
res.status(200).json({ result: data });
|
||||
} catch (error) {
|
||||
console.log("getEmployeeDetails Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getEmployeeLocationHistoryController = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = req.params;
|
||||
const { date = dayjs().subtract(330, "minute").toISOString() } = req.query;
|
||||
const start = dayjs(date).startOf("day").toISOString();
|
||||
const end = dayjs(date).endOf("day").toISOString();
|
||||
|
||||
console.log("Start Date", start, end, user);
|
||||
|
||||
const locations = await getLocationsByQuery({
|
||||
user,
|
||||
createdOn: { $gte: start, $lte: end },
|
||||
});
|
||||
console.log("LOCATIONS", locations);
|
||||
res.status(200).json({ result: locations });
|
||||
} catch (error) {
|
||||
console.log("getEmployeeLocationHistory Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user