Operational Endpoints for Admin, Products, Assignments, Location, Vendor, Venue completed. Geofence Testing PEnding
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { createAccess } from "../models/Access/operations.js";
|
||||
import { createRole, getAllRoles } from "../models/Role/operations.js";
|
||||
import {
|
||||
createRole,
|
||||
getAllRoles,
|
||||
updateRole,
|
||||
} from "../models/Role/operations.js";
|
||||
import {
|
||||
createDesignation,
|
||||
updateDesignation,
|
||||
@@ -60,6 +64,7 @@ export const updateDepartmentController = async (req, res, next) => {
|
||||
export const updateRoleController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
console.log("REQ BODY", req.body, id);
|
||||
const role = await updateRole(id, req.body);
|
||||
res.status(200).json({ result: role });
|
||||
} catch (error) {
|
||||
@@ -78,6 +83,7 @@ export const createRoleController = async (req, res) => {
|
||||
});
|
||||
res.status(200).json({ result: role });
|
||||
} catch (error) {
|
||||
console.log("createRoleController Error", error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
@@ -251,7 +257,7 @@ export const getEmployeeDetailsController = async (req, res, next) => {
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
_id: new ObjectId.createFromHexString(id),
|
||||
_id: ObjectId.createFromHexString(id),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -336,9 +342,9 @@ export const getEmployeeDetailsController = async (req, res, next) => {
|
||||
$project: {
|
||||
_id: 1,
|
||||
name: 1,
|
||||
department: "$department.name",
|
||||
designation: "$designation.name",
|
||||
role: "$role.name",
|
||||
department: 1,
|
||||
designation: 1,
|
||||
role: 1,
|
||||
profile: 1,
|
||||
branches: 1,
|
||||
},
|
||||
|
||||
@@ -2,8 +2,10 @@ import {
|
||||
createAssignment,
|
||||
updateAssignment,
|
||||
getAssignmentsByQuery,
|
||||
aggregateAssignment,
|
||||
} from "../models/Assignment/operations.js";
|
||||
import dayjs from "dayjs";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
export const createAssignmentHandler = async (req, res) => {
|
||||
try {
|
||||
@@ -13,6 +15,7 @@ export const createAssignmentHandler = async (req, res) => {
|
||||
});
|
||||
const assignment = await createAssignment({
|
||||
...req.body,
|
||||
isActive: true,
|
||||
createdOn: dayjs().toISOString(),
|
||||
createdBy: user.id,
|
||||
});
|
||||
@@ -35,7 +38,41 @@ export const updateAssignmentHandler = async (req, res) => {
|
||||
export const getAssignmentsByUserHandler = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const assignment = await getAssignmentsByQuery({ user: id });
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
user: ObjectId?.createFromHexString(id),
|
||||
isActive: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "venues",
|
||||
localField: "venue",
|
||||
foreignField: "_id",
|
||||
as: "venue",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$venue",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "partners",
|
||||
localField: "venue.entity",
|
||||
foreignField: "_id",
|
||||
as: "partner",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$partner",
|
||||
},
|
||||
},
|
||||
];
|
||||
const assignment = await aggregateAssignment(pipeline);
|
||||
res.status(200).json({ result: assignment });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Error getting assignment", error });
|
||||
|
||||
@@ -3,6 +3,8 @@ import {
|
||||
getLocationByQuery,
|
||||
getLocationByRadius,
|
||||
} from "../models/Location/operations";
|
||||
import { aggregateAssignment } from "../models/Assignment/operations";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
/*
|
||||
Sample Location Payload
|
||||
@@ -69,3 +71,48 @@ export const getLocationController = async (req, res) => {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getMyAssignedVenuesHandler = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
user: ObjectId?.createFromHexString(user.id),
|
||||
isActive: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "venues",
|
||||
localField: "venue",
|
||||
foreignField: "_id",
|
||||
as: "venue",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$venue",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "partners",
|
||||
localField: "venue.entity",
|
||||
foreignField: "_id",
|
||||
as: "partner",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$partner",
|
||||
},
|
||||
},
|
||||
];
|
||||
const assignment = await aggregateAssignment(pipeline);
|
||||
res.status(200).json({ result: assignment });
|
||||
} catch (error) {
|
||||
console.log("getMyAssignedVenuesHandler Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
createProduct,
|
||||
updateProduct,
|
||||
getAllProducts,
|
||||
getProductByQuery,
|
||||
getProductById,
|
||||
deleteProduct,
|
||||
} from "../models/Product/operations";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export const createProductController = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
|
||||
const product = await createProduct({
|
||||
...req.body,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
result: product,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("createProductController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const updateProductController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const product = await updateProduct(id, {
|
||||
...req.body,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
result: product,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("updateProductController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllProductsController = async (req, res, next) => {
|
||||
try {
|
||||
const products = await getAllProducts();
|
||||
|
||||
res.status(200).json({
|
||||
result: products,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("getAllProductsController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteProductController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const product = await deleteProduct(id);
|
||||
res.status(200).json({
|
||||
result: product,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("deleteProductController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
import dayjs from "dayjs";
|
||||
import {
|
||||
createVendor,
|
||||
updateVendor,
|
||||
deleteVendor,
|
||||
getVendorsByQuery,
|
||||
getVendorById,
|
||||
getAllVendors,
|
||||
} from "../models/Vendor/operations";
|
||||
|
||||
export const createVendorController = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const vendor = await createVendor({
|
||||
...req.body,
|
||||
createdBy: user?.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(201).json({ result: vendor });
|
||||
} catch (error) {
|
||||
console.log("createVendorController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const updateVendorController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const vendor = await updateVendor(id, req.body);
|
||||
res.status(200).json({ result: vendor });
|
||||
} catch (error) {
|
||||
console.log("updateVendorController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getActiveVendorsController = async (req, res, next) => {
|
||||
try {
|
||||
const vendors = await getVendorsByQuery({ isActive: true });
|
||||
res.status(200).json({ result: vendors });
|
||||
} catch (error) {
|
||||
console.log("getActiveVendorsController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllVendorsController = async (req, res, next) => {
|
||||
try {
|
||||
const vendors = await getAllVendors();
|
||||
res.status(200).json({ result: vendors });
|
||||
} catch (error) {
|
||||
console.log("getAllVendorsController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const deactivateVendorController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const deactivatedVendor = await updateVendor(id, { isActive: false });
|
||||
res.status(200).json({ result: deactivatedVendor });
|
||||
} catch (error) {
|
||||
console.log("deactivateVendorController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getVendorByIdController = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const vendor = await getVendorById(id);
|
||||
|
||||
res.status(200).json({ result: vendor });
|
||||
} catch (error) {
|
||||
console.log("getVendorByIdController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
updateVenue,
|
||||
aggregateVenue,
|
||||
} from "../models/Venue/operations.js";
|
||||
import { ObjectId } from "mongodb";
|
||||
import { aggregateAssignment } from "../models/Assignment/operations.js";
|
||||
import { coordinatesToGeoJson } from "../helpers/geoJson.helper.js";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
@@ -69,3 +71,106 @@ export const getVenueController = async (req, res, next) => {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAssignedVenuesController = async (req, res, next) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
// const { user } = res.locals;
|
||||
|
||||
const assignedVenues = await aggregateAssignment([
|
||||
{
|
||||
$match: {
|
||||
user: ObjectId.createFromHexString(userId),
|
||||
isActive: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "venues",
|
||||
localField: "venue",
|
||||
foreignField: "_id",
|
||||
as: "venue",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$venue",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "partners",
|
||||
localField: "venue.entity",
|
||||
foreignField: "_id",
|
||||
as: "partner",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$partner",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
res.status(200).json({ result: assignedVenues });
|
||||
} catch (error) {
|
||||
console.log("getAssignedVenues Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getUnassignedVenuesController = async (req, res, next) => {
|
||||
try {
|
||||
const unassignedVenues = await aggregateVenue([
|
||||
{
|
||||
$match: {
|
||||
category: "PARTNER"
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "assignments",
|
||||
localField: "_id",
|
||||
foreignField: "venue",
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
isActive: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
as: "activeAssignments",
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{ activeAssignments: { $size: 0 } },
|
||||
{ activeAssignments: { $exists: false } },
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "partners",
|
||||
localField: "entity",
|
||||
foreignField: "_id",
|
||||
as: "partner",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$partner",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
res.status(200).json({ result: unassignedVenues });
|
||||
} catch (error) {
|
||||
console.log("getUnassignedVenuesController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user