first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Niolla
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is proprietary and confidential.
|
||||
* Unauthorized copying, modification, distribution, or use
|
||||
* of this file, via any medium, is strictly prohibited.
|
||||
*/
|
||||
|
||||
// app/services/activity.service.js
|
||||
|
||||
const activityQueue = require("../queues/activity.queue");
|
||||
|
||||
const logActivity = async ({
|
||||
user,
|
||||
description,
|
||||
type = "ACTION",
|
||||
module
|
||||
}) => {
|
||||
try {
|
||||
await activityQueue.add("log-activity", {
|
||||
user_id: user.id || user.user_id,
|
||||
username: user.firstName,
|
||||
activity_description: description,
|
||||
module: module,
|
||||
activity_type: type,
|
||||
activity_time: new Date(),
|
||||
activity_date: new Date().toISOString().split("T")[0]
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Queue push error:", err.message);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { logActivity };
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Niolla
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is proprietary and confidential.
|
||||
* Unauthorized copying, modification, distribution, or use
|
||||
* of this file, via any medium, is strictly prohibited.
|
||||
*/
|
||||
|
||||
// app/services/permission.service.js
|
||||
|
||||
const db = require("../models");
|
||||
const User = db.User;
|
||||
const RolePermission = db.rolePermission;
|
||||
const UserPermission = db.userPermission;
|
||||
|
||||
const {
|
||||
getCachedPermissions,
|
||||
setCachedPermissions
|
||||
} = require("../utils/cache.util");
|
||||
|
||||
const getEffectivePermissions = async (userId) => {
|
||||
try {
|
||||
const cached = await getCachedPermissions(userId);
|
||||
if (cached) {
|
||||
console.log("⚡ PERMISSION CACHE HIT");
|
||||
return cached;
|
||||
}
|
||||
|
||||
console.log("🐢 PERMISSION CACHE MISS");
|
||||
|
||||
const user = await User.findByPk(userId);
|
||||
if (!user) return [];
|
||||
|
||||
const rolePermissions = await RolePermission.findAll({
|
||||
where: { role_id: user.roleID },
|
||||
attributes: ["permission_id"]
|
||||
});
|
||||
|
||||
const userPermissions = await UserPermission.findAll({
|
||||
where: { user_id: userId },
|
||||
attributes: ["permission_id"]
|
||||
});
|
||||
|
||||
const map = {};
|
||||
|
||||
// role permissions
|
||||
rolePermissions.forEach((rp) => {
|
||||
map[rp.permission_id] = true;
|
||||
});
|
||||
|
||||
// user overrides (just add)
|
||||
userPermissions.forEach((up) => {
|
||||
map[up.permission_id] = true;
|
||||
});
|
||||
|
||||
const finalPermissions = Object.keys(map);
|
||||
|
||||
console.log("FINAL PERMS:", finalPermissions);
|
||||
|
||||
await setCachedPermissions(userId, finalPermissions);
|
||||
|
||||
return finalPermissions;
|
||||
|
||||
} catch (err) {
|
||||
console.error("PERMISSION SERVICE ERROR:", err);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getEffectivePermissions };
|
||||
Reference in New Issue
Block a user