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, createDepartment,
getAllDepartments, getAllDepartments,
} from "../models/Department/operations.js"; } 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"; import dayjs from "dayjs";
export const createAccessController = async (req, res) => { 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) => { export const getAllRolesController = async (req, res) => {
try { try {
const roles = await getAllRoles(); const roles = await getAllRoles();
@@ -59,7 +78,7 @@ export const getAllDepartmentsController = async (req, res) => {
export const getAllBranchesController = async (req, res) => { export const getAllBranchesController = async (req, res) => {
try { try {
const branches = await getAllBranches(); const branches = await getAllVenues({ category: "OWN" });
res.status(200).json({ result: branches }); res.status(200).json({ result: branches });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
@@ -111,12 +130,21 @@ export const createDepartmentController = async (req, res) => {
export const createBranchController = async (req, res) => { export const createBranchController = async (req, res) => {
try { try {
const { user } = res.locals; 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, ...req.body,
category: "OWN",
location: geoJson,
createdBy: user.id, createdBy: user.id,
createdOn: dayjs().toISOString(), createdOn: dayjs().toISOString(),
}); };
res.status(200).json({ result: branch }); const venue = await createVenue(dbPayload);
res.status(200).json({ result: venue });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
} }
+7 -1
View File
@@ -7,8 +7,14 @@ import {
export const createLocationController = async (req, res) => { export const createLocationController = async (req, res) => {
try { try {
const { user } = res.locals; const { user } = res.locals;
const { location } = req.body;
console.log("BG LOCATIONREQUEST BODY", req.body, user); 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" }); res.status(200).json({ location: "location" });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); 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 });
}
};
-10
View File
@@ -1,10 +0,0 @@
import { model, Schema, Types } from "mongoose";
const schema = new Schema({
createdOn: {
type: Date,
required: true
}
});
export default model("branch", schema);
+1 -1
View File
@@ -33,7 +33,7 @@ const schema = new Schema({
type: { type: {
type: String, type: String,
required: true, required: true,
enum: ["HQ", "Branch", "Warehouse", "BackOffice"], enum: ["HQ", "BRANCH", "WAREHOUSE"],
}, },
}); });
+2 -2
View File
@@ -7,7 +7,7 @@ export const createLocation = async (data) => {
return await Model.create({ return await Model.create({
...data, ...data,
coordinates: geoJson, coordinates: geoJson,
createdOn: dayjs().toISOString(), createdOn: data?.recorded_at,
}); });
}; };
@@ -18,7 +18,7 @@ export const getLocationById = async (id) => {
// Read (Get by Query) // Read (Get by Query)
export const getLocationByQuery = async (query) => { export const getLocationByQuery = async (query) => {
return await Model.findOne(query); return await Model.findOne(query).sort({ createdOn: -1 });
}; };
// Read (Get by Radius) // Read (Get by Radius)
+4
View File
@@ -13,6 +13,10 @@ const schema = new Schema({
type: String, type: String,
required: true, required: true,
}, },
email: {
type: String,
required: false,
},
state: { state: {
type: String, type: String,
required: true, required: true,
+5
View File
@@ -25,6 +25,11 @@ const schema = new Schema({
type: Date, type: Date,
required: true, required: true,
}, },
createdBy: {
type: Types.ObjectId,
required: true,
ref: "user",
},
}); });
export default model("user", schema); export default model("user", schema);
+18 -10
View File
@@ -15,6 +15,22 @@ const schema = new Schema({
type: Date, type: Date,
required: true, required: true,
}, },
address: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
state: {
type: String,
required: true,
},
pincode: {
type: String,
required: true,
},
location: locationSchema, location: locationSchema,
category: { category: {
type: String, type: String,
@@ -25,19 +41,11 @@ const schema = new Schema({
type: { type: {
type: String, type: String,
required: true, required: true,
enum: [ enum: ["HQ", "BRANCH", "WAREHOUSE"],
"HQ",
"BRANCH",
"WAREHOUSE",
"VENDOR",
"PARTNER",
"DISTRIBUTOR",
"CLIENT",
],
}, },
entity: { entity: {
type: Types.ObjectId, type: Types.ObjectId,
required: true, required: false,
}, },
}); });
+3 -3
View File
@@ -13,11 +13,11 @@ export const getVenueById = async (id) => {
// Read (Get by Query) // Read (Get by Query)
export const getVenueByQuery = async (query) => { export const getVenueByQuery = async (query) => {
return await Model.findOne(query); return await Model.findOne(query);
} };
// Read (Get all) // Read (Get all)
export const getAllVenues = async () => { export const getAllVenues = async (query = {}) => {
return await Model.find(); return await Model.find(query);
}; };
// Update // Update
+1
View File
@@ -19,3 +19,4 @@ export { default as Designation } from "./Designation/model";
export { default as Inventory } from "./Inventory/model"; export { default as Inventory } from "./Inventory/model";
export { default as InventoryLog } from "./InventoryLog/model"; export { default as InventoryLog } from "./InventoryLog/model";
export { default as Role } from "./Role/model"; export { default as Role } from "./Role/model";
export { default as Venue } from "./Venue/model";
+3 -11
View File
@@ -3,9 +3,7 @@ import { authorize } from "../middlewares/jwt.middleware";
import { import {
createAccessController, createAccessController,
getAllBranchesController, getAllBranchesController,
getAllDesignationsController, getAllMetadataController,
getAllDepartmentsController,
getAllRolesController,
createBranchController, createBranchController,
createDepartmentController, createDepartmentController,
createDesignationController, createDesignationController,
@@ -17,16 +15,10 @@ const router = new Router();
router.post("/access", authorize("access", "write"), createAccessController); router.post("/access", authorize("access", "write"), createAccessController);
router.get("/branches", authorize("branch", "read"), getAllBranchesController); router.get("/branches", authorize("branch", "read"), getAllBranchesController);
router.get( router.get(
"/designations", "/metadata",
authorize("designation", "read"), authorize("designation", "read"),
getAllDesignationsController, getAllMetadataController,
); );
router.get(
"/departments",
authorize("department", "read"),
getAllDepartmentsController,
);
router.get("/roles", authorize("role", "read"), getAllRolesController);
router.post("/branch", authorize("branch", "write"), createBranchController); router.post("/branch", authorize("branch", "write"), createBranchController);
router.post( router.post(
"/department", "/department",