-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandPrompts.js
221 lines (201 loc) · 7.63 KB
/
commandPrompts.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const inquirer = require('inquirer');
const prompt = inquirer.createPromptModule();
const runner = require('./commandRunners.js');
const createTicket = require('./actions/createTicket');
const updateTicket = require('./actions/updateTicket');
const verifyUser = require('./actions/verifyUser');
const Panels = require('./actions/Panels');
const PanelTickets = require('./actions/PanelTickets');
const MyPanelTickets = require('./actions/MyPanelTickets');
/** GLOBAL VARIABLES **/
let globalVars;
let user_id;
let github_handle;
let board_id;
/** SET GLOBAL VARIABLES **/
const setGlobalVariables = () => {
globalVars = verifyUser.exportGlobals();
user_id = globalVars.user_id;
github_handle = globalVars.github_handle;
board_id = globalVars.board_id;
};
/** VERIFY INITIAL COMMAND PROMPT QUESTION INFO **/
const commandPromptQuestions = {
type: 'list',
name: 'command',
message: 'Select an operation',
choices: ['Display Panels', 'Display Tickets', 'Create Ticket', 'Edit Ticket', 'Close Ticket']
};
/** VERIFY DISPLAY TICKET COMMAND PROMPT QUESTION INFO **/
const commandDisplayTicketsOptions = {
type: 'list',
name: 'displayTicketsOptions',
message: 'Select a display option',
choices: ['Display My Tickets', 'Display Panel Tickets', 'Display My Panel Tickets']
};
/** TAKE IN USER INPUTTED COMMAND AND PASS THROUGH SWITCH STATEMENT **/
const commandPrompt = () => {
prompt(commandPromptQuestions)
.then(answers => {
runner.commandRunner(answers.command);
})
.catch(error => {
console.log('There was an error with this command: ', error);
commandPrompt();
});
};
/** TAKE USER INPUTTED DISPLAY TICKET COMMAND AND PASS THROUGH SWITCH STATEMENT **/
module.exports.ticketDisplayCommandPrompt = () => {
prompt(commandDisplayTicketsOptions)
.then(answers => {
runner.ticketDisplayCommandRunner(answers.displayTicketsOptions);
})
.catch(error => {
console.log('There was an error with this command: ', error);
commandPrompt();
});
};
module.exports.panelTicketsPrompt = () => {
Panels.fetchBoardPanels()
.then((panels) => {
return prompt({type: 'list', name: 'ticketPanel', message: 'Select Panel:', choices: panels.map((panel) => `${panel.id}| ${panel.name}`)});
})
.then(answer => {
PanelTickets.displayAllPanelTickets(answer.ticketPanel.split('|')[0]);
});
};
module.exports.myPanelTicketsPrompt = () => {
Panels.fetchBoardPanels()
.then((panels) => {
return prompt({type: 'list', name: 'ticketPanel', message: 'Select Panel:', choices: panels.map((panel) => `${panel.id}| ${panel.name}`)});
})
.then(answer => {
MyPanelTickets.displayMyPanelTickets(answer.ticketPanel.split('|')[0]);
});
};
/** BUILD NEW TICKET OBJECT FROM USER INPUT **/
module.exports.createTicketPrompt = () => {
!globalVars ? setGlobalVariables() : '';
let newTicket = {creator_id: user_id};
Panels.fetchBoardPanels()
.then((panels) => {
return prompt({type: 'list', name: 'ticketPanel', message: 'Select Panel:', choices: panels.map((panel) => `${panel.id}| ${panel.name}`)});
})
.then(answer => {
newTicket.panel_id = parseInt(answer.ticketPanel.split('|')[0]);
return prompt({type: 'input', name: 'ticketTitle', message: 'Title:'});
})
.then(answer => {
newTicket.title = answer.ticketTitle;
return prompt({type: 'input', name: 'ticketDescription', message: 'Description:'});
})
.then(answer => {
newTicket.description = answer.ticketDescription;
return prompt({type: 'list', name: 'ticketType', message: 'Type:', choices: ['bug', 'devops', 'feature']});
})
.then(answer => {
newTicket.type = answer.ticketType;
return prompt({type: 'list', name: 'ticketStatus', message: 'Status:', choices: ['not started', 'in progress', 'complete']});
})
.then(answer => {
newTicket.status = answer.ticketStatus;
return prompt({type: 'list', name: 'ticketPriority', message: 'Priority:', choices: ['1', '2', '3']});
})
.then(answer => {
newTicket.priority = parseInt(answer.ticketPriority);
return prompt({type: 'input', name: 'ticketAssignee', message: 'Assignee Handle:', default: github_handle});
})
.then(answer => {
newTicket.assignee_handle = answer.ticketAssignee;
return prompt({type: 'confirm', name: 'ticketConfirm', message: 'Everything look okay?'});
})
.then(answer => {
if (answer.ticketConfirm) {
createTicket.createTicket(newTicket);
}
if (!answer.ticketConfirm) {
console.log('Ticket abandoned! Sad!');
commandPrompt();
}
})
.catch(error => {
console.log('Error creating ticket: ', error);
commandPrompt();
});
};
/** BUILD NEW TICKET OBJECT FROM USER INPUT; USE ORIGINAL TICKET VALUES AS DEFAULTS **/
module.exports.updateTicketPrompt = () => {
!globalVars ? setGlobalVariables() : '';
let newTicket = {};
let oldTicket;
prompt({type: 'input', name: 'ticketId', message: 'Ticket ID:'})
.then(answer => {
newTicket.id = answer.ticketId;
return updateTicket.fetchTicket(answer.ticketId);
})
.then(ticket => {
oldTicket = ticket;
newTicket.panel_id = ticket.panel_id.toString();
return prompt({type: 'input', name: 'ticketTitle', message: 'Title:', default: oldTicket.title});
})
.then(answer => {
newTicket.title = answer.ticketTitle;
return prompt({type: 'input', name: 'ticketDescription', message: 'Description:', default: oldTicket.description});
})
.then(answer => {
newTicket.description = answer.ticketDescription;
return prompt({type: 'list', name: 'ticketType', message: 'Type:', choices: ['bug', 'devops', 'feature'], default: oldTicket.type});
})
.then(answer => {
newTicket.type = answer.ticketType;
return prompt({type: 'list', name: 'ticketStatus', message: 'Status:', choices: ['not started', 'in progress', 'complete'], default: oldTicket.status});
})
.then(answer => {
newTicket.status = answer.ticketStatus;
return prompt({type: 'list', name: 'ticketPriority', message: 'Priority:', choices: ['1', '2', '3'], default: oldTicket.priority});
})
.then(answer => {
newTicket.priority = parseInt(answer.ticketPriority);
return prompt({type: 'input', name: 'ticketAssignee', message: 'Assignee Handle:', default: github_handle, default: oldTicket.assignee_handle});
})
.then(answer => {
newTicket.assignee_handle = answer.ticketAssignee;
return prompt({type: 'confirm', name: 'ticketConfirm', message: 'Everything look okay?'});
})
.then(answer => {
if (answer.ticketConfirm) {
updateTicket.updateTicket(newTicket);
}
if (!answer.ticketConfirm) {
console.log('Ticket abandoned! Sad!');
commandPrompt();
}
})
.catch(error => {
console.log('Error updating ticket: ', error);
// TO FIX: Displays duplicate prompts
commandPrompt();
});
};
module.exports.closeTicketPrompt = () => {
!globalVars ? setGlobalVariables() : '';
prompt({type: 'input', name: 'ticketId', message: 'Ticket ID:'})
.then(answer => {
return updateTicket.fetchTicket(answer.ticketId);
})
.then(ticket => {
if (ticket.status === 'complete') {
throw 'Ticket already closed!';
}
// delete updated_at field to prevent rejection by server
delete ticket.updated_at;
delete ticket.created_at;
ticket.status = 'complete';
return updateTicket.updateTicket(ticket);
})
.catch(error => {
console.log('Error closing ticket: ', error);
commandPrompt();
});
};
module.exports.commandPrompt = commandPrompt;