Init commit

This commit is contained in:
2024-12-22 16:52:30 +03:30
parent 28ab698dfc
commit aeb38982ca
28 changed files with 1532 additions and 68 deletions
+52
View File
@@ -0,0 +1,52 @@
const catchAsync = require('../../globalErrorHandler/catchAsync')
const AppError = require('../../globalErrorHandler/AppError')
const Role = require('./roleModel')
const roleService = require('./roleService')
exports.getAllRoles = catchAsync(async (req, res, next) => {
const roles = await roleService.getAllRoles()
res.status(200).json({
status: 'success',
results: roles.length,
data: roles,
})
})
exports.createRole = catchAsync(async (req, res, next) => {
const role = await roleService.createRole(
req.body.englishName,
req.body.persianName
)
res.status(201).json({
status: 'success',
data: role,
})
})
exports.getRole = catchAsync(async (req, res, next) => {
const role = await roleService.getRole(req.body.id)
res.status(200).json({
status: 'success',
data: role,
})
})
exports.updateRole = catchAsync(async (req, res, next) => {
const updateRole = await roleService.updateRole(data)
res.status(200).json({
status: 'success',
data: role,
})
})
exports.deleteRole = catchAsync(async (req, res, next) => {
await roleService.deleteRole(req.body.id)
res.status(204).json({
status: 'success',
data: null,
})
})
+39
View File
@@ -0,0 +1,39 @@
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const roleSchema = new mongoose.Schema(
{
englishName: {
type: String,
unique: true,
required: true,
},
nativeName: {
type: String,
required: [
true,
'A role must have a equivalent in Persian language',
],
},
permissions: [
{
type: ObjectID,
ref: 'Permission',
},
],
isActive: {
type: Boolean,
default: true,
select: false,
},
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
timestamps: true,
}
)
const Role = mongoose.model('Role', roleSchema)
module.exports = Role
+20
View File
@@ -0,0 +1,20 @@
const express = require('express')
const roleController = require('./roleController')
const perm = require('../../middlewares/permissionMiddleware')
const loginCheck = require('../../middlewares/loginCheck')
const router = new express.Router()
router.use(loginCheck.loginProtect)
router.get('/admin/get-all', perm.requires('getallroles'), roleController.getAllRoles)
router.post('/admin/create', perm.requires('createarole'), roleController.createRole)
router.get('/admin/get-one/:roleId', perm.requires('getarole'), roleController.getRole)
router.put('/admin/update/:roleId', perm.requires('updatearole'), roleController.updateRole)
router.delete('/admin/delete/:roleId', perm.requires('deletearole'), roleController.deleteRole)
module.exports = router
+36
View File
@@ -0,0 +1,36 @@
const Role = require('./roleModel')
const chalk = require('chalk')
const runSeeder = async () => {
const roles = [
{
_id: '66a0144569a37efa10d13628',
englishName: 'Admin',
nativeName: 'ادمین',
permissions: ['66a014057b80c4788e41aab7'],
isActive: true,
},
{
_id: '66a2da5b9b989a8909948848',
englishName: 'SuperAdmin',
nativeName: 'ادمین اصلی',
permissions: [],
isActive: true,
},
{
_id: '66a0144569a37efa10d13629',
englishName: 'User',
nativeName: 'کاربر',
permissions: [],
isActive: true,
},
]
await Role.deleteMany({})
console.log(chalk.red('Deleted all role records.'))
await Role.insertMany(roles, null)
console.log(chalk.green.bold(`ROLE: Created ${roles.length} role(s)`))
}
module.exports = { runSeeder }
+40
View File
@@ -0,0 +1,40 @@
const Role = require('./roleModel')
const AppError = require('../../globalErrorHandler/AppError')
module.exports.getAllRoles = async () => {
return Role.find({}, null, null)
}
module.exports.createRole = async (data) => {
return Role.create(data, null)
}
module.exports.getRole = async (roleId) => {
const role = await Role.findById(roleId)
if (!role) {
throw new AppError('NoRoleFound', 404, {})
}
return role
}
module.exports.updateRole = async (roleId, data) => {
const role = await Role.findOneAndUpdate({ _id: roleId},data, null)
if (!role) {
throw new AppError('NoRoleFound', 404, {})
}
return role
}
module.exports.deleteRole = async (roleId) => {
const role = await Role.findByIdAndUpdate(roleId, { isActive: false }, null)
if (!role) {
throw new AppError('NoRoleFound', 404, {})
}
return null
}
View File