66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
let db = require('@config/database')
|
|
|
|
let Note = require('@models/Note')
|
|
|
|
let MetricTracking = module.exports = {}
|
|
|
|
|
|
MetricTracking.get = (userId, masterKey) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
.query(`
|
|
SELECT note.id FROM note WHERE quick_note = 2 AND user_id = ? LIMIT 1`, [userId])
|
|
.then((rows, fields) => {
|
|
|
|
//Quick Note is set, return note object
|
|
if(rows[0][0] != undefined){
|
|
|
|
let noteId = rows[0][0].id
|
|
const note = Note.get(userId, noteId, masterKey)
|
|
.then(noteData => {
|
|
return resolve(noteData)
|
|
})
|
|
|
|
} else {
|
|
//Or create a new note
|
|
let finalId = null
|
|
return Note.create(userId, 'Metric Tracking', '', masterKey)
|
|
.then(insertedId => {
|
|
finalId = insertedId
|
|
db.promise().query('UPDATE note SET quick_note = 2 WHERE id = ? AND user_id = ?',[insertedId, userId])
|
|
.then((rows, fields) => {
|
|
|
|
const note = Note.get(userId, finalId, masterKey)
|
|
.then(noteData => {
|
|
return resolve(noteData)
|
|
})
|
|
|
|
})
|
|
})
|
|
|
|
}
|
|
|
|
|
|
})
|
|
.catch(console.log)
|
|
})
|
|
}
|
|
|
|
MetricTracking.save = (userId, cycleData, masterKey) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let finalId = null
|
|
|
|
MetricTracking.get(userId, masterKey)
|
|
.then(noteObject => {
|
|
|
|
return Note.update(userId, noteObject.id, cycleData, noteObject.title, noteObject.color, noteObject.pinned, noteObject.archived, null, masterKey)
|
|
|
|
})
|
|
.then( saveResults => {
|
|
return resolve(saveResults)
|
|
})
|
|
})
|
|
|
|
} |