mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Domain specific data files
This commit is contained in:
parent
81577ad621
commit
f87e165084
9 changed files with 174 additions and 71 deletions
|
@ -43,6 +43,7 @@ export interface RecievedData {
|
|||
id: string
|
||||
version: string
|
||||
scriptVersion: string
|
||||
location: string
|
||||
}
|
||||
|
||||
export interface Result {
|
||||
|
@ -86,7 +87,7 @@ export function logResult(
|
|||
logger.Log('\t' + msg, color)
|
||||
})
|
||||
} else {
|
||||
logger.Log('\tNo db-s passed shouldSave!', logger.GetColor('red'))
|
||||
logger.Log('\tResults length is zero!', logger.GetColor('red'))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,9 +131,7 @@ export function processIncomingRequest(
|
|||
|
||||
// FIXME: this many promises and stuff might be unnecesarry
|
||||
const promises: Array<Promise<Result>> = questionDbs.reduce((acc, qdb) => {
|
||||
if (qdb.shouldSave(recievedData)) {
|
||||
acc.push(processIncomingRequestUsingDb(recievedData, qdb, dryRun, user))
|
||||
}
|
||||
acc.push(processIncomingRequestUsingDb(recievedData, qdb, dryRun, user))
|
||||
return acc
|
||||
}, [])
|
||||
return Promise.all(promises)
|
||||
|
@ -262,17 +261,53 @@ function processIncomingRequestUsingDb(
|
|||
})
|
||||
}
|
||||
|
||||
export function loadJSON(dataFiles: Array<DataFile>): Array<QuestionDb> {
|
||||
export function shouldSaveDataFile(
|
||||
df: DataFile,
|
||||
recievedData: RecievedData
|
||||
): Boolean {
|
||||
if (df.shouldSave.version) {
|
||||
const { compare, val } = df.shouldSave.version
|
||||
if (compare === 'equals') {
|
||||
return recievedData.version === val
|
||||
} else if (compare === 'startsWith') {
|
||||
return recievedData.version.startsWith(val)
|
||||
}
|
||||
}
|
||||
|
||||
if (df.shouldSave.version) {
|
||||
const { compare, val } = df.shouldSave.version
|
||||
if (compare === 'equals') {
|
||||
return recievedData.version === val
|
||||
} else if (compare === 'startsWith') {
|
||||
return recievedData.version.startsWith(val)
|
||||
}
|
||||
}
|
||||
|
||||
if (df.shouldSave.location) {
|
||||
const { val } = df.shouldSave.location
|
||||
return recievedData.location.includes(val)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export function loadJSON(
|
||||
dataFiles: Array<DataFile>,
|
||||
dataDir: string
|
||||
): Array<QuestionDb> {
|
||||
return dataFiles.reduce((acc, dataFile, index) => {
|
||||
if (!utils.FileExists(dataFile.path)) {
|
||||
utils.WriteFile(JSON.stringify([]), dataFile.path)
|
||||
const dataPath = dataDir + dataFile.path
|
||||
|
||||
if (!utils.FileExists(dataPath)) {
|
||||
utils.WriteFile(JSON.stringify([]), dataPath)
|
||||
}
|
||||
|
||||
try {
|
||||
acc.push({
|
||||
...dataFile,
|
||||
path: dataPath,
|
||||
index: index,
|
||||
data: JSON.parse(utils.ReadFile(dataFile.path)),
|
||||
data: JSON.parse(utils.ReadFile(dataPath)),
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
|
@ -305,12 +340,3 @@ export function backupData(questionDbs: Array<QuestionDb>): void {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function getQuestionDbsWithoutFunct(
|
||||
questionDbs: Array<QuestionDb> // FIXME: type for dis
|
||||
): Array<any> {
|
||||
return questionDbs.map((qdb) => {
|
||||
const { shouldSave, ...res } = qdb // eslint-disable-line
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue