-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTodo.js
32 lines (29 loc) · 855 Bytes
/
addTodo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// addTodo.js
// addTodo.js
var argv = require("minimist")(process.argv.slice(2));
const db = require("./models/index");
const createTodo = async (params) => {
try {
await db.Todo.addTask(params);
} catch (error) {
console.error(error);
}
};
const getJSDate = (days) => {
if (!Number.isInteger(days)) {
throw new Error("Need to pass an integer as days");
}
const today = new Date();
const oneDay = 60 * 60 * 24 * 1000;
return new Date(today.getTime() + days * oneDay);
};
(async () => {
const { title, dueInDays } = argv;
if (!title || dueInDays === undefined) {
throw new Error(
'title and dueInDays are required. \nSample command: node addTodo.js --title="Buy milk" --dueInDays=-2 '
);
}
await createTodo({ title, dueDate: getJSDate(dueInDays), completed: false });
await db.Todo.showList();
})();