-
Notifications
You must be signed in to change notification settings - Fork 9
/
library.js
943 lines (905 loc) · 26.5 KB
/
library.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
var Categories = require.main.require('./src/categories')
var Privilegs = require.main.require('./src/privileges')
var Groups = require.main.require('./src/groups')
var db = require.main.require('./src/database')
var Users = require.main.require('./src/user')
const Topics = require.main.require('./src/topics')
var async = require('async')
var _ = require('lodash')
const { v4: uuidv4 } = require('uuid')
async function createTopic (payload) {
return new Promise(function (resolve, reject) {
Topics.post(payload)
.then(topicObj => {
return resolve(topicObj)
})
.catch(error => {
return reject({
status: 400,
message: 'Error in creating topic.',
error: error
})
})
})
}
async function replyTopic (payload) {
return new Promise(function (resolve, reject) {
Topics.reply(payload)
.then(topicObj => {
return resolve(topicObj)
})
.catch(error => {
return reject({
status: 400,
message: 'Error in replying the topic.',
error: error
})
})
})
}
function createCategory (body) {
const tenantId = body.organisationId
const sections = body.sections
let sectionArray = [],
sectionObj = {},
mappingTenant = null,
existedCatId = null,
existedCatName = null
return new Promise(function (resolve, reject) {
db.getObject(`tenant_cat_map:` + tenantId)
.then(tenentObj => {
if (tenentObj) {
existedCatId = tenentObj.tenant_id
existedCatName = tenentObj.name
}
let pID = tenentObj && tenentObj.cat_id ? tenentObj.cat_id : ''
if (existedCatId == tenantId) {
return reject({
status: 400,
message: 'Already category id exist for this tenant!'
})
} else {
const reqData = {
name: body.name,
parentCid: pID,
isSection: 1
}
Categories.create(reqData)
.then(categoryObj => {
if (!pID) {
if (sections.length > 0) {
return commonAddSection(
body.name,
sections,
categoryObj.cid,
tenantId
)
.then(res => {
let response = {
categoryObj: categoryObj,
sectionObj: res
}
return resolve(response)
})
.catch(error => {
return reject(error)
})
} else {
let jsonTenanatObj = {
cat_id: categoryObj.cid,
tenant_id: tenantId,
name: body.name
}
mappingTenant = mappingFunction(
`tenant_cat_map:${tenantId}`,
jsonTenanatObj
)
Promise.all([mappingTenant])
.then(responnse => {
let response = {
categoryObj: categoryObj,
sectionObj: []
}
return resolve(response)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in mapping data.',
error: err
})
})
}
} else {
return resolve(categoryObj)
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting category',
error: err
})
})
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in finding tenant id',
error: err
})
})
})
}
function createCategory_check (body) {
// const tenantId = body.organisationId
const sections = body.sections || ['General discussion', 'Annoucement']
let sectionArray = []
return new Promise(function (resolve, reject) {
const reqData = {
name: body.name,
isSection: 1
}
Categories.create(reqData)
.then(categoryObj => {
sections.map((secName, inx) => {
let name = secName.replace(/\s+/g, '-').toLowerCase()
const bodyData = {
name: secName,
description: 'course batch',
parentCid: categoryObj.cid,
isSection: 1
}
Categories.create(bodyData)
.then(catData => {
sectionArray.push(catData)
if (sections.length == sectionArray.length) {
let response = {
categoryObj: categoryObj,
sectionObj: sectionArray
}
return resolve(response)
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting sections.',
error: err
})
})
})
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting category',
error: err
})
})
})
}
function createForum (body) {
const tenantId = body.organisationId
let sectionType = body.section
const catName = body.context.name
const identifier = body.context.identifier
const contextType = body.context.type
let pID = null,
jsonObj = null
return new Promise(function (resolve, reject) {
db.getObject(`tenant_cat_map:` + tenantId)
.then(tenentObj => {
if (tenentObj) {
if (!sectionType) {
pID = tenentObj && tenentObj.cat_id ? tenentObj.cat_id : ''
} else {
sectionType = sectionType.replace(/\s+/g, '-').toLowerCase()
pID =
tenentObj && tenentObj[`${sectionType}`]
? tenentObj[`${sectionType}`]
: ''
}
const reqData = {
name: catName,
parentCid: pID
}
return checkCat(catName, tenantId, contextType, identifier)
.then(data => {
if (data == true) {
Categories.create(reqData)
.then(categoryObj => {
db.getObject(`batchid_catid_map:` + identifier)
.then(batchData => {
if (batchData) {
jsonObj = {
...batchData,
[`cat_id:${categoryObj.cid}`]: {
cat_id: categoryObj.cid,
name: catName,
tenant_id: tenantId,
batch: contextType + identifier
}
}
} else {
jsonObj = {
batch_id: identifier,
type: contextType,
[`cat_id:${categoryObj.cid}`]: {
cat_id: categoryObj.cid,
name: catName,
tenant_id: tenantId,
batch: contextType + identifier
}
}
}
let mapping = mappingFunction(
`batchid_catid_map:${identifier}`,
jsonObj
)
Promise.all([mapping])
.then(responnse => {
return resolve(categoryObj)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in mapping data.',
error: err
})
})
})
.catch(error => {})
})
.catch(err => {
return reject({
status: 400,
message: 'Error in creating forum',
error: err
})
})
} else {
return reject({
status: 400,
message: 'Name is same for org id and identifier'
})
}
})
.catch(error => {
return reject(error)
})
} else {
return reject({
status: 400,
message: 'No tenentId exist!',
error: err
})
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in finding tenant id',
error: err
})
})
})
}
function commonAddSection (cname, sections, cid, tenantId) {
let sectionArray = [],
sectionObj = {}
return new Promise(function (resolve, reject) {
if (sections.length > 0) {
sections.map((secName, inx) => {
let name = secName.replace(/\s+/g, '-').toLowerCase()
const bodyData = {
name: secName,
parentCid: cid
}
Categories.create(bodyData)
.then(catData => {
sectionArray.push(catData)
sectionObj[name] = catData.cid
if (sections.length === sectionArray.length) {
let jsonTenanatObj = {
...sectionObj,
cat_id: cid,
tenant_id: tenantId,
name: cname
}
mappingTenant = mappingFunction(
`tenant_cat_map:${tenantId}`,
jsonTenanatObj
)
Promise.all([mappingTenant])
.then(responnse => {
// let response = {
// categoryObj: categoryObj,
// sectionObj: sectionArray
// }
return resolve(sectionArray)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in mapping data.',
error: err
})
})
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting sections.',
error: err
})
})
})
}
})
}
function addSection (body) {
const tenantId = body.organisationId
const sections = body.sections
let sectionArray = [],
sectionObj = {}
return new Promise(function (resolve, reject) {
db.getObject(`tenant_cat_map:` + tenantId)
.then(tenentObj => {
let pID = tenentObj && tenentObj.cat_id ? tenentObj.cat_id : ''
if (sections.length > 0) {
return commonAddSection(tenentObj.name, sections, pID, tenantId)
.then(res => {
let response = {
sectionObj: res
}
return resolve(response)
})
.catch(error => {
return reject(error)
})
}
})
.catch(error => {
return reject({
status: 400,
message: 'Error in finding tenant id',
error: err
})
})
})
}
async function mappingFunction (key, obj) {
try {
let mappingRes = await db.setObject(key, obj)
return mappingRes
} catch (err) {
return err
}
}
function addPrivileges (reqPrivileges, catIds) {
let preArray = [],
array = []
return new Promise(function (resolve, reject) {
return removeuserPreviliges(catIds)
.then(res => {
reqPrivileges.map((privilege, index) => {
let permissions = privilege.permissions
let users = privilege.users
let groups = privilege.groups
groups.map(group => {
return addGroupIntoCategory(group, permissions, catIds)
.then(res => {
array.push(res)
return addUserIntoGroup(group, users, permissions, catIds)
.then(response => {
preArray.push(response)
if (reqPrivileges.length === preArray.length)
return resolve(response)
})
.catch(error => {
return reject({
status: 400,
message: 'Error in adding users into category',
error: error
})
})
})
.catch(error => {
return reject({
status: 400,
message: 'Error in adding privileges into category',
error: error
})
})
})
})
})
.catch(error => {
return reject({
status: 400,
message: 'Error in removing privileges from group into category',
error: error
})
})
})
}
function createGroup (body, catIds) {
return new Promise(function (resolve, reject) {
let array = []
body.groups.map((group, inx) => {
let groupName =
body.context.type +
'-' +
body.context.identifier +
'-' +
body.context.name +
'-' +
uuidv4()
Groups.create({ name: groupName, private: 1 })
.then(groupObj => {
if (groupObj) {
return addGroupIntoCategory(groupName, group.permissions, catIds)
.then(res => {
array.push(res)
if (body.groups.length == array.length) {
return resolve(res)
}
})
.catch(error => {
return reject({
status: 400,
message: 'Error in adding group into category',
error: error
})
})
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting groups/ Already exist',
error: err
})
})
})
})
}
function createGroupDefault (body, uid, catIds) {
return new Promise(function (resolve, reject) {
let array = [],
preArray = []
let groups = [
{ name: `${body.name}-members`, permissions: ['read'] },
{
name: `${body.name}-moderators`,
permissions: ['read', 'vote', 'post', 'topic', 'moderate'],
users: [uid]
}
]
return removeuserPreviliges(catIds)
.then(res => {
console.log('res-------------', res)
groups.map((group, inx) => {
Groups.create({ name: group.name, private: 1 })
.then(groupObj => {
if (groupObj) {
return addGroupIntoCategory(
group.name,
group.permissions,
catIds
)
.then(res => {
array.push(res)
return addUserIntoGroup_new(
group.name,
group.users,
group.permissions,
catIds
)
.then(response => {
preArray.push(response)
if (groups.length === preArray.length)
return resolve(response)
})
.catch(error => {
return reject({
status: 400,
message: 'Error in adding users into category',
error: error
})
})
})
.catch(error => {
return reject({
status: 400,
message: 'Error in adding group into category',
error: error
})
})
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in inserting groups/ Already exist',
error: err
})
})
})
})
.catch(error => {
return reject({
status: 400,
message: 'Error in removing privileges from group into category',
error: error
})
})
})
}
async function addGroupIntoCategory (group, permissions, catIds) {
return new Promise(function (resolve, reject) {
let groups = [group]
let finalPrivileges = null
permissions.map((permission, inx) => {
finalPrivileges = privilegesHirerchy(permission, 'group')
})
finalPrivileges = _.uniq(finalPrivileges)
let arrData = []
catIds.map(id => {
changeGroupMembership(id, finalPrivileges, groups, 'join')
.then(data => {
arrData.push(data)
if (catIds.length === arrData.length) return resolve(data)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in adding groups previliges into category',
error: err
})
})
})
})
}
async function addUserIntoGroup (groupName, users, permissions, catIds) {
return new Promise(function (resolve, reject) {
if (users && users.length > 0) {
users.map(sunbirdAuthId => {
return getUserIdFromOauthId(sunbirdAuthId).then(uid => {
if (uid) {
Groups.join(groupName, uid)
.then(data => {
return addUserPreviliges(uid, permissions, catIds)
.then(res => {
return resolve(res)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in adding user previliges',
error: err
})
})
})
.catch(err => {
return reject({
status: 400,
message: 'Error in joining',
error: err
})
})
} else {
console.log(
'---------- no user id found -----------',
sunbirdAuthId
)
}
})
})
} else {
console.log('no id found---------------------')
return resolve(true)
}
})
}
async function addUserIntoGroup_new (groupName, users, permissions, catIds) {
return new Promise(function (resolve, reject) {
if (users && users.length > 0) {
users.map(uid => {
Groups.join(groupName, uid)
.then(data => {
return addUserPreviliges(uid, permissions, catIds)
.then(res => {
return resolve(res)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in adding user previliges',
error: err
})
})
})
.catch(err => {
return reject({
status: 400,
message: 'Error in joining',
error: err
})
})
})
} else {
console.log('no id found---------------------')
return resolve(true)
}
})
}
function addUserPreviliges (uid, permissions, catIds) {
return new Promise(function (resolve, reject) {
let groups = [uid]
let finalPrivileges = null
permissions.map((permission, inx) => {
finalPrivileges = privilegesHirerchy(permission, 'user')
})
finalPrivileges = _.uniq(finalPrivileges)
catIds.map(id => {
changeGroupMembership(id, finalPrivileges, groups, 'join')
.then(data => {
return resolve(data)
})
.catch(err => {
return reject({
status: 400,
message: 'Error in adding users previliges into category',
error: err
})
})
})
})
}
function removeuserPreviliges (catIds) {
let delgroupData = ['registered-users', 'guests', 'spiders']
let previligesArray = null
let arr = []
return new Promise(function (resolve, reject) {
delgroupData.map(name => {
name.includes('users')
? (previligesArray = [
'groups:topics:read',
'groups:read',
'groups:find',
'groups:topics:create',
'groups:topics:reply',
'groups:topics:tag',
'groups:posts:edit',
'groups:posts:history',
'groups:posts:delete',
'groups:posts:upvote',
'groups:posts:downvote',
'groups:topics:delete',
'groups:posts:view_deleted',
'groups:purge',
'groups:moderate'
])
: (previligesArray = [
'groups:read',
'groups:find',
'groups:topics:read'
])
catIds.map(id => {
changeGroupMembership(id, previligesArray, name, 'leave')
.then(data => {
arr.push(data)
if (delgroupData.length === arr.length) {
return resolve(arr)
}
})
.catch(err => {
return reject({
status: 400,
message: 'Error in removing priviliges',
error: err
})
})
})
})
})
}
async function changeGroupMembership (
cid,
privileges,
groups,
action,
callback
) {
privileges = Array.isArray(privileges) ? privileges : [privileges]
groups = Array.isArray(groups) ? groups : [groups]
async.each(
groups,
function (group, groupCb) {
async.each(
privileges,
function (privilege, privilegeCb) {
Groups[action](
'cid:' + cid + ':privileges:' + privilege,
group,
privilegeCb
)
},
groupCb
)
},
callback
)
}
async function getUserIdFromOauthId (sunbirdId) {
let uid = await db.getObjectField('sunbird-oidcId:uid', sunbirdId)
return uid
}
function privilegesHirerchy (privilegs, type) {
let readPriviliges =
type === 'user'
? ['topics:read', 'read', 'find']
: ['groups:topics:read', 'groups:read', 'groups:find']
let votePriviliges =
type === 'user'
? ['posts:upvote', 'posts:downvote']
: ['groups:posts:upvote', 'groups:posts:downvote']
let postPriviliges =
type === 'user'
? ['topics:reply', 'posts:edit', 'posts:delete']
: ['groups:topics:reply', 'groups:posts:edit', 'groups:posts:delete']
let topicPriviliges =
type === 'user'
? ['topics:create', 'topics:tag']
: ['groups:topics:create', 'groups:topics:tag']
let moderatorPriviliges =
type === 'user'
? [
'topics:delete',
'posts:view_deleted',
'purge',
'moderate',
'posts:history'
]
: [
'groups:topics:delete',
'groups:posts:view_deleted',
'groups:purge',
'groups:moderate',
'groups:posts:history'
]
let givenPrivileges = null
switch (privilegs) {
case 'read':
givenPrivileges = readPriviliges
break
case 'vote':
givenPrivileges = [...readPriviliges, ...votePriviliges]
break
case 'post':
givenPrivileges = [
...readPriviliges,
...votePriviliges,
...postPriviliges
]
break
case 'topic':
givenPrivileges = [
...readPriviliges,
...votePriviliges,
...postPriviliges,
...topicPriviliges
]
break
case 'moderate':
givenPrivileges = [
...readPriviliges,
...votePriviliges,
...postPriviliges,
...topicPriviliges,
...moderatorPriviliges
]
break
}
return givenPrivileges
}
function getForum (body) {
return new Promise(function (resolve, reject) {
db.getObject(`batchid_catid_map:` + body.context.identifier)
.then(getData => {
let finalArray = [],
totalArr = []
delete getData.type
delete getData.batch_id
let convertObjToArr = Object.values(getData)
convertObjToArr.map(val => {
if (
val.tenant_id == body.organisationId &&
val.batch == body.context.type + body.context.identifier
) {
totalArr.push(val)
Privilegs.categories
.list(val.cat_id)
.then(allGroups => {
finalArray.push({
categoryId: val.cat_id,
name: val.name,
groups: allGroups.groups
})
if (finalArray.length == totalArr.length) {
return resolve(finalArray)
}
})
.catch(error => {
return reject({
status: 400,
message: 'Error in fetching groups',
error: error
})
})
}
})
})
.catch(error => {
return reject({
status: 400,
message: 'Error in fetching forum',
error: error
})
})
})
}
function checkCat (name, organisationId, type, identifier) {
return new Promise(function (resolve, reject) {
db.getObject(`batchid_catid_map:` + identifier)
.then(getData => {
let finalArray = [],
data = true
if (getData) {
delete getData.type
delete getData.batch_id
let convertObjToArr = Object.values(getData)
convertObjToArr.map(val => {
if (
val.tenant_id == organisationId &&
val.batch == type + identifier
) {
finalArray.push(val)
}
})
finalArray.map(val => {
if (val.name == name) {
data = false
}
})
return resolve(data)
} else {
return resolve(data)
}
})
.catch(error => {
return reject({
status: 400,
message: 'Error in fetching forum',
error: error
})
})
})
}
module.exports = {
createCategory,
addPrivileges,
addSection,
createForum,
createGroup,
getForum,
createTopic,
replyTopic,
createCategory_check,
createGroupDefault,
addUserIntoGroup_new
}