Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Register
Sign in
Toggle navigation
Menu
UPSMF
knowledge-mw-service
Commits
5f0284e9
Commit
5f0284e9
authored
6 years ago
by
raghu
Browse files
Options
Download
Patches
Plain Diff
Issue #SB-8572 feat : getorgdetails changes
parent
796fb59a
master
dependabot/npm_and_yarn/src/async-2.6.4
dependabot/npm_and_yarn/src/decode-uri-component-0.2.2
dependabot/npm_and_yarn/src/flat-and-mocha-5.0.2
dependabot/npm_and_yarn/src/handlebars-4.7.7
dependabot/npm_and_yarn/src/jsonwebtoken-9.0.0
dependabot/npm_and_yarn/src/lodash-4.17.21
dependabot/npm_and_yarn/src/minimist-and-project-sunbird/telemetry-sdk-and-handlebars-and-mkdirp-1.2.8
release-3.6.0
release-4.3.0
release-4.8.0
release-5.2.0
release-5.3.0
release-5.6.0
release-6.0.0
v1.13.3
v1.13
v1.12
tmp-testPR
tmp-sunbird-bootcamp
tmp-rjshrjndrn-patch-1
tmp-revert-118-testPR
tmp-revert-110-logs
tmp-revert-99-revert-98-revert-97-revert-96-rjshrjndrn-patch-1
tmp-revert-98-revert-97-revert-96-rjshrjndrn-patch-1
tmp-revert-97-revert-96-rjshrjndrn-patch-1
tmp-revert-96-rjshrjndrn-patch-1
tmp-release-sonarcloud
tmp-release-2.6.0
tmp-release-2.3.0
tmp-release-2.1.1
tmp-release-2.1.0
tmp-release-2.1.0_pluginSearch
tmp-release-2.0.0
tmp-release-1.15
tmp-release-1.14.3
tmp-release-1.14
tmp-release-1.14-sp3
tmp-refactored-dev-deploy
tmp-master
tmp-loadtest
tmp-load-testing
tmp-jenkins-test
tmp-jenkins-pipeline
tmp-final-variable-refac
tmp-dial-assemble-api
tmp-content-update-read
tmp-circleci
release-5.6.0_RC1
release-5.2.0_RC1
release-4.8.5_RC1
release-4.8.0_RC2
release-4.8.0_RC1
release-4.3.0_RC1
release-3.8.0_RC3
release-3.8.0_RC2
release-3.8.0_RC1
release-3.7.0_RC2
release-3.7.0_RC1
release-3.6.0_RC1
release-3.2.0_RC2
release-3.2.0_RC1
release-3.1.0
release-3.1.0_RC2
release-3.1.0_RC1
release-3.0.1_RC1
release-3.0.0
release-3.0.0_RC1
release-2.10.0
release-2.10.0_RC2
release-2.10.0_RC1
release-2.9.0_RC3
release-2.9.0_RC2
release-2.9.0_RC1
release-2.8.0
release-2.8.0_RC4
release-2.8.0_RC3
release-2.8.0_RC2
release-2.8.0_RC1
release-2.7.0
release-2.7.0_RC2
release-2.7.0_RC1
release-2.6.0
release-2.6.0_RC4
release-2.6.0_RC3
release-2.6.0_RC2
release-2.6.0_RC1
release-2.3.0
release-2.3.0_RC2
release-2.3.0_RC1
release-2.1.1_RC1
release-2.1.0_RC2
release-2.1.0_RC1
release-2.0.0
release-2.0.0_RC5
release-2.0.0_RC4
release-2.0.0_RC3
release-2.0.0_RC2
release-2.0.0_RC1
release-1.15.0
release-1.15_RC7
release-1.15_RC6
release-1.15_RC5
release-1.15_RC4
release-1.15_RC3
release-1.15_RC2
release-1.15_RC1
release-1.14.0
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/config/constants.json
+2
-2
src/config/constants.json
src/helpers/orgHelper.js
+100
-28
src/helpers/orgHelper.js
with
102 additions
and
30 deletions
+102
-30
src/config/constants.json
+
2
−
2
View file @
5f0284e9
{
"orgCacheKeyName"
:
"allRootOrgs"
,
"orgCacheExpiryTime"
:
1800
,
"orgfieldsAllowedToSend"
:
[
"email"
,
"orgName"
]
"orgfieldsAllowedToSend"
:
[
"email"
,
"orgName"
],
"orgCacheKeyNamePrefix"
:
"orgdata-"
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/helpers/orgHelper.js
+
100
−
28
View file @
5f0284e9
...
...
@@ -7,6 +7,7 @@ var _ = require('lodash')
var
CacheManager
=
require
(
'
sb_cache_manager
'
)
var
cacheManager
=
new
CacheManager
({})
var
configData
=
require
(
'
../config/constants
'
)
var
async
=
require
(
'
async
'
)
/**
* This function executes the org search lms API to get all orgs
...
...
@@ -17,39 +18,46 @@ function getRootOrgs (requestObj, cb) {
LOG
.
info
(
utilsService
.
getLoggerData
({},
'
INFO
'
,
filename
,
'
getRootOrgs
'
,
'
getRootOrgs called
'
,
requestObj
))
contentProvider
.
getAllRootOrgs
(
requestObj
,
(
err
,
res
)
=>
{
if
(
!
err
&&
res
&&
res
.
result
.
response
.
count
>
0
&&
res
.
result
.
response
.
content
)
{
if
(
!
err
)
{
return
cb
(
err
,
res
)
}
else
{
LOG
.
error
(
utilsService
.
getLoggerData
({},
'
ERROR
'
,
filename
,
'
getRootOrgs
'
,
'
error in getting root orgs.
'
,
err
))
process
.
exit
(
1
)
return
cb
(
err
)
}
})
}
/**
* This function tries to get the orgdetails from cache if not exits fetches from api and sets to cache
* @param requestObj is not needed bec all rootorgdetails are fetched here
* @param requestObj is filter query that is sent to fetch org api call, tryfromcache is a boolean flag,
inputdata is array of contents that needs org data
* @param CBW callback after success or error
*/
function
getRootOrgsFromCache
(
cb
)
{
cacheManager
.
get
(
configData
.
orgCacheKeyName
,
function
(
err
,
cachedata
)
{
if
(
err
||
!
cachedata
)
{
var
inputObj
=
{
'
request
'
:
{
'
filters
'
:
{
'
isRootOrg
'
:
true
}
}
function
getRootOrgsFromCache
(
orgfetchquery
,
tryfromcache
,
inputdata
,
cb
)
{
async
.
waterfall
([
function
(
CBW
)
{
if
(
tryfromcache
)
{
var
keyNames
=
getKeyNames
(
inputdata
)
cacheManager
.
mget
(
keyNames
,
function
(
err
,
cacheresponse
)
{
var
cachedata
=
_
.
compact
(
cacheresponse
)
if
(
!
err
&&
_
.
size
(
cachedata
)
>
0
)
{
return
cb
(
null
,
cachedata
)
}
else
{
CBW
()
}
})
}
else
{
CBW
()
}
getRootOrgs
(
inputObj
,
function
(
err
,
res
)
{
},
function
(
CBW
)
{
getRootOrgs
(
orgfetchquery
,
function
(
err
,
res
)
{
if
(
err
)
{
return
cb
(
err
)
}
else
{
var
cacheinputdata
=
{
key
:
configData
.
orgCacheKeyName
,
value
:
res
.
result
.
response
.
content
,
ttl
:
configData
.
orgCacheExpiryTime
}
cacheManager
.
set
(
cacheinputdata
,
function
(
err
,
data
)
{
var
cacheinputdata
=
prepareCacheDataToInsert
(
res
.
result
.
response
.
content
)
cacheManager
.
mset
({
data
:
cacheinputdata
,
ttl
:
configData
.
orgCacheExpiryTime
},
function
(
err
,
data
)
{
if
(
err
)
{
LOG
.
error
(
utilsService
.
getLoggerData
({},
'
ERROR
'
,
filename
,
'
Setting allRootOrgs cache failed
'
,
'
Setting allRootOrgs cache data failed
'
,
err
))
...
...
@@ -57,14 +65,12 @@ function getRootOrgsFromCache (cb) {
LOG
.
info
(
utilsService
.
getLoggerData
({},
'
INFO
'
,
filename
,
'
Setting allRootOrgs cache data success
'
))
}
return
cb
(
null
,
res
.
result
.
response
.
content
)
})
return
cb
(
null
,
res
.
result
.
response
.
content
)
}
})
}
else
{
return
cb
(
null
,
cachedata
)
}
}
)
]
)
}
/**
...
...
@@ -76,18 +82,59 @@ function populateOrgDetailsByHasTag (inputdata, inputfields, cb) {
inputfields
=
inputfields
.
split
(
'
,
'
)
var
fieldsToPopulate
=
configData
.
orgfieldsAllowedToSend
.
filter
(
eachfield
=>
inputfields
.
includes
(
eachfield
))
if
(
_
.
size
(
fieldsToPopulate
)
>
0
&&
_
.
size
(
inputdata
)
>
0
)
{
getRootOrgsFromCache
(
function
(
err
,
orgdata
)
{
if
(
!
err
&&
orgdata
)
{
var
orgDetails
=
_
.
keyBy
(
orgdata
,
'
hashTagId
'
)
var
orgDetails
=
[]
var
orgFetchQuery
=
{
'
request
'
:
{
'
filters
'
:
{
'
isRootOrg
'
:
true
}
}
}
var
tryFromCache
=
true
async
.
waterfall
([
// intially fetch all the orgs till the default limit
function
(
CBW
)
{
getRootOrgsFromCache
(
orgFetchQuery
,
tryFromCache
,
inputdata
,
function
(
err
,
orgdata
)
{
if
(
!
err
&&
orgdata
)
{
orgDetails
=
orgdata
return
CBW
()
}
else
{
return
cb
(
null
,
inputdata
)
}
})
},
// fetch the orgs which are not fetched from initial api call
function
(
CBW
)
{
var
inputHashTagIds
=
_
.
uniq
(
_
.
map
(
inputdata
,
'
channel
'
))
var
fetchedhashTagIds
=
_
.
uniq
(
_
.
map
(
orgDetails
,
'
hashTagId
'
))
// diff of channels which doesnt exists in inital fetch
var
hasTagIdsNeedToFetch
=
_
.
difference
(
inputHashTagIds
,
fetchedhashTagIds
)
orgFetchQuery
.
request
.
filters
.
hashTagId
=
hasTagIdsNeedToFetch
if
(
hasTagIdsNeedToFetch
.
length
)
{
// fetch directly from api , as hashTagIdsNeedToFetch are the data which are not found from first api query
tryFromCache
=
false
getRootOrgsFromCache
(
orgFetchQuery
,
tryFromCache
,
inputdata
,
function
(
err
,
orgdata
)
{
if
(
!
err
&&
orgdata
)
{
orgDetails
=
_
.
concat
(
orgDetails
,
orgdata
)
return
CBW
()
}
else
{
return
cb
(
null
,
inputdata
)
}
})
}
else
{
CBW
()
}
},
// mapping channel with orgdetails in inputdata
function
(
CBW
)
{
var
orgDetailsWithKey
=
_
.
keyBy
(
orgDetails
,
'
hashTagId
'
)
_
.
forEach
(
inputdata
,
(
eachcontent
,
index
)
=>
{
if
(
eachcontent
.
channel
)
{
var
eachorgdetail
=
orgDetails
[
eachcontent
.
channel
]
var
eachorgdetail
=
orgDetails
WithKey
[
eachcontent
.
channel
]
inputdata
[
index
].
orgDetails
=
eachorgdetail
?
_
.
pick
(
eachorgdetail
,
fieldsToPopulate
)
:
{}
}
})
};
return
cb
(
null
,
inputdata
)
}
)
return
cb
(
null
,
inputdata
)
}
]
)
}
else
{
return
cb
(
null
,
inputdata
)
}
...
...
@@ -116,6 +163,31 @@ function includeOrgDetails (req, res, cb) {
}
}
// prepares the set data for inserting in cache
function
prepareCacheDataToInsert
(
data
)
{
var
cacheKeyValuePairs
=
[]
_
.
forEach
(
data
,
function
(
eachdata
)
{
if
(
eachdata
.
hashTagId
)
{
var
keyname
=
configData
.
orgCacheKeyNamePrefix
+
eachdata
.
hashTagId
cacheKeyValuePairs
.
push
(
keyname
)
cacheKeyValuePairs
.
push
(
eachdata
)
}
})
return
cacheKeyValuePairs
}
// prepares the get data for fetching from cache
function
getKeyNames
(
data
)
{
var
keyNames
=
[]
_
.
forEach
(
data
,
function
(
eachdata
)
{
if
(
eachdata
.
channel
)
{
var
keyname
=
configData
.
orgCacheKeyNamePrefix
+
eachdata
.
channel
keyNames
.
push
(
keyname
)
}
})
return
_
.
uniq
(
keyNames
)
}
module
.
exports
=
{
getRootOrgs
:
getRootOrgs
,
includeOrgDetails
:
includeOrgDetails
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets