2017-11-03 02:09:34 +02:00
const { BaseCommand } = require ( './base-command.js' ) ;
const { app } = require ( './app.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
2017-12-14 20:12:14 +02:00
const BaseModel = require ( 'lib/BaseModel.js' ) ;
const Note = require ( 'lib/models/Note.js' ) ;
2017-11-03 02:09:34 +02:00
const { time } = require ( 'lib/time-utils.js' ) ;
2017-07-26 23:07:27 +02:00
class Command extends BaseCommand {
usage ( ) {
2017-08-04 18:02:43 +02:00
return 'todo <todo-command> <note-pattern>' ;
2017-07-26 23:07:27 +02:00
}
description ( ) {
2017-10-30 02:37:34 +02:00
return _ ( '<todo-command> can either be "toggle" or "clear". Use "toggle" to toggle the given to-do between completed and uncompleted state (If the target is a regular note it will be converted to a to-do). Use "clear" to convert the to-do back to a regular note.' ) ;
2017-07-26 23:07:27 +02:00
}
async action ( args ) {
2017-08-04 18:02:43 +02:00
const action = args [ 'todo-command' ] ;
const pattern = args [ 'note-pattern' ] ;
2017-07-26 23:07:27 +02:00
const notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , pattern ) ;
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , pattern ) ) ;
for ( let i = 0 ; i < notes . length ; i ++ ) {
const note = notes [ i ] ;
2017-12-26 12:38:53 +02:00
this . encryptionCheck ( note ) ;
2017-07-26 23:07:27 +02:00
let toSave = {
id : note . id ,
} ;
if ( action == 'toggle' ) {
2017-07-31 20:47:06 +02:00
if ( ! note . is _todo ) {
toSave = Note . toggleIsTodo ( note ) ;
} else {
toSave . todo _completed = note . todo _completed ? 0 : time . unixMs ( ) ;
}
2017-07-26 23:07:27 +02:00
} else if ( action == 'clear' ) {
toSave . is _todo = 0 ;
2019-07-30 09:35:42 +02:00
}
2017-07-26 23:07:27 +02:00
await Note . save ( toSave ) ;
}
}
}
2019-07-30 09:35:42 +02:00
module . exports = Command ;