mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
worker db edit/update refactor
This commit is contained in:
parent
e2c15f8445
commit
a6ee415c5b
4 changed files with 428 additions and 215 deletions
|
@ -14,14 +14,13 @@ import {
|
|||
logResult,
|
||||
shouldSaveDataFile,
|
||||
Result,
|
||||
isQuestionValid,
|
||||
backupData,
|
||||
shouldSearchDataFile,
|
||||
loadJSON,
|
||||
writeData,
|
||||
editDb,
|
||||
} from '../../../utils/actions'
|
||||
import {
|
||||
createQuestion,
|
||||
dataToString,
|
||||
getSubjNameWithoutYear,
|
||||
// compareQuestionObj,
|
||||
|
@ -524,7 +523,7 @@ function setup(data: SubmoduleData): any {
|
|||
logResult(req.body, resultArray, user.id, dryRun)
|
||||
|
||||
const totalNewQuestions = resultArray.reduce((acc, sres) => {
|
||||
return acc + sres.newQuestions
|
||||
return acc + sres.newQuestions.length
|
||||
}, 0)
|
||||
|
||||
res.json({
|
||||
|
@ -534,9 +533,11 @@ function setup(data: SubmoduleData): any {
|
|||
})
|
||||
|
||||
if (totalNewQuestions > 0) {
|
||||
msgAllWorker({
|
||||
qdbs: questionDbs,
|
||||
type: 'update',
|
||||
resultArray.forEach((result) => {
|
||||
msgAllWorker({
|
||||
type: 'newQuestions',
|
||||
data: result,
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -842,7 +843,6 @@ function setup(data: SubmoduleData): any {
|
|||
logger.LogReq(req)
|
||||
const user: User = req.session.user
|
||||
const date = utils.GetDateString()
|
||||
let saveDb = false
|
||||
|
||||
const editType = req.body.type
|
||||
const selectedDb = req.body.selectedDb
|
||||
|
@ -867,44 +867,28 @@ function setup(data: SubmoduleData): any {
|
|||
return
|
||||
}
|
||||
|
||||
// {
|
||||
// "index": 0,
|
||||
// "subjName": "VHDL programozás",
|
||||
// "type": "delete",
|
||||
// "selectedDb": {
|
||||
// "path": "questionDbs/elearning.uni-obuda.hu.json",
|
||||
// "name": "elearning.uni-obuda.hu"
|
||||
// }
|
||||
// }
|
||||
// -----------------
|
||||
const {
|
||||
success,
|
||||
msg,
|
||||
resultDb,
|
||||
deletedQuestion,
|
||||
newVal,
|
||||
oldVal,
|
||||
deletedQuestions,
|
||||
changedQuestions,
|
||||
} = editDb(currDb, req.body)
|
||||
|
||||
if (!success) {
|
||||
res.json({ success: success, msg: msg })
|
||||
return
|
||||
}
|
||||
if (resultDb) {
|
||||
questionDbs[dbIndex] = resultDb
|
||||
}
|
||||
|
||||
if (editType === 'delete') {
|
||||
const { index, subjName } = req.body
|
||||
let deletedQuestion = {}
|
||||
if (isNaN(index) || !subjName) {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'No .index or .subjName !',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
questionDbs[dbIndex].data = currDb.data.map((subj) => {
|
||||
if (subj.Name !== subjName) {
|
||||
return subj
|
||||
} else {
|
||||
return {
|
||||
...subj,
|
||||
Questions: subj.Questions.filter((question, i) => {
|
||||
if (index === i) {
|
||||
deletedQuestion = question
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.Log(
|
||||
`User #${user.id} deleted a question from '${subjName}'`,
|
||||
logger.GetColor('cyan')
|
||||
|
@ -914,68 +898,10 @@ function setup(data: SubmoduleData): any {
|
|||
dataEditsLog
|
||||
)
|
||||
utils.AppendToFile(JSON.stringify(deletedQuestion, null, 2), dataEditsLog)
|
||||
saveDb = true
|
||||
}
|
||||
|
||||
// {
|
||||
// "index": 0,
|
||||
// "subjName": "Elektronika",
|
||||
// "type": "edit",
|
||||
// "newVal": {
|
||||
// "Q": "Analóg műszer esetén az érzékenység az a legkisebb mennyiség, amely a műszer kijelzőjén meghatározott mértékű változást okoz.",
|
||||
// "A": "Igaz",
|
||||
// "data": {
|
||||
// "type": "simple",
|
||||
// "possibleAnswers": [
|
||||
// "Igaz"
|
||||
// ]
|
||||
// },
|
||||
// "possibleAnswers": [
|
||||
// "Igaz"
|
||||
// ]
|
||||
// },
|
||||
// "selectedDb": {
|
||||
// "path": "questionDbs/elearning.uni-obuda.hu.json",
|
||||
// "name": "elearning.uni-obuda.hu"
|
||||
// }
|
||||
// }
|
||||
if (editType === 'edit') {
|
||||
const { index, subjName, newVal } = req.body
|
||||
let oldVal = {}
|
||||
if (isNaN(index) || !subjName) {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'No .index or .subjName !',
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!isQuestionValid(newVal)) {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'edited question is not valid',
|
||||
question: newVal,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
questionDbs[dbIndex].data = currDb.data.map((subj) => {
|
||||
if (subj.Name !== subjName) {
|
||||
return subj
|
||||
} else {
|
||||
return {
|
||||
...subj,
|
||||
Questions: subj.Questions.map((question, i) => {
|
||||
if (index === i) {
|
||||
oldVal = question
|
||||
return createQuestion(newVal)
|
||||
} else {
|
||||
return question
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const { index, subjName } = req.body
|
||||
logger.Log(
|
||||
`User #${user.id} edited a question in '${subjName}'`,
|
||||
logger.GetColor('cyan')
|
||||
|
@ -995,128 +921,48 @@ function setup(data: SubmoduleData): any {
|
|||
),
|
||||
dataEditsLog
|
||||
)
|
||||
saveDb = true
|
||||
}
|
||||
|
||||
// {
|
||||
// "subjName": "Elektronika",
|
||||
// "changedQuestions": [
|
||||
// {
|
||||
// "index": 1,
|
||||
// "value": {
|
||||
// "Q": "A műszer pontosságát a hibájával fejezzük ki, melyet az osztályjel (osztálypontosság ) mutat meg.",
|
||||
// "A": "Hamis",
|
||||
// "data": {
|
||||
// "type": "simple",
|
||||
// "possibleAnswers": [
|
||||
// "Igaz",
|
||||
// "Hamis"
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// ],
|
||||
// "deletedQuestions": [
|
||||
// 0
|
||||
// ],
|
||||
// "type": "subjEdit",
|
||||
// "selectedDb": {
|
||||
// "path": "questionDbs/elearning.uni-obuda.hu.json",
|
||||
// "name": "elearning.uni-obuda.hu"
|
||||
// }
|
||||
// }
|
||||
if (editType === 'subjEdit') {
|
||||
const { subjName, changedQuestions, deletedQuestions } = req.body
|
||||
const deletedQuestionsToWrite = []
|
||||
const changedQuestionsToWrite = []
|
||||
if (
|
||||
!Array.isArray(changedQuestions) ||
|
||||
!Array.isArray(deletedQuestions)
|
||||
) {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'no changedQuestions or deletedQuestions!',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// processing changed questions
|
||||
questionDbs[dbIndex].data = currDb.data.map((subj) => {
|
||||
if (subj.Name !== subjName) {
|
||||
return subj
|
||||
} else {
|
||||
return {
|
||||
...subj,
|
||||
Questions: subj.Questions.map((question, i) => {
|
||||
const changedTo = changedQuestions.find((cq) => {
|
||||
return cq.index === i
|
||||
})
|
||||
if (changedTo) {
|
||||
changedQuestionsToWrite.push({
|
||||
oldVal: question,
|
||||
newVal: changedTo.value,
|
||||
})
|
||||
return createQuestion(changedTo.value)
|
||||
} else {
|
||||
return question
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// processing deletedQuestions
|
||||
questionDbs[dbIndex].data = currDb.data.map((subj) => {
|
||||
if (subj.Name !== subjName) {
|
||||
return subj
|
||||
} else {
|
||||
return {
|
||||
...subj,
|
||||
Questions: subj.Questions.filter((question, i) => {
|
||||
const isDeleted = deletedQuestions.includes(i)
|
||||
if (isDeleted) {
|
||||
deletedQuestionsToWrite.push(question)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const { subjName } = req.body
|
||||
logger.Log(
|
||||
`User #${user.id} modified '${subjName}'. Edited: ${deletedQuestionsToWrite.length}, deleted: ${deletedQuestionsToWrite.length}`,
|
||||
`User #${user.id} modified '${subjName}'. Edited: ${deletedQuestions.length}, deleted: ${deletedQuestions.length}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
utils.AppendToFile(
|
||||
`${date} User #${user.id} modified '${subjName}'. Edited: ${deletedQuestionsToWrite.length}, deleted: ${deletedQuestionsToWrite.length}`,
|
||||
`${date} User #${user.id} modified '${subjName}'. Edited: ${deletedQuestions.length}, deleted: ${deletedQuestions.length}`,
|
||||
dataEditsLog
|
||||
)
|
||||
utils.AppendToFile(
|
||||
JSON.stringify(
|
||||
{
|
||||
deletedQuestions: deletedQuestionsToWrite,
|
||||
changedQuestions: changedQuestionsToWrite,
|
||||
deletedQuestions: deletedQuestions,
|
||||
changedQuestions: changedQuestions,
|
||||
},
|
||||
null,
|
||||
2
|
||||
),
|
||||
dataEditsLog
|
||||
)
|
||||
saveDb = true
|
||||
}
|
||||
// ------------------
|
||||
|
||||
if (saveDb) {
|
||||
if (success) {
|
||||
writeData(currDb.data, currDb.path)
|
||||
msgAllWorker({
|
||||
qdbs: questionDbs,
|
||||
type: 'update',
|
||||
type: 'dbEdit',
|
||||
data: {
|
||||
dbIndex: dbIndex,
|
||||
edits: req.body,
|
||||
},
|
||||
}).then((res) => {
|
||||
console.log('AAAAAAAAAAA', res)
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
status: 'OK',
|
||||
success: true,
|
||||
msg: 'OK',
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue