generated from azures04/Base-REST-API
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.
61 lines
1.3 KiB
JavaScript
61 lines
1.3 KiB
JavaScript
const database = require("../modules/database")
|
|
|
|
function create(key, productId, targetChannel) {
|
|
const query = `
|
|
INSERT INTO licenses (
|
|
key,
|
|
productId,
|
|
targetChannel,
|
|
status
|
|
)
|
|
VALUES (?, ?, ?, 'active')
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(key, productId, targetChannel)
|
|
}
|
|
|
|
function findByKey(key) {
|
|
const query = `
|
|
SELECT id, key, productId, targetChannel, status, createdAt
|
|
FROM licenses
|
|
WHERE key = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(key)
|
|
}
|
|
|
|
function updateStatus(id, status) {
|
|
const query = `
|
|
UPDATE licenses
|
|
SET status = ?
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(status, id)
|
|
}
|
|
|
|
function findByProduct(productId) {
|
|
const query = `
|
|
SELECT * FROM licenses
|
|
WHERE productId = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.all(productId)
|
|
}
|
|
|
|
function remove(id) {
|
|
const query = `
|
|
DELETE FROM licenses
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(id)
|
|
}
|
|
|
|
module.exports = {
|
|
create,
|
|
remove,
|
|
findByKey,
|
|
updateStatus,
|
|
findByProduct,
|
|
} |