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
@@ -0,0 +1,52 @@
const catchAsync = require('../../globalErrorHandler/catchAsync')
const AppError = require('../../globalErrorHandler/AppError')
const Permission = require('./permissionModel')
const permissionService = require('./permissionService')
exports.getAllPermissions = catchAsync(async (req, res, next) => {
const permissions = await permissionService.getAllPermissions()
res.status(200).json({
status: 'success',
results: permissions.length,
data: permissions,
})
})
exports.createPermission = catchAsync(async (req, res, next) => {
const permission = await permissionService.createPermission(
req.body.englishName,
req.body.persianName
)
res.status(201).json({
status: 'success',
data: permission,
})
})
exports.getPermission = catchAsync(async (req, res, next) => {
const permission = await permissionService.getPermission(req.params.id)
res.status(200).json({
status: 'success',
data: permission,
})
})
exports.updatePermission = catchAsync(async (req, res, next) => {
const updatePermission = await permissionService.updatePermission(data)
res.status(200).json({
status: 'success',
data: permission,
})
})
exports.deletePermission = catchAsync(async (req, res, next) => {
await permissionService.deletePermission(req.params.id)
res.status(204).json({
status: 'success',
data: null,
})
})
+33
View File
@@ -0,0 +1,33 @@
const mongoose = require('mongoose')
const permissionSchema = new mongoose.Schema({
englishName: {
type: String,
required: [true, 'A permission must have a English name'],
},
nativeName: {
type: String,
required: [true, 'A permission must have a Persian name'],
},
key: {
type: String,
required: [true, 'Missing the key'],
unique: true,
},
isActive: {
type: Boolean,
default: true,
select: false,
},
category: {
type: String,
},
})
permissionSchema.pre(/^find/, function (next) {
this.find({ active: { $ne: false } })
next()
})
const Permission = mongoose.model('Permission', permissionSchema)
module.exports = Permission
+40
View File
@@ -0,0 +1,40 @@
const express = require('express')
const permissionController = require('./permissionController')
const perm = require('../../middlewares/permissionMiddleware')
const loginCheck = require('../../middlewares/loginCheck')
const router = new express.Router()
router.use(loginCheck.loginProtect)
router.get(
'/',
perm.requires('getallpermissions'),
permissionController.getAllPermissions
)
router.post(
'/',
perm.requires('createpermission'),
permissionController.createPermission
)
router.get(
'/:id',
perm.requires('getpermission'),
permissionController.getPermission
)
router.put(
'/:id',
perm.requires('updatepermission'),
permissionController.updatePermission
)
router.delete(
'/:id',
perm.requires('deletepermission'),
permissionController.deletePermission
)
module.exports = router
+18
View File
@@ -0,0 +1,18 @@
const Permission = require('./permissionModel')
const chalk = require('chalk')
const runSeeder = async () => {
const permissions = []
await Permission.deleteMany({})
console.log(chalk.red('Deleted all permission records.'))
await Permission.insertMany(permissions, null)
console.log(
chalk.green.bold(
`PERMISSION: Created ${permissions.length} permission(s)`
)
)
}
module.exports = { runSeeder }
@@ -0,0 +1,39 @@
const Permission = require('./permissionModel')
const AppError = require('../../globalErrorHandler/AppError')
module.exports.getAllPermissions = async () => {
return Permission.find({})
}
module.exports.createPermission = async (englishName, persianName) => {
return await Permission.create({
englishName,
persianName,
})
}
module.exports.getPermission = async (permissionId) => {
const permission = await Permission.findById(permissionId)
if (!permission) {
throw AppError('NoPermissionFound', 404, {})
}
return permission
}
module.exports.updatePermission = async (data) => {
const permission = await Permission.findOneAndUpdate(data)
if (!permission) {
throw AppError('NoPermissionFound', 404, {})
}
return permission
}
module.exports.deletePermission = async (permissionId) => {
await Permission.findOneAndDelete({ _id: permissionId })
return null
}