location updates saved to db and base endpoints for other modules started
This commit is contained in:
@@ -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,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 });
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { model, Schema, Types } from "mongoose";
|
||||
|
||||
const schema = new Schema({
|
||||
createdOn: {
|
||||
type: Date,
|
||||
required: true
|
||||
}
|
||||
});
|
||||
|
||||
export default model("branch", schema);
|
||||
@@ -33,7 +33,7 @@ const schema = new Schema({
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ["HQ", "Branch", "Warehouse", "BackOffice"],
|
||||
enum: ["HQ", "BRANCH", "WAREHOUSE"],
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -13,6 +13,10 @@ const schema = new Schema({
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
required: false,
|
||||
},
|
||||
state: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
@@ -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);
|
||||
|
||||
+18
-10
@@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
+3
-11
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user