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 { const { user } = res.locals; const { employee, designation, department, branch, role } = req.body; const access = await createAccess({ user: employee, designation, department, branch, role, createdBy: user.id, createdOn: dayjs().toISOString(), }); res.status(200).json({ result: access }); } catch (error) { res.status(500).json({ error: error.message }); } }; export const updateDesignationController = async (req, res, next) => { try { const { id } = req.params; const designation = await updateDesignation(id, req.body); res.status(200).json({ result: designation }); } catch (error) { console.log("updateDesignationController Error", error); next(error); } }; export const updateDepartmentController = async (req, res, next) => { try { const { id } = req.params; const department = await updateDepartment(id, req.body); res.status(200).json({ result: department }); } catch (error) { console.log("updateDepartmentController Error", error); next(error); } }; export const updateRoleController = async (req, res, next) => { try { const { id } = req.params; const role = await updateRole(id, req.body); res.status(200).json({ result: role }); } catch (error) { console.log("updateRoleController Error", error); next(error); } }; export const createRoleController = async (req, res) => { try { const { user } = res.locals; const role = await createRole({ ...req.body, createdBy: user.id, createdOn: dayjs().toISOString(), }); res.status(200).json({ result: role }); } catch (error) { res.status(500).json({ error: error.message }); } }; export const createDesignationController = async (req, res) => { try { const { user } = res.locals; const designation = await createDesignation({ ...req.body, createdBy: user.id, createdOn: dayjs().toISOString(), }); res.status(200).json({ result: designation }); } catch (error) { res.status(500).json({ error: error.message }); } }; export const createDepartmentController = async (req, res) => { try { const { user } = res.locals; const department = await createDepartment({ ...req.body, createdBy: user.id, createdOn: dayjs().toISOString(), }); res.status(200).json({ result: department }); } catch (error) { res.status(500).json({ error: error.message }); } }; export const createBranchController = async (req, res) => { try { const { user } = res.locals; const { coordinates } = req.body; const geoJson = coordinatesToGeoJson({ latitude: coordinates.lat, longitude: coordinates.lng, }); const dbPayload = { ...req.body, category: "OWN", location: geoJson, createdBy: user.id, createdOn: dayjs().toISOString(), }; const venue = await createVenue(dbPayload); res.status(200).json({ result: venue }); } catch (error) { 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); } };