Refactor license and permission management, add services

Renamed licenceRepository.js to licenseRepository.js and updated its API to use status instead of userId for licenses. Moved permission checking logic from userRepository to permissionsRepository. Added new service layers for license, permissions, and user management, implementing error handling and business logic. Removed the old register.js service and cleaned up test.js. Updated database schema to remove userId from licenses.
This commit is contained in:
2026-01-27 05:49:50 +01:00
parent 1e78bd68cf
commit f8c1340b41
9 changed files with 243 additions and 69 deletions

View File

@@ -5,13 +5,10 @@ function create(key, productId, targetChannel) {
INSERT INTO licenses (
key,
productId,
targetChannel
)
VALUES (
?,
?,
?
targetChannel,
status
)
VALUES (?, ?, ?, 'active')
`
const statement = database.prepare(query)
return statement.run(key, productId, targetChannel)
@@ -19,7 +16,7 @@ function create(key, productId, targetChannel) {
function findByKey(key) {
const query = `
SELECT id, key, productId, targetChannel, userId, usedAt
SELECT id, key, productId, targetChannel, status, createdAt
FROM licenses
WHERE key = ?
`
@@ -27,24 +24,14 @@ function findByKey(key) {
return statement.get(key)
}
function activate(licenseId, userId) {
function updateStatus(id, status) {
const query = `
UPDATE licenses
SET userId = ?, usedAt = CURRENT_TIMESTAMP
WHERE id = ? AND userId IS NULL
SET status = ?
WHERE id = ?
`
const statement = database.prepare(query)
return statement.run(userId, licenseId)
}
function deactivate(licenseId, userId) {
const query = `
UPDATE licenses
SET userId = ?, usedAt = NULL
WHERE id = ? AND userId IS NULL
`
const statement = database.prepare(query)
return statement.run(userId, licenseId)
return statement.run(status, id)
}
function findByProduct(productId) {
@@ -68,8 +55,7 @@ function remove(id) {
module.exports = {
create,
remove,
activate,
findByKey,
deactivate,
updateStatus,
findByProduct,
}

View File

@@ -53,9 +53,26 @@ function revokeAllOnProduct(userId, productId) {
return statement.run(userId, productId)
}
function hasPermission(userId, productName, channel, permission) {
const query = `
SELECT 1 FROM permissions p
JOIN products pr ON p.product_id = pr.id
WHERE p.user_id = ?
AND pr.name = ?
AND (p.channel = ? OR p.channel = '*')
AND p.permission_key = ?
`
const stmt = db.prepare(query)
const result = stmt.get(userId, productName, channel, permission)
return !!result
}
module.exports = {
grant,
revoke,
hasPermission,
revokeAllOnProduct,
findByUserAndProduct,
revokeAllOnProduct
};
}

View File

@@ -59,27 +59,10 @@ function findById(identifier) {
return statement.get(identifier)
}
function hasPermission(userId, productName, channel, permission) {
const query = `
SELECT 1 FROM permissions p
JOIN products pr ON p.product_id = pr.id
WHERE p.user_id = ?
AND pr.name = ?
AND (p.channel = ? OR p.channel = '*')
AND p.permission_key = ?
`
const stmt = db.prepare(query)
const result = stmt.get(userId, productName, channel, permission)
return !!result
}
module.exports = {
remove,
register,
findById,
hasPermission,
findByUsername,
changePassword,
}