-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathident.js
611 lines (531 loc) · 23.3 KB
/
ident.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
'use strict';
// SMART identity web service
// File: ident.js
var version = '20130124';
// Required modules
var EventEmitter = require('events').EventEmitter;
var util = require('./utility');
// Configuration
var config = require('./config');
var connString =
'pg://' + config.dbCred + '@localhost/org_smartplatforms_semantic_diverse' + config.dbDeploySuffix;
// Setup for 'ready' event
module.exports = new EventEmitter();
// The 'isReady' structure to track when this module is fully setup
var isReady = {};
util.setNotReady(isReady, 'facilityTypes');
// Initialize globals
var facilityTypes = [];
setupFacilityTypes();
// The 'get facility types' call (GET /facility_types)
module.exports.getFacilityTypes = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: {desc: 'ident service', version: version},
desc: 'Get supported facility types.',
return: 'The JSON array of facility types names and ids:<div style="margin-left:30px;">' + JSON.stringify(facilityTypes) + '</div>'};
} else {
util.sendJson(req, res, facilityTypes);
return next();
}
};
// The 'create facility type' call (POST /facility_type/create)
module.exports.createFacilityType = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Create a new facility type.',
post: [{name: 'name', desc: 'the facility type name'}],
return: 'The facTypeId.',
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'INSERT INTO facility_types (fac_type_name) VALUES ($1) RETURNING fac_type_id';
util.dbQuery(connString, req, res, q, [req.params.name],
function (result) {
if (result.rowCount > 0) {
reloadFacilityTypes(function () {
util.sendText(req, res, 200, result.rows[0].fac_type_id);
});
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'edit facility type' call (POST /facility_type/edit/:facTypeId)
module.exports.editFacilityType = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Edit the facility type :facTypeId.',
params: [{name: 'facTypeId', desc: 'the facility type id'}],
post: [{name: 'name', desc: 'the new facility type name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'UPDATE facility_types SET fac_type_name = $1 WHERE fac_type_id = $2';
util.dbQuery(connString, req, res, q, [req.params.name, req.params.facTypeId],
function (result) {
if (result.rowCount > 0) {
reloadFacilityTypes(function () {
util.sendText(req, res, 200, 'OK');
});
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'delete facility type' call (POST /facility_type/delete/:facTypeId)
module.exports.deleteFacilityType = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Delete the facility type :facTypeId.',
params: [{name: 'facTypeId', desc: 'the facility type id'}],
return: "'OK'",
error: '400 if unsuccessful (not found).'};
} else {
var q = 'DELETE FROM facility_types WHERE fac_type_id = $1';
util.dbQuery(connString, req, res, q, [req.params.facTypeId],
function (result) {
if (result.rowCount > 0) {
reloadFacilityTypes(function () {
util.sendText(req, res, 200, 'OK');
});
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'create org' call (POST /ident/org/create)
module.exports.createOrg = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: '<hr style="margin-top:25px;">',
desc: 'Create a new org.',
post: [{name: 'name', desc: 'the organization name'}],
return: 'The orgId.',
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'INSERT INTO organizations (org_name) VALUES ($1) RETURNING org_id';
util.dbQuery(connString, req, res, q, [req.params.name],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, result.rows[0].org_id);
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'edit org' call (POST /ident/org/edit/:orgId)
//server.post('/ident/org/edit/:orgId', ident.editOrg);
module.exports.editOrg = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Edit the org :orgId.',
params: [{name: 'orgId', desc: 'the org id'}],
post: [{name: 'name', desc: 'the new name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'UPDATE organizations SET org_name = $1 WHERE org_id = $2';
util.dbQuery(connString, req, res, q, [req.params.name, req.params.orgId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'delete org' call (POST /ident/org/delete/:orgId)
module.exports.deleteOrg = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Delete the org :orgId.',
params: [{name: 'orgId', desc: 'the org id'}],
return: "'OK'",
error: '400 if unsuccessful (not found).'};
} else {
var q = 'DELETE FROM organizations WHERE org_id = $1';
util.dbQuery(connString, req, res, q, [req.params.orgId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'get orgs' call (GET /ident/orgs)
module.exports.getOrgs = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Get defined orgs.',
return: 'A JSON array of organizations:' +
'<div style="margin-left:30px;">[{name:orgName1, id:orgId1}, {name:orgName2, id:orgId2}, ...]</div>'};
} else {
var q = 'SELECT org_name AS name, org_id AS id FROM organizations';
util.dbQuery(connString, req, res, q, null,
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
// The 'search for orgs' call (GET /ident/orgs/find/:searchFor)
module.exports.searchForOrgs = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Search for any orgs whose name contains the search string :searchFor.',
params: [{name: 'searchFor', desc:'the search string'}],
return: 'A JSON array of matching organizations:' +
'<div style="margin-left:30px;">[{name:orgName1, id:orgId1}, {name:orgName2, id:orgId2}, ...]</div>'};
} else {
var q = 'SELECT org_name AS name, org_id AS id FROM organizations WHERE org_name ILIKE $1 LIMIT $2';
util.dbQuery(connString, req, res, q, ['%' + req.params.searchFor + '%', config.dbLimitResults],
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
// The 'create loc' call (POST /ident/loc/create)
module.exports.createLoc = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: '<hr style="margin-top:25px;">',
desc: 'Create a new loc.',
post: [{name: 'name', desc: 'the location name'},
{name: 'orgId', desc: 'the associated organization'},
{name: 'facType', desc: 'the facility type name'}],
return: 'the locId.',
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'INSERT INTO locations (org_id, loc_name, fac_type_id)' +
' SELECT $1, $2, fac_type_id FROM facility_types' +
' WHERE fac_type_name = $3' +
' RETURNING loc_id';
util.dbQuery(connString, req, res, q, [req.params.orgId, req.params.name, req.params.facType],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, result.rows[0].loc_id);
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'edit loc' call (POST /ident/loc/edit/:locId)
module.exports.editLoc = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Edit the loc :locId.',
params: [{name: 'locId', desc: 'the loc id'}],
post: [{name: 'name', desc: 'the new name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'UPDATE locations SET loc_name = $1 WHERE loc_id = $2';
util.dbQuery(connString, req, res, q, [req.params.name, req.params.locId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'delete loc' call (POST /ident/loc/delete/:locId)
module.exports.deleteLoc = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Delete the loc :locId.',
params: [{name: 'locId', desc: 'the loc id'}],
return: "'OK'",
error: '400 if unsuccessful (not found).'};
} else {
var q = 'DELETE FROM locations WHERE loc_id = $1';
util.dbQuery(connString, req, res, q, [req.params.locId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res,200, 'OK');
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'get locs for org' call (GET /ident/locs/:forOrgId)
module.exports.getLocsForOrg = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Find locs for org :forOrgId',
params: [{name: 'forOrgId', desc: 'the org id'}],
return: 'A JSON array of locations:' +
'<div style="margin-left:30px;">[{name:locName1, id:locId1}, {name:locName2, id:locId2}, ...]</div>'};
} else {
var q = 'SELECT loc_name AS name, loc_id AS id FROM locations WHERE org_id = $1';
util.dbQuery(connString, req, res, q, [req.params.forOrgId],
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
// The 'create group' call (POST /ident/group/create)
module.exports.createGroup = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: '<hr style="margin-top:25px;">',
desc: 'Create a new group.',
post: [{name: 'name', desc: 'the group name'},
{name: 'locId', desc: 'the associated location'}],
return: 'the groupId.',
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'INSERT INTO groups (loc_id, org_id, fac_type_id, group_name)' +
' SELECT $1, org_id, fac_type_id, $2 FROM locations' +
' WHERE loc_id = $1' +
' RETURNING group_id';
util.dbQuery(connString, req, res, q, [req.params.locId, req.params.name],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, result.rows[0].group_id);
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'edit group' call (POST /ident/group/edit/:groupId)
module.exports.editGroup = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Edit the group :groupId.',
params: [{name: 'groupId', desc: 'the group id'}],
post: [{name: 'name', desc: 'the new name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'UPDATE groups SET group_name = $1 WHERE group_id = $2';
util.dbQuery(connString, req, res, q, [req.params.name, req.params.groupId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'delete group' call (POST /ident/group/delete/:groupId)
module.exports.deleteGroup = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Delete the group :groupId.',
params: [{name: 'groupId', desc: 'the group id'}],
return: "'OK'",
error: '400 if unsuccessful (not found).'};
} else {
var q = 'DELETE FROM groups WHERE group_id = $1';
util.dbQuery(connString, req, res, q, [req.params.groupId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'get groups for loc' call (GET /ident/groups/:forLocId)
module.exports.getGroupsForLoc = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Find groups for loc :forLocId',
params: [{name: 'forLocId', desc: 'the loc id'}],
return: 'A JSON array of groups:' +
'<div style="margin-left:30px;">[{name:groupName1, id:groupId1}, {name:groupName2, id:groupId2}, ...]</div>'};
} else {
var q = 'SELECT group_name AS name, group_id AS id FROM groups WHERE loc_id = $1';
util.dbQuery(connString, req, res, q, [req.params.forLocId],
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
// The 'get user' call (GET /ident/user/:accountId)
module.exports.getUser = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: '<hr style="margin-top:25px;">',
desc: 'Get info for user :accountId',
params: [{name: 'accountId', desc: 'the user id'}],
return: 'A JSON hash of user info:' +
'<div style="margin-left:30px;">{name:userName, accountId:id, groupId:id, locId:id, orgId:id, facTypeId:id, specId:id}</div>',
error: '400 if unsuccessful (not found).'};
} else {
var q = 'SELECT user_name AS name, account_id AS "accountId", group_id AS "groupId",' +
' loc_id AS "locId", org_id AS "orgId", fac_type_id AS "facTypeId", spec_id AS "specId"' +
' FROM users' +
' WHERE account_id = $1';
util.dbQuery(connString, req, res, q, [req.params.accountId],
function (result) {
if (result.rowCount > 0) {
util.sendJson(req, res, result.rows[0]);
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'create user' call (POST /ident/user/create/:accountId)
module.exports.createUser = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Create a new user based on SMART account :accountId.',
params: [{name: 'accountId', desc: 'the SMART account id'}],
post: [{name: 'groupId', desc: 'the associated group'},
{name: 'specId', desc: 'the id of the default specialty for this user'},
{name: 'name', desc: 'the user name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'INSERT INTO users (account_id, group_id, user_name, loc_id, org_id, fac_type_id, spec_id)' +
' SELECT $1, $2, $3, loc_id, org_id, fac_type_id, $4 FROM groups' +
' WHERE group_id = $2';
util.dbQuery(connString, req, res, q, [req.params.accountId, req.params.groupId, req.params.name, req.params.specId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'edit user' call (POST /ident/user/edit/:accountId)
module.exports.editUser = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Edit the user :accountId.',
params: [{name: 'accountId', desc: 'the account id'}],
post: [{name: 'name', desc: 'the new name'}],
return: "'OK'",
error: '400 if unsuccessful (invalid input values).'};
} else {
var q = 'UPDATE users SET user_name = $1 WHERE account_id = $2';
util.dbQuery(connString, req, res, q, [req.params.name, req.params.accountId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Invalid input: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'delete user' call (POST /ident/user/delete/:accountId)
module.exports.deleteUser = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Delete the user :accountId.',
params: [{name: 'accountId', desc: 'the account id'}],
return: "'OK'",
error: '400 if unsuccessful (not found).'};
} else {
var q = 'DELETE FROM users WHERE account_id = $1';
util.dbQuery(connString, req, res, q, [req.params.accountId],
function (result) {
if (result.rowCount > 0) {
util.sendText(req, res, 200, 'OK');
} else {
util.sendText(req, res, 400, 'Not found: ' + JSON.stringify(req.params));
}
});
return next();
}
};
// The 'get users for group' call (GET /ident/users/:forGroupId)
module.exports.getUsersForGroup = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Find users for group :forGroupId',
params: [{name: 'forGroupId', desc: 'the group id'}],
return: 'A JSON array of users:' +
'<div style="margin-left:30px;">[{name:userName1, id:accountId1}, {name:userName2, id:accountId2}, ...]</div>'};
} else {
var q = 'SELECT user_name AS name, account_id AS id FROM users WHERE group_id = $1';
util.dbQuery(connString, req, res, q, [req.params.forGroupId],
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
// The 'search for user' call (GET /ident/users/find/:searchFor/:forOrgId)
module.exports.searchForUser = function (req, res, next) {
if (req == undefined) {
// Return documentation
return {desc: 'Search for any users for org :forOrgId, whose name contains the search string :searchFor',
params: [{name: 'searchFor', desc: 'the string to search for'},
{name: 'forOrgId', desc: 'the org id'}],
return: 'A JSON array of users:' +
'<div style="padding-left:30px;">[{user:userName1, id:accountId1, group:groupName1, loc:locName1}<br>' +
'<span style="padding-left:6px;">{user:userName2, id:accountId2, group:groupName2, loc:locName2}, ...]</span></div>'};
} else {
var q = 'SELECT user_name AS user, account_id AS id, group_name as group, loc_name as loc' +
' FROM users, groups, locations' +
' WHERE users.group_id = groups.group_id' +
' AND users.loc_id = locations.loc_id' +
' AND user_name ILIKE $1 AND users.org_id = $2 LIMIT $3';
util.dbQuery(connString, req, res, q, ['%' + req.params.searchFor + '%', req.params.forOrgId, config.dbLimitResults],
function (result) {
util.sendJson(req, res, result.rows);
});
return next();
}
};
//---------------------------------------------------------------------------------
// Initialize the set of facility types
function setupFacilityTypes () {
reloadFacilityTypes(function () {
// Check whether all queries have completed
if (util.setReady(isReady, 'facilityTypes')) {
// Send module 'ready' event to parent
module.exports.emit('ready');
}
})
}
// Reload the set of facility types
function reloadFacilityTypes(callback) {
var q = 'SELECT fac_type_id AS id, fac_type_name as name FROM facility_types';
util.dbQuery(connString, null, null, q, null,
function (result) {
facilityTypes = result.rows;
// Send complete notification
if (callback != undefined) {
callback();
}
});
}