feat: add env-based super admin authentication and complete role & permission system
deploy-chabokan / deploy (push) Canceled after 0s
deploy-chabokan / deploy (push) Canceled after 0s
This commit is contained in:
@@ -1,8 +1,52 @@
|
||||
const Permission = require('./permissionModel')
|
||||
const permissionsList = require('./permissionList')
|
||||
const AppError = require('../../globalErrorHandler/AppError')
|
||||
|
||||
module.exports.syncPermissions = async () => {
|
||||
try {
|
||||
let createdCount = 0
|
||||
let updatedCount = 0
|
||||
|
||||
for (const item of permissionsList) {
|
||||
const existing = await Permission.findOne({ key: item.key })
|
||||
if (!existing) {
|
||||
await Permission.create({
|
||||
englishName: item.englishName,
|
||||
nativeName: item.nativeName,
|
||||
key: item.key,
|
||||
category: item.category,
|
||||
isActive: true,
|
||||
})
|
||||
createdCount++
|
||||
} else {
|
||||
let changed = false
|
||||
if (existing.englishName !== item.englishName) {
|
||||
existing.englishName = item.englishName
|
||||
changed = true
|
||||
}
|
||||
if (existing.nativeName !== item.nativeName) {
|
||||
existing.nativeName = item.nativeName
|
||||
changed = true
|
||||
}
|
||||
if (existing.category !== item.category) {
|
||||
existing.category = item.category
|
||||
changed = true
|
||||
}
|
||||
if (changed) {
|
||||
await existing.save()
|
||||
updatedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[Permissions] Sync complete: ${createdCount} created, ${updatedCount} updated.`)
|
||||
} catch (error) {
|
||||
console.error('[Permissions] Error during sync:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.getAllPermissions = async () => {
|
||||
return Permission.find({})
|
||||
return Permission.find({}).sort({ category: 1, englishName: 1 })
|
||||
}
|
||||
|
||||
module.exports.createPermission = async (data) => {
|
||||
|
||||
Reference in New Issue
Block a user