generated from azures04/Base-REST-API
Introduces a new SQLite database setup using better-sqlite3, with initialization scripts and repository modules for users, permissions, and licenses. Removes legacy user and register routes and related test files. Updates dependencies to include better-sqlite3, bcryptjs, and ftp-srv.
75 lines
1.7 KiB
JavaScript
75 lines
1.7 KiB
JavaScript
const database = require("../modules/database")
|
|
|
|
function create(key, productId, targetChannel) {
|
|
const query = `
|
|
INSERT INTO licenses (
|
|
key,
|
|
productId,
|
|
targetChannel
|
|
)
|
|
VALUES (
|
|
?,
|
|
?,
|
|
?
|
|
)
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(key, productId, targetChannel)
|
|
}
|
|
|
|
function findByKey(key) {
|
|
const query = `
|
|
SELECT id, key, productId, targetChannel, userId, usedAt
|
|
FROM licenses
|
|
WHERE key = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(key)
|
|
}
|
|
|
|
function activate(licenseId, userId) {
|
|
const query = `
|
|
UPDATE licenses
|
|
SET userId = ?, usedAt = CURRENT_TIMESTAMP
|
|
WHERE id = ? AND userId IS NULL
|
|
`
|
|
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)
|
|
}
|
|
|
|
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,
|
|
activate,
|
|
findByKey,
|
|
deactivate,
|
|
findByProduct,
|
|
} |