mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Date string update, midnight timer update, other stuff
This commit is contained in:
parent
c764c4f402
commit
dbb23a6724
6 changed files with 92 additions and 61 deletions
|
@ -45,8 +45,12 @@ const passwordFile = 'data/dataEditorPasswords.json'
|
|||
const dataEditsLog = 'stats/dataEdits'
|
||||
const dailyDataCountFile = 'stats/dailyDataCount'
|
||||
const usersDBPath = 'data/dbs/users.db'
|
||||
const usersDbBackupPath = 'data/dbs/backup'
|
||||
|
||||
const maxVeteranPwGetCount = 5
|
||||
const addPWPerDay = 3 // every x day a user can give a pw
|
||||
const maxPWCount = 2 // maximum pw give opportunities a user can have at once
|
||||
const daysAfterUserGetsPWs = 2 // days after user gets pw-s
|
||||
|
||||
if (!utils.FileExists(usersDBPath)) {
|
||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||
|
@ -137,7 +141,7 @@ app.post('/getpw', function (req, res) {
|
|||
const pw = uuidv4()
|
||||
const insertRes = dbtools.Insert(authDB, 'users', {
|
||||
pw: pw,
|
||||
created: new Date().toString()
|
||||
created: utils.GetDateString()
|
||||
})
|
||||
|
||||
logger.Log(`User #${requestingUser.id} creted new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan'))
|
||||
|
@ -170,7 +174,7 @@ app.post('/getveteranpw', function (req, res) {
|
|||
} else {
|
||||
dbtools.Update(authDB, 'veteranPWRequests', {
|
||||
count: tries.count + 1,
|
||||
lastDate: new Date().toString()
|
||||
lastDate: utils.GetDateString()
|
||||
}, {
|
||||
id: tries.id
|
||||
})
|
||||
|
@ -178,7 +182,7 @@ app.post('/getveteranpw', function (req, res) {
|
|||
} else {
|
||||
dbtools.Insert(authDB, 'veteranPWRequests', {
|
||||
ip: ip,
|
||||
lastDate: new Date().toString()
|
||||
lastDate: utils.GetDateString()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -253,7 +257,7 @@ app.post('/login', (req, res) => {
|
|||
dbtools.Update(authDB, 'users', {
|
||||
loginCount: user.loginCount + 1,
|
||||
lastIP: ip,
|
||||
lastLogin: new Date().toString()
|
||||
lastLogin: utils.GetDateString()
|
||||
}, {
|
||||
id: user.id
|
||||
})
|
||||
|
@ -262,7 +266,7 @@ app.post('/login', (req, res) => {
|
|||
id: sessionID,
|
||||
ip: ip,
|
||||
userID: user.id,
|
||||
createDate: new Date().toString()
|
||||
createDate: utils.GetDateString()
|
||||
})
|
||||
|
||||
// TODO: cookie age
|
||||
|
@ -315,7 +319,7 @@ app.post('/postfeedbackfile', function (req, res) {
|
|||
app.post('/postfeedback', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
|
||||
utils.AppendToFile(logger.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
|
||||
utils.AppendToFile(utils.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
|
||||
res.json({ success: true })
|
||||
})
|
||||
|
||||
|
@ -403,7 +407,7 @@ app.post('/uploaddata', (req, res) => {
|
|||
// returning if user password is not ok
|
||||
if (!user) {
|
||||
logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red'))
|
||||
utils.AppendToFile(logger.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||
utils.AppendToFile(utils.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||
res.json({ status: respStatuses.invalidPass })
|
||||
return
|
||||
}
|
||||
|
@ -411,10 +415,10 @@ app.post('/uploaddata', (req, res) => {
|
|||
logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg'))
|
||||
logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg'))
|
||||
// saving detailed editedCount
|
||||
utils.AppendToFile(logger.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||
utils.AppendToFile(utils.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||
|
||||
// making backup
|
||||
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${new Date().toString().replace(/ /g, '_')}`)
|
||||
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${utils.GetDateString().replace(/ /g, '_')}`)
|
||||
logger.Log('Backup made')
|
||||
// writing data
|
||||
utils.WriteFile(JSON.stringify(newData), dataFile)
|
||||
|
@ -550,8 +554,9 @@ app.post('*', function (req, res) {
|
|||
})
|
||||
|
||||
function ExportDailyDataCount () {
|
||||
logger.Log('Saving daily data count ...')
|
||||
utils.AppendToFile(JSON.stringify({
|
||||
date: new Date(),
|
||||
date: utils.GetDateString(),
|
||||
subjectCount: data.Subjects.length,
|
||||
questionCOunt: data.Subjects.reduce((acc, subj) => {
|
||||
return acc + subj.Questions.length
|
||||
|
@ -560,6 +565,47 @@ function ExportDailyDataCount () {
|
|||
}), dailyDataCountFile)
|
||||
}
|
||||
|
||||
function BackupDB () {
|
||||
logger.Log('Backing up auth DB ...')
|
||||
utils.CreatePath(usersDbBackupPath, true)
|
||||
authDB.backup(`${usersDbBackupPath}/users.${utils.GetDateString().replace(/ /g, '_')}.db`)
|
||||
.then(() => {
|
||||
logger.Log('Auth DB backup complete!')
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.Log('Auth DB backup failed!', logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
function IncrementAvaiblePWs () {
|
||||
const users = dbtools.SelectAll(authDB, 'users')
|
||||
const today = new Date()
|
||||
const getDayDiff = (dateString) => {
|
||||
let msdiff = today - new Date(dateString)
|
||||
return Math.floor(msdiff / (1000 * 3600 * 24))
|
||||
}
|
||||
|
||||
users.forEach((u) => {
|
||||
if (u.avaiblePWRequests >= maxPWCount) {
|
||||
return
|
||||
}
|
||||
|
||||
const dayDiff = getDayDiff(u.created)
|
||||
if (dayDiff < daysAfterUserGetsPWs) {
|
||||
return
|
||||
}
|
||||
|
||||
if (dayDiff % addPWPerDay === 0) {
|
||||
dbtools.Update(authDB, 'users', {
|
||||
avaiblePWRequests: u.avaiblePWRequests + 1
|
||||
}, {
|
||||
id: u.id
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports.app = app
|
||||
exports.cleanup = () => {
|
||||
logger.Log('Closing Auth DB')
|
||||
|
@ -567,8 +613,8 @@ exports.cleanup = () => {
|
|||
}
|
||||
exports.dailyAction = () => {
|
||||
ExportDailyDataCount()
|
||||
|
||||
// TODO: selectAll from users, check if date is more than x, and increment every y
|
||||
BackupDB()
|
||||
IncrementAvaiblePWs()
|
||||
}
|
||||
|
||||
logger.Log('API module started', logger.GetColor('yellow'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue