-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecklists.js
197 lines (182 loc) · 5.16 KB
/
checklists.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const {client} = require('../lib/db');
const Promise = require('bluebird');
const {map} = require('rambda');
/**
* Inserts the record of the checklist being done along with the tasks
* @param {Object} data
*/
async function doingTheTaskNow(data) {
try {
console.log('Inserting done checks...');
const doneChecklist = await client.query(
`INSERT INTO DONECHECKLIST(CHECKLIST)
VALUES($1)
RETURNING ID`,
[data.checklist]
);
console.log('Insering Done tasks...');
const doneTasks = await Promise.all(
data.tasks.map(
async (doneTask) => {
const dtask = await client.query(
`INSERT INTO DONETASKS(CHECKLIST, TASK, STATUS)
VALUES($1, $2, $3)
RETURNING ID, TASK, STATUS`,
[doneChecklist.rows[0].id, doneTask.task, doneTask.status]
);
return dtask.rows[0];
}
)
);
console.log('Done inserting tasks!!!');
return {
doneId: doneChecklist.rows[0].id,
status: true,
done: doneTasks
};
} catch (error) {
console.log('Error inserting tasks...');
console.error(error);
return {
'status': false
};
}
}
/**
* Insert a record for the tasks being done from the checklists
*
* @param {Request} req
* @param {Response} res
*/
async function taskIsBeingDone(req, res) {
try {
console.log('Querying checklist...');
const userChecklist = await client.query(
`SELECT *
FROM USERCHECKLIST
WHERE ID = $1
AND USR = 1
AND STATUS = 200`,
[req.body.checklist]
)
if (userChecklist.rowCount === 1) {
const result = await doingTheTaskNow(req.body);
console.log('Finished doing the tasknow');
console.log(result);
res.send(result);
} else {
console.log('Multiple checklists matched...');
res.send({
'status': false
})
}
} catch (error) {
console.log(error);
res.send({
'status': false
});
}
}
/**
* Adds a new checklist with the tasks associated with it
* @param {Request} req
* @param {Response} res
*/
async function addNewChecklist(req, res) {
try {
console.log('Inserting checklist...');
const newChecklist = await client.query(
`INSERT INTO USERCHECKLIST(CHECKLISTNAME, USR)
VALUES($1, $2)
RETURNING *`,
[req.body.name, 1]
);
console.log('Inserted tasks...');
map(
async (task)=>{
const tsk = await client.query(
`INSERT INTO TASKS(CHECKLIST, NAME, DESCRIPTION)
VALUES($1, $2, $3)
RETURNING *`,
[newChecklist.rows[0].id, task.name, task.description]
);
},
req.body.tasks);
res.send({
'status': true
});
} catch (error) {
console.log(error);
res.send({
'status': false
});
}
}
/**
* Returns all the checklists accessible by the authenticated user
* @param {Request} req
* @param {Response} res
*/
async function getAllChecklists(req, res) {
try {
console.log('Querying...');
const result = await client.query(
`SELECT ID, CHECKLISTNAME AS NAME
FROM USERCHECKLIST
WHERE USR = 1
AND STATUS = 200
ORDER BY CHECKLISTNAME`);
console.log('Fetched result...');
res.send({
'status': true,
'value': result.rows
});
} catch (error) {
console.log(error);
res.send({
'status': false
});
}
}
/**
* Returns a single checklist identified by the id in the request params
* @param {Request} req
* @param {Response} res
*/
async function getSingleChecklist(req, res) {
try {
console.log('Querying for ' + req.params.checklist + '...');
const result = await client.query(
`SELECT TSK.ID, TSK.NAME, TSK.DESCRIPTION
FROM USERCHECKLIST AS UCL
LEFT JOIN TASKS AS TSK
ON UCL.ID = TSK.CHECKLIST
WHERE UCL.ID = $1
AND UCL.USR = 1
AND UCL.STATUS = 200
AND TSK.STATUS = 200`,
[req.params.checklist]);
console.log('Fetched result...');
res.send({
'status': true,
'value': result.rows
});
} catch (error) {
console.log(error);
res.send({
'status': false
})
}
}
module.exports = {
'AddCheckList': addNewChecklist,
'getAllChecklists': getAllChecklists,
'getCheckList': getSingleChecklist,
'DeleteCheckList': (params)=>{
return "Checklist Deleted"
},
'UpdateCheckList': (params)=>{
return "Checklist updated"
},
'ItsTasksTime': taskIsBeingDone
}