2019-12-19 21:50:50 -08:00
|
|
|
let db = require('@config/database')
|
|
|
|
|
|
|
|
let Note = require('@models/Note')
|
|
|
|
|
|
|
|
let QuickNote = module.exports = {}
|
|
|
|
|
|
|
|
|
|
|
|
QuickNote.get = (userId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
db.promise()
|
|
|
|
.query(`
|
2020-02-10 22:05:28 -08:00
|
|
|
SELECT note.id, text FROM note
|
|
|
|
JOIN note_raw_text ON (note_raw_text.id = note.note_raw_text_id)
|
|
|
|
WHERE quick_note = 1 AND user_id = ? LIMIT 1
|
2019-12-19 21:50:50 -08:00
|
|
|
`, [userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
//Quick Note is set, return note text
|
|
|
|
if(rows[0].length == 1){
|
|
|
|
resolve({
|
|
|
|
id: rows[0][0].id,
|
|
|
|
text: rows[0][0].text
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve({
|
|
|
|
id: null,
|
|
|
|
text: 'Enter something to create a quick note.'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
QuickNote.update = (userId, pushText) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
//Process pushText, split at \n (new lines), put <p> tags around each new line
|
|
|
|
let broken = '<blockquote>' +
|
|
|
|
pushText.split(/\r?\n/).map( (line, index) => {
|
|
|
|
|
|
|
|
let clean = line
|
|
|
|
.replace(/&[#A-Za-z0-9]+;/g,'') //Rip out all HTML entities
|
|
|
|
.replace(/<[^>]+>/g, '') //Rip out all HTML tags
|
|
|
|
|
|
|
|
if(clean == ''){ clean = ' ' }
|
|
|
|
let newLine = ''
|
|
|
|
if(index > 0){ newLine = '<br>' }
|
|
|
|
|
|
|
|
//Return line wrapped in p tags
|
|
|
|
return `${newLine}<span>${clean}</span>`
|
|
|
|
|
|
|
|
}).join('') + '</blockquote>'
|
|
|
|
|
|
|
|
db.promise()
|
|
|
|
.query(`
|
2020-02-10 22:05:28 -08:00
|
|
|
SELECT note.id, text, color, pinned, archived
|
|
|
|
FROM note
|
|
|
|
JOIN note_raw_text ON (note_raw_text.id = note.note_raw_text_id)
|
|
|
|
WHERE quick_note = 1 AND user_id = ? LIMIT 1
|
2019-12-19 21:50:50 -08:00
|
|
|
`, [userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
//Quick Note is set, push it the front of existing note
|
|
|
|
if(rows[0].length == 1){
|
|
|
|
|
|
|
|
let d = rows[0][0] //Get row data
|
|
|
|
|
|
|
|
//Push old text behind fresh new text
|
|
|
|
let newText = broken +''+ d.text
|
|
|
|
|
|
|
|
//Save that, then return the new text
|
2020-02-13 17:08:46 -08:00
|
|
|
Note.update(null, userId, d.id, newText, d.color, d.pinned, d.archived)
|
2019-12-19 21:50:50 -08:00
|
|
|
.then( saveResults => {
|
|
|
|
resolve({
|
|
|
|
id:d.id,
|
|
|
|
text:newText
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//Create a new note with the quick text submitted.
|
|
|
|
Note.create(userId, broken, 1)
|
|
|
|
.then( insertId => {
|
|
|
|
resolve({
|
|
|
|
id:insertId,
|
|
|
|
text:broken
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
|
|
|
|
//Lookup quick note,
|
|
|
|
|
|
|
|
//Note.create(userId, 'Quick Note', 1)
|
|
|
|
|
|
|
|
}
|