added / fixed some types

This commit is contained in:
mrfry 2022-03-14 19:35:42 +01:00
parent 5f12284bb8
commit bc5c293539
41 changed files with 4378 additions and 8304 deletions

View file

@ -21,11 +21,15 @@ const recDataFile = './stats/recdata'
const dataLockFile = './data/lockData'
import logger from '../utils/logger'
import { createQuestion } from '../utils/classes'
import {
createQuestion,
WorkerResult,
SearchResultQuestion,
} from '../utils/classes'
import { doALongTask } from './workerPool'
import idStats from '../utils/ids'
import utils from '../utils/utils'
import { SearchResult, addQuestion, getSubjNameWithoutYear } from './classes'
import { addQuestion, getSubjNameWithoutYear } from './classes'
// types
import {
@ -152,12 +156,12 @@ function processIncomingRequestUsingDb(
): Promise<Result> {
return new Promise((resolve, reject) => {
try {
const recievedQuestions = []
const recievedQuestions: Question[] = []
logger.DebugLog('recievedData JSON parsed', 'isadding', 1)
logger.DebugLog(recievedData, 'isadding', 3)
const allQLength = recievedData.quiz.length
const questionSearchPromises = []
const questionSearchPromises: Promise<WorkerResult>[] = []
recievedData.quiz.forEach((question) => {
logger.DebugLog('Question:', 'isadding', 2)
logger.DebugLog(question, 'isadding', 2)
@ -193,10 +197,10 @@ function processIncomingRequestUsingDb(
})
Promise.all(questionSearchPromises)
.then((results: Array<SearchResult>) => {
const allQuestions = [] // all new questions here that do not have result
results.forEach((result, i) => {
const add = result.result.every((res) => {
.then((results: Array<WorkerResult>) => {
const allQuestions: Question[] = [] // all new questions here that do not have result
results.forEach((result: WorkerResult, i) => {
const add = result.result.every((res: SearchResultQuestion) => {
return res.match < minMatchAmmountToAdd
})
if (add) {
@ -216,7 +220,10 @@ function processIncomingRequestUsingDb(
logger.DebugLog(currentQuestion, 'isadding', 3)
addQuestion(qdb.data, sName, {
...currentQuestion,
date: new Date(),
data: {
...currentQuestion.data,
date: new Date().getTime(),
},
})
})
@ -279,7 +286,7 @@ function processIncomingRequestUsingDb(
})
}
export function isQuestionValid(question: Question): Boolean {
export function isQuestionValid(question: Question): boolean {
if (!question.Q) {
return false
}
@ -292,8 +299,8 @@ export function isQuestionValid(question: Question): Boolean {
export function shouldSearchDataFile(
df: DataFile,
testUrl: string,
trueIfAlways?: Boolean
): Boolean {
trueIfAlways?: boolean
): boolean {
if (
typeof df.shouldSearch === 'string' &&
df.shouldSearch === 'always' &&
@ -316,7 +323,7 @@ export function shouldSearchDataFile(
export function shouldSaveDataFile(
df: DataFile,
recievedData: RecievedData
): Boolean {
): boolean {
if (df.locked) {
return false
}
@ -348,17 +355,14 @@ export function shouldSaveDataFile(
}
export function loadData(path: string): Array<Subject> {
return JSON.parse(utils.ReadFile(path)).reduce((acc, subj) => {
return [
...acc,
{
Name: subj.Name,
Questions: subj.Questions.map((question) => {
return createQuestion(question)
}),
},
]
}, [])
return JSON.parse(utils.ReadFile(path)).map((subj: Subject) => {
return {
Name: subj.Name,
Questions: subj.Questions.map((question: Question) => {
return createQuestion(question)
}),
}
})
}
export function loadJSON(
@ -455,7 +459,7 @@ function deleteFromDb(
selectedDb: { path: string; name: string }
}
): {
success: Boolean
success: boolean
msg: string
deletedQuestion?: Question
resultDb?: QuestionDb
@ -470,7 +474,7 @@ function deleteFromDb(
// }
// }
const { index, subjName } = edits
let deletedQuestion
let deletedQuestion: Question
if (isNaN(index) || !subjName) {
return {
success: false,
@ -511,10 +515,10 @@ function editQuestionInDb(
subjName: string
type: string
selectedDb: { path: string; name: string }
newVal?: Question
newVal: Question
}
): {
success: Boolean
success: boolean
msg: string
newVal?: Question
oldVal?: Question
@ -543,7 +547,8 @@ function editQuestionInDb(
// }
// }
const { index, subjName, newVal } = edits
let oldVal
let oldVal: Question
if (isNaN(index) || !subjName) {
return {
success: false,
@ -598,10 +603,10 @@ function editSubjInDb(
}>
}
): {
success: Boolean
success: boolean
msg: string
deletedQuestions?: Array<Question>
changedQuestions?: Array<Question>
changedQuestions?: { oldVal: Question; newVal: Question }[]
resultDb?: QuestionDb
} {
// {
@ -632,8 +637,8 @@ function editSubjInDb(
// }
// }
const { subjName, changedQuestions, deletedQuestions } = edits
const deletedQuestionsToWrite = []
const changedQuestionsToWrite = []
const deletedQuestionsToWrite: Question[] = []
const changedQuestionsToWrite: { oldVal: Question; newVal: Question }[] = []
if (!Array.isArray(changedQuestions) || !Array.isArray(deletedQuestions)) {
return {
success: false,
@ -695,29 +700,32 @@ function editSubjInDb(
}
}
// FIXME: newVal is optional in some places but not in others
export interface Edits {
index: number
subjName: string
selectedDb: { path: string; name: string }
type: string
newVal: Question
deletedQuestion?: Array<number>
changedQuestions?: Array<{
index: number
value: Question
}>
}
export function editDb(
questionDb: QuestionDb,
edits: {
index: number
subjName: string
selectedDb: { path: string; name: string }
type: string
newVal?: Question
deletedQuestion?: Array<number>
changedQuestions?: Array<{
index: number
value: Question
}>
}
edits: Edits
): {
success: Boolean
success: boolean
msg: string
resultDb?: QuestionDb
deletedQuestion?: Question
newVal?: Question
oldVal?: Question
deletedQuestions?: Array<Question>
changedQuestions?: Array<Question>
changedQuestions?: { oldVal: Question; newVal: Question }[]
} {
if (edits.type === 'delete') {
return deleteFromDb(questionDb, edits)
@ -729,4 +737,9 @@ export function editDb(
if (edits.type === 'subjEdit') {
return editSubjInDb(questionDb, edits)
}
return {
success: false,
msg: 'DB edit error, no matched type',
}
}