148 lines
3.7 KiB
JavaScript
148 lines
3.7 KiB
JavaScript
import { createUser, getUserByQuery } from "../models/User/operations.js";
|
|
import { aggregateAccess } from "../models/Access/operations.js";
|
|
import bcrypt from "bcrypt";
|
|
import dayjs from "dayjs";
|
|
import {
|
|
getAccessToken,
|
|
getRefreshToken,
|
|
getEncryptedPayload,
|
|
} from "../middlewares/jwt.middleware.js";
|
|
import { ObjectId } from "mongodb";
|
|
|
|
export const createUserController = async (req, res, next) => {
|
|
try {
|
|
const { id, name, password } = req.body;
|
|
const hash = await bcrypt.hash(password, 10);
|
|
|
|
const userExists = await getUserByQuery({ id });
|
|
if (userExists) {
|
|
throw { code: 400, message: "User already exists for this id" };
|
|
}
|
|
|
|
const refreshToken = getRefreshToken();
|
|
|
|
const user = await createUser({
|
|
id,
|
|
name,
|
|
hash: hash,
|
|
refreshToken,
|
|
createdOn: dayjs().toISOString(),
|
|
});
|
|
res.status(200).json({ result: user });
|
|
} catch (error) {
|
|
console.log("createUserController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const loginController = async (req, res, next) => {
|
|
try {
|
|
const { id, password } = req.body;
|
|
const userExists = await getUserByQuery({ id });
|
|
if (!userExists) {
|
|
throw { code: 404, message: "User not found" };
|
|
}
|
|
const isPasswordValid = await bcrypt.compare(password, userExists.hash);
|
|
if (!isPasswordValid) {
|
|
throw { code: 400, message: "Invalid Password" };
|
|
}
|
|
|
|
const accessToken = getAccessToken({ id: userExists._id });
|
|
const encryptedKey = getEncryptedPayload({ id: userExists._id });
|
|
const refreshToken = getRefreshToken();
|
|
|
|
userExists.refreshToken = refreshToken;
|
|
await userExists.save();
|
|
|
|
res.status(200).json({
|
|
result: {
|
|
accessToken,
|
|
refreshToken,
|
|
encryptedKey,
|
|
name: userExists?.name,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.log("loginController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const renewAccessTokenController = async (req, res, next) => {
|
|
try {
|
|
const { refreshToken } = req.body;
|
|
const userExists = await getUserByQuery({ refreshToken });
|
|
if (!userExists) {
|
|
throw { code: 404, message: "User not found" };
|
|
}
|
|
const accessToken = getAccessToken({ id: userExists._id });
|
|
res.status(200).json({ result: accessToken });
|
|
} catch (error) {
|
|
console.log("renewAccessTokenController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const getAccessibleModulesController = async (req, res, next) => {
|
|
try {
|
|
const { user } = res.locals;
|
|
|
|
const pipeline = [
|
|
{
|
|
$match: {
|
|
user: ObjectId.createFromHexString(user.id),
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "roles",
|
|
localField: "role",
|
|
foreignField: "_id",
|
|
as: "role",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$role",
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "departments",
|
|
localField: "department",
|
|
foreignField: "_id",
|
|
as: "department",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$department",
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "designations",
|
|
localField: "designation",
|
|
foreignField: "_id",
|
|
as: "designation",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$designation",
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
];
|
|
|
|
const accessibleModules = await aggregateAccess(pipeline)
|
|
|
|
res.status(200).json({ result: accessibleModules[0] });
|
|
} catch (error) {
|
|
console.log("getAccessibleModulesController Error", error);
|
|
next(error);
|
|
}
|
|
};
|