From eb84767d44080e977a8fada0d22f704e7f34dd91 Mon Sep 17 00:00:00 2001 From: Shibi Chakkaravarthy Date: Sat, 18 Apr 2026 04:15:23 +0530 Subject: [PATCH] location updates saved to db and base endpoints for other modules started --- controllers/admin.controller.js | 38 +++++++++++-- controllers/location.controller.js | 8 ++- controllers/workflow.controller.js | 91 +++++++++++++++++++++++++++++- models/Branch.model.js | 10 ---- models/Branch/model.js | 2 +- models/Location/operations.js | 4 +- models/Profile/model.js | 4 ++ models/User/model.js | 5 ++ models/Venue/model.js | 28 +++++---- models/Venue/operations.js | 6 +- models/index.js | 1 + routes/admin.route.js | 14 +---- 12 files changed, 167 insertions(+), 44 deletions(-) delete mode 100644 models/Branch.model.js diff --git a/controllers/admin.controller.js b/controllers/admin.controller.js index 358e8ab..cadb1ac 100644 --- a/controllers/admin.controller.js +++ b/controllers/admin.controller.js @@ -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 }); } diff --git a/controllers/location.controller.js b/controllers/location.controller.js index 2935c0a..bb2a111 100644 --- a/controllers/location.controller.js +++ b/controllers/location.controller.js @@ -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 }); diff --git a/controllers/workflow.controller.js b/controllers/workflow.controller.js index 33289be..5e65d69 100644 --- a/controllers/workflow.controller.js +++ b/controllers/workflow.controller.js @@ -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 }); + } +}; diff --git a/models/Branch.model.js b/models/Branch.model.js deleted file mode 100644 index 0811303..0000000 --- a/models/Branch.model.js +++ /dev/null @@ -1,10 +0,0 @@ -import { model, Schema, Types } from "mongoose"; - -const schema = new Schema({ - createdOn: { - type: Date, - required: true - } -}); - -export default model("branch", schema); diff --git a/models/Branch/model.js b/models/Branch/model.js index 6fd0d91..82ba2cc 100644 --- a/models/Branch/model.js +++ b/models/Branch/model.js @@ -33,7 +33,7 @@ const schema = new Schema({ type: { type: String, required: true, - enum: ["HQ", "Branch", "Warehouse", "BackOffice"], + enum: ["HQ", "BRANCH", "WAREHOUSE"], }, }); diff --git a/models/Location/operations.js b/models/Location/operations.js index 32b7281..45b9f72 100644 --- a/models/Location/operations.js +++ b/models/Location/operations.js @@ -7,7 +7,7 @@ export const createLocation = async (data) => { return await Model.create({ ...data, coordinates: geoJson, - createdOn: dayjs().toISOString(), + createdOn: data?.recorded_at, }); }; @@ -18,7 +18,7 @@ export const getLocationById = async (id) => { // Read (Get by Query) export const getLocationByQuery = async (query) => { - return await Model.findOne(query); + return await Model.findOne(query).sort({ createdOn: -1 }); }; // Read (Get by Radius) diff --git a/models/Profile/model.js b/models/Profile/model.js index 6b37176..d1da52c 100644 --- a/models/Profile/model.js +++ b/models/Profile/model.js @@ -13,6 +13,10 @@ const schema = new Schema({ type: String, required: true, }, + email: { + type: String, + required: false, + }, state: { type: String, required: true, diff --git a/models/User/model.js b/models/User/model.js index cd633b1..e093e72 100644 --- a/models/User/model.js +++ b/models/User/model.js @@ -25,6 +25,11 @@ const schema = new Schema({ type: Date, required: true, }, + createdBy: { + type: Types.ObjectId, + required: true, + ref: "user", + }, }); export default model("user", schema); diff --git a/models/Venue/model.js b/models/Venue/model.js index 1800106..2eecdf5 100644 --- a/models/Venue/model.js +++ b/models/Venue/model.js @@ -15,6 +15,22 @@ const schema = new Schema({ type: Date, required: true, }, + address: { + type: String, + required: true, + }, + city: { + type: String, + required: true, + }, + state: { + type: String, + required: true, + }, + pincode: { + type: String, + required: true, + }, location: locationSchema, category: { type: String, @@ -25,19 +41,11 @@ const schema = new Schema({ type: { type: String, required: true, - enum: [ - "HQ", - "BRANCH", - "WAREHOUSE", - "VENDOR", - "PARTNER", - "DISTRIBUTOR", - "CLIENT", - ], + enum: ["HQ", "BRANCH", "WAREHOUSE"], }, entity: { type: Types.ObjectId, - required: true, + required: false, }, }); diff --git a/models/Venue/operations.js b/models/Venue/operations.js index c763d24..38ae3f7 100644 --- a/models/Venue/operations.js +++ b/models/Venue/operations.js @@ -13,11 +13,11 @@ export const getVenueById = async (id) => { // Read (Get by Query) export const getVenueByQuery = async (query) => { return await Model.findOne(query); -} +}; // Read (Get all) -export const getAllVenues = async () => { - return await Model.find(); +export const getAllVenues = async (query = {}) => { + return await Model.find(query); }; // Update diff --git a/models/index.js b/models/index.js index 3f565ab..6d6f06c 100644 --- a/models/index.js +++ b/models/index.js @@ -19,3 +19,4 @@ export { default as Designation } from "./Designation/model"; export { default as Inventory } from "./Inventory/model"; export { default as InventoryLog } from "./InventoryLog/model"; export { default as Role } from "./Role/model"; +export { default as Venue } from "./Venue/model"; diff --git a/routes/admin.route.js b/routes/admin.route.js index 7b8427f..abab500 100644 --- a/routes/admin.route.js +++ b/routes/admin.route.js @@ -3,9 +3,7 @@ import { authorize } from "../middlewares/jwt.middleware"; import { createAccessController, getAllBranchesController, - getAllDesignationsController, - getAllDepartmentsController, - getAllRolesController, + getAllMetadataController, createBranchController, createDepartmentController, createDesignationController, @@ -17,16 +15,10 @@ const router = new Router(); router.post("/access", authorize("access", "write"), createAccessController); router.get("/branches", authorize("branch", "read"), getAllBranchesController); router.get( - "/designations", + "/metadata", 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( "/department",