location updates saved to db and base endpoints for other modules started

This commit is contained in:
Shibi Chakkaravarthy
2026-04-18 04:15:23 +05:30
parent 92769cfd38
commit eb84767d44
12 changed files with 167 additions and 44 deletions
+33 -5
View File
@@ -8,7 +8,8 @@ import {
createDepartment,
getAllDepartments,
} from "../models/Department/operations.js";
import { createBranch, getAllBranches } from "../models/Branch/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) => {
@@ -30,6 +31,24 @@ export const createAccessController = async (req, res) => {
}
};
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();
@@ -59,7 +78,7 @@ export const getAllDepartmentsController = async (req, res) => {
export const getAllBranchesController = async (req, res) => {
try {
const branches = await getAllBranches();
const branches = await getAllVenues({ category: "OWN" });
res.status(200).json({ result: branches });
} catch (error) {
res.status(500).json({ error: error.message });
@@ -111,12 +130,21 @@ export const createDepartmentController = async (req, res) => {
export const createBranchController = async (req, res) => {
try {
const { user } = res.locals;
const branch = await createBranch({
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(),
});
res.status(200).json({ result: branch });
};
const venue = await createVenue(dbPayload);
res.status(200).json({ result: venue });
} catch (error) {
res.status(500).json({ error: error.message });
}
+7 -1
View File
@@ -7,8 +7,14 @@ import {
export const createLocationController = async (req, res) => {
try {
const { user } = res.locals;
const { location } = req.body;
console.log("BG LOCATIONREQUEST BODY", req.body, user);
// const location = await createLocation({ ...req.body, user: user?.id });
const dbPayload = {
battery: location.battery.level,
coordinatess: location.coords,
recorded_at: location.recorded_at,
};
const uploaded = await createLocation({ ...req.body, user: user?.id });
res.status(200).json({ location: "location" });
} catch (error) {
res.status(500).json({ error: error.message });
+90 -1
View File
@@ -1 +1,90 @@
import { User, Access, Profile, Team } from "../models/index.js";
import {
User,
Access,
Profile,
Team,
Branch,
Partner,
Vendor,
Venue,
} from "../models/index.js";
import dayjs from "dayjs";
import mongoose from "mongoose";
import { coordinatesToGeoJson } from "../helpers/geoJson.helper.js";
import bcrypt from "bcryptjs";
export const createEmployee = async (req, res) => {
const session = await mongoose.startSession();
session.startTransaction();
try {
const { user } = res.locals;
const {
name,
mobile,
id,
password,
address,
city,
state,
pincode,
email,
designation,
department,
branch,
role,
} = req.body;
const hash = await bcrypt.hash(password, 10);
const userExists = await User.exists({ id });
if (userExists) {
throw { code: 400, message: "ID already assigned for Another Employee" };
}
const employee = await User.create(
[
{
name,
id,
hash,
createdOn: dayjs().toISOString(),
createdBy: user.id,
},
],
{ session },
);
const profile = await Profile.create(
[
{
user: employee?._id,
mobile,
address,
city,
email,
state,
pincode,
isActive: true,
},
],
{ session },
);
const access = await Access.create(
[
{
user: employee?._id,
designation,
department,
branch,
role,
createdBy: user.id,
createdOn: dayjs().toISOString(),
},
],
{ session },
);
res.status(200).json({ result: access });
} catch (error) {
res.status(500).json({ error: error.message });
}
};