Initialized Project with default middlewares and helpers, connected to Atlas Instance and created Swagger Setup. Created necessary DB models and authentication-authorization logic
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { createAccess } from "../models/Access/operations.js";
|
||||
import { createRole, getAllRoles } from "../models/Role/operations.js";
|
||||
import {
|
||||
createDesignation,
|
||||
getAllDesignations,
|
||||
} from "../models/Designation/operations.js";
|
||||
import {
|
||||
createDepartment,
|
||||
getAllDepartments,
|
||||
} from "../models/Department/operations.js";
|
||||
import { createBranch, getAllBranches } from "../models/Branch/operations.js";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export const createAccessController = async (req, res) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const { employee, designation, department, branch, role } = req.body;
|
||||
const access = await createAccess({
|
||||
user: employee,
|
||||
designation,
|
||||
department,
|
||||
branch,
|
||||
role,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(200).json({ result: access });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllRolesController = async (req, res) => {
|
||||
try {
|
||||
const roles = await getAllRoles();
|
||||
res.status(200).json({ result: roles });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllDesignationsController = async (req, res) => {
|
||||
try {
|
||||
const designations = await getAllDesignations();
|
||||
res.status(200).json({ result: designations });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllDepartmentsController = async (req, res) => {
|
||||
try {
|
||||
const departments = await getAllDepartments();
|
||||
res.status(200).json({ result: departments });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllBranchesController = async (req, res) => {
|
||||
try {
|
||||
const branches = await getAllBranches();
|
||||
res.status(200).json({ result: branches });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createRoleController = async (req, res) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const role = await createRole({
|
||||
...req.body,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(200).json({ result: role });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createDesignationController = async (req, res) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const designation = await createDesignation({
|
||||
...req.body,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(200).json({ result: designation });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createDepartmentController = async (req, res) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const department = await createDepartment({
|
||||
...req.body,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(200).json({ result: department });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createBranchController = async (req, res) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const branch = await createBranch({
|
||||
...req.body,
|
||||
createdBy: user.id,
|
||||
createdOn: dayjs().toISOString(),
|
||||
});
|
||||
res.status(200).json({ result: branch });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
createProfile,
|
||||
getProfileByQuery,
|
||||
updateProfile,
|
||||
} from "../models/Profile/operations.js";
|
||||
|
||||
export const createOrEditProfileController = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
|
||||
const profile = await updateProfile(user.id, req.body, {
|
||||
new: true,
|
||||
upsert: true,
|
||||
});
|
||||
|
||||
res.status(200).json({ result: profile });
|
||||
} catch (error) {
|
||||
console.log("createProfileController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getProfileController = async (req, res, next) => {
|
||||
try {
|
||||
const { user } = res.locals;
|
||||
const profile = await getProfileByQuery({ userId: user.id });
|
||||
res.status(200).json({ result: profile });
|
||||
} catch (error) {
|
||||
console.log("getProfileController Error", error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { createUser, getUserByQuery } from "../models/User/operations.js";
|
||||
import bcrypt from "bcrypt";
|
||||
import dayjs from "dayjs";
|
||||
import {
|
||||
getAccessToken,
|
||||
getRefreshToken,
|
||||
} from "../middlewares/jwt.middleware.js";
|
||||
|
||||
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 refreshToken = getRefreshToken();
|
||||
|
||||
userExists.refreshToken = refreshToken;
|
||||
await userExists.save();
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.json({ result: { accessToken, refreshToken, 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
import { User, Access, Profile, Team } from "../models/index.js";
|
||||
Reference in New Issue
Block a user