Files
fielderp-server/controllers/admin.controller.js
T

152 lines
4.1 KiB
JavaScript

import { createAccess } from "../models/Access/operations.js";
import { createRole, getAllRoles } from "../models/Role/operations.js";
import {
createDesignation,
getAllDesignations,
} from "../models/Designation/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 dayjs from "dayjs";
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 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 getAllRolesController = async (req, res) => {
try {
const roles = await getAllRoles();
res.status(200).json({ result: roles });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
export const getAllDesignationsController = async (req, res) => {
try {
const designations = await getAllDesignations();
res.status(200).json({ result: designations });
} 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 });
}
};
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 });
}
};