· 6 years ago · Nov 19, 2019, 04:42 AM
1const express = require('express');
2const router = express.Router();
3const axios = require('axios');
4const API = require('../../config/api.json');
5const sendRequestNetam = require('../Connector/apinetam').sendRequestNetam;
6
7const dataStructure = [{
8 title: 'Name',
9 key: 'name',
10 type:'text',
11 required:true,
12 readonly:true
13 },
14 {
15 title: 'Type',
16 key: 'type',
17 type: 'select',
18 required: true,
19 valueSelect: [{
20 value: 'CREATE',
21 text: 'CREATE'
22 },
23 {
24 value: 'READ',
25 text: 'READ'
26 },
27 {
28 value: 'DELETE',
29 text: 'DELETE'
30 },
31 {
32 value: 'MODIFY',
33 text: 'MODIFY'
34 },
35 {
36 value: 'RESUME',
37 text: 'RESUME'
38 },
39 {
40 value: 'SUSPEND',
41 text: 'SUSPEND'
42 },
43 ]
44 },
45 {
46 title: 'Description',
47 key: 'description',
48 type: 'textarea',
49 required: true
50 }
51];
52const authClass = [{
53 title: 'Name',
54 key: 'name',
55 type: 'text',
56 required: true,
57 readonly: true,
58 },
59 {
60 title: 'Alias',
61 key: 'alias',
62 type: 'text',
63 required: true,
64 },
65 {
66 title: 'Auth Mode',
67 key: 'authMode',
68 type: 'checkbox',
69 required: true,
70 valuecheckbox: [{
71 value: 'CREATE',
72 text: 'CREATE'
73 },
74 {
75 value: 'READ',
76 text: 'READ'
77 },
78 {
79 value: 'DELETE',
80 text: 'DELETE'
81 },
82 {
83 value: 'MODIFY',
84 text: 'MODIFY'
85 }
86 ]
87 },
88 {
89 title: 'Description',
90 key: 'description',
91 type: 'textarea',
92 required: true,
93 }
94];
95const commandGroup = [{
96 title: 'Name',
97 key: 'name',
98 type: 'text',
99 required: true,
100 readonly: true,
101 },
102 {
103 title: 'Display Name',
104 key: 'displayName',
105 type: 'text',
106 required: true,
107 },
108 {
109 title: 'Parent',
110 key: 'parentGroup',
111 type: 'select',
112 required: false
113 },
114 {
115 title: 'Description',
116 key: 'description',
117 type: 'textarea',
118 required: true
119 }
120];
121const relatedNode = [{
122 name: 'AuthClass',
123 url: '/GeneralCommand/AuthClass/'
124},
125{name: 'CommandGroup',
126 url: '/GeneralCommand/CommandGroup/'
127}
128]
129router.get('/', function(req, res, next) {
130 res.render('GeneralCommandDefinition/generalcommandview', {
131 title: 'General Command',
132 // parentMenu:null,
133 insertAction: {
134 url: '/GeneralCommand/create'
135 }
136 });
137});
138router.get('/Added', function (req, res, next) {
139 res.render('GeneralCommandDefinition/includes/recentylyAdded');
140});
141router.post('/create', function (req, res, next) {
142 data = {
143 type: req.body.actionType,
144 name: req.body.commandName,
145 idCommandGroup: req.body.commandGroup,
146 idAuthClass: req.body.authClass,
147 description: req.body.description,
148 createdBy: "risma.fitria",
149 createdVia: "WEB"
150 }
151 sendRequestNetam('admin', '/netam/GeneralCommand/create', data, 'POST', res, function (responseData) {
152 res.status(200).json(responseData);
153 });
154})
155router.get('/CommandGroup', function (req, res, next) {
156 res.render('GeneralCommandDefinition/commandGroup', {
157 title: 'Command Group Registration',
158 insertAction: {
159 url: '/GeneralCommand/CommandGroupCreate'
160 },
161 parentMenu: {
162 url: '/GeneralCommand',
163 menuName: 'General Command'
164 }
165 });
166});
167router.post('/CommandGroupCreate', function (req, res, next) {
168 properties={
169 name: req.body.name,
170 displayName: req.body.displayName,
171 description: req.body.description,
172 parentGroup: req.body.parentGroup
173 }
174 param = {
175 labels: ["CommandGroup"],
176 properties: properties,
177 createdBy: "risma.fitria",
178 createdVia: "WEB"
179 }
180 sendRequestNetam('admin', '/internal/node/create', param , 'POST', res, function (responseData) {
181 res.status(200).json(responseData);
182 });
183})
184router.get('/newgeneralCommand', function (req, res, next) {
185 res.render('GeneralCommandDefinition/newgeneralCommand', {
186 title: 'General Command',
187 insertAction: {
188 url: '/GeneralCommand/create'
189 },
190 parentMenu: {
191 url: '/NetworkCommandRegistration',
192 menuName: 'Netwok Command Registration'
193 }
194 });
195});
196router.get('/List', function (req, res, next) {
197 res.render('GeneralCommandDefinition/content', {
198 dataStructure: dataStructure,
199 title: 'General Command List',
200 label: 'GeneralCommand',
201 getData: {
202 url: '/GeneralCommand/getData',
203 method: 'POST',
204 },
205 parentMenu: null,
206 editAction: {
207 url: '/GeneralCommand/Update/GeneralCommand',
208 method: 'PUT',
209 label: 'GeneralCommand',
210 },
211 });
212});
213router.post('/getData', function (req, res, next) {
214 var label = req.body.label;
215 if (label == 'AuthClass') {
216 datastructure = authClass
217 relation = null,
218 deleteAction = {
219 label: 'AuthClass',
220 }
221 } else if (label == 'CommandGroup') {
222 datastructure = commandGroup
223 relation = null,
224 deleteAction = {
225 label: 'CommandGroup',
226 }
227 } else {
228 datastructure = dataStructure
229 relation = relatedNode
230 deleteAction= {
231 label: 'GeneralCommand',
232 }
233 }
234 const sortby = {
235 label: req.body.orderby,
236 order: req.body.sortby
237 }
238 const dataParam = {
239 label: req.body.label,
240 search: req.body.searchby,
241 sort: sortby,
242 rows: parseInt(req.body.rows),
243 page: parseInt(req.body.page),
244 parentId: req.body.parentId ? req.body.parentId : null
245 }
246 console.log(dataParam)
247 sendRequestNetam('admin', '/internal/node/queryPage', dataParam , 'POST', res, function (responseData) {
248 var ret = responseData;
249 if (!Object.keys(ret).length) {
250 res.send('');
251 } else {
252 res.render('component/includes/dataCard', {
253 dataStructure: datastructure,
254 responseData: responseData,
255 relatedNode: relation,
256 deleteAction: deleteAction,
257 });
258 }
259 })
260});
261router.get('/:parentnode/:id', function (req, res, next) {
262 const parentnode = req.params.parentnode;
263 getdata = {
264 url: '/GeneralCommand/getData',
265 method: 'POST',
266 }
267 parentMenu = {
268 url: '/GeneralCommand/List',
269 menuName: 'General Command List'
270 }
271 if (parentnode == 'AuthClass') {
272 title = 'AuthClass List';
273 url = '/GeneralCommand/Update/AuthClass';
274 } else{
275 title = 'CommandGroup List';
276 url = '/GeneralCommand/Update/CommandGroup';
277 }
278 if (parentnode == 'AuthClass') {
279 datastructure = authClass
280 } else if (parentnode == 'CommandGroup') {
281 datastructure = commandGroup
282 } else {
283 datastructure = dataStructure
284 }
285 const id = req.params.id;
286 data = ''
287 sendRequestNetam('admin', '/internal/node?id=' + id + '&includeRelations=false', null, 'GET', res, function (responseData) {
288 var ret = responseData;
289 ret.forEach(function (val, index) {
290 dataStructure.forEach(function (key, indes) {
291 data += key.title + ':' + val.properties[key.key] + '<br>'
292 })
293 })
294 res.render('GeneralCommandDefinition/content', {
295 dataStructure: datastructure,
296 title: title,
297 label: parentnode,
298 parentId: id,
299 getData: getdata,
300 parentMenu: parentMenu,
301 detailRelation: data,
302 editAction: {
303 url: url,
304 method: 'PUT',
305 label: parentnode,
306 },
307 });
308 })
309});
310router.put('/update/:label', function (req, res, next) {
311 var label = req.params.label;
312 if (label == 'GeneralCommand') {
313 properties = {
314 name: req.body.edit_name,
315 type: req.body.edit_type,
316 description: req.body.edit_description
317 }
318 }else if(label == 'AuthClass') {
319 authMode = req.body.edit_authMode
320 if (typeof authMode == 'string') {
321 authMode = authMode
322 } else {
323 authMode = authMode.join(',')
324 }
325 properties = {
326 name: req.body.edit_name,
327 alias: req.body.edit_alias,
328 authMode: authMode,
329 description: req.body.edit_description
330 }
331 }else if(label == 'CommandGroup') {
332 properties = {
333 name: req.body.edit_name,
334 displayName: req.body.edit_displayName,
335 parentGroup: req.body.edit_parentGroup,
336 description: req.body.edit_description
337 }
338 }
339 const data = {
340 label: label,
341 id: req.body.id,
342 properties: properties,
343 changedBy: 'risma.fitria@amalsolution.com',
344 changedVia: 'WEB'
345 }
346 sendRequestNetam('admin', '/internal/node', data, 'PUT', res, function (responseData) {
347 res.status(200).send({
348 data: responseData
349 });
350 });
351
352});
353
354
355module.exports = router;