Add lastUpdated and createdOn if missing on import/migrate

This commit is contained in:
Robbie Antenesse 2019-05-23 14:42:09 -06:00
parent 569a0b9fc5
commit f6a6a8cc0b
2 changed files with 22 additions and 1 deletions

View File

@ -133,6 +133,24 @@ export function importDictionary() {
const textFromFileLoaded = fileLoadedEvent.target.result;
const importedDictionary = JSON.parse(textFromFileLoaded);
if (importedDictionary && importedDictionary.hasOwnProperty('words')) {
const timestamp = getTimestampInSeconds();
if (!importedDictionary.hasOwnProperty('createdOn')) {
importedDictionary.createdOn = timestamp;
}
if (importedDictionary.words.some(word => !word.hasOwnProperty('createdOn'))) {
importedDictionary.words.forEach(word => {
if (!word.hasOwnProperty('createdOn')) {
word.createdOn = timestamp;
}
if (!word.hasOwnProperty('lastUpdated')) {
word.lastUpdated = timestamp;
}
});
}
if (importedDictionary.hasOwnProperty('externalID')) {
delete importedDictionary.externalID;
}
window.currentDictionary = importedDictionary;
saveDictionary();
renderAll();
@ -252,11 +270,14 @@ export function migrateDictionary() {
if (!window.currentDictionary.hasOwnProperty('version')) {
const fixStupidOldNonsense = string => string.replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&#92;/g, '\\').replace(/<br>/g, '\n');
window.currentDictionary.description = fixStupidOldNonsense(window.currentDictionary.description);
const timestamp = getTimestampInSeconds();
window.currentDictionary.words = window.currentDictionary.words.map(word => {
word.definition = word.simpleDefinition;
delete word.simpleDefinition;
word.details = fixStupidOldNonsense(word.longDefinition);
delete word.longDefinition;
word.lastUpdated = timestamp;
word.createdOn = timestamp;
return word;
});
window.currentDictionary = Object.assign({}, DEFAULT_DICTIONARY, window.currentDictionary);

View File

@ -235,7 +235,7 @@ WHERE dictionary=$dictionary";
$word_ids = array();
$most_recent_word_update = 0;
foreach($words as $word) {
$last_updated = is_null($word['lastUpdated']) ? $word['createdOn'] : $word['lastUpdated'];
$last_updated = isset($word['lastUpdated']) ? $word['createdOn'] : $word['lastUpdated'];
if ($most_recent_word_update < $last_updated) {
$most_recent_word_update = $last_updated;
}